0

I want to create raster from 2 input rasters. how do I get pixel values that are the sum of the pixel values of the input rasters?

So i have classified rasters where non-water pixels are 0 and water pixels in one raster are 1 and in the other 2. So I want to get a new raster where 0 are the pixels where both rasters had non-water, 1 where only the first found water, 2 only the second and value 3 would mean that both rasters classified this pixel as water. Until now what i am doing is simply sum the raster.

library(raster)
reclass_df1 <- c(0, 2160, 1, 2160, Inf, 0)
reclass_df2 <- c(0, 2160, 2, 2160, Inf, 0)
reclass_msen1 <- matrix(reclass_df1, ncol = 3,byrow = TRUE)
reclass_msen2 <- matrix(reclass_df2, ncol = 3,byrow = TRUE)
ras2_1 <- reclassify(ras2[[1]],reclass_msen1)
ras2_1 <- reclassify(ras2[[2]],reclass_msen2)
ras2_combi <- ras2_1 + ras2_2

As output I expected to have values from 0 to 3. so that 0 would be where both rasters had non-water pixels, 1 when only the one raster, 2 the other and 3 when both rasters have water pixels. but i only get values 0,1 and 3 and i doubt that this can be correct.

Chelmy88
  • 1,106
  • 1
  • 6
  • 17
Maria123
  • 13
  • 3
  • What is the ras2 object? it is not defined thus I can't redo your example – Chelmy88 Aug 16 '19 at 08:54
  • oh sorry ras2 is a stack of sentinel1 images, so just formal class raster layer? i would create an example matrix, im just not sure how?! – Maria123 Aug 16 '19 at 10:04
  • Yes would be nice if we have an example we can re-use, can't you just define two matrices with random 0 and 1 and convert is to raster? see https://www.r-bloggers.com/m-x-n-matrix-with-randomly-assigned-01/ and https://stackoverflow.com/questions/14513480/convert-matrix-to-raster-in-r – Chelmy88 Aug 16 '19 at 10:14
  • okay so this is what i did and i think thats already the answer for my problem... r <-10 c <- 10 m1 <- matrix(sample(c(0,1),r*c, replace=TRUE),r,c) m2 <- matrix(sample(c(0,2),r*c, replace=TRUE),r,c) m3 <- m1+m2 m4 <- matrix(m3, ncol = 10,byrow = TRUE) r4 <- raster(m4,xmn=577995, xmx=5780095, ymn=4095315, ymx=4095325, crs=CRS("+proj=utm +zone=10 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0")) – Maria123 Aug 16 '19 at 11:33

0 Answers0