Note that there are some explanatory texts on larger screens.

plurals
  1. POIdentifying overlap zones in R raster package
    primarykey
    data
    text
    <p>Package: </p> <ul> <li><a href="http://cran.r-project.org/web/packages/raster/index.html" rel="nofollow noreferrer">raster</a></li> </ul> <p>Data:</p> <ul> <li>A rasterStack with 10 bands.</li> <li>Each of the bands contains an image area surrounded by NAs</li> <li>Bands are logical, i.e. "1" for image data and "0"/NA for surrounding area</li> <li>The "image areas" of each band do not align completely with each other, though most have partial overlaps</li> </ul> <p>Objective:</p> <ul> <li>Write a fast function that can return either a rasterLayer or cell numbers for each "zone", for instance a pixel containing data only from bands 1 and 2 falls in zone 1, a pixel containing data only from bands 3 and 4 falls in zone 2, etc. If a rasterLayer is returned, I need to be able to match the zone value with band numbers later.</li> </ul> <p>First attempt:</p> <pre><code># Possible band combinations values = integer(0) for(i in 1:nlayers(myraster)){ combs = combn(1:nlayers(myraster), i) for(j in 1:ncol(combs)){ values = c(values, list(combs[,j])) } } # Define the zone finding function find_zones = function(bands){ # The intersection of the bands of interest a = subset(myraster, 1) values(a) = TRUE for(i in bands){ a = a &amp; myraster[[i]] } # Union of the remaining bands b = subset(myraster, 1) values(b) = FALSE for(i in seq(1:nlayers(myraster))[-bands]){ b = b | myraster[[i]] } #plot(a &amp; !b) cells = Which(a &amp; !b, cells=TRUE) return(cells) } # Applying the function results = lapply(values, find_zones) </code></pre> <p>My current function takes a very long time to execute. Can you think of a better way? Note that I don't simply want to know how many bands have data at each pixel, I also need to know which bands. The purpose of this is to process different the areas differently afterwards. </p> <p>Note also that the real-life scenario is a 3000 x 3000 or more raster with potentially more than 10 bands.</p> <hr> <p>EDIT</p> <p>Some sample data consisting of 10 offset image areas:</p> <pre><code># Sample data library(raster) for(i in 1:10) { start_line = i*10*1000 end_line = 1000000 - 800*1000 - start_line offset = i * 10 data = c(rep(0,start_line), rep(c(rep(0,offset), rep(1,800), rep(0,200-offset)), 800), rep(0, end_line)) current_layer = raster(nrows=1000, ncols=1000) values(current_layer) = data if(i == 1) { myraster = stack(current_layer) } else { myraster = addLayer(myraster, current_layer) } } NAvalue(myraster) = 0 # You may not want to do this depending on your solution... </code></pre> <p><img src="https://i.stack.imgur.com/SuVc5.jpg" alt="Showing what the sample data looks like"></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload