Update extent of layers in feature layer with python

295
2
03-16-2024 04:34 AM
Labels (3)
IB3
by
Occasional Contributor

Hi, 

I am trying to update the extent of each layers in a feature layer after overwritting the data with new features (located elsewhere), but my script doesn't seem to have any effect. As anyone managed to update the extent of all layers using python?

from arcgis.gis import GIS
from arcgis.features import FeatureLayerCollection

# Connect to ArcGIS Online (replace 'username' and 'password' with your credentials)
gis = GIS("https://www.arcgis.com", "username", "password")

# Access the feature layer by item ID
item_id = 'item_id'
item = gis.content.get(item_id)

# Access the feature layer collection
feature_layer_collection = FeatureLayerCollection.fromitem(item)

# Get the layers within the feature layer collection
layers = feature_layer_collection.layers

# Iterate through each layer
for layer in layers:
# Update the extent of the layer (replace the values with the desired extent)
layer.manager.update_definition({
'extent': {
"xmin": new_min_x,
"ymin": new_min_y,
"xmax": new_max_x,
"ymax": new_max_y,
"spatialReference": {
"wkid": new_wkid
}
}
})

# Print the properties of the updated layer
print("Properties of Layer '{}' after updating extent:".format(layer.properties.name))
print(layer.properties)

 

0 Kudos
2 Replies
David_McRitchie
Esri Contributor

Hey IB3,

I found the following community post which seems to resolve this.

Please let me know if you have any further issues on this.

Many thanks,

David

Esri UK -Technical Support Analyst
0 Kudos
IB3
by
Occasional Contributor

Yes. I had already seen it, but it does not change the extent of every layers, just the extent of the feature layer. 

In map viewer, when I click zoom to  on the feature layer (the feature layer that contains all the layers) it works. But when I click on each individual layer, it is not applied. 

0 Kudos