Geocoding With Python And OSM

Posted by Martin Seiler on Mon, Sep 22, 2014
In GI, OSM, EN,
Tags geocoding python geopy

Geocoding is the process of relating implicit location information (such as an address or a the name of a river) to explicit location information in form of geographic coordinates.

Several companies, e.g. Google and Nokia/HERE offer commercial geocoding services/APIs. Nominatim is a tool to search OSM data by name and address and to generate synthetic addresses of OSM points and hence a geocoder operating on the OSM database. While many geocoders offer easy to use APIs that can be directly utilized from a web browser, they unfold their full potential when called from e.g. a script enabling batch geocoding for many place names at once. geopy is a geocoding toolbox for Python, offering access to numerous geocoding services.

Here is an example of geocoding with Nominatim:

which returns several dicts like:

{u'display_name': u'Elbe, Landkreis Wittenberg, Sachsen-Anhalt, Deutschland, European Union', u'importance': 0.72552692721096, u'place_id': u'9208507982', u'lon': u'12.5663365', u'lat': u'51.867922', u'osm_type': u'relation', u'licence': u'Data \xa9 OpenStreetMap contributors, ODbL 1.0\. http://www.openstreetmap.org/copyright', u'osm_id': u'123822', u'boundingbox': [u'50.0168724060059', u'54.0075302124023', u'8.22170829772949', u'15.9315462112427'], u'type': u'river', u'class': u'waterway'}

Among other details these contain lat/lon coordinates and the boudning box of the feature. But wouldn’t it be nice if the result would contain the full geometry as well? Well, while this is supported by the Nominatim API it was not - until recently - by the geopy toolbox. I added support for exactly that, so now:

leads to:

MULTILINESTRING((15.53613 50.7756972,15.5364593 50.7755378,15.5367906 50.7754773,15.5372784 50.7754914,15.5378943 50.7752649,15.5382691 50.7752837,15.5384418 50.7752514,15.5388037 [...])) LINESTRING(7.0094126 51.1325696,7.0093372 51.1324799,7.009245 51.1323739,7.0091836 51.1322698,7.0090853 51.1321503,7.0090361 51.1320982,7.0089716 51.1319945 7.0085078 51.1314622 [...])

so the full geometries are returned.

EDIT (2014-09-23): (Note: Currently this is implemented in my github-fork, it should appear in the official repo eventually. A pull request is pending.) The pull request has been accepted and so the functionality is now available in the official repo (release 1.3.0).

EDIT (2014-10-18): Please make sure to respect Nominatim’s usage policy!