2

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.

12
  • Are you deleting your search cursor and row objects?
    – artwork21
    Commented May 6, 2013 at 14:43
  • Yes I am, forgot to add in the last line. Commented May 6, 2013 at 18:29
  • What about the row object (del row)?
    – artwork21
    Commented May 6, 2013 at 18:35
  • Have you checked to make sure that no records are selected in grid_lyr when you start the script? If anything is selected when you get going ONLY those records will show up in the cursor.
    – Kevin
    Commented May 6, 2013 at 18:43
  • @artwork21 I've checked and realised I did not have that in my script, although it ran fine without it last week. I'll add in del row now and check if it works. Commented May 7, 2013 at 4:51

1 Answer 1

1

I had a similar behaviour which I found out has to do with the arcpy.mapping site package. Since its the newest part in arcpy and is implemented as a wrapper for ArcObjects, it has problems in its error handling capabilities. Calls to arcpy.mapping may crash very often. And also if you try to catch exceptions, that does not work, because exceptions are never thrown. It simply kills the python interpreter. I told this to the ESRI support and they accepted it as a Bug (NIM090073). Hopefully it will be fixed with the next ServicePack.

You can easily check if this also happens here if you uncomment the Line

arcpy.mapping.ExportToTIFF(mxd,name,df,817, 534,world_file=True)

The cursor loop should finish then. Also you can check with a python debugger if the script crashes on the above line.

As a workaround our ESRI distributor told us to install ArcGIS 10.1 SP1 for Desktop Background Geoprocessing (64-Bit) from here . Of course you need ArcGIS 10.1 with Service Pack 1 installed and you need a 64-Bit machine, but that helped so my arcpy.mapping script ran through without problems.

Your Answer

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

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