[R-sig-Geo] Merge rasters with different extent

Thiago Veloso thi_veloso at yahoo.com.br
Wed Jun 20 12:52:49 CEST 2012


  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.


> sessionInfo() R version 2.15.0 (2012-03-30)
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   

________________________________
From: Robert J. Hijmans <r.hijmans at gmail.com>
To: Thiago Veloso <thi_veloso at yahoo.com.br> 
Cc: R-SIG list <r-sig-geo at r-project.org> 
Sent: Tuesday, June 19, 2012 3:43 PM
Subject: Re: [R-sig-Geo] Merge rasters with different extent


Thiago, 

Are you sure? It works well in this example:

library(raster)
r <- raster(ncol=100, nrow=100)
r1 <- crop(r, extent(-10, 11, -10, 11))
r2 <- crop(r, extent(0, 20, 0, 20))
r3 <- crop(r, extent(9, 30, 9, 30))

r1[] <- 1:ncell(r1)
r2[] <- 1:ncell(r2)
r3[] <- 1:ncell(r3)

m1 <- merge(r1, r2, r3, fun=mean)
m2 <- merge(r3, r2, r1, fun=mean)
plot(stack(m1, m2))


If your case (small rasters) a work-around (that ignores the possibility of NA values in r2) would be

cells <- cellFromXY(r1,  xyFromCell(r2, 1:ncell(r2)))
r1[cells] <- values(r2)

Robert


On Tue, Jun 19, 2012 at 9:20 AM, Thiago Veloso <thi_veloso at yahoo.com.br> wrote:

 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:
>
>> r1
>
>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 
>
>> r2
>
>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.
>
>
>_______________________________________________
>R-sig-Geo mailing list
>R-sig-Geo at r-project.org
>https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>  



More information about the R-sig-Geo mailing list