1

There are several questions about conditional reclassification on the Stack Exchange, but I haven't been able to find exactly what I need...

In my workspace, I have a list of raster layers (8 bit unsigned integer) that have values from 0-255. I am attempting to use conditional statements within the raster calculator to create a binary (0 and 1) layer in which only values between 90-100 are reclassified to 1 and the remaining values are reclassified to 0.

The code snippet I am working with can be seen below

# IMPORT MODULES
import arcpy, os

# SET GEO_PROCESSING ENVIRONMENTS
arcpy.env.workspace = r"C:\Users\BP_Mosaics"
outws = r"C:\Users\90BP_Mosaics"

# CREATE A LIST OF THE MOSAIC RASTER LAYERS
rasterList = arcpy.ListRasters("*", "tif")

# CREATE LOOP TO RECLASSIFY ALL BP MOSAICS INTO 0s and 1s
# CREATE LOOP TO RECLASSIFY ALL BP MOSAICS INTO 0s and 1s
for i in rasterList:
    inputRaster = (os.path.join(env.workspace, i))
    filename = i[-18:-4] + '_01.tif'
    outname = os.path.join(outws, filename)
    arcpy.gp.RasterCalculator_sa("Con(((" + inputRaster + ">= 90) & (" + inputRaster + "<= 100)), 1, 0)", outname)

I am not seeing any immediate errors in the code, however something does not seem to be working properly as I receive the following error message...

arcgisscripting.ExecuteError: ERROR 000539: (null)
Failed to execute (RasterCalculator).

Would anyone happen to know what is happening here?

3
1

I think there are 2 problems: i coming from your rasterList is not being constructed correctly and how you are building your expression to use in CON().

Firstly you create rasterList by searching a workspace and this returns a list of raster names, not the full path to the raster. So your input into your CON() expression should be something like:

inputRaster = os.path.join(outws, i)
arcpy.gp.RasterCalculator_sa("Con(((" + inputRaster +" >= 90) & (" + inputRaster +" <= 100)), 1, 0)", outname)

UPDATE:

I realised that you don't really need to call the CON tool within a Raster calculator, you are boxing a tool within a tool. So you just need to call the CON tool which has much easier syntax to understand. I also show you best practise which you were not doing, checking in/out extensions and encapsulating your code within a try/except to capture errors.

Having reviewed your updated code I have made the following edits so that it works, just change the paths to your requirements:

# IMPORT MODULES
import arcpy, os
arcpy.CheckOutExtension('Spatial')
try:

    # SET GEO_PROCESSING ENVIRONMENTS
    arcpy.env.workspace = r"D:\GIS\GIS Data"
    outws = r"D:\GIS"

    # CREATE A LIST OF THE MOSAIC RASTER LAYERS
    rasterList = arcpy.ListRasters("*", "tif")

    # CREATE LOOP TO RECLASSIFY ALL BP MOSAICS INTO 0s and 1s
    for i in rasterList:
        inputRaster = (os.path.join(arcpy.env.workspace, i))
        filename = i[-18:-4] + '_01.tif'
        outname = os.path.join(outws, filename)
        arcpy.gp.Con_sa(inputRaster, "1", outname, "0", '"Value" >=90 AND "Value" <=100')
except arcpy.ExecuteError:
    # Geoprocessor threw an error
    print(arcpy.GetMessages(2))
finally:
    arcpy.CheckInExtension('Spatial')
6
  • I adjusted my for loop as follows for i in rasterList: inputRaster = os.path.join(env.workspace, i) filename = i[-18:-4] + '_01.tif' outname = os.path.join(outws, filename) arcpy.gp.RasterCalculator_sa(Con(((inputRaster >= 90) & (inputRaster <= 100)), 1, 0), outname) and obtain the following error message.. "Type Error '>=' not supported between instances of 'str' and 'int'" May 22 '20 at 14:47
  • Look at my CON expression, its enclosed in quotes, yours is not.
    – Hornbydd
    May 22 '20 at 15:21
  • Can you update your question with the new fully updated code, add it as an amendment.
    – Hornbydd
    May 22 '20 at 16:03
  • See update above.. May 22 '20 at 17:11
  • 1
    Indeed, your correction made the code work as I was hoping it would. Thank you for your assistance!! May 22 '20 at 18:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.