Select to view content in your preferred language

Updating Web Map Layer Definition Expression Filters via Python 2.4

319
2
Jump to solution
02-12-2025 08:01 AM
Labels (2)
BBarbs
by
Occasional Contributor

Hello Community,

I recently upgraded ArcGIS Pro to 3.4.2, which included Python 2.4.0 and the new Map() module. I am working on updating one of my scripts and have run into a road block I was hoping someone could help me with.

My map has several grouped layers whose definition expressions are updated by python script. My usual method of defining each layer by title doesn't work anymore with the new mapping module. Does anyone know how I can point to my layers in Python 2.4's new module?

Here is a sample of my previous method:

import arcgis
from arcgis.map import Map
from arcgis.gis import GIS
gis = GIS("home")

# New method to grab web map in Python 2.4
wm_item = gis.content.get("<item id>")
webmap = Map(item=wm_item)

# report update date 
reportDTSQL = '02012025'

# (Old, not-working method of updating layer expression-- NEEDS UPDATING)
# get layers of interest
enrolledLyr = webmap.get_layer(title="Locations Enrolled Only")
 
# apply new filter expression to each layer's json
enrolledLyr["layerDefinition"]["definitionExpression"] = f"(category = 'Active') AND ({reportDTSQL})"
 
# Update web map object with new json
webmap.update_layer(dict(EnrolledLyr))

 

 

Thank you for any help!

1 Solution

Accepted Solutions
Clubdebambos
MVP Regular Contributor

Hi @BBarbs,

I've abandoned using some of the new classes in version 2.4.0 and going direct to the WebMap definition as shown below, code commented for workflow.

 

from arcgis.gis import GIS

## access ArcGIS Online
agol = GIS("home")

## get teh WebMap as an Item object
wm_item = agol.content.get("WM_ITEM_ID")

## get the WebMap JSON definition
wm_item_data = wm_item.get_data()

## What layer do you want to update?
lyr_name = "LAYER_NAME"

## What filter do you want to apply?
filter_exp = "FIELD = 'VALUE'"

## for each layer in the WebMap
for lyr in wm_item_data["operationalLayers"]:
    ## if it is a Group Layer
    if lyr["layerType"] == "GroupLayer":
        ## for each layer in teh group layer
        for grp_lyr in lyr["layers"]:
            ## if the layer title is the one we want
            if grp_lyr["title"] == lyr_name:
                ## apply the filter
                grp_lyr["layerDefinition"]["definitionExpression"] = filter_exp

    ## if the layer of interest is not a group layer and matched the layer name
    elif lyr["title"] == lyr_name:
        ## apply the fiter
        lyr["layerDefinition"]["definitionExpression"] = filter_exp

## create the update dictionary
update_properties = {"text" : wm_item_data}

## apply the update to the WebMap object (updating JSON definition)
wm_item.update(
    item_properties = update_properties
)

 

I have asked for the get_layer() to be reinstated, you can vote for it here.

All the best,

Glen

~ learn.finaldraftmapping.com

View solution in original post

2 Replies
Clubdebambos
MVP Regular Contributor

Hi @BBarbs,

I've abandoned using some of the new classes in version 2.4.0 and going direct to the WebMap definition as shown below, code commented for workflow.

 

from arcgis.gis import GIS

## access ArcGIS Online
agol = GIS("home")

## get teh WebMap as an Item object
wm_item = agol.content.get("WM_ITEM_ID")

## get the WebMap JSON definition
wm_item_data = wm_item.get_data()

## What layer do you want to update?
lyr_name = "LAYER_NAME"

## What filter do you want to apply?
filter_exp = "FIELD = 'VALUE'"

## for each layer in the WebMap
for lyr in wm_item_data["operationalLayers"]:
    ## if it is a Group Layer
    if lyr["layerType"] == "GroupLayer":
        ## for each layer in teh group layer
        for grp_lyr in lyr["layers"]:
            ## if the layer title is the one we want
            if grp_lyr["title"] == lyr_name:
                ## apply the filter
                grp_lyr["layerDefinition"]["definitionExpression"] = filter_exp

    ## if the layer of interest is not a group layer and matched the layer name
    elif lyr["title"] == lyr_name:
        ## apply the fiter
        lyr["layerDefinition"]["definitionExpression"] = filter_exp

## create the update dictionary
update_properties = {"text" : wm_item_data}

## apply the update to the WebMap object (updating JSON definition)
wm_item.update(
    item_properties = update_properties
)

 

I have asked for the get_layer() to be reinstated, you can vote for it here.

All the best,

Glen

~ learn.finaldraftmapping.com
BBarbs
by
Occasional Contributor

Thank you so much, this works perfect! I voted for your idea, hopefully it gets brought back!