Select to view content in your preferred language

Add layer to cloned Web Map api for python in Jupyter notebook

1762
3
Jump to solution
05-20-2021 02:01 PM
GIS_utahDEM
Frequent Contributor

Hello. I am attempting to clone a Web Map in AGOL for each jurisdiction in the state, and then add a layer to that map based on the jurisdiction. I am successful in the first portion of this, but despite not having any error codes, the web maps produced do not contain the layer I am trying to add. I am guessing that there is something funky with the format of my Web Map, but since I'm not getting an error to go off of, I'm not sure where I'm going wrong. 

webmapJSON = map_item.get_data()
for layer in webmapJSON['operationalLayers']:
    display(layer)

web_map_dict = map_item.get_data(try_json=True)

for index, county in enumerate(counties):
    county_name = counties.features[index].attributes['NAME']
    print(county_name) 
    title = "UCIP_Test_" + county_name + "_AddLayers4"

    web_map_properties = {'title': title,
                         'type':'Web Map',
                         'snippet':'This web map copied from template',
                         'tags':'ArcGIS Python API PC Test',
                         'text':json.dumps(web_map_dict)}

    web_map_item = gis.content.add(web_map_properties)
    
    ucipMap = WebMap(web_map_item)
    
    layer_search = gis.content.search(query = "UCIP_" + county_name + "_County_View")
    
    layer = layer_search[0]
    
    print(layer)
    
    ucipMap = ucipMap.add_layer(layer, options={'title': county_name + "View"})

The print functions return the correct county/layer pairs, so the problem is not in finding the layer, and the WebMaps show up in AGOL with appropriate names, but they don't have the "layer" that I'm trying to add. 

0 Kudos
1 Solution

Accepted Solutions
DavidPike
MVP Frequent Contributor

I think the issue is that you used the .addLayer() method on the ucipMap WebMap but then recast ucipMap as the boolean return from the method. i.e. the ucipMap no longer refers to the WebMap.

perhaps:

ucipMap.add_layer(layer, options={'title': county_name + "View"})
ucipMap.update()

  

View solution in original post

3 Replies
DavidPike
MVP Frequent Contributor

are you maybe just missing the .update() method on the WebMap?

0 Kudos
GIS_utahDEM
Frequent Contributor

I think the layers that show up on my AGOL are created from the

web_map_item = gis.content.add(web_map_properties) line. 

And I think the WebMap() method just returns a bool -- so maybe I'm not understanding WebMap(). When I try to update or save ucipMap, it returns an error that the method can't be applied to a bool. However, it appears I can only add_layer to the WebMap() and not the item itself -- so I guess I'm just not sure how to recombine the ucipMap that has the new layer back with the original item...? Maybe? Haha sorry if that doesn't make sense, I'm still pretty new to python and api for python.

0 Kudos
DavidPike
MVP Frequent Contributor

I think the issue is that you used the .addLayer() method on the ucipMap WebMap but then recast ucipMap as the boolean return from the method. i.e. the ucipMap no longer refers to the WebMap.

perhaps:

ucipMap.add_layer(layer, options={'title': county_name + "View"})
ucipMap.update()