12-14-2020 11:14 AM - edited 12-14-2020 11:15 AM
In Spatial Modeler I have an iterator that accepts a n-band raster, does analysis on each band, and then saves each resulting band as a u1 int raster (0 == NoData). The u1int raster export occurs within the iterator.
Once the iterator exits, I want to collect all the output rasters and do a simple raster sum. For example, if I have a 5 band raster, then the sum raster would have values from NoData to 5.
I am trying to do something like:
Iterator > Stack Layers > Stack Total
The Stack Layers operator works as expected - it yields an n band raster of u1 int data type. However, the Stack Total operator does not yield a simple sum. Instead it populates 0 - 1 only across the whole raster. Instead of a sum I get a binary only result.
Any insight to the cause of this? Some tests with other rasters show that Stack Total is the correct operator for what I am trying to do, but in this case it just won't do a simple sum.
Solved! Go to Solution.
12-14-2020 11:31 AM - edited 12-14-2020 12:00 PM
Hi @and_viceversa ,
Have you checked the data type of the output raster stream? I'm betting it's u1?
Boolean / Binary inputs behave differently from pretty much any other type. As noted in the Help page inputting Binary data to Stack Total will produce a Binary output data type.
Try declaring the input raster stream (to the Stack Total operation) as u8 instead (or something that's appropriate to the number of bands you have and therefore what the maximum total value might be). That way you're working with Integers, not Binary. Convert Raster Type would enable you to do this.
Also note that once you've converted to Integer, the Integer output from Stack Total will default to s32. So you may then want another Convert Raster Type to drop the data type back down to something appropriate to the data range you're expecting.
12-14-2020 12:05 PM
Ian thank you for the prompt response.
You were spot on - I thought I had tested this case (modifying data type to something larger than uInt 1), but apparently not.
My intuition tells me that summing binaries should not yield binaries so I glossed over that detail in the documentation. But after thinking about it more, it makes sense to be able to enforce a strict data type.
With that in mind, is there a better/different way to sum an n-band raster or multiple rasters?
12-14-2020 12:37 PM
Hi @and_viceversa ,
No, I think Stack Total is what you want to use. It's just that Binary u1 is sort of an oddity that, as you say, is treated as a special case. For the Integer case the calculation / output is promoted to s32 and for Float it'll stay Float and Complex stays Complex.
The only need to worry wth the Integer case is if you want to constrain the range of your data to something more appropriate. For example if you were calculating the Stack Total of four u8 bands (that used the full 8-bit range), you might want to convert the output down to u16, rather than leaving the output stream as s32.
BTW - I myself have fallen into the trap of inuitively thinking that adding multiple u1 values together should produce values potentially larger than 1. Spent hours trying to track the issue down. That's why I was able to respond so quickly. :-)