Error with Calculate Statistics

462
2
Jump to solution
08-10-2017 12:29 PM
AndrewMartin8
New Contributor III

A program that I created has a loop that runs and it constantly lowers the values of a flow accumulation Raster. There gets to a point where the Raster should hit the value zero or no data. The raster comes out with a positive big number and a negative big number, which messes up the calculate statistics function in the loop. Here it is below.

This is an example of the error I would receive

Is there a way I can create an if statement in the loop to check for a raster that looks like this and break away from the loop? Something like this but a statement that would work.

0 Kudos
1 Solution

Accepted Solutions
XanderBakker
Esri Esteemed Contributor

You can also use Get Raster Properties—Help | ArcGIS Desktop with the "ALLNODATA" option to test for it.

Example:

fa = r'C:\GeoNet\TestNull\myras.tif'
test = arcpy.GetRasterProperties_management(fa, "ALLNODATA")
test = int(test.getOutput(0))
if test == 1:
    print "Raster contains all NoData pixels..."
else:
    print "Not all pixels are NoData..."

View solution in original post

2 Replies
XanderBakker
Esri Esteemed Contributor

I assume that you are running this in the Python Window inside a session of ArcMap and the raster is present in the TOC. Otherwise, I guess you wouldn't find a raster only by its name.

You could use a try except to catch any errors:

import arcpy
fa = "facheck_102"

try:
    fa = arcpy.sa.Int(fa)
    arcpy.CalculateStatistics_management(fa)
except Exception as e:
    print "ERR:", e
XanderBakker
Esri Esteemed Contributor

You can also use Get Raster Properties—Help | ArcGIS Desktop with the "ALLNODATA" option to test for it.

Example:

fa = r'C:\GeoNet\TestNull\myras.tif'
test = arcpy.GetRasterProperties_management(fa, "ALLNODATA")
test = int(test.getOutput(0))
if test == 1:
    print "Raster contains all NoData pixels..."
else:
    print "Not all pixels are NoData..."