Created
February 12, 2014 09:54
-
-
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.
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
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