pyrosm.geocode

Contents

pyrosm.geocode#

pyrosm.geocode(query, polygon=True, base_url='https://nominatim.openstreetmap.org', user_agent=None, headers=None, timeout=60, opener=None)#

Geocode a place name, or several, to a Shapely polygon via Nominatim.

See https://pyrosm.readthedocs.io/en/stable/download_responsibly.html for what the data providers ask and how pyrosm keeps to it.

Parameters:
  • query (str | list of str) – The place name to look up, e.g. "Brighton and Hove, UK", or a list of place names, e.g. ["Helsinki, Finland", "Espoo, Finland"], each looked up in turn and their areas combined into one.

  • polygon (bool) – When True (default), return the place’s boundary polygon if Nominatim provides one; otherwise (or for point-/line-like results such as POIs and addresses) the place’s bounding-box rectangle is returned. The result is always a Polygon/MultiPolygon.

  • base_url (str) – The Nominatim base URL. Defaults to the public server; point it at your own instance for heavy use.

  • user_agent (str, optional) – The User-Agent header sent to Nominatim. Defaults to a pyrosm string (marked agent when an AI coding agent runs pyrosm), which every pyrosm user shares: once a process has sent the public server 50 requests with it, geocoding with it raises RequestRefusedError. Nominatim rejects requests without a descriptive agent.

  • headers (dict, optional) – Extra HTTP headers for the request; user_agent replaces a User-Agent in them.

  • timeout (float | tuple) – Seconds to wait for the server to accept the connection and then for each read (60 by default), or a (connect, read) pair, as for downloads.

  • opener (object, optional) – An object with open(request, timeout=...), e.g. from urllib.request.build_opener(), that makes the request instead of pyrosm.

Returns:

The place’s boundary polygon, or its bounding-box rectangle; for a list, the union of the places’ polygons (or rectangles).

Return type:

shapely.geometry.Polygon or shapely.geometry.MultiPolygon

Raises:
  • ValueError – If query is empty or not a place name or a list of them, the service cannot be reached, or no match is found; the message names the place.

  • RequestRefusedError – If the process has sent the public server 50 requests with pyrosm’s own User-Agent.

Notes

Each answer is cached for 7 days in geocoding in pyrosm’s default folder, so the same query with the same headers (but for the User-Agent) is answered from there. The public server gets at most one request a second from a process, as Nominatim’s usage policy asks (https://operations.osmfoundation.org/policies/nominatim/). Each name of a list is a request of its own, cached, spaced and counted in the same way.