0

I have three partially overlapping DEM rasters with different origins, resolutions (only slightly different) and extents. I know I need to use terra's resample function rather than merge or aggregate because of the different origins, etc., but I'm not sure how to initiate an empty raster to use for resampling that has an appropriate origin, resolution and extent, or what to do about overlapping areas.

Are there recommended approaches for selecting which origin and resolution to use of the existing rasters (e.g. median vs min/max values)? How are overlapping data best handled? Is there an efficient way to generate such a raster?

ia200
  • 255
  • 1
  • 9
  • You could, at minimum, share some info about your dems, `vol <- rast(volcano)`, `origin(vol)` `> vol class : SpatRaster dimensions : 87, 61, 1 (nrow, ncol, nlyr) resolution : 1, 1 (x, y) extent : 0, 61, 0, 87 (xmin, xmax, ymin, ymax) coord. ref. : source : memory name : lyr.1 min value : 94 max value : 195`, for each dem to help people think about suggestions. And bilinear. – Chris Apr 05 '22 at 20:23

1 Answers1

1

Here are ways that show how you might do that.

Example data

library(terra)
x <- rast(xmin=0, xmax=10, ymin=0, ymax=10, res=1, vals=1)
y <- rast(xmin=9, xmax=19, ymin=0, ymax=10, res=0.9, vals=2)
z <- rast(xmin=0, xmax=10, ymin=8.1, ymax=18.1, res=1, vals=3)

You may want to use one of the input rasters as template. Let's say you like y. In that case:

a <- list(x, z)
b <- lapply(a, \(i) {
     x <- extend(rast(y), i)
     resample(i, crop(x, i, "out"))
   })

b <- sprc(c(b, y))
m <- merge(b)
 

Or create a new template raster. In that case, first find out the combined extent

a <- list(x, y, z)
b <- sapply(a, \(i) ext(i) |> as.vector())
e <- ext(min(b[1,]), max(b[2,]), min(b[3,]), max(b[4,]))

# use the extent to create a raster with the desired spatial resolution 
r <- rast(e, res=1)

And now as above:

gg <- lapply(a, \(i) resample(i, crop(r, i, "out")))
g <- merge(sprc(gg))

Or like this

src <- sprc(a)
ss <- impose(src, r)
s <- max(ss, na.rm=TRUE)

I suppose some of this could be wrapped into a terra method.

Also see mosaic as an alternative to merge.

As for choosing the best resolution etc., there it is up to your needs and what your data might reasonably support. But one important general consideration is that you want to avoid resampling as much as possible --- as it deteriorates the data quality.

Robert Hijmans
  • 40,301
  • 4
  • 55
  • 63
  • Thanks for the two alternate approaches. I was considering using one raster as a template so I'll try that method first. Would you expect any memory/processing issues implimenting either method on larger rasters (25 - 50GB)? Terra has generally been great with large files (so thank you for this feature!) but I'm curious if this holds for resampling or merging rasters. – ia200 Apr 05 '22 at 22:59
  • @RobertHijmans What does \ i mean here? R considers this as an error. – Vijay Ramesh Apr 14 '22 at 18:14
  • In more recent versions of R `\(i)` is equivalent to `function(i)` – Robert Hijmans Apr 14 '22 at 19:49