3

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:

  1. 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.
  2. Try to use ListLayers to select multiple layers, but I cannot determine if this is possible.
  3. 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".

3
  • So, what is your problem? You have three layers in a map document and you would like to find out which of them has the largest extent of all features within (in other words, find out zooming to what layer will update the map document to the smallest scale)? Commented Sep 11, 2017 at 18:25
  • Alex - No I want to zoom to all 3 simultaneously, as the extents don't necessarily overlap completely.
    – yope
    Commented Sep 11, 2017 at 19:02
  • Well, you want to see all the features from all the three layers, right? Then you would need to find the layer with the largest extent and zoom to this one. Sadly, I don't understand what do you mean by I want to zoom to all 3 simultaneously Commented Sep 11, 2017 at 20:03

1 Answer 1

5

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)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.