I'm trying to use the arcgis api for Python to update the filter on a layer in a WebMap. The api reference says there should be a function called "update_layer", but I don't see it on the WebMap object or the layer. Is it out of date or am I missing something?
Either way, I have the layer properties saved in a dictionary, I just need to know how to update the layer now. I know how I would update the Feature Layer itself. But I need to update the layer in the map only.
Also the example given in the reference for how to use this nonexistent function doesn't make sense to me. Seems like there are some typos in this section maybe.
Solved! Go to Solution.
Nevermind, I realized I need to use get_data() on the map Item object, not the WebMap object. Then adjust the JSON for the layer within the whole map definition. Then I can update the map. Still confused about the "update_layer" function, but whatever.
Hi @Scoth
You can update the filter in a layer directly in a WebMap with the following...
from arcgis.gis import GIS
from arcgis.mapping import WebMap
## access AGOL
agol = GIS("home")
## access the WebMap Item
wm_item = agol.content.get("WebMap_Item_ID")
## create a WebMap object
webmap = WebMap(wm_item)
## get the layer of interest
lyr = webmap.get_layer(title="Name of layer as it appears in the WebMap")
## set the filter
lyr.layerDefinition.definitionExpression = "FIELDNAME = 'attribute'" # for text field
## update the webmap
webmap.update()
Nevermind, I realized I need to use get_data() on the map Item object, not the WebMap object. Then adjust the JSON for the layer within the whole map definition. Then I can update the map. Still confused about the "update_layer" function, but whatever.
It is more than likely that your version of the arcgis python module is an older version that does not have the update_layer function. When I check for it in version 2.0 it is not there, but it is available in version 2.1.
Hi @Scoth
You can update the filter in a layer directly in a WebMap with the following...
from arcgis.gis import GIS
from arcgis.mapping import WebMap
## access AGOL
agol = GIS("home")
## access the WebMap Item
wm_item = agol.content.get("WebMap_Item_ID")
## create a WebMap object
webmap = WebMap(wm_item)
## get the layer of interest
lyr = webmap.get_layer(title="Name of layer as it appears in the WebMap")
## set the filter
lyr.layerDefinition.definitionExpression = "FIELDNAME = 'attribute'" # for text field
## update the webmap
webmap.update()
Ahh cool. I guess I was kinda looking for a way to update all of the layer properties to match the layer in another map. But your answer works best for my specific question.