Hi All,
BY the help of below script, i trying to get properties of raster using mask file. I want certain area statistics value of raster.
So far i have, but its getting error while performing the task
import arcpy
arcpy.env.workspace = r"D:\Annomaly_SPEI_12"
arcpy.env.Mask = r"D:\Agro_Zone\Zone-1.shp"
Mask = arcpy.env.Mask
rasterList = arcpy.ListRasters()
for raster in rasterList:
rasterObj = arcpy.Raster(raster)
print raster
bands = arcpy.GetRasterProperties_management(Mask ,raster, "MEAN")
print "MEAN VALUE: %s" %bands
Error Massage :
ERROR 000864: Property type: The input is not within the defined domain.
ERROR 000800: The value is not a member of MAXIMUM | MINIMUM | MEAN | STD | TOP | LEFT | RIGHT | BOTTOM | CELLSIZEX | CELLSIZEY | VALUETYPE | COLUMNCOUNT | ROWCOUNT | BANDCOUNT | ALLNODATA | ANYNODATA | SENSORNAME | PRODUCTNAME | ACQUSITIONDATE | SOURCETYPE | CLOUDCOVER | SUNAZIMUTH | SUNELEVATION | SENSORAZIMUTH | SENSORELEVATION | OFFNADIR | WAVELENGTH.
ERROR 000864: Band Name: The input is not within the defined domain.
ERROR 000800: The value is not a member of Band_1.
Failed to execute (GetRasterProperties).
Solved! Go to Solution.
This should work... no need to use Get Raster Properties tool, the raster object has the statistics as properties on it already. But you need to actually clip the raster to get stats on the cells inside your mask.
import arcpy
from arcpy.sa import *
arcpy.env.workspace = r"D:\Annomaly_SPEI_12"
msk = r"D:\Agro_Zone\Zone-1.shp"
arcpy.env.extent = msk
rasterList = arcpy.ListRasters()
for raster in rasterList:
rasterObj = ExtractByMask(raster, msk)
rasterObj.save() # Save temp raster to disk so stats are available
print "MEAN VALUE: %s" % rasterObj.mean
arcpy.Delete_management(rasterObj)
Check the code examplesand retrieve the result object
A. Case is wrong: arcpy.env.mask
B. What are you trying to do? If you want to get stats of a raster within the mask, you need to apply the mask to clip the raster.
C. Dan Patterson arcpy.sa tools do not return result, they return raster objects.
D. The parameters supplied to GetRasterProperties are not correctly specified. See the help for the tool.
hmmmm I was reading this... I was thinking python
The property returned is displayed in the Results window.
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.
Yes, that's right with the Get Raster Properties tool, it does return Result. Sorry I missed that (reading on my phone).
This should work... no need to use Get Raster Properties tool, the raster object has the statistics as properties on it already. But you need to actually clip the raster to get stats on the cells inside your mask.
import arcpy
from arcpy.sa import *
arcpy.env.workspace = r"D:\Annomaly_SPEI_12"
msk = r"D:\Agro_Zone\Zone-1.shp"
arcpy.env.extent = msk
rasterList = arcpy.ListRasters()
for raster in rasterList:
rasterObj = ExtractByMask(raster, msk)
rasterObj.save() # Save temp raster to disk so stats are available
print "MEAN VALUE: %s" % rasterObj.mean
arcpy.Delete_management(rasterObj)
Hi @Curtis , Thank you. This is exactly what i was trying to do. But I want to some modification of this code,
1. I want to print the raster statistics along with raster name, current script producing only raster statistics, For Example, along with raster name Its looks like:
Annmly_SPEI12_2001.tif
MEAN VALUE: -0.6128285124898
MIN VALUE: -1.0352302789688
Max VALUE: -0.33303740620613
Annmly_SPEI12_2002.tif
MEAN VALUE: -1.2694534122944
MIN VALUE: -1.6432282924652
Max VALUE: -0.99528157711029
2. What's the statement for getting Standard deviation value? I applied below statement to get SD, but not working
print "SD VALUE: %s" % rasterObj.std
The Raster object has properties for the raster name, path, and standard deviation, the names of these are all documented in the help article I linked for you:
I really like using string formatting to get pretty output easiliy, for example:
print("{}\n Mean: {} Min: {} Max: {} Std: {}".format(
ras.name, ras.mean, ras.minimum, ras.maximum, ras.standardDeviation))
Curtis Price, Thank you. I ran the script to get statistics value of raster along with raster name, but its printing same name for all rasters which i have in folder, its printing like
Extract_anno2
Mean: -0.501958936453 Min: -0.536289691925 Max: -0.455532312393 Std: 0.0377060631886
Extract_anno2
Mean: -1.16915395856 Min: -1.18902957439 Max: -1.14363610744 Std: 0.0214943939412
Extract_anno2
Mean: -0.344216823578 Min: -0.357096046209 Max: -0.327130705118 Std: 0.0135495629474
Extract_anno2
Mean: 0.0810092817992 Min: 0.0689202919602 Max: 0.0912198796868 Std: 0.00927550311987
Extract_anno2
Mean: 0.4306351915 Min: 0.41828379035 Max: 0.440097242594 Std: 0.00911574925779
My raster name is,
annomaly_SPEI06_2001.tif,
annomaly_SPEI06_2002.tif,
annomaly_SPEI06_2003.tif,
annomaly_SPEI06_2004.tif,
annomaly_SPEI06_2005.tif,
.
.
.
.
.
annomaly_SPEI06_2012.tif,
I want to get print raster name, like
annmly_spei06_2001.tif
Mean: -0.501958936453 Min: -0.536289691925 Max: -0.455532312393 Std: 0.0377060631886
annmly_spei06_2002.tif
Mean: -1.16915395856 Min: -1.18902957439 Max: -1.14363610744 Std: 0.0214943939412
.
.
.
.
There must be a bug in your code.