Learn how to find an address the geocoding service.
Geocoding is the process of converting address or place text into a location. The geocoding service provides address and place geocoding as well as reverse geocoding.
In this tutorial, you use the geocode
class to find addresses in Los Angeles, CA.
Prerequisites
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.
Steps
Import modules and log in
-
Import the
arcgis.gis
module. This module is the entry point into the GIS and provides the functionality to manage GIS content, users, and groups. Additionally, import thegeocode
andreverse
classes from the_geocode arcgis.geocoding
module. Additionally, import thePoint
class from thearcgis.geometry
module.Use dark colors for code blocks from arcgis.gis import GIS from arcgis.geocoding import geocode, reverse_geocode from arcgis.geometry import Point from arcgis.geometry import Feature
-
Log into ArcGIS Online as an anonymous user. Simple geocoding and reverse geocoding do not require credits, hence you need not log in using your credentials.
Use dark colors for code blocks from arcgis.gis import GIS from arcgis.geocoding import geocode, reverse_geocode from arcgis.geometry import Point from arcgis.geometry import Feature portal = GIS()
Find an address
-
Search for an address by passing in the string "1000 Vin Scully Ave, Los Angeles, CA" as the address parameter of the geocode function. The
geocode()
function is versatile and accepts a number of relevant parameters. Refer to Finding points of interest to learn more.Use dark colors for code blocks portal = GIS() geocode_results = geocode(address="1000 Vin Scully Ave, Los Angeles, CA", as_featureset=True) geocode_results
Reverse geocode a coordinate
-
Construct a
Point
geometry object for the following latitude and longitude:34.13419,-118.29636
. The coordinates specified here are in latitude, longitude which correspond to the spatial referenceGCS
. For a complete list of supported IDs and their corresponding definition strings, see: Spatial references._WGS _1984 Use dark colors for code blocks geocode_results = geocode(address="1000 Vin Scully Ave, Los Angeles, CA", as_featureset=True) geocode_results location = {"Y": 34.13419, "X": -118.29636, "spatialReference": {"wkid": 4326}} unknown_pt = Point(location)
-
Use the
reverse
function to find the address from a point location. Create a_geocode Feature
from the returneddict
and it to the geocoded_resultsFeature
.Set Use dark colors for code blocks location = {"Y": 34.13419, "X": -118.29636, "spatialReference": {"wkid": 4326}} unknown_pt = Point(location) reverse_result = reverse_geocode(location=unknown_pt) reverse_result reverse_result_feature = Feature(geometry=reverse_result["location"],attributes=reverse_result["address"]) geocode_results.features.append(reverse_result_feature) geocode_results
Create a map
-
Create an instance of the map widget and set the initial extent by passing in a place name string of
Los Angeles, CA
.Use dark colors for code blocks reverse_result = reverse_geocode(location=unknown_pt) reverse_result reverse_result_feature = Feature(geometry=reverse_result["location"],attributes=reverse_result["address"]) geocode_results.features.append(reverse_result_feature) geocode_results map = portal.map("Los Angeles, CA") map
Display the results.
-
Use the
add
method to display the results on the map. Since you specified theas
optional parameter while geocoding, you received the result as a_featureset= True Feature
object instead of a dictionary.Set Feature
objects can be easily visualized on the map by passing to theSet add
method.Use dark colors for code blocks map = portal.map("Los Angeles, CA") map from arcgis.map.popups import PopupInfo map.content.add(geocode_results, popup_info=PopupInfo(title="{Addr_type}", description="Address: {LongLabel}"))
Try geocoding using the following addresses.
- Single line address:
380 New York St, Redlands, CA
- Administrative place name:
New York City
- Zipcode: 66952
You should see the results of both the geocoded address and reverse geocoded point displayed as graphics on the map.
What's next?
Learn how to use additional functionality in these tutorials: