Repository navigation
Expand file tree
/
Copy pathuf-br.js
More file actions
439 lines (410 loc) · 13.3 KB
/
Copy pathuf-br.js
File metadata and controls
439 lines (410 loc) · 13.3 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
'use strict';
/**
* Brazilian UF helpers for OSM extract (nacional, extensível).
*
* A UF de um ponto sai do POLÍGONO da UF (`uf-poly.js`, malha do IBGE). Os
* retângulos de `UF_BBOX` continuam aqui como atalho barato e como último
* recurso para ponto fora de todos os polígonos — não decidem mais nada dentro
* do Brasil.
*/
var ufPoly = require('./uf-poly');
/** First two digits of IBGE município → UF */
var IBGE_UF = {
'11': 'RO',
'12': 'AC',
'13': 'AM',
'14': 'RR',
'15': 'PA',
'16': 'AP',
'17': 'TO',
'21': 'MA',
'22': 'PI',
'23': 'CE',
'24': 'RN',
'25': 'PB',
'26': 'PE',
'27': 'AL',
'28': 'SE',
'29': 'BA',
'31': 'MG',
'32': 'ES',
'33': 'RJ',
'35': 'SP',
'41': 'PR',
'42': 'SC',
'43': 'RS',
'50': 'MS',
'51': 'MT',
'52': 'GO',
'53': 'DF'
};
/**
* Approximate UF bboxes (degrees). Borders overlap on purpose —
* ufFromPoint picks the smaller box when multiple match.
* Fontes: limites administrativos aproximados (não oficiais IBGE).
*/
var UF_BBOX = {
AC: { latMin: -11.15, latMax: -7.11, lngMin: -73.99, lngMax: -66.62 },
AL: { latMin: -10.5, latMax: -8.81, lngMin: -38.24, lngMax: -35.15 },
AP: { latMin: -1.24, latMax: 4.44, lngMin: -54.88, lngMax: -49.86 },
AM: { latMin: -9.82, latMax: 2.25, lngMin: -73.8, lngMax: -56.1 },
BA: { latMin: -18.35, latMax: -8.53, lngMin: -46.62, lngMax: -37.34 },
CE: { latMin: -7.86, latMax: -2.78, lngMin: -41.42, lngMax: -37.25 },
DF: { latMin: -16.05, latMax: -15.5, lngMin: -48.29, lngMax: -47.31 },
ES: { latMin: -21.35, latMax: -17.85, lngMin: -41.9, lngMax: -39.65 },
GO: { latMin: -19.5, latMax: -12.4, lngMin: -53.25, lngMax: -45.9 },
MA: { latMin: -10.26, latMax: -1.05, lngMin: -48.76, lngMax: -41.8 },
MT: { latMin: -18.04, latMax: -7.35, lngMin: -61.63, lngMax: -50.22 },
MS: { latMin: -24.07, latMax: -17.17, lngMin: -58.17, lngMax: -50.92 },
MG: { latMin: -22.95, latMax: -14.2, lngMin: -51.05, lngMax: -39.75 },
PA: { latMin: -9.84, latMax: 2.59, lngMin: -58.9, lngMax: -46.03 },
PB: { latMin: -8.3, latMax: -6.03, lngMin: -38.77, lngMax: -34.79 },
PR: { latMin: -26.72, latMax: -22.52, lngMin: -54.62, lngMax: -48.02 },
PE: { latMin: -9.48, latMax: -7.15, lngMin: -41.36, lngMax: -34.81 },
PI: { latMin: -10.93, latMax: -2.74, lngMin: -45.99, lngMax: -40.37 },
RJ: { latMin: -23.4, latMax: -20.7, lngMin: -44.9, lngMax: -40.85 },
RN: { latMin: -6.98, latMax: -4.83, lngMin: -38.58, lngMax: -34.97 },
RS: { latMin: -33.75, latMax: -27.08, lngMin: -57.65, lngMax: -49.69 },
RO: { latMin: -13.69, latMax: -7.97, lngMin: -66.81, lngMax: -59.77 },
RR: { latMin: -1.58, latMax: 5.27, lngMin: -64.82, lngMax: -58.89 },
SC: { latMin: -29.35, latMax: -25.96, lngMin: -53.84, lngMax: -48.33 },
SP: { latMin: -25.35, latMax: -19.75, lngMin: -53.15, lngMax: -44.05 },
SE: { latMin: -11.57, latMax: -9.52, lngMin: -38.24, lngMax: -36.39 },
TO: { latMin: -13.47, latMax: -5.17, lngMin: -50.74, lngMax: -45.7 }
};
/** Alias histórico (Sudeste) — mesmos retângulos de UF_BBOX. */
var UF_BBOX_SE = {
SP: UF_BBOX.SP,
RJ: UF_BBOX.RJ,
MG: UF_BBOX.MG,
ES: UF_BBOX.ES
};
/** Regiões IBGE → lista de UFs (para --region=). */
var REGIOES = {
norte: ['AC', 'AP', 'AM', 'PA', 'RO', 'RR', 'TO'],
nordeste: ['AL', 'BA', 'CE', 'MA', 'PB', 'PE', 'PI', 'RN', 'SE'],
'centro-oeste': ['DF', 'GO', 'MT', 'MS'],
centrooeste: ['DF', 'GO', 'MT', 'MS'],
co: ['DF', 'GO', 'MT', 'MS'],
sudeste: ['ES', 'MG', 'RJ', 'SP'],
se: ['ES', 'MG', 'RJ', 'SP'],
sul: ['PR', 'RS', 'SC']
};
var NOME_UF = {
acre: 'AC',
alagoas: 'AL',
amapa: 'AP',
amazonas: 'AM',
bahia: 'BA',
ceara: 'CE',
'distrito federal': 'DF',
'espirito santo': 'ES',
goias: 'GO',
maranhao: 'MA',
'mato grosso': 'MT',
'mato grosso do sul': 'MS',
'minas gerais': 'MG',
para: 'PA',
paraiba: 'PB',
parana: 'PR',
pernambuco: 'PE',
piaui: 'PI',
'rio de janeiro': 'RJ',
'rio grande do norte': 'RN',
'rio grande do sul': 'RS',
rondonia: 'RO',
roraima: 'RR',
'santa catarina': 'SC',
'sao paulo': 'SP',
sergipe: 'SE',
tocantins: 'TO'
};
function ufFromIbge(ibge) {
if (ibge == null || ibge === '') return '';
var s = String(ibge).replace(/\D/g, '');
if (s.length < 2) return '';
return IBGE_UF[s.slice(0, 2)] || '';
}
function normalizeUfToken(raw) {
if (raw == null || raw === '') return '';
var s = String(raw).trim();
if (!s) return '';
// ISO3166-2:BR-SP or BR-SP
var m = s.match(/^(?:ISO3166-2:)?BR-([A-Za-z]{2})$/i);
if (m) return m[1].toUpperCase();
if (/^[A-Za-z]{2}$/.test(s)) return s.toUpperCase();
var lower = s
.toLowerCase()
.normalize('NFD')
.replace(/[\u0300-\u036f]/g, '');
if (NOME_UF[lower]) return NOME_UF[lower];
return '';
}
/**
* Resolve UF from OSM tags (order: ISO, addr:state, is_in:state, ref, name for states).
*/
function ufFromTags(tags) {
if (!tags) return '';
var keys = [
'ISO3166-2',
'addr:state',
'is_in:state',
'is_in:state_code',
'state',
'ref'
];
for (var i = 0; i < keys.length; i++) {
var u = normalizeUfToken(tags[keys[i]]);
if (u && u.length === 2) return u;
}
if (tags.place === 'state' || tags.admin_level === '4') {
u = normalizeUfToken(tags.name) || normalizeUfToken(tags['name:pt']);
if (u) return u;
}
return '';
}
function pointInBbox(lat, lon, box) {
return (
lat >= box.latMin &&
lat <= box.latMax &&
lon >= box.lngMin &&
lon <= box.lngMax
);
}
/**
* Heuristic UF from a point using UF bboxes (default: Brasil inteiro).
* Prefer smaller boxes when multiple match (ES/RJ over MG/SP when nested edge).
*/
function ufFromPoint(lat, lon, bboxes) {
if (lat == null || lon == null || !isFinite(lat) || !isFinite(lon)) return '';
bboxes = bboxes || UF_BBOX;
var hits = [];
for (var uf in bboxes) {
if (!Object.prototype.hasOwnProperty.call(bboxes, uf)) continue;
var b = bboxes[uf];
if (pointInBbox(lat, lon, b)) {
var area = (b.latMax - b.latMin) * (b.lngMax - b.lngMin);
hits.push({ uf: uf, area: area });
}
}
if (!hits.length) return '';
hits.sort(function (a, b) {
return a.area - b.area;
});
return hits[0].uf;
}
/**
* UF do ponto por geometria: POLÍGONO primeiro (malha do IBGE) e, só quando o
* ponto cai fora de todos eles — mar aberto, plataforma, fora do Brasil —, o
* retângulo antigo.
*
* A ordem importa nos dois sentidos: o polígono é quem separa MG de GO, e o
* retângulo de resto garante que nenhum ponto que hoje recebe rótulo passe a
* ser XX por causa da simplificação do contorno ou de um trecho no mar.
*/
function ufFromPointGeo(lat, lon, bboxes) {
var u = ufPoly.ufFromPointPoly(lat, lon);
if (u) return u;
return ufFromPoint(lat, lon, bboxes);
}
/**
* Ponto representativo das opções: o par lat/lng, ou o centro do bbox.
*/
function pointOfOptions(options) {
var lat = options.lat;
var lon = options.lng != null ? options.lng : options.lon;
if (lat == null && options.lat_min != null && options.lat_max != null) {
lat = (Number(options.lat_min) + Number(options.lat_max)) / 2;
lon = (Number(options.lng_min) + Number(options.lng_max)) / 2;
}
return { lat: lat, lon: lon };
}
/**
* UF from feature: tags → IBGE → geometria (polígono, e retângulo de resto).
*/
function resolveUf(options) {
options = options || {};
var u = ufFromTags(options.tags);
if (u) return u;
if (options.ibge) {
u = ufFromIbge(options.ibge);
if (u) return u;
}
var p = pointOfOptions(options);
u = ufFromPointGeo(p.lat, p.lon, options.bboxes);
return u || 'XX';
}
/**
* UF da feature RESPEITANDO o filtro do run (`--uf` / `--region`).
*
* ─── por que isto existe ──────────────────────────────────────────────────
*
* `ufFromPoint` desempata caixas sobrepostas pela de MENOR ÁREA, e a de GO
* (52°²) é menor que a de MG (99°²). Rodando `--uf=MG`, uma via em Patrocínio
* (−18,93 / −46,97) cai nas duas e saía rotulada **GO** — enquanto
* `passesUfFilter` a MANTINHA no run, justamente por estar dentro da caixa de
* MG. Filtro e rótulo discordavam: a via era extraída e escrita em
* `OSM_LOGRADOURO_GEOM_GO`, onde nenhum consumidor de MG vai procurar.
*
* O estrago não é de borda. A caixa de GO cobre todo o Triângulo, o Alto
* Paranaíba e o Noroeste de MG; a da BA (91°²) cobre o norte. Na carga de MG do
* DDSOFT isso deixou 29.505 dos 96.426 ways referenciados sem traçado — e
* Patrocínio, cidade inteira de um cliente, com 3,5% de cobertura real
* (18/08/2026).
*
* ─── a regra, desde o polígono (18/08/2026) ──────────────────────────────
*
* Tag e IBGE continuam mandando: são dado explícito da feature, e quem decide
* se ela entra ou não no run é o filtro, não o rótulo.
*
* Faltando os dois, quem nomeia é o POLÍGONO da UF — e ele nomeia igual com ou
* sem filtro. A muleta acima (deixar a caixa permitida nomear) ficou só para o
* ponto que cai fora de TODOS os polígonos: mar aberto, plataforma, um trecho
* fora do Brasil. Ali continua valendo o retângulo permitido, porque ali o
* retângulo é tudo o que existe.
*
* O par rótulo/filtro voltou a sair da mesma conta, agora sem mentir: num run
* `--uf=MG`, uma via realmente goiana é rotulada GO **e descartada** pelo
* `passesUfFilter` — antes ela era mantida e escrita como se fosse de MG.
*/
function resolveUfFiltered(options, ufAllow) {
if (!ufAllow) return resolveUf(options);
options = options || {};
var byTag = ufFromTags(options.tags);
if (!byTag && options.ibge) byTag = ufFromIbge(options.ibge);
if (byTag) return byTag;
var p = pointOfOptions(options);
var byPoly = ufPoly.ufFromPointPoly(p.lat, p.lon);
if (byPoly) return byPoly;
var allowed = ufFromPoint(p.lat, p.lon, bboxesForFilter(ufAllow));
if (allowed) return allowed;
return ufFromPoint(p.lat, p.lon, options.bboxes) || 'XX';
}
function extractIbge(tags) {
if (!tags) return { ibge: '', source_tag: '' };
if (tags['IBGE:GEOCODIGO']) {
return { ibge: String(tags['IBGE:GEOCODIGO']).trim(), source_tag: 'IBGE:GEOCODIGO' };
}
if (tags['ref:IBGE']) {
return { ibge: String(tags['ref:IBGE']).trim(), source_tag: 'ref:IBGE' };
}
if (tags['IBGE']) {
return { ibge: String(tags['IBGE']).trim(), source_tag: 'IBGE' };
}
return { ibge: '', source_tag: '' };
}
/**
* Expand --region / --uf CLI tokens into a map of UF codes.
* Accepts: "SP,RJ", "sudeste", "norte+nordeste", "centro-oeste".
* Returns null if empty (sem filtro).
*/
function parseUfFilter(ufStr, regionStr) {
var out = Object.create(null);
var n = 0;
function addUf(u) {
if (!u || !isKnownUf(u) || out[u]) return;
out[u] = true;
n++;
}
function addToken(tok) {
if (!tok) return;
tok = String(tok).trim().toLowerCase();
if (!tok) return;
var regKey = tok
.normalize('NFD')
.replace(/[\u0300-\u036f]/g, '')
.replace(/\s+/g, '-');
if (REGIOES[regKey]) {
REGIOES[regKey].forEach(addUf);
return;
}
addUf(normalizeUfToken(tok));
}
function splitList(s) {
if (!s) return;
String(s)
.split(/[,+\s]+/)
.forEach(addToken);
}
splitList(ufStr);
splitList(regionStr);
if (!n) return null;
return out;
}
function isKnownUf(u) {
if (!u || u.length !== 2) return false;
for (var k in IBGE_UF) {
if (IBGE_UF[k] === u) return true;
}
return false;
}
/** Bboxes only for UFs in the allow-map (or all if null). */
function bboxesForFilter(ufAllow) {
if (!ufAllow) return UF_BBOX;
var out = {};
for (var uf in ufAllow) {
if (ufAllow[uf] && UF_BBOX[uf]) out[uf] = UF_BBOX[uf];
}
return out;
}
/**
* Whether a resolved UF / point should be kept under an optional filter.
*
* Mesma fonte de verdade da nomeação: quem decide é o POLÍGONO. Feature que o
* polígono põe fora de todas as UFs permitidas é DESCARTADA do run — antes o
* retângulo a mantinha, e ela era gravada com o nome de uma UF que não era a
* dela, que é o defeito que originou tudo isto.
*
* Só quando o ponto está fora de todos os polígonos (mar, plataforma, fora do
* Brasil) o retângulo volta a decidir — sem ele, um trecho no mar perderia o
* rótulo que hoje tem.
*/
function passesUfFilter(ufAllow, uf, lat, lon) {
if (!ufAllow) return true;
if (uf && uf !== 'XX' && ufAllow[uf]) return true;
if (lat != null && lon != null && isFinite(lat) && isFinite(lon)) {
var byPoly = ufPoly.ufFromPointPoly(lat, lon);
if (byPoly) return ufAllow[byPoly] === true;
var boxes = bboxesForFilter(ufAllow);
for (var k in boxes) {
if (pointInBbox(lat, lon, boxes[k])) return true;
}
}
return false;
}
/** Early reject when tags already resolve to a disallowed UF. */
function tagsDisallowedByFilter(ufAllow, tags, ibge) {
if (!ufAllow) return false;
var u = ufFromTags(tags);
if (!u && ibge) u = ufFromIbge(ibge);
if (u && u !== 'XX' && !ufAllow[u]) return true;
return false;
}
function ufAllowList(ufAllow) {
if (!ufAllow) return [];
return Object.keys(ufAllow).filter(function (k) {
return ufAllow[k];
}).sort();
}
module.exports = {
IBGE_UF: IBGE_UF,
UF_BBOX: UF_BBOX,
UF_BBOX_SE: UF_BBOX_SE,
REGIOES: REGIOES,
ufFromIbge: ufFromIbge,
normalizeUfToken: normalizeUfToken,
ufFromTags: ufFromTags,
ufFromPoint: ufFromPoint,
ufFromPointPoly: ufPoly.ufFromPointPoly,
ufFromPointGeo: ufFromPointGeo,
resolveUf: resolveUf,
resolveUfFiltered: resolveUfFiltered,
extractIbge: extractIbge,
pointInBbox: pointInBbox,
parseUfFilter: parseUfFilter,
bboxesForFilter: bboxesForFilter,
passesUfFilter: passesUfFilter,
tagsDisallowedByFilter: tagsDisallowedByFilter,
ufAllowList: ufAllowList,
isKnownUf: isKnownUf
};