Skip to content

Instantly share code, notes, and snippets.

@trevormunoz
Created March 9, 2017 11:35
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 trevormunoz/435e96e970dddc65250e55622904fe64 to your computer and use it in GitHub Desktop.
Save trevormunoz/435e96e970dddc65250e55622904fe64 to your computer and use it in GitHub Desktop.
Using the Mapzen API to geocode locations
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Geocoding using the Mapzen API\n",
"\n",
"Trevor Muñoz <tmunoz@umd.edu> for the [AADHum Initiative](http://aadhum.umd.edu) Digital Humanities Incubators, Spring 2017"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import os\n",
"import csv\n",
"import requests\n",
"import folium"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"DATA_FILE = './data/BlackBroadwayonU-Step2.csv'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Read the CSV file containing the places from the [Black Broadway on U](http://blackbroadwayonu.com/) that were not geocoded."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"DATASET = []\n",
"with open(DATA_FILE, 'r') as csvfile:\n",
" reader = csv.DictReader(csvfile)\n",
" for row in reader:\n",
" DATASET.append(row)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Check to make sure we got the data we were expecting …"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"{'city': 'Washington',\n",
" 'description': 'Born to former slaves in 1872 in Dayton, Ohio, Paul Laurence Dunbar became the first African-American to gain national eminence as a poet. Inspired by his mother Matilda, who had learned to read by listening to her slave master read poetry, Dunbar began writing poetry at the age of six. He published his first book of poetry, Oak and Ivy, in 1892, and was praised by Frederick Douglas as \"the most promising young colored man in America.\" In October 1897, Dunbar took a job at the Library of Congress in Washington, DC and he and his wife, Alice Dunbar Nelson moved into the comfortable LeDroit Park neighborhood. Under the urging of his wife, Dunbar soon left the job to focus on his writing, which he promoted through public readings. Beginning in 1902, Dunbar entered a downward spiral of depression, tuberculosis, and alcohol abuse. He died in Dayton in 1906, and Washington\\'s premier black high school was named in his honor. Though he died at the age of 33, Dunbar penned a large body of dialect poems, standard English poems, essays, novels, and short stories. His work often addressed the difficulties encountered by members of his race and the efforts of African-Americans to achieve quality in America.',\n",
" 'latitude': '',\n",
" 'longitude': '',\n",
" 'place_name': 'PAUL LAURENCE DUNBAR RESIDENCE',\n",
" 'state': 'DC',\n",
" 'street_address': '321 U Street, NW'}"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"DATASET[0]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's do a basic search query against the Mapzen API. We'll use the [Requests](http://docs.python-requests.org/en/master/) library to make calls to the web service. The endpoint we will use has the following structure: `https://search.mapzen.com/v1/search?api_key=` + `$API_KEY` + `&text=` + `\"your search text\"` …"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"NB: You'll need your own Mapzen API key. Make sure this is accessible as an environment variable (e.g. $MAPZEN_API_KEY)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"results = []\n",
"for place in DATASET:\n",
" search_string = place['place_name']\n",
" search_request = requests.get('https://search.mapzen.com/v1/search?api_key={0}&text={1}'.format(os.environ['MAPZEN_API_KEY'], search_string))\n",
" results.append(search_request.json())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's look at the first result …"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"{'bbox': [-87.620757, 39.295385, -76.598578, 41.838556],\n",
" 'features': [{'bbox': [-84.21811, 39.739376, -84.217492, 39.755191],\n",
" 'geometry': {'coordinates': [-84.217945, 39.747256], 'type': 'Point'},\n",
" 'properties': {'accuracy': 'centroid',\n",
" 'confidence': 0.887,\n",
" 'country': 'United States',\n",
" 'country_a': 'USA',\n",
" 'country_gid': 'whosonfirst:country:85633793',\n",
" 'county': 'Montgomery County',\n",
" 'county_gid': 'whosonfirst:county:102083667',\n",
" 'gid': 'openstreetmap:street:polyline:11188999',\n",
" 'id': 'polyline:11188999',\n",
" 'label': 'South Paul Laurence Dunbar Street, Dayton, OH, USA',\n",
" 'layer': 'street',\n",
" 'localadmin': 'Dayton',\n",
" 'localadmin_gid': 'whosonfirst:localadmin:404524075',\n",
" 'locality': 'Dayton',\n",
" 'locality_gid': 'whosonfirst:locality:101712161',\n",
" 'name': 'South Paul Laurence Dunbar Street',\n",
" 'neighbourhood': 'Five Points',\n",
" 'neighbourhood_gid': 'whosonfirst:neighbourhood:85877005',\n",
" 'region': 'Ohio',\n",
" 'region_a': 'OH',\n",
" 'region_gid': 'whosonfirst:region:85688485',\n",
" 'source': 'openstreetmap',\n",
" 'source_id': 'polyline:11188999',\n",
" 'street': 'South Paul Laurence Dunbar Street'},\n",
" 'type': 'Feature'},\n",
" {'bbox': [-84.224289, 39.758216, -84.206421, 39.760372],\n",
" 'geometry': {'coordinates': [-84.216152, 39.760365], 'type': 'Point'},\n",
" 'properties': {'accuracy': 'centroid',\n",
" 'confidence': 0.887,\n",
" 'country': 'United States',\n",
" 'country_a': 'USA',\n",
" 'country_gid': 'whosonfirst:country:85633793',\n",
" 'county': 'Montgomery County',\n",
" 'county_gid': 'whosonfirst:county:102083667',\n",
" 'gid': 'openstreetmap:street:polyline:11190887',\n",
" 'id': 'polyline:11190887',\n",
" 'label': 'Paul Laurence Dunbar Connector, Dayton, OH, USA',\n",
" 'layer': 'street',\n",
" 'localadmin': 'Dayton',\n",
" 'localadmin_gid': 'whosonfirst:localadmin:404524075',\n",
" 'locality': 'Dayton',\n",
" 'locality_gid': 'whosonfirst:locality:101712161',\n",
" 'name': 'Paul Laurence Dunbar Connector',\n",
" 'region': 'Ohio',\n",
" 'region_a': 'OH',\n",
" 'region_gid': 'whosonfirst:region:85688485',\n",
" 'source': 'openstreetmap',\n",
" 'source_id': 'polyline:11190887',\n",
" 'street': 'Paul Laurence Dunbar Connector'},\n",
" 'type': 'Feature'},\n",
" {'bbox': [-84.062113, 39.781983, -84.061179, 39.78304],\n",
" 'geometry': {'coordinates': [-84.061612, 39.782588], 'type': 'Point'},\n",
" 'properties': {'accuracy': 'point',\n",
" 'confidence': 0.887,\n",
" 'country': 'United States',\n",
" 'country_a': 'USA',\n",
" 'country_gid': 'whosonfirst:country:85633793',\n",
" 'county': 'Greene County',\n",
" 'county_gid': 'whosonfirst:county:102084765',\n",
" 'gid': 'openstreetmap:venue:way:61671615',\n",
" 'id': 'way:61671615',\n",
" 'label': 'Paul Laurence Dunbar Library, Fairborn, OH, USA',\n",
" 'layer': 'venue',\n",
" 'localadmin': 'Bath',\n",
" 'localadmin_gid': 'whosonfirst:localadmin:404525273',\n",
" 'locality': 'Fairborn',\n",
" 'locality_gid': 'whosonfirst:locality:101712677',\n",
" 'name': 'Paul Laurence Dunbar Library',\n",
" 'neighbourhood': 'New Germany',\n",
" 'neighbourhood_gid': 'whosonfirst:neighbourhood:85836629',\n",
" 'region': 'Ohio',\n",
" 'region_a': 'OH',\n",
" 'region_gid': 'whosonfirst:region:85688485',\n",
" 'source': 'openstreetmap',\n",
" 'source_id': 'way:61671615'},\n",
" 'type': 'Feature'},\n",
" {'geometry': {'coordinates': [-87.620757, 41.838556], 'type': 'Point'},\n",
" 'properties': {'accuracy': 'point',\n",
" 'confidence': 0.887,\n",
" 'country': 'United States',\n",
" 'country_a': 'USA',\n",
" 'country_gid': 'whosonfirst:country:85633793',\n",
" 'county': 'Cook County',\n",
" 'county_gid': 'whosonfirst:county:102084317',\n",
" 'gid': 'openstreetmap:venue:node:3566588861',\n",
" 'id': 'node:3566588861',\n",
" 'label': 'Paul Laurence Dunbar Monument, Chicago, IL, USA',\n",
" 'layer': 'venue',\n",
" 'localadmin': 'Chicago',\n",
" 'localadmin_gid': 'whosonfirst:localadmin:404496273',\n",
" 'locality': 'Chicago',\n",
" 'locality_gid': 'whosonfirst:locality:85940195',\n",
" 'name': 'Paul Laurence Dunbar Monument',\n",
" 'neighbourhood': 'South Commons',\n",
" 'neighbourhood_gid': 'whosonfirst:neighbourhood:85867771',\n",
" 'region': 'Illinois',\n",
" 'region_a': 'IL',\n",
" 'region_gid': 'whosonfirst:region:85688697',\n",
" 'source': 'openstreetmap',\n",
" 'source_id': 'node:3566588861'},\n",
" 'type': 'Feature'},\n",
" {'geometry': {'coordinates': [-76.598578, 39.295385], 'type': 'Point'},\n",
" 'properties': {'accuracy': 'point',\n",
" 'confidence': 0.658,\n",
" 'country': 'United States',\n",
" 'country_a': 'USA',\n",
" 'country_gid': 'whosonfirst:country:85633793',\n",
" 'county': 'Baltimore city',\n",
" 'county_gid': 'whosonfirst:county:102081589',\n",
" 'gid': 'openstreetmap:venue:node:358248336',\n",
" 'id': 'node:358248336',\n",
" 'label': 'Paul Laurence Dunbar High School, Baltimore, MD, USA',\n",
" 'layer': 'venue',\n",
" 'locality': 'Baltimore',\n",
" 'locality_gid': 'whosonfirst:locality:85949461',\n",
" 'name': 'Paul Laurence Dunbar High School',\n",
" 'neighbourhood': 'Dunbar',\n",
" 'neighbourhood_gid': 'whosonfirst:neighbourhood:85870217',\n",
" 'region': 'Maryland',\n",
" 'region_a': 'MD',\n",
" 'region_gid': 'whosonfirst:region:85688501',\n",
" 'source': 'openstreetmap',\n",
" 'source_id': 'node:358248336'},\n",
" 'type': 'Feature'},\n",
" {'geometry': {'coordinates': [-84.217737, 39.748145], 'type': 'Point'},\n",
" 'properties': {'accuracy': 'point',\n",
" 'confidence': 0.658,\n",
" 'country': 'United States',\n",
" 'country_a': 'USA',\n",
" 'country_gid': 'whosonfirst:country:85633793',\n",
" 'county': 'Montgomery County',\n",
" 'county_gid': 'whosonfirst:county:102083667',\n",
" 'gid': 'openaddresses:address:us/oh/montgomery:e4aeef55056adca0',\n",
" 'housenumber': '608',\n",
" 'id': 'us/oh/montgomery:e4aeef55056adca0',\n",
" 'label': '608 S Paul Laurence Dunbar St, Dayton, OH, USA',\n",
" 'layer': 'address',\n",
" 'localadmin': 'Dayton',\n",
" 'localadmin_gid': 'whosonfirst:localadmin:404524075',\n",
" 'locality': 'Dayton',\n",
" 'locality_gid': 'whosonfirst:locality:101712161',\n",
" 'name': '608 S Paul Laurence Dunbar St',\n",
" 'neighbourhood': 'Five Points',\n",
" 'neighbourhood_gid': 'whosonfirst:neighbourhood:85877005',\n",
" 'postalcode': '45402',\n",
" 'region': 'Ohio',\n",
" 'region_a': 'OH',\n",
" 'region_gid': 'whosonfirst:region:85688485',\n",
" 'source': 'openaddresses',\n",
" 'source_id': 'us/oh/montgomery:e4aeef55056adca0',\n",
" 'street': 'S Paul Laurence Dunbar St'},\n",
" 'type': 'Feature'},\n",
" {'geometry': {'coordinates': [-84.217827, 39.743464], 'type': 'Point'},\n",
" 'properties': {'accuracy': 'point',\n",
" 'confidence': 0.658,\n",
" 'country': 'United States',\n",
" 'country_a': 'USA',\n",
" 'country_gid': 'whosonfirst:country:85633793',\n",
" 'county': 'Montgomery County',\n",
" 'county_gid': 'whosonfirst:county:102083667',\n",
" 'gid': 'openaddresses:address:us/oh/montgomery:f0b7b6080f90c1fb',\n",
" 'housenumber': '929',\n",
" 'id': 'us/oh/montgomery:f0b7b6080f90c1fb',\n",
" 'label': '929 S Paul Laurence Dunbar St, Dayton, OH, USA',\n",
" 'layer': 'address',\n",
" 'localadmin': 'Dayton',\n",
" 'localadmin_gid': 'whosonfirst:localadmin:404524075',\n",
" 'locality': 'Dayton',\n",
" 'locality_gid': 'whosonfirst:locality:101712161',\n",
" 'name': '929 S Paul Laurence Dunbar St',\n",
" 'neighbourhood': 'Five Points',\n",
" 'neighbourhood_gid': 'whosonfirst:neighbourhood:85877005',\n",
" 'postalcode': '45417',\n",
" 'region': 'Ohio',\n",
" 'region_a': 'OH',\n",
" 'region_gid': 'whosonfirst:region:85688485',\n",
" 'source': 'openaddresses',\n",
" 'source_id': 'us/oh/montgomery:f0b7b6080f90c1fb',\n",
" 'street': 'S Paul Laurence Dunbar St'},\n",
" 'type': 'Feature'},\n",
" {'geometry': {'coordinates': [-84.217674, 39.751668], 'type': 'Point'},\n",
" 'properties': {'accuracy': 'point',\n",
" 'confidence': 0.658,\n",
" 'country': 'United States',\n",
" 'country_a': 'USA',\n",
" 'country_gid': 'whosonfirst:country:85633793',\n",
" 'county': 'Montgomery County',\n",
" 'county_gid': 'whosonfirst:county:102083667',\n",
" 'gid': 'openaddresses:address:us/oh/montgomery:5d4a98354ce9b29b',\n",
" 'housenumber': '306',\n",
" 'id': 'us/oh/montgomery:5d4a98354ce9b29b',\n",
" 'label': '306 S Paul Laurence Dunbar St, Dayton, OH, USA',\n",
" 'layer': 'address',\n",
" 'localadmin': 'Dayton',\n",
" 'localadmin_gid': 'whosonfirst:localadmin:404524075',\n",
" 'locality': 'Dayton',\n",
" 'locality_gid': 'whosonfirst:locality:101712161',\n",
" 'name': '306 S Paul Laurence Dunbar St',\n",
" 'postalcode': '45402',\n",
" 'region': 'Ohio',\n",
" 'region_a': 'OH',\n",
" 'region_gid': 'whosonfirst:region:85688485',\n",
" 'source': 'openaddresses',\n",
" 'source_id': 'us/oh/montgomery:5d4a98354ce9b29b',\n",
" 'street': 'S Paul Laurence Dunbar St'},\n",
" 'type': 'Feature'},\n",
" {'geometry': {'coordinates': [-84.217701, 39.748886], 'type': 'Point'},\n",
" 'properties': {'accuracy': 'point',\n",
" 'confidence': 0.658,\n",
" 'country': 'United States',\n",
" 'country_a': 'USA',\n",
" 'country_gid': 'whosonfirst:country:85633793',\n",
" 'county': 'Montgomery County',\n",
" 'county_gid': 'whosonfirst:county:102083667',\n",
" 'gid': 'openaddresses:address:us/oh/montgomery:37dfd1f9d7655836',\n",
" 'housenumber': '538',\n",
" 'id': 'us/oh/montgomery:37dfd1f9d7655836',\n",
" 'label': '538 S Paul Laurence Dunbar St, Dayton, OH, USA',\n",
" 'layer': 'address',\n",
" 'localadmin': 'Dayton',\n",
" 'localadmin_gid': 'whosonfirst:localadmin:404524075',\n",
" 'locality': 'Dayton',\n",
" 'locality_gid': 'whosonfirst:locality:101712161',\n",
" 'name': '538 S Paul Laurence Dunbar St',\n",
" 'neighbourhood': 'Five Points',\n",
" 'neighbourhood_gid': 'whosonfirst:neighbourhood:85877005',\n",
" 'postalcode': '45402',\n",
" 'region': 'Ohio',\n",
" 'region_a': 'OH',\n",
" 'region_gid': 'whosonfirst:region:85688485',\n",
" 'source': 'openaddresses',\n",
" 'source_id': 'us/oh/montgomery:37dfd1f9d7655836',\n",
" 'street': 'S Paul Laurence Dunbar St'},\n",
" 'type': 'Feature'},\n",
" {'geometry': {'coordinates': [-84.217678, 39.739573], 'type': 'Point'},\n",
" 'properties': {'accuracy': 'point',\n",
" 'confidence': 0.658,\n",
" 'country': 'United States',\n",
" 'country_a': 'USA',\n",
" 'country_gid': 'whosonfirst:country:85633793',\n",
" 'county': 'Montgomery County',\n",
" 'county_gid': 'whosonfirst:county:102083667',\n",
" 'gid': 'openaddresses:address:us/oh/montgomery:7d8c78e63a02dcef',\n",
" 'housenumber': '1219',\n",
" 'id': 'us/oh/montgomery:7d8c78e63a02dcef',\n",
" 'label': '1219 S Paul Laurence Dunbar St, Dayton, OH, USA',\n",
" 'layer': 'address',\n",
" 'localadmin': 'Dayton',\n",
" 'localadmin_gid': 'whosonfirst:localadmin:404524075',\n",
" 'locality': 'Dayton',\n",
" 'locality_gid': 'whosonfirst:locality:101712161',\n",
" 'name': '1219 S Paul Laurence Dunbar St',\n",
" 'neighbourhood': 'Five Points',\n",
" 'neighbourhood_gid': 'whosonfirst:neighbourhood:85877005',\n",
" 'postalcode': '45417',\n",
" 'region': 'Ohio',\n",
" 'region_a': 'OH',\n",
" 'region_gid': 'whosonfirst:region:85688485',\n",
" 'source': 'openaddresses',\n",
" 'source_id': 'us/oh/montgomery:7d8c78e63a02dcef',\n",
" 'street': 'S Paul Laurence Dunbar St'},\n",
" 'type': 'Feature'}],\n",
" 'geocoding': {'attribution': 'https://search.mapzen.com/v1/attribution',\n",
" 'engine': {'author': 'Mapzen', 'name': 'Pelias', 'version': '1.0'},\n",
" 'query': {'private': False,\n",
" 'querySize': 20,\n",
" 'size': 10,\n",
" 'text': 'PAUL LAURENCE DUNBAR RESIDENCE'},\n",
" 'timestamp': 1489009685369,\n",
" 'version': '0.2'},\n",
" 'type': 'FeatureCollection'}"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"results[0]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Note that the first result for this search ('South Paul Laurence Dunbar Street, Dayton, OH, USA') is definitely not what we want. Since, we're not providing all the location we have to the search (we only used the place name), the performance is not nearly as good as it could be (basically, we have to hope that the place we're looking for is significant enough to be identified in one of the open datasets behind the geocoder)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's combine the four pieces of location information we have (place name, street address, city, state) together before we pass it to the search query."
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"better_results = []\n",
"for place in DATASET:\n",
" search_string = ', '.join([place['place_name'], place['street_address'], place['city'], place['state']])\n",
" search_request = requests.get('https://search.mapzen.com/v1/search?api_key={0}&text={1}'.format(os.environ['MAPZEN_API_KEY'], search_string))\n",
" better_results.append(search_request.json())"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"{'bbox': [-77.119759, 38.791645, -76.909393, 38.995548],\n",
" 'features': [{'bbox': [-77.119759, 38.791645, -76.909393, 38.995548],\n",
" 'geometry': {'coordinates': [-77.014681, 38.912167], 'type': 'Point'},\n",
" 'properties': {'accuracy': 'centroid',\n",
" 'confidence': 0.6,\n",
" 'country': 'United States',\n",
" 'country_a': 'USA',\n",
" 'country_gid': 'whosonfirst:country:85633793',\n",
" 'county': 'District of Columbia',\n",
" 'county_gid': 'whosonfirst:county:102084889',\n",
" 'gid': 'whosonfirst:locality:85931779',\n",
" 'id': '85931779',\n",
" 'label': 'Washington, DC, USA',\n",
" 'layer': 'locality',\n",
" 'locality': 'Washington',\n",
" 'locality_gid': 'whosonfirst:locality:85931779',\n",
" 'match_type': 'fallback',\n",
" 'name': 'Washington',\n",
" 'region': 'District of Columbia',\n",
" 'region_a': 'DC',\n",
" 'region_gid': 'whosonfirst:region:85688741',\n",
" 'source': 'whosonfirst',\n",
" 'source_id': '85931779'},\n",
" 'type': 'Feature'}],\n",
" 'geocoding': {'attribution': 'https://search.mapzen.com/v1/attribution',\n",
" 'engine': {'author': 'Mapzen', 'name': 'Pelias', 'version': '1.0'},\n",
" 'query': {'parsed_text': {'city': 'washington',\n",
" 'number': '321',\n",
" 'query': 'paul laurence dunbar residence',\n",
" 'state': 'dc',\n",
" 'street': 'u street nw'},\n",
" 'private': False,\n",
" 'querySize': 20,\n",
" 'size': 10,\n",
" 'text': 'PAUL LAURENCE DUNBAR RESIDENCE, 321 U Street, NW, Washington, DC'},\n",
" 'timestamp': 1489055115092,\n",
" 'version': '0.2'},\n",
" 'type': 'FeatureCollection'}"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"better_results[0]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The top result is in Washington, DC, so this already looks much more promising! We could also use Mapzen's [structured search](https://mapzen.com/documentation/search/structured-geocoding/) endpoint"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"still_better_results = []\n",
"for place in DATASET:\n",
" payload = {\n",
" 'api_key': os.environ['MAPZEN_API_KEY'],\n",
" 'address': place['street_address'],\n",
" 'locality': place['city'],\n",
" 'region': place['state']\n",
" }\n",
" search_request = requests.get('https://search.mapzen.com/v1/search/structured', params=payload)\n",
" still_better_results.append(search_request.json())"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"{'bbox': [-77.119759, 38.791645, -76.909393, 38.995548],\n",
" 'features': [{'bbox': [-77.119759, 38.791645, -76.909393, 38.995548],\n",
" 'geometry': {'coordinates': [-77.014681, 38.912167], 'type': 'Point'},\n",
" 'properties': {'accuracy': 'centroid',\n",
" 'confidence': 0.6,\n",
" 'country': 'United States',\n",
" 'country_a': 'USA',\n",
" 'country_gid': 'whosonfirst:country:85633793',\n",
" 'county': 'District of Columbia',\n",
" 'county_gid': 'whosonfirst:county:102084889',\n",
" 'gid': 'whosonfirst:locality:85931779',\n",
" 'id': '85931779',\n",
" 'label': 'Washington, DC, USA',\n",
" 'layer': 'locality',\n",
" 'locality': 'Washington',\n",
" 'locality_gid': 'whosonfirst:locality:85931779',\n",
" 'match_type': 'fallback',\n",
" 'name': 'Washington',\n",
" 'region': 'District of Columbia',\n",
" 'region_a': 'DC',\n",
" 'region_gid': 'whosonfirst:region:85688741',\n",
" 'source': 'whosonfirst',\n",
" 'source_id': '85931779'},\n",
" 'type': 'Feature'}],\n",
" 'geocoding': {'attribution': 'https://search.mapzen.com/v1/attribution',\n",
" 'engine': {'author': 'Mapzen', 'name': 'Pelias', 'version': '1.0'},\n",
" 'query': {'parsed_text': {'city': 'washington',\n",
" 'number': '321',\n",
" 'state': 'DC',\n",
" 'street': 'U Street, NW'},\n",
" 'private': False,\n",
" 'querySize': 20,\n",
" 'size': 10},\n",
" 'timestamp': 1489055591607,\n",
" 'version': '0.2'},\n",
" 'type': 'FeatureCollection'}"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"still_better_results[0]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's map the result to check our work …"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"<div style=\"width:100%;\"><div style=\"position:relative;width:100%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AgICAgICB2YXIgbm9ydGhFYXN0ID0gTC5sYXRMbmcoOTAsIDE4MCk7CiAgICAgICAgICAgICAgICB2YXIgYm91bmRzID0gTC5sYXRMbmdCb3VuZHMoc291dGhXZXN0LCBub3J0aEVhc3QpOwogICAgICAgICAgICAKCiAgICAgICAgICAgIHZhciBtYXBfMGY0ZTA1NGFlOGEzNGMyYzg5OTA1OGU4NmFhYzFmMDMgPSBMLm1hcCgKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICdtYXBfMGY0ZTA1NGFlOGEzNGMyYzg5OTA1OGU4NmFhYzFmMDMnLAogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAge2NlbnRlcjogWzM4LjkxMjE2NywtNzcuMDE0NjgxXSwKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHpvb206IDE2LAogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgbWF4Qm91bmRzOiBib3VuZHMsCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBsYXllcnM6IFtdLAogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgd29ybGRDb3B5SnVtcDogZmFsc2UsCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBjcnM6IEwuQ1JTLkVQU0czODU3CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIH0pOwogICAgICAgICAgICAKICAgICAgICAKICAgIAogICAgICAgICAgICB2YXIgdGlsZV9sYXllcl9hZDBkMmNiZTI4NTM0OWFhYTA0ZGNjZTQzZjlmNDAzMyA9IEwudGlsZUxheWVyKAogICAgICAgICAgICAgICAgJ2h0dHBzOi8vc3RhbWVuLXRpbGVzLXtzfS5hLnNzbC5mYXN0bHkubmV0L3RvbmVyL3t6fS97eH0ve3l9LnBuZycsCiAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgbWF4Wm9vbTogMTgsCiAgICAgICAgICAgICAgICAgICAgbWluWm9vbTogMTAsCiAgICAgICAgICAgICAgICAgICAgY29udGludW91c1dvcmxkOiBmYWxzZSwKICAgICAgICAgICAgICAgICAgICBub1dyYXA6IGZhbHNlLAogICAgICAgICAgICAgICAgICAgIGF0dHJpYnV0aW9uOiAnTWFwIHRpbGVzIGJ5IDxhIGhyZWY9Imh0dHA6Ly9zdGFtZW4uY29tIj5TdGFtZW4gRGVzaWduPC9hPiwgdW5kZXIgPGEgaHJlZj0iaHR0cDovL2NyZWF0aXZlY29tbW9ucy5vcmcvbGljZW5zZXMvYnkvMy4wIj5DQyBCWSAzLjA8L2E+LiBEYXRhIGJ5IDxhIGhyZWY9Imh0dHA6Ly9vcGVuc3RyZWV0bWFwLm9yZyI+T3BlblN0cmVldE1hcDwvYT4sIHVuZGVyIDxhIGhyZWY9Imh0dHA6Ly93d3cub3BlbnN0cmVldG1hcC5vcmcvY29weXJpZ2h0Ij5PRGJMPC9hPi4nLAogICAgICAgICAgICAgICAgICAgIGRldGVjdFJldGluYTogZmFsc2UKICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICApLmFkZFRvKG1hcF8wZjRlMDU0YWU4YTM0YzJjODk5MDU4ZTg2YWFjMWYwMyk7CgogICAgICAgIAogICAgCgogICAgICAgICAgICB2YXIgbWFya2VyXzgxYmZhZGQ0N2MwNjRjM2M5YjU5OGE1N2YxZGUyZTI0ID0gTC5tYXJrZXIoCiAgICAgICAgICAgICAgICBbMzguOTEyMTY3LC03Ny4wMTQ2ODFdLAogICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgIGljb246IG5ldyBMLkljb24uRGVmYXVsdCgpCiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgKQogICAgICAgICAgICAgICAgLmFkZFRvKG1hcF8wZjRlMDU0YWU4YTM0YzJjODk5MDU4ZTg2YWFjMWYwMyk7CiAgICAgICAgICAgIAogICAgCiAgICAgICAgICAgIHZhciBwb3B1cF9mNmZlMmY4ODgwOWE0OGNhYmI4YjVlMTRjZjFmYWExYSA9IEwucG9wdXAoe21heFdpZHRoOiAnMzAwJ30pOwoKICAgICAgICAgICAgCiAgICAgICAgICAgICAgICB2YXIgaHRtbF84M2M2ZTljNDVkZmM0ZTFlYmQ2ODZlNDc2ZjZiNTcwNCA9ICQoJzxkaXYgaWQ9Imh0bWxfODNjNmU5YzQ1ZGZjNGUxZWJkNjg2ZTQ3NmY2YjU3MDQiIHN0eWxlPSJ3aWR0aDogMTAwLjAlOyBoZWlnaHQ6IDEwMC4wJTsiPlBBVUwgTEFVUkVOQ0UgRFVOQkFSIFJFU0lERU5DRTwvZGl2PicpWzBdOwogICAgICAgICAgICAgICAgcG9wdXBfZjZmZTJmODg4MDlhNDhjYWJiOGI1ZTE0Y2YxZmFhMWEuc2V0Q29udGVudChodG1sXzgzYzZlOWM0NWRmYzRlMWViZDY4NmU0NzZmNmI1NzA0KTsKICAgICAgICAgICAgCgogICAgICAgICAgICBtYXJrZXJfODFiZmFkZDQ3YzA2NGMzYzliNTk4YTU3ZjFkZTJlMjQuYmluZFBvcHVwKHBvcHVwX2Y2ZmUyZjg4ODA5YTQ4Y2FiYjhiNWUxNGNmMWZhYTFhKTsKCiAgICAgICAgICAgIAogICAgICAgIAo8L3NjcmlwdD4=\" style=\"position:absolute;width:100%;height:100%;left:0;top:0;border:none !important;\" allowfullscreen webkitallowfullscreen mozallowfullscreen></iframe></div></div>"
],
"text/plain": [
"<folium.folium.Map at 0x1072c5f28>"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"point_of_interest = still_better_results[0]['features'][0]['geometry']['coordinates']\n",
"# Mapzen returns GeoJSON so we have to remember to switch the order of lat/lon\n",
"coords = [point_of_interest[1], point_of_interest[0]]\n",
"\n",
"quick_map = folium.Map(location=coords, tiles='Stamen Toner', min_zoom=10, zoom_start=16)\n",
"folium.Marker(coords, popup=DATASET[0]['place_name']).add_to(quick_map)\n",
"quick_map"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python [conda env:base]",
"language": "python",
"name": "conda-env-base-py"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.4.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment