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:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from geopy.geocoders import Nominatim | |
def geolo_basic(name): | |
geolocator = Nominatim() | |
location = geolocator.geocode(name, exactly_one='False') | |
for loc in location: | |
print loc.raw | |
def main(): | |
geolo_basic("Elbe") | |
if __name__ == '__main__': | |
main() |
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:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from geopy.geocoders import Nominatim | |
def geolo_geometry(name, geom_format): | |
geolocator = Nominatim() | |
location = geolocator.geocode(name, geometry=geom_format, exactly_one='False') | |
geom_output = None | |
if geom_format == 'WKT': | |
geom_output = 'geotext' | |
elif geom_format == 'svg': | |
geom_output = 'svg' | |
elif geom_format == 'kml': | |
geom_output = 'geokml' | |
elif geom_format == 'geojson': | |
geom_output = 'geojson' | |
for loc in location: | |
print loc.raw.get(geom_output) | |
def main(): | |
geolo_geometry("Elbe","WKT") | |
if __name__ == '__main__': | |
main() |
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):
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!
Follow @spatialbits
Keine Kommentare:
Kommentar veröffentlichen