Assign a Mask file to Get Raster Properties

2068
9
Jump to solution
03-31-2017 05:47 AM
ShouvikJha
Occasional Contributor III

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).
0 Kudos
1 Solution

Accepted Solutions
curtvprice
MVP Esteemed Contributor

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)
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

View solution in original post

9 Replies
DanPatterson_Retired
MVP Emeritus

Check the code examplesand retrieve the result object

curtvprice
MVP Esteemed Contributor

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. 

DanPatterson_Retired
MVP Emeritus

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.

curtvprice
MVP Esteemed Contributor

Yes, that's right with the Get Raster Properties tool, it does return Result. Sorry I missed that (reading on my phone).  

curtvprice
MVP Esteemed Contributor

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)
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
ShouvikJha
Occasional Contributor III

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
0 Kudos
curtvprice
MVP Esteemed Contributor

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:

Raster—Help | ArcGIS Desktop 

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))‍‍‍‍‍‍‍‍‍‍‍‍
ShouvikJha
Occasional Contributor III

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‍‍‍‍‍‍‍‍‍‍‍‍
.
.
.
.
0 Kudos
curtvprice
MVP Esteemed Contributor

There must be a bug in your code.

0 Kudos