Watch Me Code 2: Geocoding

  • Get Lat/Long for address
  • Get city / state for zipcode

We will use the OpenStreetMaps API:

https://nominatim.openstreetmap.org/search


In [3]:
import requests

text = 'Syracuse Hancock Airport'
data =  { 'q' : text, 'format' : 'json'}
url='https://nominatim.openstreetmap.org/search' 
response = requests.get(url, params=data)
geodata = response.json()
geodata


Out[3]:
[{'place_id': '111878987',
  'licence': 'Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright',
  'osm_type': 'way',
  'osm_id': '157309115',
  'boundingbox': ['43.103859', '43.1244225', '-76.1413199', '-76.0880625'],
  'lat': '43.1141752',
  'lon': '-76.1191445114361',
  'display_name': 'Syracuse Hancock International Airport, Colonel Eileen Collins Boulevard, Hinsdale, Town of Salina, Onondaga County, New York, 13211, USA',
  'class': 'aeroway',
  'type': 'aerodrome',
  'importance': 0.7489572928996011,
  'icon': 'https://nominatim.openstreetmap.org/images/mapicons/transport_airport2.p.20.png'},
 {'place_id': '113624545',
  'licence': 'Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright',
  'osm_type': 'way',
  'osm_id': '159452639',
  'boundingbox': ['43.1129398', '43.1147662', '-76.1120426', '-76.110914'],
  'lat': '43.1138409',
  'lon': '-76.1114685894623',
  'display_name': 'Syracuse Hancock International Airport - Main Concourse, Hancock International Airport, Town of DeWitt, Onondaga County, New York, 13211, USA',
  'class': 'aeroway',
  'type': 'terminal',
  'importance': 0.30100000000000005}]

In [8]:
def geocode(text):
    data =  { 'q' : text, 'format' : 'json'}
    url='https://nominatim.openstreetmap.org/search' 
    response = requests.get(url, params=data)
    geodata = response.json()
    return geodata

geocode('13244 USA')


Out[8]:
[{'place_id': '78764052',
  'licence': 'Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright',
  'osm_type': 'way',
  'osm_id': '20149345',
  'boundingbox': ['43.0348516', '43.0354208', '-76.1344986', '-76.1341443'],
  'lat': '43.0350996',
  'lon': '-76.1341474',
  'display_name': 'Syracuse, Onondaga County, New York, 13244, USA',
  'class': 'place',
  'type': 'postcode',
  'importance': 0.44500000000000006},
 {'place_id': '200373302',
  'licence': 'Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright',
  'boundingbox': ['43.036452557967',
   '43.036552557967',
   '-76.13685887663',
   '-76.13675887663'],
  'lat': '43.0365025579672',
  'lon': '-76.1368088766295',
  'display_name': 'Syracuse, New York, 13244, USA',
  'class': 'place',
  'type': 'postcode',
  'importance': 0.44500000000000006}]

In [11]:
usa_zips = ['13244', '60601', '10001', '90210', '77030']
for zipcode in usa_zips:
    data = geocode(zipcode + ' USA')
    print(zipcode, data[0]['display_name'])


13244 Syracuse, Onondaga County, New York, 13244, USA
60601 Chicago, Illinois, 60601, USA
10001 Chelsea, NYC, New York, 10001, USA
90210 LA, California, 90210, USA
77030 Houston, Harris County, Texas, 77030, USA

In [ ]: