kkvh5369

Python "Restart" mid-script

Discussion created by kkvh5369 on Mar 27, 2012
Latest reply on Apr 20, 2012 by kkvh5369
Hi all,
I have written a script that extracts features from a shapefile, converts to a raster then reclassifies that raster.  It loops through all the features in the original shapefile (there are approx. 10000 of them) and has worked fine for the first 3396, however it now will not reclassify.  The issue is with the following line:

            gp.Reclassify_sa(OutputRaster0, "VALUE", "0 1;NODATA 0", OutputRaster01, "DATA")

I can reclassify OutputRaster0 manually in ArcMap fine, but when I try to do this as a script Python simply "Restarts"  (i.e. it doesn't close down, but stops running the script and "forgets" all the variables - see screenshot below).

[ATTACH=CONFIG]13041[/ATTACH]

I have tried restarting all Arc and Python applications, but that hasn't worked.  As you can see, I don't get any error messages.

I'm running on v9.3, ArcInfo, on Windows 7, 64 bit.

Any help gratefully received!
Thanks
Kate.

Full script:

# ---------------------------------------------------------------------------
# CreateSR.py
# Created on: Tue Mar 13 2012 05:01:00 PM
#   (generated by ArcGIS/ModelBuilder)
# ---------------------------------------------------------------------------


# This script creates Boolean raster files for each of the polygon features in the input shapefile

# Local variables...
ShapefileName = "MAMMTERR_Dissolve.shp"  # Original shapefile of overlapping polygons
DBFName = "MAMMTERR_Dissolve.dbf"        # DBF of original shapefile
fpath_in = "C:\\Documents\\SpeciesRichness\\IUCN_data\\MAMMTERR\\" # Filepath of input data and record files
fpath_boolean_out = "C:\\Documents\\SpeciesRichness\\Mammals\\Boolean\\" # Filepath of final Boolean output files
fpath_shape_out = "C:\\Documents\\SpeciesRichness\\Mammals\\Shapes\\" # Filepath of shapefile output files
fpath_raster_out = "C:\\Documents\\SpeciesRichness\\Mammals\\Rasters\\" # Filepath of raster output files (non-boolean)
FieldName = "Binomial"  # Name on which to base selection (i.e. species name) 
TextDoc = "SpeciesList_MAMMTERR.txt"    # Record of species list and file names of Boolean Rasters
ErrorDoc = "ErrorList_MAMMTERR.txt"     # Records list of species that didn't process
CellSizeDD = 0.1    # Resolution of raster (in decimal degrees)
Start = 3398    # Number of last Boolean raster map created
                # Use to ensure continuous numbering if have to stop mid-run

# Empty variables...
species = []
FieldNameSelection = []

# Import system modules
import sys, string, os, arcgisscripting
from dbfpy import dbf


# Create the Geoprocessor object
gp = arcgisscripting.create()

# Turn on overwrite
gp.overwriteoutput = 1

# Set the extent
gp.Extent = fpath_in + ShapefileName

# Load required toolboxes...
gp.AddToolbox("C:/Program Files (x86)/ArcGIS/ArcToolbox/Toolboxes/Analysis Tools.tbx")
gp.AddToolbox("C:/Program Files (x86)/ArcGIS/ArcToolbox/Toolboxes/Conversion Tools.tbx")
gp.AddToolbox("C:/Program Files (x86)/ArcGIS/ArcToolbox/Toolboxes/Spatial Analyst Tools.tbx")

# Set product level
gp.SetProduct("ArcInfo")
print "Licence level is set to: " + gp.ProductInfo()

# Create list of all species in shapefile
DBName = fpath_in + DBFName
db = dbf.Dbf(DBName)
for k in range(Start,len(db)):
    feature = db[k]
    speciesX = feature[0]
    species.append(speciesX)

db.close()

# Check out spatial analyst extension
try:
    if gp.CheckExtension("spatial") == "Available":
        gp.CheckOutExtension("spatial")
    else:
        raise "LicenseError"

##    # Create empty Raster Overlay file
##    print "Creating empty species richness map"
##    SppRich = fpath + RasterOverlay
##    OverlayRaster1 = fpath + RasterOverlay + "1"
##    gp.FeatureToRaster_conversion(ShapefileName, "ID", OverlayRaster1, CellSizeDD)
##    gp.Reclassify_sa(OverlayRaster1, "VALUE", "0 0;NODATA 0", SppRich, "DATA")

# Loop through and process each species
    for i in range(0, len(species)):
        try:
            # Create layer of species1
            print "*********** " + str(species[i])
            print "Creating layer"
            ShapefileSelectionName = fpath_shape_out + species[i] + ".shp"
            FieldNameSelection = '\"' + FieldName + '\" = ' + "\'" + species[i] + "\'"
            gp.Select_analysis(ShapefileName, ShapefileSelectionName, FieldNameSelection) 

            # Convert layer to raster with Binomial name
            print "Creating raster"
            name = species[i]
            NameSplit = name.partition(" ")
            SpeciesName = NameSplit[0] + "_" + NameSplit[2]
            OutputRaster0 = fpath_raster_out + "SPP" + str(Start + i)
            gp.FeatureToRaster_conversion(ShapefileSelectionName, "FID", OutputRaster0, CellSizeDD)

            # Reclassify raster layer to Boolean
            print "Converting raster to Boolean"
            OutputRaster01 = fpath_boolean_out + "B_" + "SPP" + str(Start + i)
            print OutputRaster01
            gp.Reclassify_sa(OutputRaster0, "VALUE", "0 1;NODATA 0", OutputRaster01, "DATA")
            print "done reclassifying"

            # Add species name and number to text document
            print "Adding to text document"
            fname = OutputRaster01
            Sname = str(species[i])
            line = Sname + "," + fname + "\n"
            TextDocName = fpath_in + TextDoc
            TextDocName = open(TextDocName, "a")
            TextDocName.write(line)
            TextDocName.close()

            # Delete unnecessary files
            gp.Delete_management(OutputRaster0, "")
            
        except:
            print "Error with: " + str(species[i])
            ErrorDocName = fpath_in + ErrorDoc
            ErrorDocName = open(ErrorDocName, "a")
            ErrorDocName.write(str(species[i]) + "\n")
            ErrorDocName.close()

        # Return spatial analyst extension
    gp.CheckInExtension("spatial")
# Print error messages if license problems
except "LicenseError":
    print "Spatial Analyst license is unavailable"
except:
    print gp.GetMessages(2)
     
    

Attachments

Outcomes