Tutorial: Add a layer from a portal item

Learn how to use an to access and display a in a .

You can host a variety of geographic data and other resources using . These items can also define how the data is presented. A or , for example, not only defines the layers for a map or scene, but also how layers are symbolized, the minimum and/or maximum scales at which they display, and several other properties. Likewise, a hosted feature layer contains the data for the layer and also defines the symbols and other display properties for how it is presented. When you add a map, scene, or layer from a portal item to your app, everything that has been saved with the item is applied in your application. Adding items to your application rather than creating them programmatically saves you from writing a lot of code, and can provide consistency across applications that use the same data.

In this tutorial, you will add a hosted feature layer to display trailheads in the Santa Monica mountains region of southern California. The hosted layer defines the trailhead locations (points) as well as the symbols used to display them.

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

  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.

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

Access the item by ID

  1. Create a variable to hold the of the public dataset. An item hosted in ArcGIS has a unique item ID attribute that can be referenced. For example, the item ID for this feature layer is:

    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
    portal = GIS()
    
    trailheads_id = "2e4b3df6ba4b44969a3bc9827de746b3"
    
    
  2. Use the content property on the GIS class, passing in the trailheads_id variable to return an item object.

    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
    trailheads_id = "2e4b3df6ba4b44969a3bc9827de746b3"
    
    trailheads_item = portal.content.get(trailheads_id)
    trailheads_item
    
    

Display the item on a map

  1. Create a map widget instance by calling the map method of the GIS. Pass the item value to the add method to display the layer.

    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
    trailheads_item = portal.content.get(trailheads_id)
    trailheads_item
    
    m = portal.map()
    m.content.add(trailheads_item)
    m
    
    
  2. Use the center and zoom methods to set the display extent of 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
    m = portal.map()
    m.content.add(trailheads_item)
    m
    
    m.center = [34.09042, -118.71511]  # [latitude, longitude]
    m.zoom = 11
    
    

Your map should display the locations of trailheads 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
    import os
    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-layer-from-an-item.html")
    
    m.export_to_html(export_path, title="Add a layer from an item")

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