Select to view content in your preferred language

"Con" Statement using Python

596
5
06-30-2010 01:41 PM
JamesSnider
Deactivated User
Hi I'm trying to implement a "nested" conditional statement using the singleoutmapalgebra tool via Python. Since there are multiple nested con statements I've been trying to use concatentation, however I'm having trouble with the syntax. Any advice would be greatly appreciated.

Here's a simplified portion of the script, which still yields the following error: "TypeError: cannot concatenate 'str' and 'int' objects"

import sys, string, os, arcgisscripting

gp = arcgisscripting.create()
gp.overwriteoutput = 1

gp.CheckOutExtension("spatial")

gp.AddToolbox("C:/Program Files/ArcGIS/ArcToolbox/Toolboxes/Spatial Analyst Tools.tbx")
gp.AddToolbox("C:/Program Files/ArcGIS/ArcToolbox/Toolboxes/Data Management Tools.tbx")


inFolder = r"F:\input"
gp.workspace = inFolder

#List Rasters..
rasters = gp.ListRasters("*int")

rasters.Reset()
raster = rasters.Next()

while raster:
   
    print raster
    output_location = r"Z:\out"

    output_folder = str(raster)
    OutPath = output_location + "\\" + output_folder
    print OutPath
    Output = OutPath + "\\" + "_Out"
   
    gp.CreateFolder_management(output_location, output_folder)
    gp.refreshcatalog(output_location)

    count = gp.GetRasterProperties_management(raster, "UNIQUEVALUECOUNT")
    print count
    ID99 = int ( (count * 99 /100 + 0.5))
    print ID99
    ID95 = int ( (count * 95 /100 + 0.5))
    print ID95
    ID90 = int ( (count * 90 /100 + 0.5))
    print ID90
    ID85 = int ( (count * 85 /100 + 0.5))
    print ID85
    ID80 = int ( (count * 80 /100 + 0.5))
    print ID80
   
    rows = gp.searchcursor(raster)
    row = rows.Next()

    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( " + raster + " >= " + value99 + ", 99, 0)", Output)
    print "Output"

    raster = rasters.Next()               
print "done"


Many thanks for your help.
James
0 Kudos
5 Replies
DanPatterson_Retired
MVP Emeritus
if your value field is numeric then you will have to str value99, and I am not sure what the purpose of the last +  is for
gp.SingleOutputMapAlgebra_sa("CON( " + raster + " >= " + str(value99) + ", 99, 0)", Output)
0 Kudos
ChrisSnyder
Honored Contributor
Seems like your value99 variable is numeric, right? Maybe as simple as converting value99 to a string so its "concatenatable" with the SOMA exp?

gp.SingleOutputMapAlgebra_sa("CON( " + raster + " >= " + str(value99) + ", 99, 0)", Output)
0 Kudos
ChrisSnyder
Honored Contributor
Jinx!!!!!!!
0 Kudos
DanPatterson_Retired
MVP Emeritus
also it seems that the last + allows to concatenate the last part
  ", 99, 0)",  but I would check in any event.
0 Kudos
JamesSnider
Deactivated User
Many thanks to you both, Dan and Chris. I appreciate such quick responses. This  is really helpful!!
Makes sense that everything has to be a string.

Cheers!
James
0 Kudos