I have a project where we create multiple mxd's per geographic area. Each mxd contains a unique definition query for 3 layers. I then select the 3 layers in the Table of Contents view and right-click for "Zoom to Layers". None of the 3 layers are consistently the largest, so I need to approximate this behavior using Arcpy.
Is there a way to do so?
Here are the steps I have taken so far:
- Group the layers, then zoom to layer on the group. I have been getting strange behavior from this and the zoom goes out much further than the actual extent when I zoom to the layers individually selected.
- Try to use ListLayers to select multiple layers, but I cannot determine if this is possible.
- Use the following code to zoom to the extents:
def GoToLayer(LayerNum):
... lyr = arcpy.mapping.ListLayers(mxd, "", df)[LayerNum]
... ext = lyr.getExtent()
... df.extent = ext
GoToLayer(0)
I get inconsistent zooms from this, where it will zoom out VERY far (58 mil), and then zoom in incrementally (35 mill, 6 mill, etc) as I apply the function again until it finally zooms to the actual layer extent.
My next solution will be to try selecting the individual features of each layer and using "Zoom to selected features".
1 Answer 1
If you want to find an extent that covers all three layers, I think this could be accomplished by making one extent object that is a combination of the extents of all the layers:
# set up layer list
layerList = [layer1, layer2, layer3]
# get current map extent
xmin, xmax = df.extent.XMin, df.extent.XMax
ymin, ymax = df.extent.YMin, df.extent.YMax
# loop through def query layer extents and create one extent to fit them all
for lyr in layerList:
ext = lyr.getExtent()
if ext.XMin < xmin:
xmin = ext.XMin
if ext.YMin < ymin:
ymin = ext.YMin
if ext.XMax > xmax:
xmax = ext.XMax
if ext.YMax > ymax:
ymax = ext.YMax
# set df extent to new extent
df.extent = arcpy.Extent(xmin, ymin, xmax, ymax)
I want to zoom to all 3 simultaneously