Zoom/Pan to layer using Arcpy

7945
2
Jump to solution
10-10-2012 08:37 AM
ClaudineSicker
New Contributor III
I have the following script to be used inside ArcMap 10.1. It prompts the user for a shape file, creates a 2 mile buffer around it and then puts the buffered shapefile into a layer group. I want to then zoom to the buffer file. I tried to use pan to selected but since since I do not really have the buffer file selected, it is not working. Anybody know how to zoom/pan to a layer using python? Below is what I have working so far. I just need the code to zoom/pan to the extents of the "Two Mile Buffer" layer. 

Thank you in advance

Claudine

    import os.path     import arcpy      mxd = arcpy.mapping.MapDocument("CURRENT")     dirPath = os.path.dirname(mxd.filePath)     arcpy.env.overwriteOutput = True      df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]     tocLayer = arcpy.mapping.ListLayers(mxd, "Buffer Layers", df)[0]     project = arcpy.GetParameterAsText(0)     buffer_shp = dirPath + "\\buffer.shp"      arcpy.Buffer_analysis(project, buffer_shp, "2 Miles", "FULL", "ROUND", "ALL", "")      twoMileBuffer_shp = arcpy.mapping.Layer(buffer_shp)     twoMileBuffer_shp.name = "Two Mile Buffer"     arcpy.mapping.AddLayerToGroup(df, tocLayer, twoMileBuffer_shp, "AUTO_ARRANGE")
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
ClaudineSicker
New Contributor III
I finally figured it out and of course it was easier than I originally made it.

I just put this after the code referenced above:

lyr=arcpy.mapping.ListLayers(mxd, "Two Mile Buffer", df)[0] df.extent = lyr.getExtent(True) arcpy.RefreshActiveView()

View solution in original post

0 Kudos
2 Replies
AnthonyTimpson2
Occasional Contributor
Im not sure how helpful this code will be.

I use it to get the extents of grid features and zoom to those extents.

the line value in df.extent can be multiplied as well if you want to zoom out say 10%

you can do df.extent= newExtent * 1.1

import arcpy
from arcpy import env
from arcpy import mapping
env.workspace = r"C:\Users\atimpson\Desktop\Grid"

mxd = mapping.MapDocument("Current")

fc = "GRID"
count = str(arcpy.GetCount_management(fc))

x = 0

while x < int(count):
    rows = arcpy.SearchCursor(fc, "PageNumber = " + str(x))
    for row in rows:
        xmin, ymin, xmax, ymax  = row.shape.extent.XMin, row.shape.extent.YMin, row.shape.extent.XMax, row.shape.extent.YMax
        df = arcpy.mapping.ListDataFrames(mxd)[0]
        newExtent = df.extent
        newExtent.XMin, newExtent.YMin, newExtent.XMax, newExtent.YMax = xmin, ymin, xmax, ymax
        df.extent = newExtent
        arcpy.RefreshActiveView()
        mapping.ExportToJPEG(mxd, r"C:\Users\atimpson\Desktop\Grid\JPEG_" + str(x) + ".jpg", df, df_export_width=1728, df_export_height=948, world_file=True)
        print('Exported image' )
        print(x, 'of', count)
    x += 1

print("Export Complete")

0 Kudos
ClaudineSicker
New Contributor III
I finally figured it out and of course it was easier than I originally made it.

I just put this after the code referenced above:

lyr=arcpy.mapping.ListLayers(mxd, "Two Mile Buffer", df)[0] df.extent = lyr.getExtent(True) arcpy.RefreshActiveView()
0 Kudos