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]



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.

  • Can you clarify what you mean by "without selected until the end of table one by one"? It's unclear to me whether you do or do not want to zoom to the extent of the whole shapefile if there is no selection. In either case, the comparison of df.zoomToSelectedFeatures() and df.extent = lyr.getSelectedExtent() here or here may be helpful. – anoved Dec 20 '12 at 20:59
  • Are you using ArcGIS 10.0 or 10.1? If the latter then there is a code example at resources.arcgis.com/en/help/main/10.1/index.html#//… which should help but it needs the new Data Access module. – PolyGeo Dec 20 '12 at 21:52

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

    #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.

| improve this answer | |
  • Thats exactly what i was looking for but its soo fast and can even tell whats going on – Bilgehan Meral Dec 20 '12 at 22:25
  • Happy to help :) Basically, the cursor object loops over each row of the datset in turn. We grab the row and the geometry from the row (with row.SHAPE), and all geometries have an extent property which we can use to set the dataframe extent. So after you've zoomed to the new extent (while still in the loop) export the map and you should be all good. – om_henners Dec 20 '12 at 22:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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