2

Introduction

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.

Aim

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

Background

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

## EXAMPLE ##

library(gdistance)

# 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
plot(raster(conduct))

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

Issue

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

Question:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.