Tuesday, 15 September 2015

arcpy - Zoom to Shapefile attribute table row feature


I am working with the python code below in ArcGIS to zoom into a shapefile's attribute table row features without selected until the end of table one by one.


I am trying to use this code but this one requires that a row is selected.


import arcpy

mxd = arcpy.mapping.MapDocument('CURRENT')

df = arcpy.mapping.ListDataFrames(mxd, "Layers") [0]


df.zoomToSelectedFeatures()

arcpy.RefreshActiveView()

Lets say, I have mapindex shapefile which has a 10 record in the attribute table. I want to zoom into first row automatically, then save the ArcMap .mxd or export as pdf. then move to next row, and do the same process(zoom in, save and export) until the end of record(row 10) in mapindex's attribute table.



Answer



You're actually probably better off cursoring through the features rather than trying to build a feature collection (sadly Iterate Datasets doesn't work in Python scripting, at least in ArcGIS 10.0). For example:


import arcpy

mxd = arcpy.mapping.MapDocument('CURRENT')


df = arcpy.mapping.ListDataFrames(mxd, "Layers") [0]

for row in arcpy.SearchCursor("path\to\map_index"):
df.extent = row.SHAPE.extent #Set the dataframe extent to the extent of the feature
df.scale = df.scale * 1.07 #Optionally give the shape a bit of padding around the edges

arcpy.RefreshActiveView()
#Put in your export here


This avoids having to know the contents of your dataset before running the script, but only allows you to zoom to a single feature at a time. If you wanted to zoom to groups of objects you could use the search cursor to construct a set of objects to base your selection.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...