Overview

You will learn: how to use the ArcGIS API for Python to load a web map for display.

A web map is a JSON structure that contains the settings required to display a 2D map. ArcGIS and custom applications can load web maps and automatically configure the map extent, basemap, layers and styles, pop-ups, labels and more. Web maps can be created interactively with the Map Viewer and ArcGIS Pro. Web maps are stored in ArcGIS Online or ArcGIS Enterprise as an item with a unique ID.

In this tutorial you will search ArcGIS Online for an existing web map and display it.

Before you begin

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

Create a new notebook

  1. Go to Esri Juptyter Notebooks and click New > Python 3 to create a new notebook.

    In each step below, type (or copy and paste) the commands into a new notebook cell and run the code by clicking run cell or pressing shift + Enter.

  1. Import the GIS module.

    from arcgis.gis import GIS
    
  2. Make an anonymous connection to ArcGIS Online.

    gis = GIS()
    

Display a web map with the ArcGIS API for Python

  1. Search for a publicly available web map titled LA Parks and Trails Map (styled with popups) owned by esri_devlabs. It contains datasets about Los Angeles, CA parks and trails.

    webmap_search = gis.content.search(query = "LA Parks and Trails *", item_type = "Web Map")
    webmap_search
    
  2. Retrieve the web map item from the resulting list.

    webmap = webmap_search[2]
    webmap
    
  3. Import the WebMap class from the arcgis.mapping module and visualize the web map.

    from arcgis.mapping import WebMap
    la_parks_trails = WebMap(webmap)
    la_parks_trails
    

Congratulations, you're done!

Your map and layers (feature services) should look something like this.

Challenge

Explore the layers in the Web Map with the REST API.

All hosted feature layers have a REST endpoint that you can use to access the data directly. This can be accomplished with a URL request from a web browser, a custom application, or the ArcGIS APIs.

Let's use the web map to retrieve the layers. You can query the web map as a dictionary.

  1. operational_layers = la_parks_trails.layers
    n = len(operational_layers)
    print("The webmap has {} layers.".format(n))
    
  2. Query the list for the id and url values of each layer.

    for layer in operational_layers:
        print("{}\n\t{}".format(layer['id'], layer['url']))
    
  3. In your browser, add the URL for the Trails_7558 layer to the browser (or just copy/paste from below) to explore the REST endpoint:

    https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Trails/FeatureServer/0

  4. Explore the other layers through the same steps.

To learn more about querying data with the REST API, see the Query a feature layer tutorial.