geopy

geopy is a geocoding toolbox for Python.

Description

geopy makes it easy for developers to locate the coordinates of addresses, cities, countries, and landmarks across the globe using third-party geocoders and other sources of data, such as wikis.

Development Status

Under development since September 2006. Latest release: 0.93.

License

geopy is open source software released under the MIT license.

Contact

Questions, comments, and bug reports are welcome at exogen@gmail.com, the geopy mailing list, and on brian's blog.

Install

From the Cheese Shop:

$ sudo easy_install geopy

From the Subversion repository:

$ svn co http://geopy.googlecode.com/svn/trunk/ geopy-trunk
$ cd geopy-trunk/
$ sudo python setup.py install

...or use easy_install:

$ sudo easy_install http://geopy.googlecode.com/svn/trunk/

Geocoders

geopy includes geocoder classes for MediaWiki (with the GIS extension), Semantic MediaWiki, the Google geocoder, the Yahoo! geocoder, geocoder.us, Virtual Earth, and GeoNames.

The geocoder classes are located in geopy.geocoders.

Using the Google geocoder (get an API Key):

You can use the Google geocoder without an API key by querying the actual Google Maps interface instead. This geocoder is supposedly more up-to-date and thus may have more addresses available:

To geocode addresses in other countries with the Google geocoder, specify an alternate domain to query. For example, this currently seems to produce the best results for UK addresses:

Using the Yahoo! geocoder (get an Application ID):

Using the Virtual Earth geocoder:

Using the geocoder.us geocoder (free for non-commercial purposes, otherwise get an account):

Using the GeoNames geocoder:

GeoNames only returns city, country and zip code in its "canonical" name, so it looks a little funny.

Using the MediaWiki geocoder:

Using the Semantic MediaWiki geocoder:

Every geocoder accepts an argument format_string that defaults to '%s' where the input string to geocode is interpolated. For example, if you only need to geocode locations in Cleveland, Ohio, you could do:

Geocoders that can return multiple possible locations for a query accept an argument exactly_one to their geocode method which defaults to True. A ValueError will be raised if 0 or more than 1 locations are returned. If exactly_one is False, geocode will return a generator of the retured locations:

For advanced usage, see help(geocoders), the docstrings for each class are fairly complete.

You can also view the syntax-highlighted source code for geopy.geocoders.

Parsing Utilities

If you just need to parse geographical coordinates, the util.parse_geo method makes this easy:

Distance Calculation

In version 0.93, support was added for geodesic distance calculation. Two distance formulas are included: great-circle distance and Vincenty distance.

Great-circle distance uses a spherical model of the earth, using the average great-circle radius of 6372.795 kilometers, resulting in an error of up to about 0.5%. The radius value is stored in distance.EARTH_RADIUS, so it can be customized (it should always be in kilometers, however).

Vincenty distance uses a more accurate ellipsoidal model of the earth. This is the default distance formula, and is thus aliased as distance.distance — so you can easily swap out distance formulas just by changing distance.distance at the top of your code. There are multiple popular ellipsoidal models, and which one will be the most accurate depends on where your points are located on the earth. The default is the WGS-84 ellipsoid, which is the most globally accurate. geopy includes a few other models in the distance.ELLIPSOIDS dictionary:

Here's an example usage of distance.distance:

Using Great-circle distance:

You can change the ellipsoid model used by the Vincenty formula like so:

The above model name will automatically be retrieved from the ELLIPSOIDS dictionary. Alternatively, you can specify the model values directly:

Distances support simple arithmetic, making it easy to do things like calculate the length of a path:

The distance module also contains useful routines for converting between length and angle units. Read the syntax-highlighted source code or use help(distance) for more documentation.