Learn how to access local data, such as spending trends, for the United States with the GeoEnrichment service.
The GeoEnrichment service provides detailed local data for specific countries. Each individual data field is represented by an analysis variable. Analysis variables are organized into data categories such as spending and market behaviors with names such as 2024 Educational Attainment or 2024 Seen Video Ad at Gas Station Last 30 Days. The data available varies by country and by data provider.
In this tutorial, you use the GeoEnrichment service to display spending trend information for a study area within the United States.
Steps
Import modules and log in
-
Import the
GIS
class from thearcgis.gis
module. TheGIS
class provides helper objects to manage (search, create, and manage) GIS resources such as content, users, and groups. Additionally, import theGeometry
andPoint
classes from thearcgis.geometry
module, theFeature
class fromSet arcgis.features
module and theCountry
method from thearcgis.geoenrichment
module.Use dark colors for code blocks from arcgis.gis import GIS from arcgis.features import FeatureSet from arcgis.geoenrichment import Country from arcgis.geometry import Geometry, Point
-
Log in to your portal. In a hosted notebook, You can use the
home
parameter to use the credentials of the currently logged in account.Use dark colors for code blocks from arcgis.gis import GIS from arcgis.features import FeatureSet from arcgis.geoenrichment import Country from arcgis.geometry import Geometry, Point portal = GIS("home") print(f"Connected to {portal.properties.name} as {portal.properties.user.username}")
Create study area point
-
Use the
Point
class to construct a point geometry by passing in the coordinates and spatial reference representing a location in Nashville, Tennessee.Use dark colors for code blocks portal = GIS("home") print(f"Connected to {portal.properties.name} as {portal.properties.user.username}") study_area_pt = Point({"x": -86.7679, "y": 36.1745, "spatialReference": {"wkid": 4326}})
Get demographic data
-
Use the
Country
class from thearcgis.geoenrichment
module. This provides access to local variables for a specific country. Pass in the study area point and the following variable names :Psychographics
Shopping. M P28067 A _B transportation.
X7027 _I entertainment.
X9005 _I lifemodegroups
NE W. TLIFENAME
To learn more about the data collections and variables, go to Data Collections and GeoEnrichment coverage.
Use dark colors for code blocks study_area_pt = Point({"x": -86.7679, "y": 36.1745, "spatialReference": {"wkid": 4326}}) usa_data = Country("usa", portal) enrich_results = usa_data.enrich( study_areas=[study_area_pt], enrich_variables=[ "PsychographicsShopping.MP28067A_B", "transportation.X7027_I", "entertainment.X9005_I", "lifemodegroupsNEW.TLIFENAME", ], )
-
To view the values returned from the enrichment, use the
dataframe
object returned from theenrich
method to display the results as a table. Pass in an array of field names to limit the results to a subset of columns.Use dark colors for code blocks usa_data = Country("usa", portal) enrich_results = usa_data.enrich( study_areas=[study_area_pt], enrich_variables=[ "PsychographicsShopping.MP28067A_B", "transportation.X7027_I", "entertainment.X9005_I", "lifemodegroupsNEW.TLIFENAME", ], ) display_cols = [ "aggregation_method", "population_to_polygon_size_rating", "apportionment_confidence", "tlifename", "x9005_i", "mp28067a_b", "x7027_i", ] enrich_results[display_cols]
Display the results on a map
-
Use the
map
property from theGIS
class to create a map. To set the initial extent of the map, pass in the stringNashville, TN
This will set the map to an extent that is appropriate for the results. Set the basemap of the map toarcgis-navigation
.Use dark colors for code blocks display_cols = [ "aggregation_method", "population_to_polygon_size_rating", "apportionment_confidence", "tlifename", "x9005_i", "mp28067a_b", "x7027_i", ] enrich_results[display_cols] map = portal.map("Nashville, TN") map.basemap.basemap = "arcgis-navigation" map
-
Use the
Feature
class to convert the returned Pandas DataFrame into a feature set. Create a popup object that will display formatted attribute values when a feature is clicked in the map.Set Use dark colors for code blocks map = portal.map("Nashville, TN") map.basemap.basemap = "arcgis-navigation" map enrich_features = FeatureSet.from_dataframe(enrich_results) enrich_features # set popup info from arcgis.map.popups import PopupInfo popup_fields = [ "Buys natural products: {mp28067a_b}", "Membership fees for Social Clubs: {x9005_i}", "Auto/Truck rental on trips: {x7027_i}", "Tapestry group name: {tlifename}", ] pi = PopupInfo( title="Data for a 1 mile search radius", description="<br>".join(popup_fields) )
-
Use the
add
method to pass in the enriched geometry and popup info to add the results to the map content.Use dark colors for code blocks enrich_features = FeatureSet.from_dataframe(enrich_results) enrich_features # set popup info from arcgis.map.popups import PopupInfo popup_fields = [ "Buys natural products: {mp28067a_b}", "Membership fees for Social Clubs: {x9005_i}", "Auto/Truck rental on trips: {x7027_i}", "Tapestry group name: {tlifename}", ] pi = PopupInfo( title="Data for a 1 mile search radius", description="<br>".join(popup_fields) ) map.content.add(enrich_features, popup_info=pi)
You should see a map centered around Nashville, TN with a graphic representing a 1 mile radius around the study area point. Click on the graphic to display a popup containing local demographic values.
What's next?
Learn how to use additional functionality in these tutorials: