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.
python
tag by mistake or are you willing to use Python to accomplish this as well?