This question is indirectly related to a previous post of mine (Calculating accumulated walking pace from starting location outwards using R?), but asks guidance about a different aspect, that's why I am creating a new post.

I have put togheter some doce (below) to provide a reproducible example.


Calculating cumulative cost-surface expressing walking time from a given location outwards.


I have followed the code provided by this source (https://mran.microsoft.com/snapshot/2014-11-17/web/packages/gdistance/vignettes/gdistance.pdf) to calculate an accumulated cost surface from a location outwards, using the 'gdistance' package. The cost surface has to represent walking time according to the Tobler's hiking function (https://en.wikipedia.org/wiki/Tobler%27s_hiking_function).



# create the data to work with
dtm <- raster(system.file("external/maungawhau.grd", package="gdistance"))
A <- c(2667670,6479000)

# define the Tobler's function, re-expressed not in Kmh but in meters per hour
tobler.mperh <- function(x){ (6 * exp(-3.5 * abs(x + 0.05))) * 1000 }

altDiff <- function(x){x[2] - x[1]}
hd <- transition(dtm, altDiff, 8, symm=FALSE)
slope <- geoCorrection(hd,scl=FALSE)

adj <- adjacent(x=dtm, cells=1:ncell(dtm), direction=8)
speed <- slope
speed[adj] <- tobler.mperh(slope[adj]) #this should be speed in m/h

#sanity check for speed:
plot(raster(speed)) #flat areas should feature around 5000 m/h

#geocorrection divides speed by distance (i.e., cells' centers)
conduct <- geoCorrection(speed, scl=FALSE) # conductance = speed/distance = 1/travel time

cost_surface <- accCost(conduct, A)
plot(cost_surface)      # = 1/travel time  

Workflow followed

The abovementioned PDF was implementing Tobler's function not to create an accumulated cost surface but to calculate shortest path. With reference to the code above, I am rather confused about how to work out travel time from the last result of the code, i.e. the accumulated cost surface.

In other words:

all seems fine till the calculation of the speed (meters per hour) data (see code flagged by #sanity check for speed). The speed raster I got (see below) seems to make sense, since flat areas feature a speed of about 5000 m/h (i.e., 5 Kmh) as predicted by the Tobler's function.

enter image description here

The above is confirmed by the following chart of speed vs slope: enter image description here

The subsequent operation that uses geocorrection divide the speed (m/h) by the distance (i.e., distance between cell's center), so producing what is called conductance. The latter (according to the abovementioned PDF) is equal to 1/travel time.

The accumulated conductance looks like the image below (see plot(cost_surface) # = 1/travel time):

enter image description here


In an attempt to devise the travel time (tv), I simple calculated the reciprocal of the accumulated cost, the latter being based on the conductance data. But taking the inverse produces a raster that seems to make no sense:

tv <-cost_surface
values(tv) <- 1/values(cost_surface)

enter image description here


Is the issue caused by including 0 values into the calculation of the reciprocal?

1 Answer 1


Extracted from "R Package gdistance: Distances and Routes on Geographical Grids" (Jacob van Etten)

What do these values mean? The function geoCorrection divides the values in the matrix between the distance between cell centres. So, with our last command we calculated this: conductance = speed/distance This looks a lot like a measure that we are more familiar with: traveltime = distance/speed In fact, the conductance values we have calculated are the reciprocal of travel time. 1/traveltime = speed/distance = conductance

So, just invert the last formula, so: traveltime = 1/conductance

tv <- raster(conduct)
values(tv) <- 1/values(raster(conduct))


You should see something like this (conductance): enter image description here

...to this (traveltime): enter image description here

Here I wrote a post about the topic with other examples and layouts. There a final test of the outputs in order to see if travel times are as expected.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.