Saturday 28 September 2019

r - Different origin problem while merging rasters


I have two dem files downloaded via the getData function in the raster package. Here you can see the code:


I tried to make the example reproducible so there you have it.


library(raster)

dem_n1<-getData("SRTM",lon=26,lat=43)
dem_n2<-getData("SRTM",lon=31,lat=43)
demALL<-merge(dem_n1,dem_n2)

While attempting to perform the merge, the following error appears:


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

Answer



This works as expected for me:


> demALL

class : RasterLayer
dimensions : 6000, 12000, 7.2e+07 (nrow, ncol, ncell)
resolution : 0.0008333333, 0.0008333333 (x, y)
extent : 25, 35, 40, 45 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0
data source : c:\Temp\R_raster_rhijmans\r_tmp_2015-10-22_102651_340_06019.grd
names : layer
values : -37, 2573 (min, max)

However, there is a small difference in the origin due to floating-point imprecision



 origin(dem_n1)
#[1] 0 0
origin(dem_n2)
#[1] -3.552714e-15 0.000000e+00

but that should not matter


So I wonder if you are using the current versions of R and raster? If you are, you could probably get around this by setting


 rasterOptions(tolerance = 0.1)

(That is actually the default value) or perhaps



.Machine$double.eps <- 0.000000001

or something like that


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...