[GIS] Cropping raster to same extent using R

cliprraster

S2_1 <- crop(S2_1, area)

In general it seems that this command in R works properly. But after comparing the extent of my area and S2_1- file I realised that they have not exactly the same extent.

> area@extent
class       : Extent 
xmin        : 556161.6 
xmax        : 604551.6 
ymin        : 5305990 
ymax        : 5359290 

> S2_1@extent
class       : Extent 
xmin        : 556160 
xmax        : 604550 
ymin        : 5305990 
ymax        : 5359290 

What can be a reason? and what can I do to avoid this? because I need to stack them 🙂

Best Answer

This might be due to imprecesision within the data that you end up with cells cut in half or something.

When you use the crop function you can use the optional argument "snap" to align your extent to the nearest border (snap="near") or adjust the size to full cells(snap="in" or snap="out"). Like your 1.6 Extent would then be put to 1 or 2 depending on the used function.

If this does not work you can force the extent with the alignExtent function.

alignExtent(extent, object, snap='near')
Related Question