1

I’m using a while statement to count down from x until it = y. Each loop decreases x by 1. This variable (among other variables) is passed into a geoprocessing tool’s expression variable as a string. The string exactly mimics a user specified expression. Alas the ArcPy .gp does not accept my identical variable as an expression.

How does one take a text string, and pass it off as a text expression?

####### Raster values to separate rasters
import arcpy
arcpy.CheckOutExtension("spatial")

######## Script arguments

FolderLocation = "C:\\Data\\Testing\\"
InRaster = "C:\\Data\\ basin00_dem"

TopVal = 236
BotVal = 127
Range = TopVal-BotVal
print "There are: "+ str(Range) +" values to iterate"
print " "
SingleValue = "s1_"

MegaCount = TopVal

while (MegaCount) > BotVal:
    MegaCount = MegaCount-1
    ## Reclass Single Values
    SingleValueName = SingleValue + str(MegaCount) +".tif"
    SingleValueNamePath = FolderLocation + SingleValueName
    RC1 = "\"" + str(BotVal) +" "
    RC2 = MegaCount -1
    NODATA = " NODATA;"
    RC3 = str(MegaCount) +" "
    RC4 = str(TopVal)+ " "
    Val = "1 " + "\""
    Qu = "\""
    Equ = str(RC1) +str(RC2) + NODATA + str(RC3) + str(RC4) + str(Val) +str(Qu)
    Range = Range -1
    print "Number: " + str(Range) + " Equ: " +str(Equ)
    arcpy.gp.Reclassify_sa(InRaster, "Value", Equ , SingleValueNamePath, "NODATA")


# WORKING .GP
# arcpy.gp.Reclassify_sa(InRaster, "Value", "127 233 NODATA;234 236 1", SingleValueNamePath, "NODATA")
# Example print of Equ: "127 129 NODATA;130 236 1"
1
  • Can you include an error message/traceback? Commented Jun 26, 2014 at 0:50

2 Answers 2

1

I'm only addressing the "How does one take a text string, and pass it off as a text expression?" part here but try using Python string formatting as below:

RC1 = str(BotVal)
RC2 = str(MegaCount - 1)
NODATA = "NODATA"
RC3 = str(MegaCount)
RC4 = str(TopVal)
Val = "1"
Qu = ""
Equ = "{0} {1} {2}; {3} {4} {5} {6}".format(RC1,RC2,NODATA,RC3,RC4,Val,Qu)
print Equ
1
  • Yes, the string as formatted previously would have no spaces so just be one contiguous value, like instead of 10 1 NODATA; it would be 101NODATA; Commented Jun 25, 2014 at 23:52
0

I think you'll find your problem is:

arcpy.gp.Reclassify_sa

should be:

arcpy.sa.Reclassify

it goes like this:

Raster = arcpy.sa.Reclassify(InRaster,"Value",Equ, "NODATA")
Raster.save(SingleValueNamePath)

The spatial analyst / raster objects have changed since 9.x.

BTW, there's nothing wrong with your iteration but the pythonic way to do it is:

TopVal = 236
BotVal = 127
ValRange = range(TopVal,BotVal,-1) # creates a list of integers
print "There are: "+ str(len(ValRange)) +" values to iterate"

for MegaCount in ValRange:
    # now step through each value in the range
    # The MegaCount works as normal but don't do MegaCount = MegaCount-1
    SingleValueName = SingleValue + str(MegaCount) +".tif"
    SingleValueNamePath = FolderLocation + SingleValueName
    # and so on...

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.