[R-sig-Geo] Specify merging mode

Robert J. Hijmans r.hijmans at gmail.com
Wed Mar 31 21:24:50 CEST 2010


Etienne,

This is an interesting problem. I do not know what is going on, but
have insufficient time to study this right now.

This slightly adjusted code works in unexpected ways:

fmerge <- function(rasters, fun, ...){
	ex <- raster(unionExtent(rasters))	
	res(ex) <- res(rasters[[1]])
	for( i in 1:length(rasters) )
		rasters[[i]] <- merge(rasters[[i]], ex)
	fun(rasters[[1]], rasters[-1], na.rm=TRUE)  # to force the first
argument to be a RasterLayer object
}

rfmn <- fmerge(rasters, fun=mean, na.rm=T)   #works
rfx <- fmerge(rasters, fun=max, na.rm=T)   #wrong
fun = max
rfx <- fmerge(rasters, fun=max, na.rm=T)   #works
rfn <- fmerge(rasters, fun=min, na.rm=T)   #works, but still returns max !
fun = min
rfn <- fmerge(rasters, fun=min, na.rm=T)   #works

So something or other that is different in the environment of the
function fmerge...  I suspect it has something to do with mean being a
normal generic while min and max are primitive functions and part of a
group-generic.


This is an alternative approach that I believe does work:

fmerge <- function(rasters, fun, ...){
	ex <- raster(unionExtent(rasters))
	res(ex) <- res(rasters[[1]])
	for( i in 1:length(rasters) )
	  	rasters[[i]] <- merge(rasters[[i]], ex)
	rasters <- stack(rasters)
	fun(rasters, ...)
}

rfm <- fmerge(rasters, mean, na.rm=T)  #ok
rfx <- fmerge(rasters, max, na.rm=T)   #wrong


Also, I have now added a function 'mosaic' in raster (version 1.0.0-6)
that does this (merging with a function). mosaic() should make the
above functions redundant, but I would like to figure out what's going
on nevertheless. Ideas, anyone?

Robert







1.0.0-6

On Tue, Mar 30, 2010 at 8:32 AM, Etienne Bellemare Racine
<etiennebr at gmail.com> wrote:
> A correction to my previous example, a line was missing :
>
> Thanks (again) Robert,
>
> From what you showed, I tried to wrap the operations in a generic function
> for my own purpose, but I don't know why, I can only apply some functions
> e.g. a mean function is ok, but not a max function. Could you point what's
> wrong ? What's the better way to apply a function to a raster list ? My
> problem is that I have a variable number of rasters to merge.
>
> r <- raster()
> r1 <- crop(r, extent(-10, 10, -10, 10))
> r2 <- crop(r, extent(0, 20, 0, 20))
> r3 <- crop(r, extent(10, 30, 10, 30))
>
> r1[] <- 1:ncell(r1)
> r2[] <- 1:ncell(r2)
> r3[] <- 1:ncell(r3)
> rasters <- list(r1, r2, r3)
>
> fmerge <- function(rasters, fun, ...){
>  ex <- raster(unionExtent(rasters))
>  res(ex) <- res(rasters[[1]])
>  ex[] <- NA
>    for( i in 1:length(rasters) )
>      rasters[[i]] <- merge(rasters[[i]], ex)
>    do.call( fun, c(rasters, ...) )
> }
>
> rfm <- fmerge(rasters, mean, na.rm=T)  #ok
> rfx <- fmerge(rasters, max, na.rm=T)   #wrong
>
> Etienne
>>
>> Robert J. Hijmans wrote :
>>>
>>> Dear Etienne,
>>>
>>> This is not in raster (as a single function) but it is a good idea
>>> that I'll look at (and merge needs to be improved for speed also). A
>>> function that would blend overlapping layers based on distance to
>>> their edges would also be useful.
>>>
>>> Here are two ways in which you can accomplish what you asked for:
>>>
>>> r <- raster()
>>> r1 <- crop(r, extent(-10, 10, -10, 10))
>>> r2 <- crop(r, extent(0, 20, 0, 20))
>>>
>>> r1[] <- 1:ncell(r1)
>>> r2[] <- 1:ncell(r2)
>>>
>>> # the trick, first merge with empty RasterLayers
>>> r1 <- merge(r1, raster(r2))
>>> r2 <- merge(raster(r1), r2)
>>>
>>> # Now combine with a function
>>> rm = mean(r1, r2, na.rm=TRUE)
>>> plot(rm)
>>>
>>>
>>> # alternative solution:
>>>
>>> r <- raster()
>>> r1 <- crop(r, extent(-10, 10, -10, 10))
>>> r2 <- crop(r, extent(0, 20, 0, 20))
>>> r1[] <- 1:ncell(r1)
>>> r2[] <- 1:ncell(r2)
>>>
>>> x = (r1 + r2) / 2   # this returns the spatial intersection or r1 & r2
>>> rm = merge(x, r1, r2)
>>> plot(rma)
>>>
>>> Robert
>>>
>>>
>>> On Mon, Mar 29, 2010 at 1:39 PM, Etienne Bellemare Racine
>>> <etiennebr at gmail.com> wrote:
>>>
>>>>
>>>> Hi list,
>>>>
>>>> In the raster package, is it possible to specify which way I want to
>>>> blend the two rasters. e.g. :
>>>>
>>>> r1 <- raster(xmn = -10, ymn = -10, xmx = 10, ymx = 10)
>>>> r2 <- raster(xmn = 0, ymn = 0, xmx = 20, ymx = 20)
>>>>
>>>> r1[] <- 1:ncell(r1)
>>>> r2[] <- 1:ncell(r2)
>>>>
>>>> rm <- merge(r1, r2)
>>>> plot(rm)
>>>>
>>>> You get the first raster on top (r1), but I would like to merge them
>>>> with a function.
>>>>
>>>> I would like to use something like e.g. fun = mean or max. If it's not
>>>> in the raster package, is there any package that allow to merge images
>>>> and specify the way it merge.
>>>>
>>>> Thanks,
>>>> Etienne
>>>>
>>>>       [[alternative HTML version deleted]]
>>>>
>>>> _______________________________________________
>>>> R-sig-Geo mailing list
>>>> R-sig-Geo at stat.math.ethz.ch
>>>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>>>>
>>>>
>



More information about the R-sig-Geo mailing list