-
-
Save jalbertbowden/d31e6cf30d49a922eb64 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 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