Iterating using max raster value as criteria to stop model

378
2
03-07-2013 12:42 PM
MarkBoucher
Regular Contributor II
I've got a model working as I want. I use the while iterator to keep it going until I can visually see it is time to stop. Then I manually stop it with the Cancel button. In this model, the final output raster is written over the input raster.

What I want to do is test the maximum value in the resulting raster and stop the iteration if the max value does not equal -999 (the value in the cells is -999 they need to be changed). This model takes a raster of
infiltration rates gained from other sources that are mosaiced and then modifies it to fill in "holes" iterating again and again until the holes are filled in. The holes are filled in by using the maximum value surrounding the cell (8 directions). This is not a "fill sinks" issue in a DEM.


I can't figure out how to make the "while" or "if" iterators work with raster statistics (max cell value).


Any thoughts or suggestions?
0 Kudos
2 Replies
LT
by
Occasional Contributor
Perhaps you could clarify your goal...

You say you want to stop the iteration if the max value does not equal -999

Then you say, the value in the cells is -999 they need to be changed.

--><--
???
0 Kudos
curtvprice
MVP Esteemed Contributor
What I want to do is test the maximum value in the resulting raster and stop the iteration if the max value does not equal -999 (the value in the cells is -999 they need to be changed).

I can't figure out how to make the "while" or "if" iterators work with raster statistics (max cell value).


One way is to write a little Python script using the the Calculate Value tool, the tool output can then be referenced in the While tool.

For example:

Expression:  StopTest(r"%output raster%")

Code Block:
def StopTest(outRaster):
  import arcpy
  max = int(arcpy.GetRasterProperties_management (outRaster,"MAX").getOutput(0))
  if max == -999:
    return True  # keep iterating
  else:
    return False # time to stop


Data Type: Boolean

A less python-heavy approach would be to add the GetRasterProperties tool to the model, connect your raster to it, then connect the GetRasterProperties output to Calculate Value and use this simple python expression instead (no code block needed):

int(%RasterPropertyOut%) == -999

The downside to this second approach is that if the output of GetRasterProperties is zero, that evaluates to False and the Calculate Value will not run, as its input will be false. Such is the simplicity and elelegance that is ModelBuilder.
0 Kudos