1

I'm trying to reclassify rasters based on standard deviation. I created this code, but I think I'm wrong using "Con". I would like to assign value 1 to rasters >= to threshold and rasters <= to threshold1 and assign NODATA to the remaining.

This is my code:

    import arcpy
    ws2 = r"C:\unsupervised_classification\output\raster_tagliati_su_flowacc"
    v_name_sc_img = r"C:\unsupervised_classification\output\outliers\{0}_out1.img"
    v_name1_sc_img = r"C:\unsupervised_classification\output\outliers\{0}_out2.img"
    arcpy.env.workspace = ws2
    ras_names = arcpy.ListRasters()
    for ras_name in ras_names:
        name, ext = os.path.splitext(ras_name)
        ras = arcpy.Raster(os.path.join(ws2, ras_name))
        mean =float(arcpy.GetRasterProperties_management(ras, "MEAN").getOutput(0).replace(',','.'))
        STD=float(arcpy.GetRasterProperties_management(ras, "STD").getOutput(0).replace(',','.'))
        threshold_sup=mean + 3* STD
        threshold_inf= mean - 3* STD
        expression="Con(" + ras + "=>" + str(threshold_sup) + ",1,setnull([ras])"
        a= arcpy.gp.RasterCalculator_sa(expression, v_name_sc_img)
        expression1="Con(" + ras + "<=" + str(threshold_inf) + ",1,setnull([ras])"
        b= arcpy.gp.RasterCalculator_sa(expression1, v_name1_sc_img)
        a.save(v_name_sc_img.format(name))
        b.save(v_name1_sc_img.format(name))

Arcpy give me this error:

RuntimeError: ERROR 000732: Input Raster: Dataset Con( does not exist or is not supported
1
  • I agree that it is not exactly the same problem, but the solution is the same. you are mixing python map algebra and gp.rasterclaculator. If you want to use rastercalculator, your should use a string to name your raster instead of a raster object (you cannot "add" a string with a raster object. In any case, it is better to use map algebra in a script. And the setnull will cause a problem after solving the string + raster object issue (just remove the setnull)
    – radouxju
    Commented Mar 20, 2019 at 9:11

1 Answer 1

2

Try this piece of code. I do not think that you have to cast the mean/std to float. Try it out this way. Of course you need an Spatial Analyst, but since you are using RasterCalculator I think you have it.

import arcpy
from arcpy.sa import *
arcpy.CheckOutExtension('Spatial')

ws2 = r"C:\unsupervised_classification\output\raster_tagliati_su_flowacc"
v_name_sc_img = r"C:\unsupervised_classification\output\outliers\{0}_out1.img"
v_name1_sc_img = r"C:\unsupervised_classification\output\outliers\{0}_out2.img"
arcpy.env.workspace = ws2
ras_names = arcpy.ListRasters()
for ras_name in ras_names:
    name, ext = os.path.splitext(ras_name)
    ras = Raster(ras_name)

    mean = ras.mean
    STD = ras.standardDeviation

    threshold_sup= mean + 3* STD
    threshold_inf= mean - 3* STD

    out = Con(ras >= threshold_sup, 1)
    out2 = Con(ras <= threshold_inf, 1)

    out.save(v_name_sc_img.format(name))
    out2.save(v_name1_sc_img.format(name))

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