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


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

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/

  • I don't think that this is what the WebMap constructor does. Calling the WebMap constructor and passing in an Item of type "Web Map" will create a WebMap object corresponding to the Item. The set of layers (basemap and operational) will be the same as in the Item. – Jonathan Bailey Nov 5 '18 at 12:49
  • Hmm, yeah that looks to be the case. I just tried different layers and they put them as operational. The options look pretty limited at this point. You could go in and hack the JSON that makes up the WebMap (but I don't see any functions in the Python API to do that, you'd need to roll your own code) – KHibma Nov 5 '18 at 13:19

While the WebMap.basemap property may be read-only WebMap.basemap.baseMapLayers ( a list of dicts ) is writeable. The way I've been doing it is have a template (you can make yourself by using the existing ones as a guide, or just have an actual webmap published to use as a template with the basemap layers you want).

Replacing the basemap in a WebMap object is a s simple as

webmap = WebMap()
template_webmap = WebMap(gis.contet.get(template_map_id)
webmap.basemap.baseMapLayers = template_webmap.basemap.BaseMapLayers

You can also just replace the url entry in a member dict of WebMap.basemap.BaseMapLayers, this will change that basemap component to use that URL, but it does not automatically update the rest of the dict, so the title, etc. will remain the same unless you change them manually.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.