Skip to content

Instantly share code, notes, and snippets.

@cjwinchester
Created February 2, 2015 22:18
Show Gist options
  • Save cjwinchester/a48dce96ab9152d39adc to your computer and use it in GitHub Desktop.
Save cjwinchester/a48dce96ab9152d39adc to your computer and use it in GitHub Desktop.
Google geocoder script
"""
assumes you're feeding it a text file of addresses
spits out a pipe-delimited file of coordinates
"""
import csv
import json
import requests
f = open('output.txt', 'wb')
with open('input.txt', 'rb') as csvfile:
xreader = csv.reader(csvfile, delimiter="\t")
for row in xreader:
row = row[0].replace(" ","+")
r = requests.get('https://maps.googleapis.com/maps/api/geocode/json?address=' + row[0] + '&key=YOUR_API_KEY_HERE')
j = r.json()
lat = j['results'][0]['geometry']['location']['lat']
lng = j['results'][0]['geometry']['location']['lng']
w = "|".join((str(lat),str(lng)))
f.write(w + "\n")
print row
f.flush()
f.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment