Search code examples
rraster

Resizing a raster to match specific dimensions in R


I want to resize (enlarge in this case) a raster to match the dimensions of another. Using disaggregate I can enlarge the raster, but the resize factor argument only accepts integers, so the output is closer to but not matching the target dimensions. The following example illustrates by trying to resize ras2 to match ras1 dimensions:

> require(raster)
> ras1 = raster(volcano)
> ras2 = aggregate(raster(apply(volcano, 1, rev)), fact=2, FUN=mean) # aggregated & rotated
> extent(ras1) = extent(ras2) = c(0,100,0,100)
> crs(ras1) = crs(ras2) = '+init=epsg:27700'
> fact = c(ncol(ras1) / ncol(ras2), nrow(ras1) / nrow(ras2))
> ras2_resized = disaggregate(ras2, fact, method='')
> dim(ras2_resized)
[1] 93 44  1
> dim(ras1)
[1] 87 61  1

Grateful if anyone knows of a way to specify the dimensions of a resize instead of factor. Thanks in advance.

Bonus for a generalised method that can resizing either way not just enlarging, but this isn't the priority.


Solution

  • The resample function can also be used for this:

     ras2_resized <- resample(ras2, ras1)