Trying to add a layer (Portal item) to a webmap. The ESRI example for this I'm looking at is very simple:
streets_item = gis.content.search("LA Streets","Map Service")[0]
wm = WebMap() # create an empty web map with a default basemap
wm.add_layer(streets_item)
Okay, so I have a web map that's not empty that I created like this:
print(type(target_item))
from arcgis.mapping import WebMap
web_map_obj = WebMap(target_item)
print(type(web_map_obj))
#>>> <class 'arcgis.gis.Item'>
#>>> <class 'arcgis.mapping._types.WebMap'>
The print() statements are only there to assure myself that I'm actually looking at items on and not itemId's or who knows what. So I have a legit Python web map based on on a web map item in Portal. As a check, I do...
web_map_obj.layers
# prints out all the layers (map image layers) that nelong to the Portal web map
So now, I want to add another layer
newLayer = gis.content.search(query = "new layer"))[0]
# again just checking this is what I expect
print(type(newLayer))
web_map_obj.add_layer(newLayer)
Well, this bombs...
<class 'arcgis.gis.Item'>
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-191-62f982ec1c55> in <module>
13 # again just checking this is what I expect
14 print(type(newLayer))
---> 15 web_map_obj.add_layer(newLayer)
C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\arcgis\mapping\_types.py in add_layer(self, layer, options)
323
324 for lyr in layer.layers: # recurse - works for all.
--> 325 self.add_layer(lyr, options)
326 return True # end add_layer execution after iterating through each layer.
327 else:
C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\arcgis\mapping\_types.py in add_layer(self, layer, options)
510 fields_list = layer.properties.layerDefinition.fields
511
--> 512 for f in fields_list:
513 if isinstance(f, dict) or isinstance(f, PropertyMap):
514 field_dict = {'fieldName': f['name'],
TypeError: 'NoneType' object is not iterable
What's wrong with what I'm doing? I realize there is also arcgis.gis.
Layer. Do I first need to create a Layer from the item and then add that to the web map maybe?
So it looks like the following works:
newLayer_item = gis.content.search(query = "new layer"))[0]
###############################################
# the following line is new and made this work
newLayer = arcgis.gis.Layer(newLayer_item)
###############################################
web_map_obj.add_layer(newLayer)
# Now I can see the new layers with
web_map_obj.layers
Anyone know if the above referenced original ESRI example will ever work and if I was doing something wrong?
Where are you executing your code from, Python interactive windows in ArcGIS Pro, Python command prompt, Jupyter notebook?
Jupyter Notebooks.
Regarding newLayer_item, what exactly is it? I get the Esri examples to work in Jupyter, after a minor change to search outside my org, so I am wondering about your layer.
NewLayer is a Portal item that references a Map Service on our federated GIS Server. Where this all started was when I looked into copying a web map item from one Portal environment (Test) to another (Prod) and then trying to replace some map services .
Since the Esri examples work for me in Jupyter, and I can't test directly using your data, I don't have any other suggestions/ideas at this point. Maybe a Pro version thing. What version of Pro are you running?
This is Pro 2.5... I might try upgrading or at checking which version of arcgis modules my conda environment is using.
Turns out the syntax of the ESRI example works when adding an item (as layer) to web map using add_layer() works when it was created as empty new web map like this:
webmap = Webmap()
But not when the web map was created off another item and already has some layers...
webmap = Webmap(item)
This may be explained somewhere in ESRI documentation. Good to know!