Tutorial: Add a feature layer

Learn how to display features in a .

Python map widget displaying trailheads, trails, and Parks in Malibu, CA

A contains of geographic data. A map contains a and, optionally, one or more . This tutorial shows you how to access and display feature layers in a map. You access feature layers with an ID or URL.

A is a spatially-enabled table in a that contains . Each feature layer contains features with a single geometry type (point, polyline, or polygon), and a set of attributes. You can access and display features by making query requests to the feature service and displaying them in a map.

In this tutorial, you access and display three different feature layers hosted in :

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

  1. Import the arcgis.gis module. This module is the entry point into the GIS and provides the functionality to manage GIS , users, and groups. Additionally, import the Service class from the arcgis.layers module, which has functions for accessing, managing, and displaying from .

    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
    
    from arcgis.gis import GIS
    from arcgis.layers import Service
    
    
    
  2. Log in anonymously to access publicly shared content. The data used in this tutorial is public so you do not need credentials to access it. If it were private data, you would be required to provide authentication.

    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
    
    from arcgis.gis import GIS
    from arcgis.layers import Service
    
    
    portal = GIS()
    
    

Create feature layers using URLs

  1. Use the Service class to create a featurelayer instance for each layer URL.

    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
    
    portal = GIS()
    
    points_layer = Service(
        "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Trailheads/FeatureServer/0"
    )
    points_layer
    
    polylines_layer = Service(
        "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Trails/FeatureServer/0"
    )
    polylines_layer
    
    polygons_layer = Service(
        "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Parks_and_Open_Space/FeatureServer/0"
    )
    polygons_layer
    
    

Display the feature layers

  1. Create a map widget instance by calling the map method and pass in the location of Malibu, CA to set the maps initial extent. Use the add method of the arcgis.layer.ContentManager class to add each feature layer to the map.

    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
    
    points_layer = Service(
        "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Trailheads/FeatureServer/0"
    )
    points_layer
    
    polylines_layer = Service(
        "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Trails/FeatureServer/0"
    )
    polylines_layer
    
    polygons_layer = Service(
        "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Parks_and_Open_Space/FeatureServer/0"
    )
    polygons_layer
    
    map = portal.map("Malibu, CA")
    map
    
    
    for layer in [points_layer, polylines_layer, polygons_layer]:
        map.content.add(layer)
    
    map.center = [34.0761,-118.7055]
    map.zoom = 12
    
    
    

You should see point, line, and polygon features (representing trailheads, trails, and parks) drawn on the map for an area in the Santa Monica Mountains.

Optional: Export the map widget

  1. Create a standalone HTML page that can be shared and rendered in a web browser.
    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
    
    from os import path, getcwd
    
    export_dir = path.join(getcwd(), "home")
    if not path.isdir(export_dir):
        os.mkdir(export_dir)
    
    export_path = path.join(export_dir, "add-a-feature-layer.html")
    
    map.export_to_html(export_path, title="Add a feature layer")

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.

You can no longer sign into this site. Go to your ArcGIS portal or the ArcGIS Location Platform dashboard to perform management tasks.

Your ArcGIS portal

Create, manage, and access API keys and OAuth 2.0 developer credentials, hosted layers, and data services.

Your ArcGIS Location Platform dashboard

Manage billing, monitor service usage, and access additional resources.

Learn more about these changes in the What's new in Esri Developers June 2024 blog post.

Close