Select to view content in your preferred language

Export raster statistics?

3886
10
10-08-2010 05:38 AM
StefanHaglund1
Esri Contributor
Hi!

Does anyone know if there is a tool out there that lets me export the statistics of a raster datatset?

Basically, I would like to write out the statistics of several rasters to a table or textfile. Think of how it looks under the Statistics heading in the Rater Dataset Properties window.

There is the Get raster properties tool, which I could call several times to get most of what I want, but I was hoping for something smoother than that.

I could try and come up with a script that does this but I've looked through the geoprocessor and can't seem to find anything useful there either.

Any suggestions?
Nooski
0 Kudos
10 Replies
RobertBerger
Occasional Contributor
Hi Nooski,

I don't know of any other tool/script that easily lets you export the statistics. In ArcMap you can export the current statistics to an xml file (when using statistics from current display extent) but I don't think there is a way to script this.
Get Raster Properties might be your best bet.

Robert
0 Kudos
Luke_Pinner
MVP Regular Contributor
Something like:
# GetRasterProperties.py 
# Description: Write raster properties to a file. 
# Note: Written for ArcGIS 9.3. 

# Create the Geoprocessor object 
import arcgisscripting
gp = arcgisscripting.create()

# Get input parameters
InRaster = gp.GetParameterAsText(0)
OutFile = gp.GetParameterAsText(1)

#Property Types
PropertyTypes=['MINIMUM','MAXIMUM','MEAN','STD','UNIQUEVALUECOUNT','TOP','LEFT','RIGHT','BOTTOM','CELLSIZEX','CELLSIZEY','VALUETYPE','COLUMNCOUNT','ROWCOUNT','BANDCOUNT']

#Output file
OutFile=open(OutFile,'w')

# Process: GetRasterProperties 
for PropertyType in PropertyTypes:
    try:
        Property = gp.GetRasterProperties(InRaster, PropertyType)
        gp.AddMessage('%s=%s'%(PropertyType,Property))
        OutFile.write('%s=%s\n'%(PropertyType,Property))
    except:
        # Print error message if an error occurs 
        gp.AddMessage(gp.GetMessages(2))

OutFile.close()
0 Kudos
BryceStath
Emerging Contributor
This could be really useful if it writes the statistic I need (mean) for many rasters to one file. 

How would I run this?  I've tried running it stand alone in PyWin, no dice, and by creating a script within Toolbox within an ArcMap session that has the rasters I want to generate stats for.

thanks
0 Kudos
StefanHaglund1
Esri Contributor
All,

Here's a follow up.

With help from Luke I managed to get a crude script for 10. It looks like this:

import arcpy
from arcpy import env
env.workspace = "C:\Path"
outfile = open("C:\RastStats.txt", 'w')
PropTypes = ["MINIMUM", "MAXIMUM", "MEAN"]
rastlist = arcpy.ListRasters("*", "")
for raster in rastlist:
    rastname = str(raster)
    outfile.write(rastname + "\n")
    for PropType in PropTypes:
        print PropType
        Prop = arcpy.GetRasterProperties_management(raster, PropType)
        textinfo = (PropType, Prop)
        textstring = str(textinfo)
        outfile.write(textstring + "\n")
outfile.close()


There is still some formatting to do, the result looks like this:
--------
raster1
('MINIMUM', <Result '0'>)
('MAXIMUM', <Result '2203.59692382813'>)
('MEAN', <Result '400.089714329223'>)
--------

The lines ('MINIMUM', <Result '0'>) and so on is what comes out of the GetRasterProperties tool in the script, it would be good to have it formatted nicer, like:
--------
raster1:
MINIMUM = 0
MAXIMUM = 2203.59692382813
MEAN = 400.089714329223
--------

What I have now is enough for me at the moment, but I wonder why ESRI (or arcpy) formats the results like it does? If you run it as a tool dialog you get "MEAN = 400.089714329223".

Also, the script is pretty slow, maybe the nested for-loops slows things down?
0 Kudos
BryceStath
Emerging Contributor
All,

Here's a follow up.

With help from Luke I managed to get a crude script for 10. It looks like this:

import arcpy
from arcpy import env
env.workspace = "C:\Path"
outfile = open("C:\RastStats.txt", 'w')
PropTypes = ["MINIMUM", "MAXIMUM", "MEAN"]
rastlist = arcpy.ListRasters("*", "")
for raster in rastlist:
    rastname = str(raster)
    outfile.write(rastname + "\n")
    for PropType in PropTypes:
        print PropType
        Prop = arcpy.GetRasterProperties_management(raster, PropType)
        textinfo = (PropType, Prop)
        textstring = str(textinfo)
        outfile.write(textstring + "\n")
outfile.close()





This looks promising, but I'm still on 9.3
I'll be trying to get this to work, but could use help if anyone feels like translating this.
0 Kudos
Luke_Pinner
MVP Regular Contributor
This looks promising, but I'm still on 9.3
I'll be trying to get this to work, but could use help if anyone feels like translating this.


See the 3rd post - (http://forums.arcgis.com/showthread.php?p=45823#poststop) it's for 9.3 and will do what you want with minimal changes. Basically instead of looping through statistic types, loop through rasters and get the mean - ie mean = gp.GetRasterProperties(someraster, 'MEAN')
.
0 Kudos
BryceStath
Emerging Contributor
I've been working on this (see code below)....  and it runs although it doesn't want to produce an output file for each grid within  gp.Workspace = "C:\\working\\Temp\\testing\\grids

Lets say I have two sample grids in there
f1p060_10c2
f1p060_10c4

I end up only getting in my output folder
C:\working\Temp\testing\outputs3\f1p060_10c2_stats.dbf

So it appears that my while loop isn't looping. 
The end goal here is to have all of my raster stats in one file.  If I can get this process to work, to write the stats from each input raster to one file, that's good enough.  I have an excel script that will push all files in one directory into one master file.
Thanks for any help.


# BatchZonalStatisticsAsTable.py
# Description: Summarizes values of a raster within the zones of another dataset and reports the results to a table
# Requirements: None
# Author: David Coley
# Date: 05/10/06

#import win32com.client, sys, os, string
import arcgisscripting
gp = arcgisscripting.create()
gp.CheckOutExtension("Spatial")
gp.CellSize = 1 #can set as argument
gp.OverWriteOutput = 1

try:
    gp.Workspace = "C:\\working\\Temp\\testing\\grids" 
    InZonalLayer = "C:\\working\\Temp\\testing\\TreatmentFranksTract2010.mdb\\Controls"
    InZonalValueField = "PIN" #can set as argumment
    #set output workspace which holds tables in a gdb
    OutWorkSpace = "C:\\working\\Temp\\testing\\outputs3"
    #Get the raster datasets in the input workspace and loop through them from the start

    InputRasters = gp.ListRasters()
    InputRasters.reset()
    InputRaster = InputRasters.next()
    OutTable = "C:\\working\\Temp\\testing\\outputs3\\" + InputRaster + "_stats.dbf" 

    while InputRaster:
     
        # Process: Zonal Statistics as Table 
            gp.ZonalStatisticsAsTable_sa(InZonalLayer, InZonalValueField, InputRaster, OutTable, "DATA")
        # print InputRaster
            InputRaster = InputRasters.next()
        #print OutTable
except:
    gp.AddMessage(gp.GetMessages(2))
    print gp.GetMessages(2)
0 Kudos
ArvindBhuta
Deactivated User
What parameters must I input in the parameters tab to get this to work?  I am new to scripts and getting them to work.
0 Kudos
curtvprice
MVP Alum
What parameters must I input in the parameters tab to get this to work?  I am new to scripts and getting them to work.


This script is hard coded - it does not take parameters.
0 Kudos