[R-sig-Geo] gdistance -- How to combine transition Layers?

Thomas Chadefaux chadefaux at gmail.com
Mon Sep 26 17:37:58 CEST 2011

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

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:


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

More information about the R-sig-Geo mailing list