Tutorial: Get local data

Learn how to access local data, such as spending trends, for the United States with the GeoEnrichment service.

Get local data

The GeoEnrichment service provides detailed local data for specific countries. Each individual data field is represented by an analysis variable that are organized into data categories such as spending and market behaviors such as 2022 Educational Attainment or 2022 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

  1. Import the GIS class from the arcgis.gis module. The GIS class provides helper objects to manage (search, create, and manage) GIS resources such as content, users, and groups. Additionally, import the Geometry and Point classes from the arcgis.geometry module, the FeatureSet class from arcgis.features and the Country method from the arcgis.geoenrichment module.

    Use dark colors for code blocks
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    from arcgis.gis import GIS
    from arcgis.features import FeatureSet
    from arcgis.geoenrichment import Country
    from arcgis.geometry import Geometry, Point
    
    
    
  2. Log in to ArcGIS Online using your credentials. 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
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    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

  1. 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
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    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

  1. Use the Country class from the arcgis.geoenrichment module. This provides acces to local variables for a specific country. Pass in the study area point and the enrich variables : PsychographicsShopping.MP28067A_B, transportation.X7027_I, entertainment.X9005_I, and lifemodegroupsNEW.TLIFENAME as arrays. To learn more about the data collections and variables, go to the data enrichment section of this guide.

    Use dark colors for code blocks
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    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",
        ],
    )
    
    
  2. To view the values returned from the enrichment, use the dataframe object returned from the enrich 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
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    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

  1. Use the map property from the GIS class to create a map. To set the initial extent of the map, pass in the string Nashville, TN This will set the map to an extent that is appropriate for the results. Set the basemap of the map to streets-navigation-vector.

    Use dark colors for code blocks
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    
    display_cols = [
        "aggregation_method",
        "population_to_polygon_size_rating",
        "apportionment_confidence",
        "tlifename",
        "x9005_i",
        "mp28067a_b",
        "x7027_i",
    ]
    enrich_results[display_cols]
    
    map_view = portal.map("Nashville, TN")
    map_view.basemap = "streets-navigation-vector"
    map_view
    
    
  2. Use the FeatureSet 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.

    Use dark colors for code blocks
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    map_view = portal.map("Nashville, TN")
    map_view.basemap = "streets-navigation-vector"
    map_view
    
    enrich_features = FeatureSet.from_dataframe(enrich_results)
    enrich_features
    
    popup_data = [
        f"Buys natural products: {enrich_features.features[0].attributes['mp28067a_b']}",
        f"Membership fees for Social Clubs: {enrich_features.features[0].attributes['x9005_i']}",
        f"Auto/Truck rental on trips: {enrich_features.features[0].attributes['x7027_i']}",
        f"Tapestry group name: {enrich_features.features[0].attributes['tlifename']}",
    ]
    popup_info = {
        "title": "<b>Data for a 1 mile search radius</b>",
        "content": "<br>".join(popup_data),
    }
    
    
    
  3. Use the clear_graphics() method to remove any previous results from the _Mapping. Then use the draw method and pass in the enriched geomety and popup info to add the results to the map. Set the extent of the map to the result geometry's extent.

    Use dark colors for code blocks
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    enrich_features = FeatureSet.from_dataframe(enrich_results)
    enrich_features
    
    popup_data = [
        f"Buys natural products: {enrich_features.features[0].attributes['mp28067a_b']}",
        f"Membership fees for Social Clubs: {enrich_features.features[0].attributes['x9005_i']}",
        f"Auto/Truck rental on trips: {enrich_features.features[0].attributes['x7027_i']}",
        f"Tapestry group name: {enrich_features.features[0].attributes['tlifename']}",
    ]
    popup_info = {
        "title": "<b>Data for a 1 mile search radius</b>",
        "content": "<br>".join(popup_data),
    }
    
    
    map_view.clear_graphics()
    
    map_view.draw(enrich_features.features[0].geometry, popup=popup_info)
    
    map_view.extent = Geometry(enrich_features.features[0].geometry).extent

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:

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.