I have two rasters and I want to make the spatial extent of one to another. Then save it as a new raster. I used following code. However, I cannot save the 2013 images with new spatial extent as a new raster. Any guidance is greatly appreciated.
raster_2013 <- raster("avgt2013.tif")
extent(raster_2013)
class : Extent
xmin : 112.91
xmax : 153.64
ymin : -43.75
ymax : -9
> res(raster_2013)
[1] 0.01 0.01
>
> raster_2015 <- raster("avgt2015.tif")
> extent(raster_2015)
class : Extent
xmin : 112
xmax : 154
ymin : -44
ymax : -9
> res(raster_2015)
[1] 0.01 0.01
>
> e <- extent(112, 154, -44, -9)
>
> ex = extent(raster_2015)
> r2 = crop(raster_2013, ex)
>
>
> new_2013 <- alignExtent(e, raster_2013, snap='near')
> str(new_2013)
Formal class 'Extent' [package "raster"] with 4 slots
..@ xmin: num 112
..@ xmax: num 154
..@ ymin: num -44
..@ ymax: num -9
>
> rc <- crop(raster_2013, e, snap='near')
> extent(rc)
class : Extent
xmin : 112.91
xmax : 153.64
ymin : -43.75
ymax : -9
resample
function. You can usewriteRaster
function to save the file to disk. – Geo-spresample
would do the job but would be much more time- and resource-consuming than a simplecrop(extend())
chain given the two rasters are aligned here.resample
should be considered as a brute force method to be used in emergency cases only ;-) – ztl