Home
pygeocoder 1.2
pygeocoder is a Python library that helps you make use of Google's Geocoding functionality. With this module, you can easily find addresses corresponding to coordinates and vice versa. You can also use it to validate and format addresses.
It is based on googlemaps 1.0.2 but upgraded to make use of Google's newest geocoding API V3
License
BSD
Installation
To install, you can simply use:
$ pip install pygeocoder
pip is a newer generation replacement for easy_install. Support Python advancement for a better package distribution system and make the switch today.
Usage
New in pygeocoder 1.1.3: it is no longer necessary to instantiate a Geocoder class. Geocoder methods are static methods but still support instance calls.
Note in pygeocoder 1.2: address_to_latlng and latlng_to_address were removed. They were 'shortcuts' that were long to type and feature incomplete. Just use Geocoder.geocode('some address').coordinates instead.
Geocoding
from pygeocoder import Geocoder
results = Geocoder.geocode("Tian'anmen, Beijing")
print(results[0].coordinates)
>>> (39.908715, 116.397389)
print(results[0])
>>> Tiananmen, Dongcheng, Beijing, China, 100051
For the first result, you don't have to specify the index.
print(results.count)
>>> 1
print(results)
>>> Tiananmen, Dongcheng, Beijing, China, 100051
Besides the coordinates, returns of the geocode method also returns all location component types used by Google API. For instance:
print(results[0].country)
>>> 'China'
print(results[0].postal_code)
>>> '100051'
Instead of using results with indices, you can also use iterators
results = Geocoder.geocode("McGill University")
for result in results:
print(result.postal_code)
>>> H3A
>>> H2X 2C3
>>> G1V
>>> H2X 2C3
You can also access the dictionary of results directly using
results.raw
The contents are described in the Google documentation for Geocoding
Reverse Geocoding
results = Geocoder.reverse_geocode(45.424571, -75.695661)
print(results[0])
>>> 1 Wellington St, Ottawa, ON K1A 0A6, Canada
Descriptions of the raw dictionary content from reverse geocoding are found here
Address Validation and Formatting
This module can be used to validate and format user inputted addresses.
Geocoder.geocode("1600 amphitheater parkway, mountain view").valid_address
>>> True
Geocoder.geocode("1600000 amphicinema parkway, mountain view").valid_address
>>> False
It can also correct minor spelling mistakes and give back a properly formatted address
result = Geocoder.geocode("1600 amphiteather parkway, mountain view")
result.valid_address
>>> True
print(result)
>>> 1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA
Then, you can use the properly split address components to store separately in database as used above
result.street_number
>>> 1600
result.route
>>> 'Amphitheater Pkwy'
Maps API for Business Users
Use an initialised instance of Geocoder like this
business_geocoder = Geocoder('MY_CLIENT_ID', 'MY_PRIVATE_KEY')
business_geocoder.geocode('blah') #business as usual
Updated