Crop a Geospatial Raster in R
Today you will learn how to crop a raster into a vector. In this task, we will use the raster and rgdal packages. Let’s load them (read here). Then we load a raster (in the example we use Sentinel-2 satellite imagery) into the variable r, which we will crop (read here). We display the loaded raster (read here):
We load the polygon to be clipped into the variable border (read here) and display it on the raster:
We crop the raster with the polygon using the crop function from the raster package:
r2 <- crop(r,border)
We display the result:
The raster has been cropped to the extent of the polygon only. To mask the pixels outside the polygon, you still need to use the mask function:
r3 <- mask(r2,border)
Show the result:
As a result of our action, we have obtained a raster that is clipped exactly to our polygon.