'while loop' on rasters

590
6
01-09-2012 01:37 AM
MattiasVan_Opstal
New Contributor
A beginners question:
I am looking for the right syntax to perform a while loop on a raster "x",
I want to do something like
while cell (in raster x) > 0:
value of cell= value of cell-1

How can I do this? Probably something easy but I can't find it anywhere.

Thank you
Tags (2)
0 Kudos
6 Replies
AlessandroCinnirella
New Contributor III
you can do that using MapAlgebra and the Con() statement:

Con(rasterX > 0, rasterX -1, rasterX)

This means :
if the cell of rasterX is > 0, than the value of the cell will be changed to the value -1,
else, the value will remain unchanged.

ciao
AC
0 Kudos
MattiasVan_Opstal
New Contributor
Oké, but I want this Con() statement to be repeated until every value of the raster has reached 0.
Similar to the traditional "while-loop" in python...
0 Kudos
DanPatterson_Retired
MVP Emeritus
This question is confusing.  If the cell values are >0 do you simply want to subtract 1 from each value?  If so, then use the raster calculator to subtract 1 from the grid and produce a new grid which will be 1 less than the input grid.
0 Kudos
MattiasVan_Opstal
New Contributor
Sorry for the confusion, what I want to do is:
"While" (as long as...) the cell values of Ras1>0:
Ras2= do something
Ras3= do something
Ras1= Ras1 - 1
0 Kudos
AlessandroCinnirella
New Contributor III
ok, understood:


allzero = 1
while allzero > 0:
   newraster = Con(rasterX > 0, rasterX -1, rasterX)
   maxcellvalue = getRasterProperties_management(newraster, "MAXIMUM")
   if maxcellvalue <= 0:
       allzero = 0

sorry for the bad syntax and the poorness of the code, keep it as an advice.
should do what you need with the proper adjustments 🙂

ciao
AC
0 Kudos
MattiasVan_Opstal
New Contributor
thanks! I'll try, looks like it will work.
Thank you!
0 Kudos