Skip to content

Instantly share code, notes, and snippets.

@miraculixx
Created May 11, 2016 17:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save miraculixx/f01304186fc47d041da5a712774ac487 to your computer and use it in GitHub Desktop.
Save miraculixx/f01304186fc47d041da5a712774ac487 to your computer and use it in GitHub Desktop.
quickly get geojson objects
pip install requests
pip install geojson

example output

$ python getsamplecoords.py
[('New York', (-74.0059413, 40.7127837)), ('Geneva', (6.1431577, 46.2043907)), ('Bern', (7.4474468, 46.9479739)), ('Zurich', (8.541694, 47.3768866))]
[{'location': {'coordinates': [-74.0059413, 40.7127837], 'type': 'Point'},
  'place': 'New York'},
 {'location': {'coordinates': [6.1431577, 46.2043907], 'type': 'Point'},
  'place': 'Geneva'},
 {'location': {'coordinates': [7.4474468, 46.9479739], 'type': 'Point'},
  'place': 'Bern'},
 {'location': {'coordinates': [8.541694, 47.3768866], 'type': 'Point'},
  'place': 'Zurich'}]
import requests
def get_coord(address):
url = 'https://maps.googleapis.com/maps/api/geocode/json'
params = {'sensor': 'false', 'address': address}
r = requests.get(url, params=params)
results = r.json()['results']
location = results[0]['geometry']['location']
return location['lng'], location['lat']
places = [(place, get_coord(place)) for place in 'New York,Geneva,Bern,Zurich'.split(',')]
pprint([{ 'place' : place, 'location' : GeoJSON(*coord) } for place, coord in places])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment