jsnider@wwfcanada.org

Trouble migrating SOMA expression into ArcGIS 10 using arcpy?

Discussion created by jsnider@wwfcanada.org on Jan 19, 2011
Latest reply on Jan 19, 2011 by james_snider
Hi,

In 9.3, I had successfully applied the following nested Con statement:
    while row:
        value = row.getvalue("ID")
        if value == ID99:
            print "Value99..."
            value99 = row.getvalue("VALUE")
            print value99
        elif value == ID95:
            print "Value95..."
            value95 = row.getvalue("VALUE")
            print value95
        elif value == ID90:
            print "Value90..."
            value90 = row.getvalue("VALUE")
            print value90
        elif value == ID85:
            print "Value85..."
            value85 = row.getvalue("VALUE")
            print value85
        elif value == ID80:
            print "Value80..."
            value80 = row.getvalue("VALUE")
            print value80  
        row = rows.Next()

          
    gp.SingleOutputMapAlgebra_sa("CON ( " + str(raster) + " >= " + str(value99) + ", 99 , CON ( " + str(raster) + " >= " + str(value95) + ", 95, CON ( " + str(raster) + " >= " + str(value90) + ", 90, CON ( " + str(raster) + " >= " + str(value85) + ", 85, CON ( " + str(raster) + " >= " + str(value80) + " , 80, 0 ) ) ) ) )", Output)
    print "Output percentile raster created"


However, I've had some issues implementing the same processes using arcpy.
I've tried using both the Con and Reclassify approaches, but both raise errors.

Using the Con tool, the expression I've used is:

for row in rows:
        value99 = row.getValue("VAL_99")
        value95 = row.getValue("VAL_95")
        value90 = row.getValue("VAL_90")
        value85 = row.getValue("VAL_85")
        value80 = row.getValue("VAL_80")

try:
        outRas = Con(raster >= value99, 99, Con(raster >= value95, 95, Con(raster >= value90, 90, Con(raster >= value85, 85, Con(raster >= value80, 80, 0)))))
        print "Con statement completed"
except Exception as e:
        print e.message

    try:
        outRas.save(r"F:\outRas")
        print "output raster saved"
        
    except Exception as e:
        print e.message


Which raises the following error:
ERROR 010240: Could not save raster dataset to F:\outRas with output format GRID.


Similarly, when using the Reclassify approach:
try:
        in_raster = CAVM_het_output
        reclass_field = "VALUE"
        
        valueMAX = arcpy.GetRasterProperties_management(CAVM_het_output, "MAXIMUM")
        print "max = ", valueMAX
        
        valueMIN = arcpy.GetRasterProperties_management(CAVM_het_output, "MINIMUM")
        print "min = ", valueMIN
        
        remap = ([value99, valueMAX, 99], [value95, value99, 95], [value90, value95, 90], [value85, value90, 85],[value80, value85, 80], [valueMIN, value80, 0])
        print "remap = ", remap        

        outRas = Reclassify(in_raster, reclass_field, remap, "NODATA")
        print "outRas created"
        
        print "Reclassify statement completed"
        
    except Exception as e:
        print e.message

    try:
        outRas.save(r"F:/outRas")
        print "output raster saved"
        

This approach raises the following error:
Object: Error in executing tool


In both cases, my thinking is the trouble is attributed to having variables in the expressions (e.g. value99 or valueMAX), but perhaps I'm wrong.

E.g.
remap =  ([565.0, <Result '585'>, 99], [546.0, 565.0, 95], [525.0, 546.0, 90], [504.0, 525.0, 85], [483.0, 504.0, 80], [<Result '0'>, 483.0, 0])


Does anyone have any ideas here? I'm guessing this is an easy fix, but I'm hitting the wall...

Many thanks,
James

Outcomes