Skip to content

Instantly share code, notes, and snippets.

@jalbertbowden
Forked from philshem/revgeo.py
Last active August 29, 2015 14:17
Show Gist options
  • Select an option

  • Save jalbertbowden/d31e6cf30d49a922eb64 to your computer and use it in GitHub Desktop.

Select an option

Save jalbertbowden/d31e6cf30d49a922eb64 to your computer and use it in GitHub Desktop.
import requests
urlbase = 'http://maps.googleapis.com/maps/api/geocode/json?latlng='
key = None
# list of latitude, longitude pairs
latlong = [(40.714224,-73.961452), (47.3667, 8.5500)]
for xy in latlong:
url = urlbase + str(xy[0])+','+str(xy[1])
if key is not None:
url += '&key='+str(key)
# make request to google
r = requests.get(url)
# parse data
data = r.json()
# get desired country fields
short_name = data.get('results')[-1].get('address_components')[0].get('short_name')
long_name = data.get('results')[-1].get('address_components')[0].get('long_name')
# print csv output
print ','.join((str(xy[0]),str(xy[1]), short_name, long_name))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment