teqo/src/lib/bahiaGeometries.ts
Francisco Ignácio Fontoura Solla e5968f81d5 Add static Bahia municipality and territory geometries for the campaign map.
Ship TopoJSON meshes, IBGE code lookups, a re-runnable build script, and evidence tests so B3 can render Leaflet surfaces without PostGIS.
2026-07-18 22:07:41 -03:00

76 lines
2.5 KiB
TypeScript

/**
* Static Bahia municipality and identity-territory geometries for the campaign map.
*
* TopoJSON files are generated by `pnpm build:geometries` from IBGE Malhas (municípios)
* and dissolved by the official Territórios de Identidade composition in
* bahiaTerritories. See scripts/build-bahia-geometries.mjs for full provenance.
*
* Framework-free: safe to import from client components in B3 (Leaflet surfaces).
*/
import { feature } from 'topojson-client'
import type { Feature, MultiPolygon, Polygon } from 'geojson'
import type { GeometryCollection, Topology } from 'topojson-specification'
import municipalityTopologyJson from '@/lib/geometries/bahia-municipalities.topo.json'
import territoryTopologyJson from '@/lib/geometries/bahia-identity-territories.topo.json'
export type BahiaMunicipalityProperties = {
codarea: string
name: string
}
export type BahiaTerritoryProperties = {
code: string
name: string
}
export type BahiaMunicipalityFeature = Feature<
Polygon | MultiPolygon,
BahiaMunicipalityProperties
>
export type BahiaTerritoryFeature = Feature<Polygon | MultiPolygon, BahiaTerritoryProperties>
type MunicipalityTopology = Topology<{
municipalities: GeometryCollection<BahiaMunicipalityProperties>
}>
type TerritoryTopology = Topology<{
territories: GeometryCollection<BahiaTerritoryProperties>
}>
export const bahiaMunicipalitiesTopology =
municipalityTopologyJson as unknown as MunicipalityTopology
export const bahiaIdentityTerritoriesTopology =
territoryTopologyJson as unknown as TerritoryTopology
const municipalityFeatures = feature(
bahiaMunicipalitiesTopology,
bahiaMunicipalitiesTopology.objects.municipalities,
).features as BahiaMunicipalityFeature[]
const territoryFeatures = feature(
bahiaIdentityTerritoriesTopology,
bahiaIdentityTerritoriesTopology.objects.territories,
).features as BahiaTerritoryFeature[]
const municipalityByCodarea = new Map(
municipalityFeatures.map((entry) => [entry.properties.codarea, entry]),
)
const territoryByCode = new Map(
territoryFeatures.map((entry) => [entry.properties.code, entry]),
)
export const getMunicipalityFeature = (
codarea: string,
): BahiaMunicipalityFeature | undefined => municipalityByCodarea.get(codarea)
export const getTerritoryFeature = (code: string): BahiaTerritoryFeature | undefined =>
territoryByCode.get(code)
export const bahiaMunicipalityFeatures: readonly BahiaMunicipalityFeature[] =
municipalityFeatures
export const bahiaTerritoryFeatures: readonly BahiaTerritoryFeature[] = territoryFeatures