1

I'm trying to automate a process to select each feature of a feature class one by one, zoom to it and export as pdf. using the code below, the first feature is selected and the pdf is exported, and the loop stops there. How can I modify this code to loop through all records?

import arcpy
mxd = arcpy.mapping.MapDocument('CURRENT')
df = arcpy.mapping.ListDataFrames(mxd, "Layers") [0]
fc = "This_FC" 
with arcpy.da.SearchCursor(fc, ("SHAPE@", "FeatureID", "OID@")) as cursor:
 for row in cursor:
 query_str = """ "FeatureID" = '{0}' """.format(row[1])
 arcpy.SelectLayerByAttribute_management(fc, "NEW_SELECTION", query_str) 
 df.extent = row[0].extent
 df.scale = df.scale * 5
 arcpy.RefreshActiveView()
 arcpy.mapping.ExportToPDF(mxd, "C:/test/" + fc + "_" + str(row[1]) + "_" + str(row[2]) + ".pdf") 
asked Sep 10, 2014 at 12:10
2
  • 1
    just a guess, but there could be a conflict between the cursor and the selection on the same feature class. try to select on a copy of your fc, or don't use a cursor and run the selection on each OID (for i in range(count): / query_str = """ "OID" = {0} """.format(i) Commented Sep 10, 2014 at 12:29
  • abandoning the search cursor in favor of range(count) method worked. It takes a lot longer without the search cursor : <3 minutes vs. about 12 minutes. I can use the search cursor code by removing the selecting lines and the process works - the only negative is the feature won't be highlighted. Commented Sep 10, 2014 at 18:19

2 Answers 2

1

Try referencing the layer as an arcpy.mapping layer:

fc = arcpy.mapping.ListLayers(mxd, 'This_FC', df)[0]
answered Sep 10, 2014 at 13:23
0

I would consider looking into Data Driven Pages if you are not familiar with them already because they handle this workflow quite well:

Exporting Data Driven Pages

DataDrivenPages (arcpy.mapping)

answered Sep 11, 2014 at 20:24

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.