4

I am trying to merge seven tiles of raster in R version 4.0.3 but it shows error as:

Error in compareRaster(x, extent = FALSE, rowcol = FALSE, orig = TRUE,  : 
  different origin.

The origin of raster are:

> origin(f1)
[1] 1.549293 0.000000
> origin(f2)
[1] 1.666904 2.306145
> origin(f3)
[1]  0.02155916 -0.67734774
> origin(f4)
[1] 2.921843 4.800345
> origin(f5)
[1] -2.036317 -1.870532
> origin(f6)
[1] 3.293903 4.393251
> origin(f7)
[1] -0.3293477 -0.2457405

and I tried tolerance=0.1 but its still showing the same error. The R code is here:

library(raster)
merge_forest <- raster::merge(f1,f2,f3,f4,f5,f6,f7,tolerance=0.1)

These are the raster tiles of landcover(https://www.arcgis.com/apps/instant/media/index.html?appid=fc92d38533d440078f17678ebc20e8e2&fbclid=IwAR3Y3SN7iyaPrzViPzagVH086dZTygvmwoIdZrudbb4EJlJyZOf6zdA13g4).

I downloaded seven tiles that covers the area of Norway and wanted to merge it together and clip it to the extent of Norway.

4
  • 1
    You should show us the function call that created the error (I guess merge(f1,f2,f3,...)?) and complete code (including library(raster) so we know what merge you are using) and more information about your data than just the origin (summary is usually a good idea). You can only merge if data has the same origin and resolution. Yours don't.
    – Spacedman
    Commented Aug 5, 2021 at 11:21
  • Thank you. I have edited the question. Commented Aug 6, 2021 at 19:18
  • Wrong function, you want do.call(mosaic, list(f1, f2, f3, f4, f5, f6, f7)) Commented Aug 6, 2021 at 20:49
  • Tried but got the same error. merge_forest <-do.call(mosaic,list(f1,f2,f3,f4,f5,f6,f7)) Error in compareRaster(x, extent = FALSE, rowcol = FALSE, orig = TRUE, : different origin. Commented Aug 7, 2021 at 18:00

3 Answers 3

10

The terra package has a new object class SpatRasterCollection for image collections than can be used with terra::mosaic or terra::merge. This greatly simplifies mosaic operations. Here is an example where we: 1) create a vector of raster files; 2) use lapply to create raster objects wrapped in terra::src to create a SpatRasterCollection; 3) pass the image collection to the terra::mosaic function.

library(terra)
img <- list.files(getwd(), "tif$", full.names=TRUE) # the $ exludes aux-files
  ic <- sprc(lapply(img, rast))
    r <- mosaic(ic)
2
  • 1
    I believe the "sarimg" should be img in order to work. I'm editing, but feel free to change back if I'm missing something.
    – Smerla
    Commented Feb 22, 2022 at 20:20
  • No need to change back but, the original "sarimg" object was just a vector of raster file names with full paths. It could have been anything that was not an existing R function or object. Commented Apr 4 at 22:24
2

Lets consider only tile 32V and 32W.

I tried to create a R script to mosaic those tiles, but it takes too long to compile because of the resample process (10m resolution for Norway extent)...

So I used GDAL:

gdalbuildvrt mosaic.vrt 32V_20200101-20210101.tif 32W_20200101-20210101.tif

Where mosaic.vrt is the output and 32V_20200101-20210101.tif 32W_20200101-20210101.tif are the inputs (you can include all 7 tiles).

gdal_translate mosaic.vrt MOSAIC.tif

Where mosaic.vrt is the input (previously created) and MOSAIC.tif is the output in tif format.

I opened the MOSAIC in R:

library(raster)

Mosaic <- "MOSAIC.tif"
RasterM <- raster(Mosaic)
plot(RasterM)

enter image description here

Check if this is what you are looking for. I can share my R script but I don't know the final result because it takes a lot of time to compile.

2
  • Thank you for the information. I will have a look at this. Commented Aug 13, 2021 at 7:31
  • Great, fast, clean and working perfectly thanks :)
    – Dorian Grv
    Commented Apr 23, 2023 at 12:39
1

You stand a better chance of getting the right solution or help by providing the code you used and some details of your raster data. Without it is difficult to be of help.

Without knowing the purpose of the merge, the solution below might be of help

merged_f <- merge(f1 ... f7, fun = ) #insert the function you want to use e.g sum, mean etc

If the purpose is to create a mosaic (considering the extent of the rasters) then below by SpatialProgramming (see https://stackoverflow.com/questions/50740097/merge-rasters-of-different-extents-sum-overlapping-cell-values-in-r), might help

merged_f <- mosaic(f1 ... f7, fun = )
6
  • Thank you for the feedback. I have edited the question. Commented Aug 6, 2021 at 19:18
  • You should use mosaic instead of merge as noted in the solutions I gave and pointed out by Jeffrey Evans. You can read the raster package manual for more details on the differences between mosaic and merge functions. see rdocumentation.org/packages/raster/versions/3.4-13/topics/… Commented Aug 7, 2021 at 15:23
  • I did it and the error is still the same. merge_forest <-do.call(mosaic,list(f1,f2,f3,f4,f5,f6,f7)) Error in compareRaster(x, extent = FALSE, rowcol = FALSE, orig = TRUE, : different origin. In the RDocumentation link you have provided, it says all objects must have the same origin. Commented Aug 7, 2021 at 17:56
  • if you google the error message you should find some solutions that you can try. I don't have your data or anything similar hence difficult to offer any advice. I googled the error and found some plausible solutions even on this platform. See gis.stackexchange.com/questions/224781/… Commented Aug 7, 2021 at 18:29
  • you can try the solution in this link as well asktowolrd.com/geographic-information-systems/… Commented Aug 7, 2021 at 18:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.