Skip to content

Instantly share code, notes, and snippets.

@efruchter
Created February 12, 2014 09:54
Show Gist options
  • Save efruchter/8952701 to your computer and use it in GitHub Desktop.
Save efruchter/8952701 to your computer and use it in GitHub Desktop.
A python script to convert addresses into lat/lng pairs via Google's API.
import time
import json
import urllib2
import csv
def geocode(a):
h = a.replace(' ', '+')
req = 'http://maps.googleapis.com/maps/api/geocode/json?address=' + h + '&sensor=false'
data = json.load(urllib2.urlopen(req))
ploc = data['results']
if not ploc:
print "CANT FIND: ", a
return -1, -1
else:
loc = ploc[0]['geometry']['location']
return loc['lat'], loc['lng']
def printGeocodes(csv_file, wait_s=True):
with open(csv_file, 'rb') as csvfile:
reader = csv.reader(csvfile, delimiter=',')
for row in reader:
time.sleep(1)
lat, lng = geocode(row[-1])
print lat, ',', lng
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment