// SPDX-FileCopyrightText: Teffen Ellis and Mailwoman contributors // SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial /** * `ide-uy`: Uruguay's national address points, `Direcciones Geográficas del Uruguay`, published * by the Infraestructura de Datos Espaciales (IDE) on catalogodatos.gub.uy as one CSV per department. * * The CSVs are Windows-1252 (`ORDOÑEZ` reads `ORDO\xd1EZ`), and quote a street name that * holds a nickname in doubled quotes (`"AVENIDA JOSE ""PEPE"" D' ELIA"`). * Absent values read `N/A`. * * The adapter reads `nombre_via` as the street, `num_puerta` with `letra_puerta` as * the house number, `codigo_postal` as the postcode, `localidad` as the locality, * `departamento` as the region and `nombre_inmueble` as a venue. * The census value ` RURAL` represents the department's rural remainder * rather than a place, so the row keeps the department and omits locality. * * The register assigns a generated code (`UYARMQA_SN_012`) to an unnamed street, * and the adapter drops that row. * It also drops a point located by a route kilometer because the record has no door number to render. * * The IDE publishes the dataset under the Licencia de Datos Abiertos – Uruguay. * The license grants adaptation and commercial use. * * Its note of origin must name the provider, the license and the dataset, and mention changes. * Every row records that license. */ import { formatAddressRow } from "@mailwoman/codex/address/format" import { readLocalBuffer } from "@mailwoman/core/fs/readers" import { tryStat } from "@mailwoman/core/fs/readers/stat" import { decodeBytes } from "@mailwoman/core/fs/streams" import { acceptPostcode } from "@mailwoman/postcode/country-formats" import { resolvePath } from "path-ts" import { CSVSpliterator } from "spliterator" import { Globerator } from "spliterator/node/fs" import { UnsupportedCountryError } from "#adapters/errors" import { present } from "#adapters/present" import { stableSourceIDFromParts } from "#adapters/source-id" import { composeHouseNumber } from "#adapters/street/line" import { SourceRegister } from "#registers" import { AddressRole, type AdapterOptions, type CanonicalRow, type CorpusAdapter, countDropped, SurfaceOrigin, } from "#types" /** * Registry id for this adapter, stamped into every row it emits. */ export const IDE_UY_ADAPTER_ID = "ide-uy" /** * The license the IDE publishes the dataset under. * It has no SPDX identifier. */ export const IDE_UY_LICENSE = "LicenseRef-Licencia-Datos-Abiertos-Uruguay-0.1" /** * Every jurisdiction this adapter emits, checked against a caller's `--country`. */ export const IDE_UY_COUNTRIES: readonly string[] = ["UY"] /** * The columns the adapter reads from each department's CSV. */ export interface IDEUruguayRecord { codigo_postal: string codigo_via: string nombre_via: string num_puerta: string letra_puerta: string km: string nombre_inmueble: string localidad: string departamento: string latitud: string longitud: string } const ABSENT = "N/A" const PRESENT_OPTIONS = { absentValues: [ABSENT] } as const /** * The house-number suffixes `letra_puerta` holds that a door number is written with: * `BIS` and a single letter. * * The two-letter values (`DU`, `CC`, `TR`) are codes the dataset's metadata * doesn't define, so they're left off. */ const LETTER_SUFFIX = /^(?:BIS|[A-Z])$/u /** * The register assigns a generated code, such as `UYARMQA_SN_012`, to a street it leaves unnamed. */ const GENERATED_STREET = /_SN_|^[A-Z0-9]+_[A-Z0-9_]+$/u /** * Maps one record onto the components the corpus asserts, or returns null when the row is refused. * * Each refusal increments its reason in `dropped`. */ export function componentsForIDERecord( record: IDEUruguayRecord, dropped?: Pick ): CanonicalRow["components"] | null { const tally = dropped ?? {} const street = present(record.nombre_via, PRESENT_OPTIONS) if (!street) { countDropped(tally, "row:no-street") return null } if (GENERATED_STREET.test(street)) { countDropped(tally, "row:street-generated-code") return null } const number = present(record.num_puerta, PRESENT_OPTIONS) if (!number) { countDropped( tally, present(record.km, PRESENT_OPTIONS) ? "row:route-kilometer-without-door-number" : "row:no-door-number" ) return null } const letter = present(record.letra_puerta, PRESENT_OPTIONS) const suffix = letter && LETTER_SUFFIX.test(letter) ? letter : "" if (letter && !suffix) { countDropped(tally, "component:letra_puerta:code") } const components: CanonicalRow["components"] = { house_number: composeHouseNumber(number, suffix, " "), street, } const venue = present(record.nombre_inmueble, PRESENT_OPTIONS) // A post office's distribution agent is listed under the agent's own name (`Agente` and a person's names), // and a code such as `CH109` names no place, so neither becomes a venue. if (venue && /^AGENTE\b/iu.test(venue)) { countDropped(tally, "component:venue:names-a-person") } else if (venue && !/\p{L}{3}/u.test(venue)) { countDropped(tally, "component:venue:code") } else if (venue) { components.venue = venue } const postcode = present(record.codigo_postal, PRESENT_OPTIONS) const matched = postcode ? acceptPostcode(postcode, "UY") : null if (matched) { components.postcode = matched } else if (postcode) { countDropped(tally, "component:postcode:not-country-format") } const locality = present(record.localidad, PRESENT_OPTIONS) const region = present(record.departamento, PRESENT_OPTIONS) if (locality && !locality.endsWith(" RURAL")) { components.locality = locality } else if (locality) { countDropped(tally, "component:locality:rural-remainder") } if (region && region !== components.locality) { components.region = region } if (!components.locality && !components.postcode) { countDropped(tally, "row:no-locality-or-postcode") return null } return components } /** * The department CSVs an input names: the file itself, or every `.csv` in a directory, in name order. */ async function departmentFiles(inputPath: string): Promise { const stat = await tryStat(inputPath) if (!stat?.isDirectory()) return [inputPath] const names = await Globerator.from("*.csv", { cwd: inputPath, absolute: false }).toSorted() return names.map((name) => `${inputPath}/${name}`) } export function createIDEUruguayAdapter(): CorpusAdapter { return { id: IDE_UY_ADAPTER_ID, defaultLicense: IDE_UY_LICENSE, addressRole: AddressRole.Premise, register: SourceRegister.UruguayIDEDirecciones, surface: SurfaceOrigin.Rendered, description: "Direcciones Geográficas del Uruguay (IDE Uruguay): national address points, one CSV per department.", async *rows(opts: AdapterOptions): AsyncIterable { if (opts.country && !IDE_UY_COUNTRIES.includes(opts.country)) { throw new UnsupportedCountryError(IDE_UY_ADAPTER_ID, IDE_UY_COUNTRIES, opts.country) } let emitted = 0 for (const file of await departmentFiles(resolvePath(opts.inputPath))) { // The largest department file is 65 MB, and the publisher fixes the partition, so each is decoded whole. const text = decodeBytes(await readLocalBuffer(file), "windows-1252") const records = CSVSpliterator.from(Buffer.from(text, "utf8"), { normalizeKeys: false }) for (const record of records) { if (opts.signal?.aborted) return if (typeof opts.limit === "number" && emitted >= opts.limit) return const components = componentsForIDERecord(record, opts) if (!components) continue const rendered = formatAddressRow(components, "UY", { singleLine: true }) if (!rendered) { countDropped(opts, "row:render-failed") continue } for (const tag of rendered.unplaced) { countDropped(opts, `component:${tag}:not-rendered`) } const { raw, components: aligned } = rendered yield { raw, components: aligned, country: "UY", locale: "es-UY", source: IDE_UY_ADAPTER_ID, // The point's coordinates separate two doors the rendered components cannot, // such as the several access points one `num_puerta` has on a corner lot. source_id: stableSourceIDFromParts(IDE_UY_ADAPTER_ID, { ...aligned, lat: record.latitud ?? "", lon: record.longitud ?? "", }), license: IDE_UY_LICENSE, } emitted++ } } }, } } /** * The configured adapter instance registered with the corpus builder. */ export const ideUruguayAdapter = createIDEUruguayAdapter()