2

I have a binary raster in R. It has only 55 cells with value 1 and all the others have value 0 or NA. Now I want to write 55 rasters. For each cell with value 1, I want to write one raster. In these final rasters I only want to have one cell with value 1. My Idea is to loop through all the cells of the input-raster and in case it finds a cell with value 1 write a new raster with only that cell beeing of value 1 and all others of value 0 or NA.

The input raster looks like this:

class      : RasterLayer 
dimensions : 1482, 1274, 1888068  (nrow, ncol, ncell)
resolution : 12.5, 12.5  (x, y)
extent     : 347279.6, 363204.6, 6255207, 6273732  (xmin, xmax, ymin, ymax)
crs        : +proj=utm +zone=19 +south +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0 
source     : memory
names      : raster
values     : 0, 1  (min, max)

My idea would lood kind of like this:

for (i in 1:length(raster){
    if (raster[i] == 1){
    name = paste0("rastercell_", i, ".tif")
    ras = raster(...)
    writeRaster(ras, name)
    }
}

It's just that I don't know how to create the raster (ras = raster()) from scratch with only that single cell being of value 1 and all others 0 or NA.

  • 1
    Did you use the python tag by mistake or are you willing to use Python to accomplish this as well? – Marcelo Villa-Piñeros Mar 2 at 15:27
  • sorry the confusion. This was due to my mixed up head.. – Robin Kohrs Mar 2 at 15:33
  • 1
    This workflow is unique! Could you please describe what your ultimate goal is? Perhaps we can recommend a different approach. – Aaron Mar 2 at 15:40
  • My goal is to run a module from SAGA GIS to model the runout length for debris flows. This model takes as input a raster with release cells. And I wanted a single result for each cell;) Don't know if this helped... – Robin Kohrs Mar 2 at 16:56
3

It seems like there might be a better solution to the basic issue, but without details on what that is, we can only solve the stated problem. So let us do that...
Let us start by reducing the length of the loop. There is absolutely no reason to iterate over all the 0-pixels, when you only want the 1s.

targetPixels <- which(values(inputRaster)==1)

From there, we go onto the loop:

for (i in targetPixels){
  if (inputRaster[i] == 1){
    name <- paste0("rastercell_", i, ".tif")
    ras <- inputRaster*0   # creating a raster that is entirely 0
    ras[i] <- 1            # putting in the single 1
    writeRaster(ras, name) # writing the raster
  }
}
| improve this answer | |
  • thank you very much!! Already helped a lot!! – Robin Kohrs Mar 2 at 15:34
  • @RobinKohrs - don't forget to upvote / accept answers that provide you with the requested information. :) – Mikkel Lydholm Rasmussen Mar 2 at 15:38
  • 1
    right right sorry about that. Completely forgot it... – Robin Kohrs Mar 2 at 15:39
2

Just for reference, here is another way to do this. Functionally, I create a vector of cell ids that have the desired value in the original raster. Then I replicate the raster into a stack and iterate through each raster in the stack, turning everything but the single index cell location to zeros.

First, create some example data

library(raster)
r <- raster(nrow=20,ncol=20)
  r[] <- rep(0,ncell(r))
    r[sample(1:ncell(r),5)] <- 1

Then, create a cell index and a stack of the replicated raster. The idx vector represents all of the cells in the raster and is used to assign zeros.

n = which(r[] == 1)
  idx <- 1:ncell(r)
    r <- stack(replicate(length(n), r))

for(i in 1:length(n)) { 
  r[[i]][idx[-n[i]]] <- 0
}

plot(r)
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.