Hi Thomas,

Currently, max() is not implemented in gdistance, because it is not implemented in Matrix.

But there are ways to work around these limitations.

See the example below, based on your self contained example.

Meanwhile, I will think about a more elegant and permanent solution.

Jacob.

###################

require(raster)
require(gdistance)

#Create sample rasters
roads <- raster(nrows=10, ncols=10)
altitude <- raster(nrows=10, ncols=10)
roads <- setValues(roads, round(runif(10*10)) )  ; plot(roads)
altitude <- setValues(altitude, runif(10*10))        ; plot(altitude)

#Define transition layers
tr.roads <- transition(roads, transitionFunction=function(x) x[1]*x[2]*60, 8)  #  driving at 60km/h is possible iff both cells are roads (is this correct)?
#Yes, this is a sensible approach.
tr.alt <- transition(altitude, transitionFunction=function(x) abs(x[1]-x[2]), 8, symm=F)  #just a sample function.
#Take a look a the vignette for a more realistic (but not state-of-the-art) approach.

#Now how to combine them by choosing the "fastest" transportation method?
tr.stacked <- max(tr.roads, tr.alt)  #does not work

#Alternative approach using indexing
adjacencyFromTransition <- function(transition) #Probably a function I need to include in the package
{
    transitionMatr <- as(transition,"sparseMatrix")
    transition.dgT <- as(transitionMatr,"dgTMatrix")
    adjacency <- cbind(transition.dgT@i+1,transition.dgT@j+1)
    return(adjacency)
}

adj <- adjacencyFromTransition(tr.roads)

#Now use the index together with pmax to do what you want
tr.alt[adj] <- pmax(tr.roads[adj], tr.alt[adj])

###############



________________________________
From: Thomas Chadefaux <chadefaux@gmail.com>
To: r-sig-geo@r-project.org
Sent: Monday, 26 September 2011, 17:37
Subject: [R-sig-Geo] gdistance -- How to combine transition Layers?

Dear all,

This is my first post here, so I thank you in advance for your help.

My goal is to calculate the cost of traveling (friction) between two
geographical points, given underlying "friction" rasters.

Suppose for example that my data is composed of two rasters, one
binary for roads (a given cell has a road or not), and another,
continuous, for, say, altitude. Using the "gdistance" package, one can
calculate a transition function between cells. The resulting
transitionLayers can be combined by addition or multiplication (see
http://cran.r-project.org/web/packages/gdistance/vignettes/gdistance-vignette.pdf).

However, what I would need is to combine them using some kind of max
function. In other words, the transition to use would be roads if
there is one, otherwise by foot (in which case the altitude transition
layer should be used). Here is a working example that shows where I
get stuck:


require(raster)
require(gdistance)

#Create sample rasters
roads <- raster(nrows=10, ncols=10)
altitude <- raster(nrows=10, ncols=10)
roads <- setValues(roads, round(runif(10*10)) )  ; plot(roads)
altitude <- setValues(altitude, runif(10*10))        ; plot(altitude)

#Define transition layers
tr.roads <- transition(roads, transitionFunction=function(x)
x[1]*x[2]*60, 8)  #  driving at 60km/h is possible iff both cells are
roads (is this correct)?
tr.alt <- transition(altitude, transitionFunction=function(x)
x[1]-x[2], 8, symm=F)  #just a sample function.

#Now how to combine them by choosing the "fastest" transportation method?
tr.stacked <- max(tr.roads, tr.alt)  #does not work

Thank you very much in advance.
Thomas

_______________________________________________
R-sig-Geo mailing list
R-sig-Geo@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
	[[alternative HTML version deleted]]

