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")
-
1just 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)radouxju– radouxju2014年09月10日 12:29:58 +00:00Commented 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.detroit_hc– detroit_hc2014年09月10日 18:19:20 +00:00Commented Sep 10, 2014 at 18:19
2 Answers 2
Try referencing the layer as an arcpy.mapping layer:
fc = arcpy.mapping.ListLayers(mxd, 'This_FC', df)[0]
I would consider looking into Data Driven Pages if you are not familiar with them already because they handle this workflow quite well: