Skip to main content
added 8 characters in body
Source Link
MarujoRe
  • 376
  • 2
  • 10
require(raster)

# make 2 dummy rasters, different extents but same origin and resolution. Values 1:6
slope <- raster(xmn = 0, xmx = 5, ymn = 0, ymx = 5, res = 1)
slope[] <- sample(1:6, ncell(slope), replace = T)

soil <- raster(xmn = 0, xmx = 7, ymn = -1, ymx = 4, res = 1)
soil[] <- sample(1:6, ncell(soil), replace = T)

# create your blank raster to populate. This is only the area both inputs occupy.
r_intersec <- raster(raster::intersect(slope,soil))

# The other 2 rasters should be cropped to the extent of your target raster, to avoid extent errors as mentioned in the comments
croped_slope <- crop(slope,extent(r_intersec))
croped_soil <- crop(soil,extent(r_intersec))

tmp <- croped_slope
tmp[which(croped_soil[] <= 0)] <- NA

par(mfrow=c(1,3))
plot(slope)
plot(soil)
plot(tmp)

*raster sample and crop idea from https://gis.stackexchange.com/a/328883/77836

require(raster)

# make 2 dummy rasters, different extents but same origin and resolution. Values 1:6
slope <- raster(xmn = 0, xmx = 5, ymn = 0, ymx = 5, res = 1)
slope[] <- sample(1:6, ncell(slope), replace = T)

soil <- raster(xmn = 0, xmx = 7, ymn = -1, ymx = 4, res = 1)
soil[] <- sample(1:6, ncell(soil), replace = T)

# create your blank raster to populate. This is only the area both inputs occupy.
r_intersec <- raster(intersect(slope,soil))

# The other 2 rasters should be cropped to the extent of your target raster, to avoid extent errors as mentioned in the comments
croped_slope <- crop(slope,extent(r_intersec))
croped_soil <- crop(soil,extent(r_intersec))

tmp <- croped_slope
tmp[which(croped_soil[] <= 0)] <- NA

par(mfrow=c(1,3))
plot(slope)
plot(soil)
plot(tmp)

*raster sample and crop idea from https://gis.stackexchange.com/a/328883/77836

require(raster)

# make 2 dummy rasters, different extents but same origin and resolution. Values 1:6
slope <- raster(xmn = 0, xmx = 5, ymn = 0, ymx = 5, res = 1)
slope[] <- sample(1:6, ncell(slope), replace = T)

soil <- raster(xmn = 0, xmx = 7, ymn = -1, ymx = 4, res = 1)
soil[] <- sample(1:6, ncell(soil), replace = T)

# create your blank raster to populate. This is only the area both inputs occupy.
r_intersec <- raster(raster::intersect(slope,soil))

# The other 2 rasters should be cropped to the extent of your target raster, to avoid extent errors as mentioned in the comments
croped_slope <- crop(slope,extent(r_intersec))
croped_soil <- crop(soil,extent(r_intersec))

tmp <- croped_slope
tmp[which(croped_soil[] <= 0)] <- NA

par(mfrow=c(1,3))
plot(slope)
plot(soil)
plot(tmp)

*raster sample and crop idea from https://gis.stackexchange.com/a/328883/77836

added 361 characters in body
Source Link
MarujoRe
  • 376
  • 2
  • 10
require(raster)

#generate# samplemake 2 dummy rasters, different extents but same origin and resolution. Values 1:6
slope <- raster(matrix(c(10,20,30,40,20,30,40,10,30,40,10,20,40,10xmn = 0,20 xmx = 5,30) ymn = 0,4 ymx = 5,4) res = 1)
plotslope[] <- sample(1:6, ncell(slope), replace = T)

enter image description here

 
soil <- raster(matrix(c(10,10,10,10,10,20,20,10,xmn = 0,0 xmx = 7,0 ymn = -1,0 ymx = 4,0 res = 1)
soil[] <- sample(1:6,0 ncell(soil),0 replace = T)

# create your blank raster to populate. This is only the area both inputs occupy.
r_intersec <- raster(intersect(slope,0soil))

# The other 2 rasters should be cropped to the extent of your target raster,4 to avoid extent errors as mentioned in the comments
croped_slope <- crop(slope,4extent(r_intersec))
plotcroped_soil <- crop(soil,extent(r_intersec))

enter image description here

 
tmp <- slopecroped_slope
tmp[which(soil[]croped_soil[] <= 0)] <- NA 

par(mfrow=c(1,3))
plot(slope)
plot(soil)
plot(tmp)

enter image description here *raster sample and crop idea from https://gis.stackexchange.com/a/328883/77836

require(raster)

#generate sample rasters
slope <- raster(matrix(c(10,20,30,40,20,30,40,10,30,40,10,20,40,10,20,30),4,4))
plot(slope)

enter image description here

soil <- raster(matrix(c(10,10,10,10,10,20,20,10,0,0,0,0,0,0,0,0),4,4))
plot(soil)

enter image description here

tmp <- slope
tmp[which(soil[] <= 0)] <- NA
plot(tmp)

enter image description here

require(raster)

# make 2 dummy rasters, different extents but same origin and resolution. Values 1:6
slope <- raster(xmn = 0, xmx = 5, ymn = 0, ymx = 5, res = 1)
slope[] <- sample(1:6, ncell(slope), replace = T)
 
soil <- raster(xmn = 0, xmx = 7, ymn = -1, ymx = 4, res = 1)
soil[] <- sample(1:6, ncell(soil), replace = T)

# create your blank raster to populate. This is only the area both inputs occupy.
r_intersec <- raster(intersect(slope,soil))

# The other 2 rasters should be cropped to the extent of your target raster, to avoid extent errors as mentioned in the comments
croped_slope <- crop(slope,extent(r_intersec))
croped_soil <- crop(soil,extent(r_intersec))
 
tmp <- croped_slope
tmp[which(croped_soil[] <= 0)] <- NA 

par(mfrow=c(1,3))
plot(slope)
plot(soil)
plot(tmp)

*raster sample and crop idea from https://gis.stackexchange.com/a/328883/77836

added 324 characters in body
Source Link
MarujoRe
  • 376
  • 2
  • 10
require(raster)

#generate sample rasters
slope <- raster(matrix(c(10,20,30,40,20,30,40,10,30,40,10,20,40,10,20,30),4,4))
plot(slope)

enter image description here

soil <- raster(matrix(c(10,10,10,10,10,20,20,10,0,0,0,0,0,0,0,0),4,4))
plot(soil)

enter image description here

tmp <- slope
tmp[which(soil[] <= 0)] <- NA
plot(tmp)

enter image description here

require(raster)

slope <- raster(matrix(c(10,20,30,40,20,30,40,10,30,40,10,20,40,10,20,30),4,4))
soil <- raster(matrix(c(10,10,10,10,10,20,20,10,0,0,0,0,0,0,0,0),4,4))

tmp <- slope
tmp[which(soil[] <= 0)] <- NA
plot(tmp)
require(raster)

#generate sample rasters
slope <- raster(matrix(c(10,20,30,40,20,30,40,10,30,40,10,20,40,10,20,30),4,4))
plot(slope)

enter image description here

soil <- raster(matrix(c(10,10,10,10,10,20,20,10,0,0,0,0,0,0,0,0),4,4))
plot(soil)

enter image description here

tmp <- slope
tmp[which(soil[] <= 0)] <- NA
plot(tmp)

enter image description here

Post Undeleted by MarujoRe
Post Deleted by MarujoRe
Source Link
MarujoRe
  • 376
  • 2
  • 10
Loading