Select to view content in your preferred language

Python update filter on layer in map

804
4
Jump to solution
06-14-2023 03:50 PM
Scoth
by
New Contributor III

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.

Scoth_0-1686783042420.png

 

2 Solutions

Accepted Solutions
Scoth
by
New Contributor III

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.

View solution in original post

0 Kudos
Clubdebambos
Occasional Contributor III

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()
~ learn.finaldraftmapping.com

View solution in original post

4 Replies
Scoth
by
New Contributor III

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.

0 Kudos
Clubdebambos
Occasional Contributor III

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.

~ learn.finaldraftmapping.com
Clubdebambos
Occasional Contributor III

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()
~ learn.finaldraftmapping.com
Scoth
by
New Contributor III

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.

0 Kudos