# Getting Con statement in ArcPy to work?

I have a script in Python that includes a series of Spatial Analyst tools. I need to reclassify a raster and have tried using Remap which fails and have also tried a Con statement.

The Con statement is:

outCon = Con(IsNull("median141516_nn_test"),0,Con("median141516_nn_test" <= 5.0,(("median141516_nn_test"-0.0)/(5.0-0.0)*(1.25-0.0)),Con("median141516_nn_test" <= 10.0,((("median141516_nn_test"-5.0)/(10.0-5.0)*(2.5-1.25))+1.25),Con("median141516_nn_test" <= 15.0,((("median141516_nn_test"-10.0)/(15.0-10.0)*(3.75-2.5))+2.5),Con("median141516_nn_test" <= 20.0,((("median141516_nn_test"-15.0)/(20.0-15.0)*(5.0-3.75))+3.75),Con("median141516_nn_test" <= 25.0,((("median141516_nn_test"-20.0)/(25.0-20.0)*(6.25-5.0))+5.0),Con("median141516_nn_test" <= 30.0,((("median141516_nn_test"-25.0)/(30.0-25.0)*(7.5-6.25))+6.25),Con("median141516_nn_test" <= 65.0,((("median141516_nn_test"-30.0)/(65.0-30.0)*(8.75-7.5))+7.5),Con("median141516_nn_test" > 65.0,((("median141516_nn_test"-65.0)/(100.0-65.0)*(10.0-8.75))+8.75))))))))))

# Save the output
outCon.save("rcl_141516_test")

And the error is:

Runtime error Traceback (most recent call last): File "", line 1, in TypeError: unsupported operand type(s) for -: 'str' and 'float'

The raster is a floating point. The only thing I can think is that Python thinks the raster is a string, but I have saved a raster after each tool (similar to outCon.save) and have not received this error

I am new to writing multi tool scripts.

• On which line does the error occur? Please Edit the question. Commented Jul 9, 2019 at 13:55
• Hi Vince. I have added the whole error. Thanks for your help! Commented Jul 10, 2019 at 8:22

## 1 Answer

As you are not showing us the full code I can only guess as to the problem, you really should provide the full code, how do I even know you are importing the spatial analyst extension or setting the workspace? The answer is I don't. So I and the rest of the user community have to second guess what you actually did.

So assuming you have written a script that you are running in an IDE and not in ArcMap then this is what you are showing us (albeit a cut down version)

import arcpy
from arcpy.sa import *
myraster = "SRTM_India_Clipped.tif"
outcon = Con(IsNull(myraster),0,Con(myraster <= 5.0,((myraster-0.0)/(5.0-0.0)*(1.25-0.0)),-99))
outcon.save("processed.tif")

At no point are you explicitly stating the path name, or setting a workspace. I now set the workspace and check out/in the extension like this:

import arcpy
from arcpy.sa import *
arcpy.CheckOutExtension("Spatial")
arcpy.env.workspace = r"G:\LiveData\UC1369_DECCMA\Raster\Elevation\SRTM_Clipped"
myraster = "SRTM_India_Clipped.tif"
outcon = Con(IsNull(myraster),0,Con(myraster <= 5.0,((myraster-0.0)/(5.0-0.0)*(1.25-0.0)),-99))
outcon.save("processed.tif")
arcpy.CheckInExtension("Spatial")

I too get the same error message! It's because your raster variable is nothing more than a string. A string minus a number is meaningless, you need to explicitly state it is a raster layer using Raster(). So the final working code with some basic error trapping would be:

import arcpy
from arcpy.sa import *
try:
arcpy.CheckOutExtension("Spatial")
arcpy.env.workspace = r"G:\LiveData\UC1369_DECCMA\Raster\Elevation\SRTM_Clipped"
myraster = Raster("SRTM_India_Clipped.tif")
outcon = Con(IsNull(myraster),0,Con(myraster <= 5.0,((myraster-0.0)/(5.0-0.0)*(1.25-0.0)),-99))
outcon.save("processed.tif")
except Exception as e:
print("Error is: " + str(e))
finally:
arcpy.CheckInExtension("Spatial")