chile-reverse-geocoder 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ """chile-reverse-geocoder: Offline administrative reverse geocoding for Chile."""
2
+
3
+ from .exceptions import InvalidCoordinatesError, InvalidLevelError
4
+ from .geocoder import lookup
5
+
6
+ __all__ = ["lookup", "InvalidCoordinatesError", "InvalidLevelError"]
7
+ __version__ = "0.1.0"
Binary file
@@ -0,0 +1,6 @@
1
+ class InvalidCoordinatesError(ValueError):
2
+ """Raised when lat/lon values are outside valid geographic ranges or are not finite numbers."""
3
+
4
+
5
+ class InvalidLevelError(ValueError):
6
+ """Raised when the 'level' argument is not one of: None, 'region', 'province', 'commune'."""
@@ -0,0 +1,81 @@
1
+ from __future__ import annotations
2
+
3
+ import math
4
+
5
+ import shapely
6
+
7
+ from .exceptions import InvalidCoordinatesError, InvalidLevelError
8
+ from .loader import GeocoderData, load_data
9
+
10
+ _VALID_LEVELS = frozenset({"region", "province", "commune"})
11
+
12
+ _cache: GeocoderData | None = None
13
+
14
+
15
+ def _get_data() -> GeocoderData:
16
+ global _cache
17
+ if _cache is None:
18
+ _cache = load_data()
19
+ return _cache
20
+
21
+
22
+ def _validate_coordinates(lat: float, lon: float) -> None:
23
+ if not isinstance(lat, (int, float)) or not isinstance(lon, (int, float)):
24
+ raise InvalidCoordinatesError(
25
+ f"lat and lon must be numeric, got {type(lat).__name__}, {type(lon).__name__}"
26
+ )
27
+ if math.isnan(lat) or math.isnan(lon) or math.isinf(lat) or math.isinf(lon):
28
+ raise InvalidCoordinatesError(
29
+ f"lat and lon must be finite numbers, got lat={lat}, lon={lon}"
30
+ )
31
+ if not (-90.0 <= lat <= 90.0):
32
+ raise InvalidCoordinatesError(f"lat must be in [-90, 90], got {lat}")
33
+ if not (-180.0 <= lon <= 180.0):
34
+ raise InvalidCoordinatesError(f"lon must be in [-180, 180], got {lon}")
35
+
36
+
37
+ def lookup(lat: float, lon: float, level: str | None = None) -> dict | None:
38
+ """Return administrative divisions for the given coordinates.
39
+
40
+ Args:
41
+ lat: Latitude in decimal degrees (WGS84).
42
+ lon: Longitude in decimal degrees (WGS84).
43
+ level: One of 'region', 'province', 'commune', or None for all levels.
44
+
45
+ Returns:
46
+ Dict with administrative division(s), or None if outside Chile.
47
+
48
+ Raises:
49
+ InvalidCoordinatesError: If lat/lon are not valid geographic coordinates.
50
+ InvalidLevelError: If level is not a recognized value.
51
+ """
52
+ _validate_coordinates(lat, lon)
53
+ if level is not None and level not in _VALID_LEVELS:
54
+ raise InvalidLevelError(
55
+ f"level must be one of {sorted(_VALID_LEVELS)} or None, got {level!r}"
56
+ )
57
+
58
+ data = _get_data()
59
+ point = shapely.Point(lon, lat)
60
+ candidates = data.tree.query(point, predicate="intersects")
61
+
62
+ if len(candidates) == 0:
63
+ return None
64
+
65
+ idx = int(candidates[0])
66
+ attrs = data.attributes
67
+
68
+ if level is None:
69
+ return {
70
+ "region": attrs["REGION"][idx],
71
+ "province": attrs["PROVINCIA"][idx],
72
+ "commune": attrs["COMUNA"][idx],
73
+ "region_code": attrs["CUT_REG"][idx],
74
+ "province_code": attrs["CUT_PROV"][idx],
75
+ "commune_code": attrs["CUT_COM"][idx],
76
+ }
77
+ if level == "region":
78
+ return {"region": attrs["REGION"][idx], "region_code": attrs["CUT_REG"][idx]}
79
+ if level == "province":
80
+ return {"province": attrs["PROVINCIA"][idx], "province_code": attrs["CUT_PROV"][idx]}
81
+ return {"commune": attrs["COMUNA"][idx], "commune_code": attrs["CUT_COM"][idx]}
@@ -0,0 +1,25 @@
1
+ from __future__ import annotations
2
+
3
+ from dataclasses import dataclass
4
+ from pathlib import Path
5
+
6
+ import shapely
7
+ import pyarrow.parquet as pq
8
+
9
+ _DATA_PATH = Path(__file__).parent / "data" / "dpa.parquet"
10
+
11
+ _FIELDS = ("CUT_REG", "CUT_PROV", "CUT_COM", "REGION", "PROVINCIA", "COMUNA")
12
+
13
+
14
+ @dataclass(slots=True, frozen=True)
15
+ class GeocoderData:
16
+ tree: shapely.STRtree
17
+ attributes: dict[str, list]
18
+
19
+
20
+ def load_data() -> GeocoderData:
21
+ table = pq.read_table(_DATA_PATH, columns=[*_FIELDS, "geometry"])
22
+ geometries = shapely.from_wkb(table["geometry"].to_pylist())
23
+ tree = shapely.STRtree(geometries)
24
+ attributes = {f: table[f].to_pylist() for f in _FIELDS}
25
+ return GeocoderData(tree=tree, attributes=attributes)
@@ -0,0 +1,149 @@
1
+ Metadata-Version: 2.4
2
+ Name: chile-reverse-geocoder
3
+ Version: 0.1.0
4
+ Summary: Offline administrative reverse geocoding for Chile (region, province, commune)
5
+ Author-email: Gonzalo Sepúlveda Navarrete <sepulveda.navarrete1996@gmail.com>
6
+ License: MIT License
7
+
8
+ Copyright (c) 2026 Gonzalo Sepúlveda Navarrete
9
+
10
+ Permission is hereby granted, free of charge, to any person obtaining a copy
11
+ of this software and associated documentation files (the "Software"), to deal
12
+ in the Software without restriction, including without limitation the rights
13
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14
+ copies of the Software, and to permit persons to whom the Software is
15
+ furnished to do so, subject to the following conditions:
16
+
17
+ The above copyright notice and this permission notice shall be included in all
18
+ copies or substantial portions of the Software.
19
+
20
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26
+ SOFTWARE.
27
+
28
+ Project-URL: Homepage, https://github.com/gonzalosn/chile-reverse-geocoder
29
+ Project-URL: Bug Tracker, https://github.com/gonzalosn/chile-reverse-geocoder/issues
30
+ Keywords: geocoding,chile,reverse-geocoding,gis,offline,spatial
31
+ Classifier: Programming Language :: Python :: 3
32
+ Classifier: Programming Language :: Python :: 3.10
33
+ Classifier: Programming Language :: Python :: 3.11
34
+ Classifier: Programming Language :: Python :: 3.12
35
+ Classifier: Programming Language :: Python :: 3.13
36
+ Classifier: License :: OSI Approved :: MIT License
37
+ Classifier: Operating System :: OS Independent
38
+ Classifier: Topic :: Scientific/Engineering :: GIS
39
+ Classifier: Intended Audience :: Developers
40
+ Classifier: Intended Audience :: Science/Research
41
+ Requires-Python: >=3.10
42
+ Description-Content-Type: text/markdown
43
+ License-File: LICENSE
44
+ License-File: NOTICE
45
+ Requires-Dist: shapely>=2.0
46
+ Requires-Dist: pyogrio>=0.9
47
+ Requires-Dist: pyarrow>=13.0
48
+ Provides-Extra: dev
49
+ Requires-Dist: pytest>=7.0; extra == "dev"
50
+ Dynamic: license-file
51
+
52
+ # chile-reverse-geocoder
53
+
54
+ Reverse geocoding administrativo offline para Chile. Dado un par de coordenadas (lat, lon), retorna la región, provincia y comuna sin necesidad de API externa ni conexión a internet.
55
+
56
+ ## Fuente de datos
57
+
58
+ Esta librería utiliza como base la capa oficial **División Política Administrativa (DPA) 2023** publicada por la Infraestructura de Datos Geoespaciales de Chile [IDE Chile](https://geoportal.cl/geoportal/catalog/36391/Divisi%C3%B3n%20Pol%C3%ADtica%20Administrativa%202023)
59
+
60
+ Los límites administrativos y códigos territoriales corresponden a la cartografía oficial distribuida por IDE Chile.
61
+
62
+ Fuente:
63
+
64
+ * IDE Chile — División Político Administrativa (DPA)
65
+
66
+ Los datos fueron procesados para su uso offline mediante:
67
+
68
+ * eliminación de atributos no utilizados,
69
+ * simplificación de geometrías,
70
+ * conversión a formato GeoParquet,
71
+ * construcción de índice espacial.
72
+
73
+ Las modificaciones realizadas corresponden únicamente al formato y optimización del almacenamiento; la fuente original y propiedad de los datos permanecen con el organismo correspondiente.
74
+
75
+ Consulte la licencia y condiciones de uso del dataset original antes de reutilizar los datos.
76
+
77
+
78
+ ## Instalación
79
+
80
+ ```bash
81
+ pip install chile-reverse-geocoder
82
+ ```
83
+
84
+ ## Uso básico
85
+
86
+ ```python
87
+ from chile_reverse_geocoder import lookup
88
+
89
+ result = lookup(-33.4489, -70.6693)
90
+ print(result)
91
+ # {
92
+ # "region": "Metropolitana de Santiago",
93
+ # "province": "Santiago",
94
+ # "commune": "Santiago",
95
+ # "region_code": "13",
96
+ # "province_code": "131",
97
+ # "commune_code": "13101"
98
+ # }
99
+ ```
100
+
101
+ ## Filtrar por nivel administrativo
102
+
103
+ ```python
104
+ # Solo región
105
+ lookup(-33.4489, -70.6693, level="region")
106
+ # {"region": "Metropolitana de Santiago", "region_code": "13"}
107
+
108
+ # Solo provincia
109
+ lookup(-33.4489, -70.6693, level="province")
110
+ # {"province": "Santiago", "province_code": "131"}
111
+
112
+ # Solo comuna
113
+ lookup(-33.4489, -70.6693, level="commune")
114
+ # {"commune": "Santiago", "commune_code": "13101"}
115
+ ```
116
+
117
+ ## Casos especiales
118
+
119
+ ```python
120
+ # Coordenadas fuera de Chile → None
121
+ lookup(-34.6037, -58.3816) # Buenos Aires → None
122
+
123
+ # Coordenadas inválidas → excepción
124
+ from chile_reverse_geocoder import InvalidCoordinatesError
125
+ lookup(91.0, 0.0) # raises InvalidCoordinatesError
126
+
127
+ # Nivel inválido → excepción
128
+ from chile_reverse_geocoder import InvalidLevelError
129
+ lookup(-33.4489, -70.6693, level="city") # raises InvalidLevelError
130
+ ```
131
+
132
+ ## Descripción técnica
133
+
134
+ - **Offline**: No realiza llamadas a servicios externos. Los datos están empaquetados dentro de la librería (~2 MB).
135
+ - **Sin API keys**: No requiere configuración de credenciales.
136
+ - **Índice espacial STRtree**: Usa `shapely.STRtree` para búsqueda espacial eficiente O(log n). El índice se construye una sola vez (lazy loading) y se reutiliza en llamadas sucesivas mediante un singleton en memoria.
137
+ - **Datos**: División Político-Administrativa (DPA) de Chile con 345 comunas. Almacenado en formato GeoParquet con geometrías simplificadas.
138
+
139
+ ## Dependencias
140
+
141
+ - `shapely >= 2.0`
142
+ - `pyogrio >= 0.9`
143
+ - `pyarrow >= 13.0`
144
+
145
+ ## Licencia
146
+
147
+ MIT
148
+
149
+ Los datos administrativos incluidos o derivados mantienen las condiciones de uso establecidas por la fuente oficial correspondiente (IDE Chile).
@@ -0,0 +1,11 @@
1
+ chile_reverse_geocoder/__init__.py,sha256=qywCsUy-L3-VtUiaLLPZyma5Lc5ZWJTlfwzmChAhVRc,271
2
+ chile_reverse_geocoder/exceptions.py,sha256=BBL430U7bgjijgynvePVVTW81merEF_IHA799lMiIyY,279
3
+ chile_reverse_geocoder/geocoder.py,sha256=VvCq_ClFqanm400hyZPNj-o0N0xIg_lPGK2nNDHa__M,2812
4
+ chile_reverse_geocoder/loader.py,sha256=7rgOxty0M4TI41flZDp0CTw9fQ-KpBypLXdlL3Anvis,723
5
+ chile_reverse_geocoder/data/dpa.parquet,sha256=sjczq6n1AlizOUEVmdVlnFCek2Sxpo85d4Vpa-woY9k,3235801
6
+ chile_reverse_geocoder-0.1.0.dist-info/licenses/LICENSE,sha256=CUC08lwt7oaJ_a2sPCMCR-9JjGz5Y0htvaiUm-5we5M,1085
7
+ chile_reverse_geocoder-0.1.0.dist-info/licenses/NOTICE,sha256=hYDt1l3GzeWC8HmeJhR30E6tGGR8asTnmlECnh_d2p4,532
8
+ chile_reverse_geocoder-0.1.0.dist-info/METADATA,sha256=AAzT04Lzv1E1p5HfWf7fI438X8uctKz9AFOofGsXLg8,5709
9
+ chile_reverse_geocoder-0.1.0.dist-info/WHEEL,sha256=aeYiig01lYGDzBgS8HxWXOg3uV61G9ijOsup-k9o1sk,91
10
+ chile_reverse_geocoder-0.1.0.dist-info/top_level.txt,sha256=Wd9asf2QSE1-l1kTHizYUbXjzwrmf7bSIbzSRYqTgqg,23
11
+ chile_reverse_geocoder-0.1.0.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (82.0.1)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Gonzalo Sepúlveda Navarrete
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,17 @@
1
+ NOTICE
2
+
3
+ This package includes administrative boundary data derived from the official División Política Administrativa (DPA) dataset published by IDE Chile.
4
+
5
+ Source:
6
+ https://www.geoportal.cl/
7
+
8
+ The original geographic data remains subject to its original licensing and usage terms.
9
+
10
+ Transformations applied by this project:
11
+
12
+ * Removed unused attributes
13
+ * Simplified geometries
14
+ * Converted to GeoParquet
15
+ * Built spatial index for offline reverse geocoding
16
+
17
+ No changes were made to the administrative meaning of the original dataset.
@@ -0,0 +1 @@
1
+ chile_reverse_geocoder