Skip to content

Instantly share code, notes, and snippets.

@dannguyen
Last active November 24, 2015 13:24
Show Gist options
  • Select an option

  • Save dannguyen/f6d14be4d917961b50b2 to your computer and use it in GitHub Desktop.

Select an option

Save dannguyen/f6d14be4d917961b50b2 to your computer and use it in GitHub Desktop.
Example of using Mapzen's Search API to do geocoding, using Python 3.x and the requests library. More advanced version here: https://gist.github.com/dannguyen/036a62e32fc9f6765b82
# Example of using Mapzen Search for free geocoding
# https://mapzen.com/documentation/search/search/
# result of geocoding all of New York's Starbucks: http://bit.ly/1P34T7h
from os import environ
import requests
API_KEY = environ['MAPZEN_SEARCH_KEY']
BASE_URL = 'https://search.mapzen.com/v1/search'
txt = '1007 MORRIS PARK AVE, 10462'
resp = requests.get(BASE_URL, params = {'api_key': API_KEY, 'size': 1, 'text': txt})
data = resp.json()
bbox = data['bbox']
# The data object returns a `bbox` attribute; by definition, for a single point
# the two points will be the same, e.g.
# 'bbox': [-73.85607, 40.848467, -73.85607, 40.848467]
# However, we take the average just in case the sole result is some kind of shape
pt = {'longitude': (bbox[0] + bbox[2]) / 2, 'latitude': (bbox[1] + bbox[3]) / 2 }
Display the source blob
Display the rendered blob
Raw
{
"geocoding": {
"attribution": "https://search.mapzen.com/v1/attribution",
"query": {
"text": "1007 MORRIS PARK AVE, 10462",
"private": false,
"parsed_text": {
"number": 1007,
"street": "MORRIS PARK AVE",
"postalcode": 10462,
"admin_parts": "10462",
"name": "1007 MORRIS PARK AVE",
"regions": []
},
"size": 1
},
"timestamp": 1448293084662,
"engine": {
"name": "Pelias",
"author": "Mapzen",
"version": "1.0"
},
"version": "0.1"
},
"type": "FeatureCollection",
"features": [
{
"geometry": {
"coordinates": [
-73.85607,
40.848467
],
"type": "Point"
},
"type": "Feature",
"properties": {
"confidence": 0.5,
"country": "United States",
"name": "1007 Morris Park Avenue",
"locality": "New York",
"street": "Morris Park Avenue",
"postalcode": "10462",
"country_a": "USA",
"housenumber": "1007",
"region_a": "NY",
"layer": "address",
"county": "Bronx County",
"label": "1007 Morris Park Avenue, Bronx, NY",
"region": "New York",
"id": "address-osmway-281729442",
"neighbourhood": "Van Nest",
"localadmin": "Bronx",
"source": "osm",
"gid": "osm:address:address-osmway-281729442"
}
}
],
"bbox": [
-73.85607,
40.848467,
-73.85607,
40.848467
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment