I have a set of rasters (8 in total) that only every contain a 1 or a 0 for each cell. Each raster represents a different years worth of data. For arguments sake year 1 to year 8.

I can add all the rasters together which will give me a final grid with values ranging from 0 to 8. An 8 indicating that the cell was constantly 1 for the set of rasters (all years).

I would like to find out for each cell the longest consecutive number of 1's.

So for example the total grid may record for a single cell a value of say 5 but over the 8 grids that cell has the largest consecutive number of 1's equal to 3. Or another way of expressing this is for 3 years that cell was a 1 then it started to oscillate between zeros and ones.

My raster processing skills is not as hot as my vector processing skill and I have had a good look at the ESRI help file but I can't figure out how one would achieve this using off the shelf geo-processing tools?

That's actually a pretty cool analysis. As usual, there's more than one way to do what you're trying to do. I think you're going to need to do some programming to loop through all the combinations though.
–
MLowryJun 14 '12 at 18:32

1

A general comment (inspired by this remark by @MLowry): please upvote questions whenever they look interesting or are clearly articulated. Good questions drive everything on our site; please let's do what we can to reward those who ask them!
–
whuber♦Jun 14 '12 at 18:44

3 Answers
3

Because this is a local operation, let's work out how to do it for a single cell: Map Algebra will take care of the rest.

First note that the order of the rasters obviously matters. Therefore single-shot cell statistics, such as a cell sum, won't do it.

If we were to encounter a sequence such as 01110101 at a given cell, we would process this from start to end and

Begin with a count at zero.

Increment the count each time we encounter a 1.

Reset the count each time we encounter a 0, after saving the last count.

At the end, take the maximum saved count (including the final count).

Step 1 is implemented with a constant zero grid. Steps 2 and 3 depend on what we encounter: it is therefore a conditional operation. Step 4 clearly is a local maximum. We would code this, then, a little more formally as:

That's best done with a Python script when you have many grids, but with eight it's not onerous to unroll the loop and write out the steps by hand. This reveals a slight problem: the result=max(longest,count) is sort of a "side-effect" that's hard to code with raster operations. (But it can be done, as shown in the second solution below.) It's also inefficient, because it adds an extra calculation at each step. We therefore modify the approach a little, with an aim to putting off the max operation until the end. This will require saving a separate count at each stage.

In going through this process I also found a shortcut for the first step. This leads to the following solution, which although a little long and RAM-intensive, is simple and involves quickly-executed steps:

The actual syntax varies with your version of ArcMap. (For instance, CellStatistics is new to version 10, I believe, but a local maximum operation has always been available.)

In the example with input 01110101, the sequence of "result*" grids would contain the values 0, 1, 2, 3, 0, 1, 0, 1, so at the end CellStatistics would return 3, the length of the longest string of 1's.

If RAM is scarce, the solution can be modified to re-use the intermediate results, at a cost of approximately doubling the execution time:

In the example with input 01110101, the ("temp", "result") values would be (NoData, 0) after the first line and after each pair of ("Con", "CellStatistics") operations the values would be (1, 1), (2, 2), (3, 3), (0, 3), (1, 3), (0, 3), (1, 3). Once again the final value is 3.

The regular pattern of Map Algebra expressions in either solution indicates how to code the algorithm as a loop in a script, changing indexes as appropriate with each iteration.

There might be a typo in the type code block: count=count=1 should probably be count=count+1
–
MLowryJun 14 '12 at 19:25

1

@ML Thanks (good eyes!): it's fixed now. It's hard to make pseudocode absolutely correct; human review is a real asset in finding errors. Also, although I did not test the solutions in ArcGIS, I did implement the first solution in R, so I have some assurance the approach is correct.
–
whuber♦Jun 14 '12 at 20:33

"whuber" yet again you are THE man who knows! God help the rest of us if you ever get run over by a bus! Your initial Python approach was the direction I was thinking but I know with rasters one can often do everything so much slicker which you have proven. If you find yourself in Britain it would be a honour to buy you a pint of our best room temperature flat beer! :)
–
HornbyddJun 15 '12 at 9:46

Just chatting about this and wondering whether you could approach the problem by treating the input grids as a binary stream. This would allow you to combine them to give a unique summary integer for the sequence - ie 01110101 = 117. This value could then be reclassified to give the maximum number of consecutive 1s.

Bitwise operations could also be pressed into service for this step. Alternatively, you can use combine followed by a field calculation. (The field calculation will have an expression similar to the preceding one.)

The reclassification table has to provide max run lengths for all values between 00000000B = 0 and 11111111B = 255. In order, here they are:

A huge +1: This is a very good idea. (The only limitation is that when more than 31 grids are involved, you will run out of bits to use.) I have taken the liberty to flesh out your idea a little so that others can see how easy it is to implement.
–
whuber♦Jun 15 '12 at 11:59

Have you thought of changing the values from 0 and 1 to values with the power of 2 (1,2,4,8,16,32). When you combine the 8 grids you will get unique values for each cell which will give you consecutive info (ie: a value of 3 means year 1 and 2, where a value of 54 would be years 6 to 8).