Skip to content

Geocode

Geocoding module for getting a geometry from query using Nominatim.

geocode_to_geometry(query)

Geocode a query to a (Multi)Polygon geometry using Nominatim.

Source code in quackosm/geocode.py
def geocode_to_geometry(query: Union[str, list[str]]) -> BaseGeometry:
    """Geocode a query to a (Multi)Polygon geometry using Nominatim."""
    if not isinstance(query, str):
        return unary_union([geocode_to_geometry(sub_query) for sub_query in query])

    h = hashlib.new("sha256")
    h.update(query.encode())
    query_hash = h.hexdigest()
    query_file_path = Path("cache").resolve() / f"{query_hash}.json"

    if not query_file_path.exists():
        query_results = Nominatim(
            user_agent="QuackOSM Python package (https://github.com/kraina-ai/quackosm)"
        ).geocode(query, geometry="geojson", exactly_one=False)

        if not query_results:
            raise QueryNotGeocodedError(f"Zero results from Nominatim for query '{query}'.")

        polygon_result = _get_first_polygon(query_results)

        if not polygon_result:
            raise QueryNotGeocodedError(f"No polygon found for query '{query}'.")

        query_file_path.parent.mkdir(parents=True, exist_ok=True)
        query_file_path.write_text(json.dumps(polygon_result))
    else:
        polygon_result = json.loads(query_file_path.read_text())

    return unary_union(shape(polygon_result))