To change raster values under SpatialPoints
you can simply use [
.
r <- raster(system.file("external/test.grd", package="raster"))
rTp <- rasterToPoints(r, spatial = T)
set.seed(666)
rTpS <- rTp[sample(1:length(rTp), 500),]
plot(r)
plot(rTpS, add = TRUE, pch = ".")
Now, the raster values beneath the spatial points are changed like this:
r1 <- r
r1[rTpS] <- 99999
plot(r1)
Raster with SpatialPoints (left) and raster with changed values (right):
Thus, I assumed that polygons work the same way. Therefore generate some polygons from this raster data set:
rst <- as.integer(stretch(r, 0, 10))
rTpol <- rasterToPolygons(rst, dissolve = T)
rTpol <- rTpol[rTpol$layer > 3,]
plot(r)
plot(rTpol, add = T)
rst[rTpol[,]] <- 100
plot(rst)
# also tried
# rst[rTpol] <- 100
However, when i Try to change the raster values below some SpatialPolygons
, somehow the ID (or something) of the polygons. Yet, it should be 100.
Raster with SpatialPolygons (left) and raster with strangely changed values (right):
Therefore, my question is how to change raster values within polygons.