ArcGIS API for Python Update Layers Skips Some Maps

222
0
07-31-2019 08:19 AM
JCGuarneri
Occasional Contributor

I'm having some trouble with updating layers using the ArcGIS API for Python. I had updated a map image layer with some new sublayers, and wanted to make sure the new layers were applied everywhere the original MIL is used. I wrote a notebook to find and remove the instances of this MIL and re-add to the map. This worked for most of the maps, but enough didn't that I ended up having to check them all manually. I'll step through my code below if anyone can tell me if I'm missing something.

First, I listed all the web maps owned by our admin user and created an Item object for the MIL:

maps = portal.content.search("type: Web Map NOT Application owner: GeoNexusAdmin@PENNICHUCK", max_items = 100)

newLayer = portal.content.search('id:b8c15eb2b33b4c0bbb7dfa0011cb28be')[0]‍‍‍‍‍‍

Next, I iterated through the maps and looked for the MIL in question. If it was found, I removed and replaced the layer:

for m in maps:
    map = arcgis.mapping.WebMap(m)
    #loop through layers in each map
    layers = map.layers
    for l in layers:
        #store layer index number
        layerNum = map.layers.index(l)
        #if a matching itemid is found, remove layer and add it back in
        if 'itemId' in layers[layerNum]:
            if layers[layerNum]['itemId'] =='b8c15eb2b33b4c0bbb7dfa0011cb28be':
                map.remove_layer(map.layers[layerNum])
                map.add_layer(newLayer)
                #print map title to confirm
                print(m.title)‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Almost every map that was printed in the output list updated the MIL succesfully, but about 3 did not. Is this a glitch in the system, or do I need to make some tweaks to my code? At the very least, it would be nice to be able to identify and list the maps that did not update successfully.

0 Kudos
0 Replies