How to cut a brick with a polygon?

I have a brick file of bioclim variables, the brick was combined with four bricks from 30sec, so it is a bit large. I would like to get a brick file of my research area, cut it, using the polygon as a border. What should I do? Otherwise, if it is impossible to do with a brick, can I do it with a raster?

Thanks in advance ~

Marco

+4
source share
1 answer

Check extent() if you want to trim the brick with a smaller rectangle. Also drawExtent() if you prefer to select by clicking.

EDIT: Since you used the terms "cut" and "mask", I'm not sure I understood correctly, but here are two ways that might help. You can even use both.

 # an example with dimensions: 77, 101, 3 (nrow, ncol, nlayers) myGrid_Brick <- brick(system.file("external/rlogo.grd", package="raster")) # a simple polygon within those dimensions myTriangle_P <- Polygon(cbind(c(10, 80, 50, 10), c(10, 20, 65, 10))) myTriangle_Ps <- Polygons(list(myTriangle_P), "fubar") myTriangle_SP <- SpatialPolygons(list(myTriangle_Ps)) myTriangle_Ras <- rasterize(myTriangle_SP, myBrick) # this will crop a brick to minimal rectangle that circumscribes the polygon # extent(myCrop) is smaller than extent(myGrid) but no values are changed myCrop_Brick <- crop(myGrid_Brick, myTriangle_SP) # while this converts every coordinate that is NA in # the mask to become NA in the returned brick # while leaving the brick extent unchanged myMask_Brick <- mask(myGrid_Brick, myTriangle_Ras) 
+4
source

Source: https://habr.com/ru/post/1339297/


All Articles