Created
September 20, 2017 12:38
-
-
Save FelipeSBarros/999c13e058d6e123697fa93ca2349045 to your computer and use it in GitHub Desktop.
Small function for geocoding using GraphHopper API. From Adress, recieve json with lat/long point location
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 requests | |
import urllib | |
def geocode(address, city, state, zip_code): | |
try: | |
location_param = ("%s+%s+%s+%s" % ("Pedernera", "Posadas", "Misiones", "2037")) | |
#url_request = "http://nominatim.openstreetmap.org/search?q=" + location_param + "&format=json&polygon_geojson=1" | |
#url_request = "maps.googleapis.com/maps/api/geocode/json?address=%s&sensor=false" % location_param | |
url_request = "http://graphhopper.com/api/1/geocode?q=" + location_param + "&key=PUT_YOUR_KEY_HERE" | |
result = requests.get(url_request) | |
data = result.json() | |
#location = data['results'][0]['geometry']['location'] | |
#location = json.dumps(data["hits"][0]["point"]) | |
lat = location = json.dumps(data["hits"][0]["point"]["lat"]) | |
lng = location = json.dumps(data["hits"][0]["point"]['lng']) | |
return lat, lng | |
except Exception: | |
return None | |
geocode("Pedernera", "Posadas", "Misiones", "202037") | |
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 requests | |
import urllib | |
def geocode(address, city, state, zip_code): | |
try: | |
location_param = ("%s+%s+%s+%s" % ("Pedernera", "Posadas", "Misiones", "2037")) | |
#url_request = "http://nominatim.openstreetmap.org/search?q=" + location_param + "&format=json&polygon_geojson=1" | |
#url_request = "maps.googleapis.com/maps/api/geocode/json?address=%s&sensor=false" % location_param | |
url_request = "http://graphhopper.com/api/1/geocode?q=" + location_param + "&key=PUT_YOUR_KEY_HERE" | |
result = requests.get(url_request) | |
data = result.json() | |
#location = data['results'][0]['geometry']['location'] | |
#location = json.dumps(data["hits"][0]["point"]) | |
lat = location = json.dumps(data["hits"][0]["point"]["lat"]) | |
lng = location = json.dumps(data["hits"][0]["point"]['lng']) | |
return lat, lng | |
except Exception: | |
return None | |
geocode("Pedernera", "Posadas", "Misiones", "202037") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment