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

Thiago Veloso thi_veloso at yahoo.com.br
Sat Jun 23 00:51:29 CEST 2012


  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.

________________________________
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: Friday, June 22, 2012 3:54 PM
Subject: Re: [R-sig-Geo] Merge rasters with different extent




On Fri, Jun 22, 2012 at 6:06 AM, Thiago Veloso <thi_veloso at yahoo.com.br> wrote:

  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.
>
>

That is correct, and what you are after is  

x <- merge (r2, r1)


>  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)"
>
>

No, it won't, because the data have very different ranges (0 to 1 vs. 0 to 0.02),  If you want something that looks like that you can first scale r2 (or r1), e.g. 

x = merge( r2/50 ,  r1)
plot(x)

Whether this makes sense numerically, I cannot know. 

Robert






  Best regards,
>  Thiago.
>
>
>________________________________
>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: Wednesday, June 20, 2012 1:13 PM
>
>Subject: Re: [R-sig-Geo] Merge rasters with different extent
>
>
>Thiago, 
>As far as I can see it works fine. You have not said what you meant with "without sucess". Perhaps a map of the result does not look good? That would be because the values of the global raster range from 0 to 0.02 whereas the values of the Brazil raster range from 0 to 1. After merging the Brazilian values dominate the legend. 
>Robert
>
>On Wed, Jun 20, 2012 at 3:52 AM, Thiago Veloso <thi_veloso at yahoo.com.br> wrote:
>
>  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