[GIS] Setting basemap layers of WebMap using ArcGIS Python API

arcgis-online-webmaparcgis-python-apibasemap

Using the ArcGIS Python API, I've create a WebMap and two Layers (one containing features, the other containing labels):

cbmt = WebMap()
cbmt_geometry_layer = Layer(url = <url of geometry layer>)
cbmt_text_layer = Layer(url = <url of text layer>)

I'd like to add both of these layers to the map as basemap layers, like you can in ArcGIS Online or Portal when adding layers to a web map. The WebMap.basemap property is read-only. Is there a property on the WebMap.add_layer() method that specifies to add layers as basemap layers?

xref: https://community.esri.com/thread/223790-how-to-set-the-basemap-layers-of-a-webmap-using-the-arcgis-python-api

Best Answer

I don't see any methods in the API that allow you to add / modify the basemap of a webmap.

You might be able to take the long-way-approach to solving this. You'll need have a webmap with your layers ready to be used (or create it using Python API). Then you could create a new webmap and pass in the previous webmap. It'll be added to the basemap of this new webmap. Once you have this created, you can call add (more operational layers) / save to finish the webmap. Yes, unfortunately in the end you're making new items instead of updating existing ones.

from arcgis.mapping import WebMap
from arcgis.gis import GIS

gis = GIS("https://www.arcgis.com", "user", "password")

#grab an existing webmap which will be used as a basemap (below finds dark gray)
darkGray = gis.content.search('title:dark', 
                                    outside_org=True, item_type='web map')[0]
#Create a new webmap, passing in your existing webmap. This will set it as the basemap.
newWM = WebMap(darkGray)
props = {'title':'My New Map',
             'snippet':'Map created using Python API ',
             'tags':['automation', 'map', 'python']}

#Save the webmap, setting the basic properties
newWM.save(props)

reference: https://esri.github.io/arcgis-python-api/apidoc/html/arcgis.mapping.html#webmap and https://developers.arcgis.com/python/sample-notebooks/using-and-updating-gis-content/

Related Question