Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- library(raster)
- library(sp)
- # create rasters and store them in a list
- r1 <- raster(xmn=1, xmx=5, ymn=1, ymx=5, nrows=4, ncols=4)
- r1[] <- 1:length(r1)
- # create SpatialPolygonsDataFrame
- Sr1 = Polygon(cbind(c(1,5,4,1,1),c(1,2,5,4,1)))
- Sr2 = Polygon(cbind(c(2,4,3,2),c(3,2,4,3)))
- SpP = SpatialPolygons(list(Polygons(list(Sr1), "s1"), Polygons(list(Sr2), "s2")),
- 1:2)
- dat = data.frame(ID = c("s1", "s2"), value = c("a", "b"))
- row.names(dat) <- c("s1", "s2")
- p <- SpatialPolygonsDataFrame(SpP, data = dat,
- match.ID = TRUE)
- AddHoleToPolygon <-function(poly,hole){
- # invert the coordinates for Polygons to flag it as a hole
- coordsHole <- hole@polygons[[1]]@Polygons[[1]]@coords
- newHole <- Polygon(coordsHole,hole=TRUE)
- # punch the hole in the main poly
- listPol <- poly@polygons[[1]]@Polygons
- listPol[[length(listPol)+1]] <- newHole
- punch <- Polygons(listPol,poly@polygons[[1]]@ID)
- # make the polygon a SpatialPolygonsDataFrame as the entry
- new <- SpatialPolygons(list(punch),proj4string=poly@proj4string)
- new <- SpatialPolygonsDataFrame(new,data=as(poly,"data.frame"))
- return(new)
- }
- punchedPoly <-AddHoleToPolygon(p[1,],p[2,])
- p1 <- rbind(p, punchedPoly, makeUniqueIDs = TRUE)
- p1 <- p1[2:3,]
- masked_hole <- mask(r1, p1)
- plot(masked_hole)
- m3 <- mask(r1, p1[c(2,1),])
- plot(m3)
- r2 <- rasterize(p1, r1, field = "value")
- plot(r2)
- r3 <- rasterize(p1[c(2,1),], r1, field = "value")
- plot(r3)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement