geohierarchy.geocoding¶
Geocoding helpers (one implementation for every package of the project): a place's boundary, place-name suggestions for a query or an area, and the country / subdivision of a point.
Nominatim (OpenStreetMap) is asked with a timeout and retries on rate limits; reverse-geocoding answers are cached on disk
(<cache>/geocoding/reverse.json; cache folder from cache_dir, else GEOHIERARCHY_CACHE_DIR, else the temp folder), since
the country of a point never changes and the analysis date of a feed must not depend on a busy geocoder.
get_cache_dir(cache_dir=None)
¶
Folder of geohierarchy's caches: cache_dir, else GEOHIERARCHY_CACHE_DIR, else <tempdir>/geohierarchy-cache.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cache_dir
|
Optional[Union[str, Path]]
|
Explicit folder. |
None
|
Returns:
| Type | Description |
|---|---|
Path
|
The folder (not created). |
reverse_geocode(lat, lon, cache_dir=None)
¶
Nominatim's address of a point (zoom 10: city level), cached on disk by the point rounded to 0.01 degree.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lat
|
float
|
Latitude. |
required |
lon
|
float
|
Longitude. |
required |
cache_dir
|
Optional[Union[str, Path]]
|
Cache folder (see :func: |
None
|
Returns:
| Type | Description |
|---|---|
dict
|
The |
get_country_region(lat, lon, code_format='alpha_2', get_region=True, cache_dir=None)
¶
Country (and ISO 3166-2 subdivision) of a point, by reverse geocoding.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lat
|
float
|
Latitude. |
required |
lon
|
float
|
Longitude. |
required |
code_format
|
str
|
Country code returned: |
'alpha_2'
|
get_region
|
bool
|
Also return the subdivision code (the address' state / region matched to |
True
|
cache_dir
|
Optional[Union[str, Path]]
|
Cache folder (see :func: |
None
|
Returns:
| Type | Description |
|---|---|
Union[str, Tuple[Optional[str], Optional[str]]]
|
|
get_city_geometry(city_name, user_agent=USER_AGENT, cache_dir=None, limit=10, attempts=4)
¶
A place's boundary polygon from Nominatim: the first Polygon / MultiPolygon among its answers (a bare point -- a place marker -- is skipped).
Nominatim is rate limited and sometimes answers differently under load: answers without a polygon are asked again
(attempts times, with a growing wait). With cache_dir the boundary is saved there and read back next time, so the
same name always gives the same polygon (delete the file to search again).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
city_name
|
str
|
Place name. |
required |
user_agent
|
str
|
User agent sent to Nominatim. |
USER_AGENT
|
cache_dir
|
Optional[Union[str, Path]]
|
Folder of cached boundaries ( |
None
|
limit
|
int
|
Answers requested per search. |
10
|
attempts
|
int
|
Searches before giving up. |
4
|
Returns:
| Type | Description |
|---|---|
GeoDataFrame
|
One-row GeoDataFrame (EPSG:4326) with |
Raises:
| Type | Description |
|---|---|
ValueError
|
If no boundary polygon is found. |
get_geographic_suggestions_from_string(query, user_agent='MobilityDatabaseClient', max_results=25)
¶
Suggest country codes, subdivisions, and municipalities from a query string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
str
|
Place name. |
required |
user_agent
|
str
|
User agent sent to Nominatim. |
'MobilityDatabaseClient'
|
max_results
|
int
|
Maximum number of results. |
25
|
Returns:
| Type | Description |
|---|---|
Dict[str, List[str]]
|
|
get_geographic_suggestions_from_aoi(aoi, num_points=1, user_agent='MobilityDatabaseClient')
¶
Reverse-geocode AOI geometry to suggest country, subdivision, and municipality.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
aoi
|
Union[Polygon, MultiPolygon, GeoDataFrame, GeoSeries]
|
Area (polygon or GeoDataFrame / GeoSeries). |
required |
num_points
|
int
|
Random points inside it to reverse-geocode. |
1
|
user_agent
|
str
|
User agent sent to Nominatim. |
'MobilityDatabaseClient'
|
Returns:
| Type | Description |
|---|---|
Dict[str, List[str]]
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If the area is empty. |
TypeError
|
For an unsupported area type. |