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")
Thank you for any help!
Solved! Go to Solution.
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
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