Skip to content

Merge rasters with different extent

7 messages · Thiago V. dos Santos, Robert J. Hijmans

#
?Dear list,

?I have two rasters (r1 and r2). r1 has larger extent; r2 is smaller and its extents fall totally within r1. Here are the rasters:
class ? ? ? : RasterLayer?
dimensions ?: 360, 720, 259200 ?(nrow, ncol, ncell)
resolution ?: 0.5, 0.5 ?(x, y)
extent ? ? ?: -180, 180, -90, 90 ?(xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0?
values ? ? ?: in memory
layer name ?: layer?
min value ? : 0?
max value ? : 0.055233?
class ? ? ? : RasterLayer?
dimensions ?: 47, 65, 3055 ?(nrow, ncol, ncell)
resolution ?: 0.5, 0.5 ?(x, y)
extent ? ? ?: -74.5, -42, -18, 5.5 ?(xmin, xmax, ymin, ymax)
coord. ref. : NA?
values ? ? ?: in memory
layer name ?: layer?
min value ? : 0?
max value ? : 1?

?What I would like to be able to do is merge both rasters, but keeping r2 values when cells overlap. In other words, replace r1 values with r2 values only when cells overlap. Following older discussions, I have tried "merge" command without sucess. Neither "r3 <- merge(r1,r2)" nor "r3 <- merge(r2,r1)" yield what I pursue.?

?Any ideas?

?Many thanks in advance,

?Thiago.
#
? Robert,

?Thanks for the help.?

? Yes, I just checked again. Let me share the files I am working with so everyone can reproduce the problem:?https://www.dropbox.com/s/56ltwigore1n1wv/gflvp.1700.nc?(larger extent, 1021kb) and?https://www.dropbox.com/s/69qmko0kc6muguo/defor_latlon.img?(smaller extent, 40kb).

? Output of session info is below.

? Best regards,
? Thiago.
Platform: x86_64-pc-linux-gnu (64-bit) locale: [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8     [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8    [7] LC_PAPER=C                 LC_NAME=C                  [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C        attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods  
[7] base      other attached packages:
[1] rgdal_0.7-11  ncdf_1.6.6    raster_2.0-04 sp_0.9-99     loaded via a namespace (and not attached):
[1] grid_2.15.0    lattice_0.20-6 tools_2.15.0
1 day later
#
? Robert,

? Thanks for this discussion. Here in my pc I cannot see both rasters merged. Even after the merge operation, scale of result map range from 0 to 0.2 if I type "merge (r1,r2)" or 0 to 1 if I type "merge (r2,1)" - considering that r1 is the global one.


? What I expected to see, and please correct me if I am wrong, was a final map that looked pretty much like the result of

?"plot (r1)"
?"plot (r2, add=T)"

? Best regards,
? Thiago.
#
? Robert,

? I am very sorry for this mistake! I could only notice what you talking about when I wrote the raster to a netcdf and visualized it using other software and color scale. You were right: merge has worked and I misinterpreted the result.


? Time to consider wearing glasses or analysing rasters in other way apart from graphically!

? Best wishes,
? Thiago.