I have a script which selects an extent from a grid and exports the data frame.
import os, arcpy
mxd = arcpy.mapping.MapDocument('CURRENT')
df = arcpy.mapping.ListDataFrames(mxd,"Layers")[0]
grid_lyr = arcpy.mapping.ListLayers(mxd,"grid")[0]
scur = arcpy.SearchCursor(grid_lyr)
for row in scur:
sqlExp = """{0} = '{1}'""".format(arcpy.AddFieldDelimiters(grid_lyr,"PageName"),row.getValue("PageName"))
arcpy.SelectLayerByAttribute_management(grid_lyr,"NEW_SELECTION",sqlExp)
print "Selecting {}".format(row.getValue("PageName"))
df.extent = grid_lyr.getSelectedExtent(False)
df.zoomToSelectedFeatures()
df.scale = df.scale*1.05
arcpy.mapping.ExportToTIFF(mxd,os.path.join(r'C:\temp\109147',row.getValue("PageName")+".tif"),df,817, 534,world_file=True)
arcpy.RefreshActiveView()
del scur, mxd
I ran this script last week, and it crashed ArcMap after exporting the first tiff. I then ran it in a blank ArcMap document, changed CURRENT
to the full path and it worked (all 414 tiff files exported).
I am running the same script again today, on the same data, from a blank document. It now successfully exports the first two tiffs, and then stops executing as if it is finished, without any errors. When I try to run it again, ArcMap crashes again. I am not sure what the problem is, last week when it failed I just opened a blank document, as that normally solves the issues half the time.
I updated the code to include print statements for debugging:
for row in scur:
print "Row count: " +str(arcpy.GetCount_management(grid_lyr))
sqlExp = """{0} = '{1}'""".format(arcpy.AddFieldDelimiters(grid_lyr,"PageName"),row.getValue("PageName"))
arcpy.SelectLayerByAttribute_management(grid_lyr,"NEW_SELECTION",sqlExp)
print "SQL is: {}".format(sqlExp)
df.extent = grid_lyr.getSelectedExtent(False)
df.zoomToSelectedFeatures()
df.scale = df.scale*1.05
print "Data frame {0} has a scale of {1}".format(df.name, df.scale)
name = os.path.join(r'C:\temp\109147',row.getValue("PageName")+".tif")
arcpy.mapping.ExportToTIFF(mxd,name,df,817, 534,world_file=True)
print "Exported {}".format(name)
arcpy.RefreshActiveView()
del scur, mxd, row
The output was:
Row count: 500
SQL is: "PageName" = 'E3'
Data frame Layers has a scale of 2276.80888604
Exported C:\temp\109147\E3.tif
Row count: 1
SQL is: "PageName" = 'E4'
Data frame Layers has a scale of 2276.80888604
Exported C:\temp\109147\E4.tif
My grid layer only has 414 features, so the row count numbers are a bit weird. Also, if I try to define the cursor variable again in the same session, ArcMap crashes.
del row
now and check if it works.