1

I'm trying to do a for cycle to calculate standard deviation and mean for each raster in a list and after that to do a mathematical operation between them. Can someone help me to understand what is the problem?

ws2 = r"C:\Users\Elisa\Model_Builder\passo_5_6_7_8\output\raster_tagliati_su_flowacc"
v_name_sc_img = r"C:\Users\Elisa\Model_Builder\passo_5_6_7_8\output\outliers\{0}_out1.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 =arcpy.GetRasterProperties_management(ras, "MEAN")
    STD=arcpy.GetRasterProperties_management(ras, "STD")
    out_ras = ras - (mean +  3 * STD )
    out_ras.save(v_name_sc_img.format(name))

2 Answers 2

4

GetRasterProperties is returning a Result object, not the actual value of for example mean:

The Python result of this tool is a geoprocessing Result object. In order to obtain the string value, use the Result object's getOutput method.

Try using getOutput method. The output will be a string so convert it to float (or int if you want):

mean =float(arcpy.GetRasterProperties_management(ras, "MEAN").getOutput(0).replace(',','.'))
STD=float(arcpy.GetRasterProperties_management(ras, "STD").getOutput(0).replace(',','.'))
6
  • Thank you for your reply! there is another error now: INVALID LITERAL FOR FLOAT ()
    – Elisa m
    Jul 5, 2018 at 9:49
  • @ElisaMammoliti I got the same error. It is because the digits are comma-separated and needs to be dot-separated for the float conversion. See my modified code
    – BERA
    Jul 5, 2018 at 9:52
  • You're great! Now it works! thank you very very much! The last question (please be patient with me!): i need to have the full path of the previous rasters saved (path and raster name with extensions) to recall them into variables. I've tried with this: layer = arcpy.GetParameterAsText(0) desc = arcpy.Describe(layer) layersource = desc.catalogPath print layer
    – Elisa m
    Jul 5, 2018 at 11:26
  • Nice could you accempt my answer with the checkbox? I dont understand your last question. The full path and name of the saved out_ras? You could list them with listrasters and a wildcard.
    – BERA
    Jul 5, 2018 at 11:31
  • For example like this: fullpathandname = [os.path.join(arcpy.env.workspace, raster) for raster in arcpy.ListRasters(wild_card='*insertwildcardhere*')]
    – BERA
    Jul 5, 2018 at 11:37
0

As an alternative to calling GetRasterProperties, you can access the mean and standardDeviation properties directly.

ws2 = r"C:\Users\Elisa\Model_Builder\passo_5_6_7_8\output\raster_tagliati_su_flowacc"
v_name_sc_img = r"C:\Users\Elisa\Model_Builder\passo_5_6_7_8\output\outliers\{0}_out1.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 = ras.mean 
    STD = ras.standardDeviation
    out_ras = ras - (mean + 3 * STD )
    out_ras.save(v_name_sc_img.format(name))

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.