I would like to change the resolution of a raster. For example, let’s take this Landsat 7 images at ~ 30m resolution.
library(terra)
#> terra 1.5.21
f <- system.file("tif/L7_ETMs.tif", package = "stars")
r <- rast(f)
# 30m x 30m resolution
res(r)
#> [1] 28.5 28.5
plot(r, 1)
I can use aggregate()
with an integer factor such as:
# 10 * 28.5
r2 <- aggregate(r, fact = 10)
res(r2)
#> [1] 285 285
plot(r2, 1)
My question is, how can I specify an exact resolution. For example, I would like to have a pixel resolution of 1.234 km (1234 m).
fact <- 1234 / 28.5
fact
#> [1] 43.29825
r3 <- aggregate(r, fact = fact)
res(r3)
#> [1] 1225.5 1225.5
plot(r3, 1)
The documentation says that fact
should be an integer, so here it is
flooring fact
to 43.
res(aggregate(r, 43))
#> [1] 1225.5 1225.5
Any ways to have an exact resolution of 1234 m?
Created on 2022-04-28 by the reprex package (v2.0.1)
aggregate
implies that you want a fractional number of cells in your resulting raster, which doesn't really make sense. Of course, it should be possible if cropping were allowed, but that option doesn't seem to be included. I guess you could crop the raster to a multiple offact
then aggregate it?r
be cropped using a factor? Would it be possible to create a grid of the desired resolution before and "translate"r
into it?