Add a layer from an item

In this tutorial you will learn how to retrieve a layer stored in ArcGIS Online when you know its Item ID. You will get the layer item using its ID and display the layer on a map.

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

  1. Add the following code to model a GIS portal from the ArcGIS API for Python. arcgis.gis is the ArcGIS API for Python GIS model.

    Use dark colors for code blocks
                     
    Add line.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    from arcgis.gis import GIS
    
  2. Create a variable to hold the item ID of the public dataset. Any Item hosted on ArcGIS Online has a unique item ID attribute that can be referenced. The data item for this tutorial was prepared in advance, so we retrieved the item ID from the REST endpoint of the data (visible as the id parameter in the query component of the URL in the address bar).

    Use dark colors for code blocks
                     
    Add line.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    from arcgis.gis import GIS
    
    trailheads_id = "883cedb8c9fe4524b64d47666ed234a7"
    
    
  3. Create an anonymous connection to ArcGIS Online to retrieve this public data.

    Use dark colors for code blocks
                     
    Add line.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    from arcgis.gis import GIS
    
    trailheads_id = "883cedb8c9fe4524b64d47666ed234a7"
    
    gis = GIS()
    
    
  4. Access this data an an ArcGIS Python API item object.

    Use dark colors for code blocks
                     
    Add line.Add line.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    from arcgis.gis import GIS
    
    trailheads_id = "883cedb8c9fe4524b64d47666ed234a7"
    
    gis = GIS()
    
    trailheads_item = gis.content.get(trailheads_id)
    trailheads_item
    
    
  5. Create a map and add the new layer.

    Use dark colors for code blocks
                     
    Add line.Add line.Add line.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    from arcgis.gis import GIS
    
    trailheads_id = "883cedb8c9fe4524b64d47666ed234a7"
    
    gis = GIS()
    
    trailheads_item = gis.content.get(trailheads_id)
    trailheads_item
    
    m = gis.map()
    m.add_layer(trailheads_item)
    m
    
    
  6. Set the zoom level and center point of the map.

    Use dark colors for code blocks
                     
    Add line.Add line.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    from arcgis.gis import GIS
    
    trailheads_id = "883cedb8c9fe4524b64d47666ed234a7"
    
    gis = GIS()
    
    trailheads_item = gis.content.get(trailheads_id)
    trailheads_item
    
    m = gis.map()
    m.add_layer(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.

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