Select to view content in your preferred language

How to determine whether Raster statistics have been calculated

2477
4
04-15-2011 02:03 AM
MichaelStjernholm
Regular Contributor
Hi

I have a script which fails when I use the GetRasterProperties_management function on a dataset where statistics have not yet been calculated.

How can I in a simple way check whether the statistics have been calculated ?

There is strangely enough not a Raster set attribute named "hasStatistics" or similar to get from Describe fuunction
neither does the GetRasterProperties simply rerturn a Null object
so I could use something like :

if arcpy.GetRasterProperties_management (outRaster, "MINIMUM") == None:
               CalculateStatistics_management (outRaster)

All help is most welcome

Michael
Tags (2)
0 Kudos
4 Replies
HongmeiZhu
New Contributor
After raster statistics are calculated, a file with aux.xml extension is created in the same directory as the raster resides. The auxiliary file saves information of statistics for future use. Therefore, if you can see an aux.xml file in the same location, that indicates Raster statistics have been calculated. Or you can simplly put a line after CalculateStatistics_management( ) to inform the calculation has been done successfully.

Print "Done successfully!"
0 Kudos
MichaelStjernholm
Regular Contributor
Thanks Hongmei for reminding me of using the test of whether the aux.xml file exists - I use similar approach in other calculations.

regards michael
0 Kudos
NiklasNorrthon
Frequent Contributor
Another way to solve the problem is to just assume that statistics is there and deal with the problem when it isn't.
# Pseudocode:
while True:
    try: 
        arcpy.FunctionThatNeedsRasterStatistics(raster)
        break
    except arcpy.ExecuteError:
        arcpy.CalculateStatistics(raster)


This pattern also has the advantage that it isn't subject to race conditions (not that those are likely for raster statistics, but in other contexts that can be a real issue.
JakeSkinner
Esri Esteemed Contributor
I just wanted to note that an aux.xml can exist when statistics do not.  For example, the aux.xml will also be used as a pointer to the pyramid file.  This is explained here:

http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//009t00000027000000.htm

You may want to go along the lines Niklas mentioned.  Ex:

try:
    arcpy.GetRasterProperties_management(input, "STD")
    print "Statistics exist"
except arcpy.ExecuteError:
    arcpy.CalculateStatistics_management(input)
    print "Statistics were calculated"
0 Kudos