Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- library(sp)
- library(raster)
- # Create Raster
- r <- raster(ncol=40, nrow=20)
- r[] <- rnorm(n=ncell(r))
- # Create Polygon
- a <- seq(-150,150,1)
- b <- 50*a^2/(150^2)
- df <- data.frame(a=a,b=b)
- srl <- Polygon(df, hole=as.logical(NA))
- Srl <- Polygons(list(srl), 1)
- poly <- SpatialPolygons(list(Srl), pO = 1:length(Srl), proj4string=CRS(as.character(NA)))
- # Plot raster and polygon
- plot(r)
- plot(poly, add=T)
- # Mask and plot
- rr <- mask(r, poly)
- plot(rr)
- # The previous code snippet must be executed prior to this one
- e1 <- c(-180,180,180,-180)
- e2 <- c(90,90,-90,-90)
- df2 <- data.frame(e1=e1,e2=e2)
- srl2 <- Polygon(df2, hole=F)
- Srl2 <- Polygons(list(srl2), 2)
- poly2 <- SpatialPolygons(list(Srl2))
- srl3 <- Polygon(df)
- Srl3 <- Polygons(list(srl3), 1)
- poly3 <- SpatialPolygons(list(Srl2,Srl3))
- hole <- poly
- polyX <- poly2
- coordsHole <- hole@polygons[[1]]@Polygons[[1]]@coords
- newHole <- Polygon(coordsHole,hole=TRUE)
- listPol <- polyX@polygons[[1]]@Polygons
- listPol[[length(listPol)+1]] <- newHole
- punch <- Polygons(listPol,polyX@polygons[[1]]@ID)
- new <- SpatialPolygons(list(punch),proj4string=poly@proj4string)
- plot(r)
- plot(new, col="white", border="NA", add=T)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement