AnsweredAssumed Answered

'NoneType" Error when adding layer to WebMap using Python API

Question asked by agelfert on Jul 16, 2020
Latest reply on Jul 23, 2020 by agelfert

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?

Outcomes