<jupyter_start><jupyter_text>GeocodingGeocoding is the process of getting coordinates (latitude, longitude) for a street address or landmark.  You can also do the reverse and get a street address for a given set of coordinates.  In order to accomplish this task you need an up to date system of record to map back and forth which is something the [HERE Geocoder API](https://developer.here.com/documentation/geocoder/topics/what-is.html) can provide.We'll look at a few common libraries that can be used for geocoding operations:* `requests`* `geopy`* `geocoder`* `geopandas`After looking at a few more variations you can experiment with a few example projects.* Analyzing Image Metadata* Analyzing Text  CredentialsAccess control for HERE services uses an APP_ID and APP_CODE that can be retrieved from the [Developer Portal](https://developer.here.com/projects) for your project.  I like to store these as environment variables.<jupyter_code>import os

APP_ID_HERE = os.environ['APP_ID_HERE']
APP_CODE_HERE = os.environ['APP_CODE_HERE']
TEST_ADDRESS = '300 Lakeside Ave. Cleveland, OH 44113'<jupyter_output><empty_output><jupyter_text>Popular Python Libraries for Geocoding  RequestsSimple example of making an HTTP call with the **requests** library to retrieve a street address.<jupyter_code>import json
import requests

uri = 'https://geocoder.api.here.com/6.2/geocode.json'
params = {
    'app_id': APP_ID_HERE,
    'app_code': APP_CODE_HERE,
    'searchtext': TEST_ADDRESS,
}

response = requests.get(uri, params=params)
data = json.loads(response.text)
print(json.dumps(data, indent=4, sort_keys=True))<jupyter_output><empty_output><jupyter_text>GeopyA client to consider instead of direct `requests` is `geopy`.  It includes classes for multiple geocoder services including HERE.https://pypi.org/project/geopy/<jupyter_code>from geopy.geocoders import Here

geocoder = Here(APP_ID_HERE, APP_CODE_HERE)
result = geocoder.geocode(TEST_ADDRESS)
result.address
result.raw<jupyter_output><empty_output><jupyter_text>GeocoderAnother library to consider is `geocoder`.https://geocoder.readthedocs.io/<jupyter_code>import geocoder

result = geocoder.here(TEST_ADDRESS, app_id=APP_ID_HERE, app_code=APP_CODE_HERE)
result.latlng<jupyter_output><empty_output><jupyter_text>GeoPandasGeoPandas is another powerful library that provides geocoding methods.http://geopandas.org/geocoding.html<jupyter_code>import geopandas

result = geopandas.tools.geocode(TEST_ADDRESS, provider='Here', app_id=APP_ID_HERE, app_code=APP_CODE_HERE)
result<jupyter_output><empty_output><jupyter_text>More ExamplesDepending on your use case the underlying Geocoding APIs from HERE can give much finer level control over the results and how you might use it.<jupyter_code>GEOCODER_URI = 'https://geocoder.api.here.com/6.2/geocode.json'
REVERSE_GEOCODER_URI = 'https://reverse.geocoder.api.here.com/6.2/reversegeocode.json'<jupyter_output><empty_output><jupyter_text>Bounding BoxWhen displaying a map you may already have a viewport that allows you to search for partial matches within that area.<jupyter_code>params = {
    'app_id': APP_ID_HERE,
    'app_code': APP_CODE_HERE,
    'mapview': '42.3902,-71.1293;42.3312,-71.0228',
    'searchtext': '1 main'
}

response = requests.get(GEOCODER_URI, params=params)
response.json()<jupyter_output><empty_output><jupyter_text>Address By PartsYou may know specific parts of the street address and want to do some marginal address validation.<jupyter_code>params = {
    'app_id': APP_ID_HERE,
    'app_code': APP_CODE_HERE,
    'housenumber': '427',
    'street': 'randolph',
    'city': 'chicago'
}

response = requests.get(GEOCODER_URI, params=params)
response.json()<jupyter_output><empty_output><jupyter_text>Street IntersectionYou may not be looking for a specific building footprint but rather a roadway or intersection.<jupyter_code>params = {
    'app_id': APP_ID_HERE,
    'app_code': APP_CODE_HERE,
    'street': 'invaliden @ chaussee',
    'city': 'berlin',
    'country': 'germany'
}

response = requests.get(GEOCODER_URI, params=params)
response.json()<jupyter_output><empty_output><jupyter_text>Reverse GeocodeHERE provides a positioning API that can be used to find an approximate latitude and longitude based on GPS, Cell Towers, and WiFi access points as part of a radio map database.If you have a lat, long you can reverse geocode to identify a street address.<jupyter_code>params = {
    'app_id': APP_ID_HERE,
    'app_code': APP_CODE_HERE,
    'prox': '41.504,-81.6941,250',
    'mode': 'retrieveAddresses',
    'maxresults': '1'
}

response = requests.get(REVERSE_GEOCODER_URI, params=params)
response.json()['Response']['View'][0]['Result'][0]['Location']<jupyter_output><empty_output><jupyter_text>Shape GeometryThe geocoder can also retrieve geometry representing the border of a particular area such as a zip code.  See Python below that you can work with or that this webapp also demonstrates.https://dbabbs.github.io/geojson-shape-tool/<jupyter_code>import shapely.wkt

params = {
    'app_id': APP_ID_HERE,
    'app_code': APP_CODE_HERE,
    'prox': '41.504,-81.6941,250',
    'mode': 'retrieveAddresses',
    'maxresults': '1',
    'additionaldata': 'IncludeShapeLevel,postalCode'
}

response = requests.get(REVERSE_GEOCODER_URI, params=params)
shp = response.json()['Response']['View'][0]['Result'][0]['Location']['Shape']['Value']
shapely.wkt.loads(shp)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10657095527332965, 'fraction_numerical': 0.01417264862875023, 'mean_word_length': 6.112565445026178, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 45, 'https://': 8, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '619887', 'license': 'Apache-2.0', 'n_tokens_mistral': 1870, 'n_tokens_neox': 1813, 'n_words': 470, 'path': 'python/pycon19/01_geocoding.ipynb', 'repo_name': 'sangeeta2306/devrel-workshops', 'size': '5433'}