Geocoding with the Esri World Geocoding Service

The Esri World Geocoding Service can be used to find addresses and places in supported countries. This service contains both free and paid operations. The find operation, which finds one address per request, is always a free service. The geocodeAddresses operation accepts a list of addresses for geocoding and is a paid service only. The other operations can be free or paid. If you are using the operations in a temporary capacity, they are free. Temporary simply means that you aren't storing the results for later use. If this is the case, then it is a paid service. In this recipe, you will use the Esri World Geocoding service to geocode an address.

Getting ready

The ArcGIS REST API find operation can be used to find the geographic coordinates of a single address. As we've done in the past few recipes, we'll use the Python requests module to make the request and process the response.

How to do it…

  1. In IDLE or another Python development environment, create a new Python script called GeocodeAddress.py and save it to the C:ArcpyBookCh12 folder.
  2. Import the requests and json modules:
    import requests
    import json
  3. Create the following agisurl variable. This will point to the Esri World Geocoding Service and the find operation for a particular service. Also, define a Python dictionary that will hold the address to be submitted and the output format. You can change the address, if you wish:
    import requests
    import json
    agisurl = "http://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer/find"
    payload = { 'text': '1202 Sand Wedge, San Antonio, TX, 78258','f':'pjson'}
    
  4. Call the requests.get() method and pass the URL and parameters. The response will be stored in a variable called r. Then, convert the returned JSON object to a Python dictionary:
    import requests
    import json
    agisurl = "http://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer/find"
    payload = { 'text': '1202 Sand Wedge, San Antonio, TX, 78258','f':'pjson'}
    
    r = requests.get(agisurl, params=payload)
    
    decoded = json.loads(r.text)
    
  5. Print out some of the results:
    import requests
    import json
    agisurl = "http://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer/find"
    payload = { 'text': '1202 Sand Wedge, San Antonio, TX, 78258','f':'pjson'}
    
    r = requests.get(agisurl, params=payload)
    
    decoded = json.loads(r.text)
    
    print("The geocoded address: " + decoded['locations'][0]['name'])
    print("The longitude: " + str(decoded['locations'][0]['feature']['geometry']['x']))
    print("The lattitude: " + str(decoded['locations'][0]['feature']['geometry']['y']))
    print("The geocode score: " + str(decoded['locations'][0]['feature']['attributes']['Score']))
    print("The address type: " + str(decoded['locations'][0]['feature']['attributes']['Addr_Type']))
    
  6. You can check your work by examining the C:ArcpyBookcodeCh12GeocodeAddress.py solution file.
  7. Save the script and run it to see the following output:
    The geocoded address: 1202 Sand Wedge, San Antonio, Texas, 78258
    The longitude: -98.4744442811
    The lattitude: 29.6618639681
    The geocode score: 100
    The address type: PointAddress
    

How it works…

The find operation in the ArcGIS REST API can be used to perform geocoding operations for a single address. As we've done in the past few recipes, we used the Python requests.get() method to submit an operation request (find, in this case) along parameters that include the address to be geocoded. The response that was returned included the latitude and longitude of the address, geocoded score, and the address type.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset