/batch_geocode

a python module to geocode addresses in batch via Google Geocode API.

Primary LanguagePythonMIT LicenseMIT

batch_geocode.py

A python module to geocode addresses in batch via Google Geocoding API, i.e. it turns addresses into latitude-longitude coordinates.

What it is

The library contains a few easily extendable functions to make Google Geocoding API more convenient to use. The library handles the frequent, interrupting OVER_QUERY_LIMIT response elegantly for you. It optionally caches the responses to conserve your request quota. The implementation uses generator to minimize memory use and enables to geocode larger-than-memory files or streams. Functions takes advantage of default values and extensive function arguments to keep simple tasks simple while maintaining high flexibility.

Uses

The library contains a convenient script to geocode a CSV.

$ geocode_csv.py <in-csv-filename> [<out-csv-filename>]

Examples

from batch_geocode import batch_geocode, batch_geocode_csv

# example 0
# geocoding a list of places
places = ['North Pole', 'South Pole', 'Royal Observatory Greenwich']

# implement a simple process function
# print out response jsons' status and location
def print_result(r, j):
	print r, j['status'], extract_latlong(j)

batch_geocode(places, process_func=print_result)

# example 1
# geocoding data stored in CSV
reader = csv.DictReader(open('sample.csv', 'r'))
writer = csv.DictWriter(open('sample.geocoded.csv', 'wb'), fieldnames=reader.fieldnames+['latitude', 'longitude'])
writer.writeheader()

batch_geocode_csv(reader, writer, address_func=lambda x: x['address'], process_func=print_result)

Restrictions

The library relies on Google Geocoding API to geocode: this dependency might change in the future. But while this is the case, users should observe restrictions on their use according to Google Geocoding API's documentation. “[T]he Geocoding API may only be used in conjunction with a Google map; geocoding results without displaying them on a map is prohibited. For complete details on allowed usage, consult the Maps API Terms of Service License Restrictions.” You should consider to use tools such as marker_map to layout the geocoded result on Google map.

License

MIT License

Author

Falcon Dai