You will learn: how to build an app to find addresses and places with the ArcGIS World Geocoding Service.
The ArcGIS World Geocoding Service can find addresses and places, convert addresses to coordinates, and perform batch geocoding. The geocoding
module contains the geocode()
function for forward geocoding that can be used to search for single field addresses, multi-field addresses, points of interest etc. The reverse_geocode()
function can be used to find addresses of coordinates.
The ArcGIS API for Python tutorials use Jupyter Notebooks to execute Python code. If you are new to this environment, please see the guide to install the API and use notebooks locally.
Go to Esri Juptyter Notebooks and click New > Python 3 to create a new notebook.
In each step below, type (or copy and paste) the commands into a new notebook cell and run the code by clicking run cell or pressing shift + Enter.
Add the following code in a cell to import the ArcGIS API for Python.
from arcgis.gis import GIS
from arcgis.geocoding import geocode, reverse_geocode
from arcgis.geometry import Point
Log into ArcGIS Online as an anonymous user.
gis = GIS()
Geocode by query to locate the Hollywood sign.
geocode_result = geocode(address="Hollywood sign", as_featureset=True)
# A list of features
geocode_result.features
Display a map of Los Angeles to display the geocode result.
m = gis.map("Los Angeles, CA", zoomlevel=11)
m
Draw the geocode result on the map.
m.draw(geocode_result)
Clear the map and try geocoding the following addresses.
380 New York St, Redlands, CA
New York City
92354
m.clear_graphics()
Construct a Point
geometry object for the following latitude and longitude: 34.13419,-118.29636
.
location = {
'Y': 34.13419, # `Y` is latitude
'X': -118.29636, # `X` is longitude
'spatialReference': {
'wkid':4326
}
}
unknown_pt = Point(location)
Reverse geocode the Point
to get the address.
address = reverse_geocode(location=unknown_pt)
address
And draw it on the map.
m.draw(address)
Your notebook should look something like this.
Find multiple points of interest using the geocode
function. For example, suppose you want to search for Fish Tacos in Portland, Oregon.
First create a query as a dict
.
fish_query_portland = {
'Address': 'fish tacos',
'City': 'Portland',
'Region': 'OR'
}
Then create a map located in Portland, OR.
m = gis.map('Portland, Oregon', zoomlevel=11)
m
And then draw the geocoded locations.
fish_tacos_places = geocode(fish_query_portland)
for fish_taco_place in fish_taco_places:
m.draw(fish_taco_place['location'])