ArcGIS Developers
Dashboard

ArcGIS API for Python

Publishing web maps and web scenes

An ArcGIS web map is an interactive display of geographic information through a composition of web layers, basemap and much more. A web scene is analogous to a web map but in the 3D space. To get and overview, visit the product documentation for web maps and web scenes.

This sample demonstrates how to create and publish simple examples of web maps and scenes using the Python API. If you are interested in learning more about the specification to author and publish complex and more illustrative maps, refer to this documentation.

In [11]:
import os
import json
from IPython.display import display
import arcgis
from arcgis.gis import GIS
from arcgis.mapping import WebMap, WebScene

# connect to your GIS
# Create an anonymous connection to ArcGIS Online to search for contents
gis1 = GIS()
# Create a connection to your portal for publishing
gis2 = GIS("https://pythonapi.playground.esri.com/portal")

Publish a web map

The ArcGIS API for Python at version 1.3 extends the WebMap class with the capability to author new web maps and edit existing ones. You can perform basic operations such as adding, removing layers.

In [12]:
# Create an empty web map with a default basemap
wm = WebMap()
wm.definition
Out[12]:
{
  "operationalLayers": [],
  "baseMap": {
    "baseMapLayers": [
      {
        "id": "defaultBasemap",
        "layerType": "ArcGISTiledMapServiceLayer",
        "url": "https://services.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer",
        "visibility": true,
        "opacity": 1,
        "title": "World Topographic Map"
      }
    ],
    "title": "Topographic"
  },
  "spatialReference": {
    "wkid": 102100,
    "latestWkid": 3857
  },
  "version": "2.10",
  "authoringApp": "ArcGISPythonAPI",
  "authoringAppVersion": "1.8.4"
}

The above represents a template of a simple web map. This web map consists of a basemap web layer and an array of operational web layers. Notice the opertaional layer is empty without any web layer urls. We will search for a public web layer titled 2020 USA Median Age published by esri account and apply that as an operational layer for this web map.

In [13]:
search_result = gis1.content.search("title:2020 USA Median AND owner:esri", 
                                   item_type = "Map Service", outside_org = True)
In [14]:
display(search_result)
[<Item title:"2020 USA Median Household Income" type:Map Image Layer owner:esri>,
 <Item title:"2020 USA Median Age" type:Map Image Layer owner:esri>,
 <Item title:"2020 USA Tapestry Segmentation" type:Map Image Layer owner:esri>,
 <Item title:"2020 USA Average Household Size" type:Map Image Layer owner:esri>,
 <Item title:"2020 USA Median Disposable Income" type:Map Image Layer owner:esri>,
 <Item title:"2020 USA Average Household Income" type:Map Image Layer owner:esri>,
 <Item title:"2020 USA Median Home Value" type:Map Image Layer owner:esri>]
In [5]:
median_age_item = search_result[1] # pick the second search result
display(median_age_item)
2020 USA Median Age
This layer shows the median age of the U.S. population in 2020 in a multiscale map by country, state, county, ZIP Code, tract, and block group. ArcGIS Online subscription required.Map Image Layer by esri
Last Modified: June 29, 2020
0 comments, 66,314 views

Add an operational layer to the web map

To add 'USA median age' web layer as an operational layer, you can call the add_layer() method and pass the MapImageLayer object.

In [6]:
#query the layers in the item
median_age_item.layers
Out[6]:
[<MapFeatureLayer url:"https://demographics5.arcgis.com/arcgis/rest/services/USA_Demographics_and_Boundaries_2020/MapServer/0">,
 <MapFeatureLayer url:"https://demographics5.arcgis.com/arcgis/rest/services/USA_Demographics_and_Boundaries_2020/MapServer/1">,
 <MapFeatureLayer url:"https://demographics5.arcgis.com/arcgis/rest/services/USA_Demographics_and_Boundaries_2020/MapServer/2">,
 <MapFeatureLayer url:"https://demographics5.arcgis.com/arcgis/rest/services/USA_Demographics_and_Boundaries_2020/MapServer/3">,
 <MapFeatureLayer url:"https://demographics5.arcgis.com/arcgis/rest/services/USA_Demographics_and_Boundaries_2020/MapServer/4">,
 <MapFeatureLayer url:"https://demographics5.arcgis.com/arcgis/rest/services/USA_Demographics_and_Boundaries_2020/MapServer/5">,
 <MapFeatureLayer url:"https://demographics5.arcgis.com/arcgis/rest/services/USA_Demographics_and_Boundaries_2020/MapServer/6">,
 <MapFeatureLayer url:"https://demographics5.arcgis.com/arcgis/rest/services/USA_Demographics_and_Boundaries_2020/MapServer/7">,
 <MapFeatureLayer url:"https://demographics5.arcgis.com/arcgis/rest/services/USA_Demographics_and_Boundaries_2020/MapServer/8">,
 <MapFeatureLayer url:"https://demographics5.arcgis.com/arcgis/rest/services/USA_Demographics_and_Boundaries_2020/MapServer/9">,
 <MapFeatureLayer url:"https://demographics5.arcgis.com/arcgis/rest/services/USA_Demographics_and_Boundaries_2020/MapServer/10">,
 <MapFeatureLayer url:"https://demographics5.arcgis.com/arcgis/rest/services/USA_Demographics_and_Boundaries_2020/MapServer/11">,
 <MapFeatureLayer url:"https://demographics5.arcgis.com/arcgis/rest/services/USA_Demographics_and_Boundaries_2020/MapServer/12">,
 <MapFeatureLayer url:"https://demographics5.arcgis.com/arcgis/rest/services/USA_Demographics_and_Boundaries_2020/MapServer/13">,
 <MapFeatureLayer url:"https://demographics5.arcgis.com/arcgis/rest/services/USA_Demographics_and_Boundaries_2020/MapServer/14">,
 <MapFeatureLayer url:"https://demographics5.arcgis.com/arcgis/rest/services/USA_Demographics_and_Boundaries_2020/MapServer/15">,
 <MapFeatureLayer url:"https://demographics5.arcgis.com/arcgis/rest/services/USA_Demographics_and_Boundaries_2020/MapServer/16">,
 <MapFeatureLayer url:"https://demographics5.arcgis.com/arcgis/rest/services/USA_Demographics_and_Boundaries_2020/MapServer/17">,
 <MapFeatureLayer url:"https://demographics5.arcgis.com/arcgis/rest/services/USA_Demographics_and_Boundaries_2020/MapServer/18">,
 <MapFeatureLayer url:"https://demographics5.arcgis.com/arcgis/rest/services/USA_Demographics_and_Boundaries_2020/MapServer/19">,
 <MapFeatureLayer url:"https://demographics5.arcgis.com/arcgis/rest/services/USA_Demographics_and_Boundaries_2020/MapServer/20">,
 <MapFeatureLayer url:"https://demographics5.arcgis.com/arcgis/rest/services/USA_Demographics_and_Boundaries_2020/MapServer/21">,
 <MapFeatureLayer url:"https://demographics5.arcgis.com/arcgis/rest/services/USA_Demographics_and_Boundaries_2020/MapServer/22">,
 <MapFeatureLayer url:"https://demographics5.arcgis.com/arcgis/rest/services/USA_Demographics_and_Boundaries_2020/MapServer/23">,
 <MapFeatureLayer url:"https://demographics5.arcgis.com/arcgis/rest/services/USA_Demographics_and_Boundaries_2020/MapServer/24">,
 <MapFeatureLayer url:"https://demographics5.arcgis.com/arcgis/rest/services/USA_Demographics_and_Boundaries_2020/MapServer/25">,
 <MapFeatureLayer url:"https://demographics5.arcgis.com/arcgis/rest/services/USA_Demographics_and_Boundaries_2020/MapServer/26">,
 <MapFeatureLayer url:"https://demographics5.arcgis.com/arcgis/rest/services/USA_Demographics_and_Boundaries_2020/MapServer/27">,
 <MapFeatureLayer url:"https://demographics5.arcgis.com/arcgis/rest/services/USA_Demographics_and_Boundaries_2020/MapServer/28">,
 <MapFeatureLayer url:"https://demographics5.arcgis.com/arcgis/rest/services/USA_Demographics_and_Boundaries_2020/MapServer/29">,
 <MapFeatureLayer url:"https://demographics5.arcgis.com/arcgis/rest/services/USA_Demographics_and_Boundaries_2020/MapServer/30">,
 <MapFeatureLayer url:"https://demographics5.arcgis.com/arcgis/rest/services/USA_Demographics_and_Boundaries_2020/MapServer/31">,
 <MapFeatureLayer url:"https://demographics5.arcgis.com/arcgis/rest/services/USA_Demographics_and_Boundaries_2020/MapServer/32">,
 <MapFeatureLayer url:"https://demographics5.arcgis.com/arcgis/rest/services/USA_Demographics_and_Boundaries_2020/MapServer/33">,
 <MapFeatureLayer url:"https://demographics5.arcgis.com/arcgis/rest/services/USA_Demographics_and_Boundaries_2020/MapServer/34">,
 <MapFeatureLayer url:"https://demographics5.arcgis.com/arcgis/rest/services/USA_Demographics_and_Boundaries_2020/MapServer/35">,
 <MapFeatureLayer url:"https://demographics5.arcgis.com/arcgis/rest/services/USA_Demographics_and_Boundaries_2020/MapServer/36">,
 <MapFeatureLayer url:"https://demographics5.arcgis.com/arcgis/rest/services/USA_Demographics_and_Boundaries_2020/MapServer/37">,
 <MapFeatureLayer url:"https://demographics5.arcgis.com/arcgis/rest/services/USA_Demographics_and_Boundaries_2020/MapServer/38">,
 <MapFeatureLayer url:"https://demographics5.arcgis.com/arcgis/rest/services/USA_Demographics_and_Boundaries_2020/MapServer/39">,
 <MapFeatureLayer url:"https://demographics5.arcgis.com/arcgis/rest/services/USA_Demographics_and_Boundaries_2020/MapServer/40">,
 <MapFeatureLayer url:"https://demographics5.arcgis.com/arcgis/rest/services/USA_Demographics_and_Boundaries_2020/MapServer/41">,
 <MapFeatureLayer url:"https://demographics5.arcgis.com/arcgis/rest/services/USA_Demographics_and_Boundaries_2020/MapServer/42">,
 <MapFeatureLayer url:"https://demographics5.arcgis.com/arcgis/rest/services/USA_Demographics_and_Boundaries_2020/MapServer/43">,
 <MapFeatureLayer url:"https://demographics5.arcgis.com/arcgis/rest/services/USA_Demographics_and_Boundaries_2020/MapServer/44">,
 <MapFeatureLayer url:"https://demographics5.arcgis.com/arcgis/rest/services/USA_Demographics_and_Boundaries_2020/MapServer/45">,
 <MapFeatureLayer url:"https://demographics5.arcgis.com/arcgis/rest/services/USA_Demographics_and_Boundaries_2020/MapServer/46">,
 <MapFeatureLayer url:"https://demographics5.arcgis.com/arcgis/rest/services/USA_Demographics_and_Boundaries_2020/MapServer/47">,
 <MapFeatureLayer url:"https://demographics5.arcgis.com/arcgis/rest/services/USA_Demographics_and_Boundaries_2020/MapServer/48">,
 <MapFeatureLayer url:"https://demographics5.arcgis.com/arcgis/rest/services/USA_Demographics_and_Boundaries_2020/MapServer/49">]
In [15]:
for lyr in median_age_item.layers:
    wm.add_layer(lyr)
In [16]:
# query the layers added to the web map
for lyr in wm.layers:
    print(lyr.title)
State Boundaries

Publish the web map as an item to the portal

Now that the web map content is ready, we will use the save() method to save the WebMap object as a web map item in the GIS. As parameters to the save() method, you need to specify some essential properties for the new web map item.

In [18]:
web_map_properties = {'title':'USA median age map',
                     'snippet':'This map service shows the median age of people' +\
                     'in the United States as of 2020 census.',
                     'tags':'ArcGIS Python API'}

# Call the save() with web map item's properties.
web_map_item = wm.save(item_properties=web_map_properties)
web_map_item
Out[18]:
USA median age map
This map service shows the median age of peoplein the United States as of 2020 census.Web Map by arcgis_python
Last Modified: June 22, 2021
0 comments, 0 views

Display the web map

We have successfully published a web map with consisting of a basemap and desired web layer as the operational layer. We will use arcgis.viz module to read the published map as a WebMap object and interact with it.

In [10]:
web_map_obj = arcgis.mapping.WebMap(web_map_item)

# display the web map in an interactive widget
web_map_obj