Skip to main content
deleted 19 characters in body
Source Link

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(os.path.join(ws2, 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))

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(os.path.join(ws2, 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))

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))
Source Link

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(os.path.join(ws2, 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))