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.
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.
GeocodeAddress.py
and save it to the C:ArcpyBookCh12
folder.requests
and json
modules:import requests import json
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'}
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)
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']))
C:ArcpyBookcodeCh12GeocodeAddress.py
solution file.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
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.