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.
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.
# Create an empty web map with a default basemap
wm = WebMap()
wm.definition
{ "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.
search_result = gis1.content.search("title:2020 USA Median AND owner:esri",
item_type = "Map Service", outside_org = True)
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>]
median_age_item = search_result[1] # pick the second search result
display(median_age_item)
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.
#query the layers in the item
median_age_item.layers
[<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">]
for lyr in median_age_item.layers:
wm.add_layer(lyr)
# 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.
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
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.
web_map_obj = arcgis.mapping.WebMap(web_map_item)
# display the web map in an interactive widget
web_map_obj

The map by zooms to the extent of the content by default. You can interact with the widget and explore the spatial distribution of people's age.
Publish a web scene
So far, we have seen how to publish a web map. In this section, we will observe how to publish a web scene. We will read a template web scene json from a text file, add an interesting 3D Scene Service as an operational layer. Then using the add()
method, we will publish this as a web scene item.
# Read sample web scene json from text file. We use built-in json module to parse it into a Python dictionary
web_scene_dict = dict()
with open("data/web_scene_simple.json","r") as file_handle:
web_scene_dict = json.load(file_handle)
display(web_scene_dict)
{'operationalLayers': [{'itemId': '', 'title': '', 'visibility': True, 'opacity': 1, 'url': '', 'layerType': ''}], 'baseMap': {'baseMapLayers': [{'id': '933075fa973f49948a88b84dae743704', 'visibility': True, 'opacity': 1, 'layerDefinition': {}, 'url': 'http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer', 'layerType': 'ArcGISTiledMapServiceLayer'}], 'title': 'World Street Map', 'elevationLayers': [{'url': 'https://elevation3d.arcgis.com/arcgis/rest/services/WorldElevation3D/Terrain3D/ImageServer', 'id': 'globalElevation_0', 'layerType': 'ArcGISTiledElevationServiceLayer'}]}, 'spatialReference': {'wkid': 102100, 'latestWkid': 3857}, 'version': '1.4', 'viewingMode': 'global', 'tables': []}
The web scene above is pretty simple. Notice the operationalLayers
array does not contain url
to the service. Now we will search for a scene service titled Montreal, Canada Buildings published by esri_3d and apply that to the web scene.
search_result = gis1.content.search("title:Montreal, Canada Buildings AND owner:esri_3d",
item_type="scene service", outside_org = True)
display(search_result)
[<Item title:"Buildings_Montreal" type:Scene Layer owner:esri_3d>, <Item title:"Montreal, Canada Buildings" type:Scene Layer owner:esri_3d>]
buildings_layer = search_result[0]
display(buildings_layer)
# Update web scene's opertaional layer with properties of buildings_layer
web_scene_dict['operationalLayers'][0]['itemId'] = buildings_layer.itemid
web_scene_dict['operationalLayers'][0]['layerType'] = "ArcGISSceneServiceLayer"
web_scene_dict['operationalLayers'][0]['title'] = buildings_layer.title
web_scene_dict['operationalLayers'][0]['url'] = buildings_layer.url
Publish the web scene as an item to the portal
We will use the add()
from ContentManager
class to create a web scene item. As parameters we will send a dictionary containing properties of the web scene item. The text
property will contain the web scene dictionary updated in the previous step as a string.
web_scene_item_properties = {'title':'Web scene with photo realistic buildings',
'type':'Web Scene',
'snippet':'This scene highlights buildings of Montreal, Canada',
'tags':'ArcGIS Python API',
'text': json.dumps(web_scene_dict)}
# Use the add() method to publish a new web scenej
web_scene_item = gis2.content.add(web_scene_item_properties)
web_scene_item.share(True)
display(web_scene_item)
Display the web scene
We have successfully published a web scene with consisting of a basemap, elevation layer and desired web layer as the operational layer. We will use arcgis.mapping
module to read the published scene as a WebScene
object and interact with it.
web_scene_obj = arcgis.mapping.WebScene(web_scene_item)
# display the interactive web scene in the notebook
web_scene_obj
