Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Differences between raster::merge and terra::merge & NA handling #160

Closed
vvirkki opened this issue Feb 24, 2021 · 6 comments
Closed

Differences between raster::merge and terra::merge & NA handling #160

vvirkki opened this issue Feb 24, 2021 · 6 comments

Comments

@vvirkki
Copy link

vvirkki commented Feb 24, 2021

Hello,

I am composing global rasters from small parts using ideally merge. I am running terra 1.0.10.

I've noticed some difference between raster and terra: in raster::merge, the parameter overlap controls how NA values should be handled and this seems not to be available in terra::merge. See below for a reprex adapted from terra manual.

There are two issues in total:

  • terra::merge lacks support for discarding NA values
  • when merging, terra::merge orders the rasters from bottom to top in left-to-right order whereas raster::merge does it right-to-left. In my opinion, terra version is more logical (first raster left is the bottom raster) but a note about this could be added to documentation.

Thanks for superb development with terra; it's incredibly fast and versatile!

# create data
x <- rast(xmin=-110, xmax=-50, ymin=40, ymax=70, ncols=60, nrows=30)
y <- rast(xmin=-80, xmax=-20, ymax=60, ymin=30)
res(y) <- res(x)

# insert values and some NA
vx <- 1:ncell(x)
vx[floor(runif(100, min = 1600, max = 1800))] <- NA
vy <- 1:ncell(y)
vy[floor(runif(100, min = 0, max = 200))] <- NA
values(x) <- vx
values(y) <- vy

plot(c(x,y))

image

# do merge with raster - discards NA and sets last non-NA value as result by default
raster_merge <- raster::merge(raster::raster(x), raster::raster(y))
plot(raster_merge)

image

# do merge with terra - y value overrides x value even though y would be NA
terra_merge <- terra::merge(x, y)
plot(terra_merge)

image

# terra merge behaviour corresponds to raster::merge with overlap = FALSE
rm_no_overlap <- raster::merge(raster::raster(x), raster::raster(y), overlap = FALSE)
plot(rm_no_overlap)

image

# flipping y and x provides exact same result
rm_no_overlap <- raster::merge(raster::raster(y), raster::raster(x), overlap = FALSE)
plot(rm_no_overlap)

image

@Rapsodia86
Copy link

Hi,
I agree that option to handle NA would be appreciated.
However, I have just explored your example and the expected result can be achieved using terra::mosaic with fun=max.
Of course, I am aware that using other function instead is not a solution.
terra_mosaic <- terra::mosaic(x, y,fun="max")
plot(terra_mosaic)
Rplot

@rhijmans
Copy link
Member

rhijmans commented Feb 24, 2021

Thank you for reporting this bug.

terra::merge lacks support for discarding NA values

This has already been fixed in the development version available here (currently version 1.1.2)

terra::merge orders the rasters from bottom to top in left-to-right order (raster used the opposite order) ... a note about this could be added to documentation.

terra now behaves like raster --- the idea is the earlier layers are "more important", but I understand why you think the opposite could be more logical. I will keep it as it is now (like raster). The documentation already states that "If objects overlap, the values get priority in the same order as the arguments" .

terra::mosaic

Indeed a good work-around in the CRAN version

@vvirkki
Copy link
Author

vvirkki commented Feb 25, 2021

Thanks for providing the workaround and addressing the comments! I ended up doing my task via data frames, after all, but good to know that the merge/mosaic workflows would be as applicable.

@HassanMasoomi
Copy link

HassanMasoomi commented Dec 6, 2022

I have large number of raster files (with large sizes). The merge function in terra fails when using the code below (with this error: "Error in rc@ptr$merge(opt) : std::bad_alloc"), but it works with the same function in raster package (although very slow).

x <- list(r1, r2)
# add arguments such as filename
x$filename <- 'test.tif'
m <- do.call(merge, x)

@kadyb
Copy link
Contributor

kadyb commented Dec 7, 2022

@HassanMasoomi, have you tried using SpatRasterCollection sprc()?

ls <- list(r1, r2)
ras_coll <- sprc(ls)
m <- merge(ras_coll)

Or vrt() on files and then writeRaster()?

@HassanMasoomi
Copy link

@kadyb, yes, I did try SpatRasterCollection and it had the same issue. But, haven't tried vrt().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants