AnalysisController.java
5.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
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<Map<String, Object>> 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<SncDist> 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<String> mappedSnc = new HashSet<>();
for (Map<String, Object> 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<SncDist> 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<SncDist> parseSncData(String body) {
List<SncDist> 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; }
}
}