package com.sigem.gis.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern; @RestController @RequestMapping("/api/analysis") public class AnalysisController { @Autowired @Qualifier("masterJdbcTemplate") private JdbcTemplate masterJdbcTemplate; private final RestTemplate restTemplate = new RestTemplate(); @GetMapping("/snc-mapping") public String generateSncMappingReport() { try { // 1. Obtener Entidades de .254 String sql = "SELECT entidad, nombre, activo FROM public.entidades ORDER BY entidad"; List> sigemEntities = masterJdbcTemplate.queryForList(sql); // 2. Obtener Distritos de SNC String sncUrl = "https://www.catastro.gov.py/geoserver/ows?service=WFS&version=1.0.0&request=GetFeature&typeName=snc:ly_dist&maxFeatures=500&outputFormat=application/json"; String sncData = restTemplate.getForObject(sncUrl, String.class); List sncDistricts = parseSncData(sncData); // 3. Generar Reporte StringBuilder sb = new StringBuilder(); sb.append("# REPORTE COMPARATIVO FINAL: SIGEM vs SNC\n\n"); sb.append("| ID SIGEM | MUNICIPIO SIGEM | ESTADO | EQUIVALENTE SNC | DPTO | DIST |\n"); sb.append("| :--- | :--- | :--- | :--- | :--- | :--- |\n"); int matches = 0; Set mappedSnc = new HashSet<>(); for (Map entity : sigemEntities) { String id = String.valueOf(entity.get("entidad")); String nombre = (String) entity.get("nombre"); boolean activo = (boolean) entity.get("activo"); SncDist match = findMatch(nombre, sncDistricts); if (match != null) { matches++; mappedSnc.add(match.dept + "|" + match.code); sb.append(String.format("| %s | %s | %s | %s | %s | %s |\n", id, nombre, (activo ? "**ACTIVO**" : "INACTIVO"), match.name, match.dept, match.code)); } else { sb.append(String.format("| %s | %s | %s | *NO ENCONTRADO* | - | - |\n", id, nombre, (activo ? "**ACTIVO**" : "INACTIVO"))); } } sb.append("\n\n**Resumen:** Se encontraron " + matches + " coincidencias de un total de " + sigemEntities.size() + " entidades.\n"); return sb.toString(); } catch (Exception e) { return "Error: " + e.getMessage(); } } private SncDist findMatch(String name, List districts) { if (name == null) return null; String n = normalize(name); if (n.isEmpty()) return null; // 1. Coincidencia Exacta for (SncDist sd : districts) { if (normalize(sd.name).equals(n)) return sd; } // 2. Coincidencia de Contenido for (SncDist sd : districts) { String sn = normalize(sd.name); if (n.contains(sn) || sn.contains(n)) return sd; } return null; } private String normalize(String s) { if (s == null) return ""; return s.toUpperCase() .replace("Á", "A").replace("É", "E").replace("Í", "I").replace("Ó", "O").replace("Ú", "U") .replace("MUNICIPALIDAD DE ", "").replace("MUNICIPALIDAD ", "") .replace("MUNICIP. DE ", "").replace("MUNICIP ", "") .replace("CIUDAD DE ", "").replace("CIUDAD ", "") .replace("VILLA ", "").replace("SANTA ", "STA. ") .replaceAll("[^A-Z0-9 ]", "") .trim(); } private List parseSncData(String body) { List list = new ArrayList<>(); Pattern p = Pattern.compile("\\{\"type\":\"Feature\".*?\"properties\":\\{(.*?)\\}\\}"); Matcher m = p.matcher(body); while (m.find()) { String props = m.group(1); list.add(new SncDist( extract(props, "cod_dist"), extract(props, "cod_dpto"), extract(props, "nom_dist") )); } return list; } private String extract(String props, String key) { Pattern p = Pattern.compile("\"" + key + "\":\"?(.*?)\"?[,\\}]"); Matcher m = p.matcher(props); if (m.find()) return m.group(1).trim(); return "N/A"; } private String truncate(String s, int n) { if (s == null) return ""; return s.length() > n ? s.substring(0, n-3) + "..." : s; } static class SncDist { String code, dept, name; SncDist(String c, String d, String n) { this.code = c; this.dept = d; this.name = n; } } }