# [R-sig-Geo] Least-cost path in meter

Rafael Wüest rafael.wueest at gmail.com
Fri Nov 8 11:42:22 CET 2013

```Hi Jeremy

You should be able to apply this to your path1 object. It might, in fact, help you to shortcut, as
gdistance::shortestPath allows also coordinate matrices as input AFAIK.

Rafael

On 08.11.13 10:54, Jeremy Larroque wrote:
> I wrote something to compute the along-path distance in meters using the
> shortestPath function.
> It works but it takes too much time. Advice to improve efficiency?
>
> # I create an empty matrix of size n*n (n = number of individuals)
> APD <- matrix(nrow=dim(xy)[1],ncol=dim(xy)[1])
>
> # A loop by row and column
> # I compute the shortestPath for all pairs of individuals
> for (m in 1:dim(xy)[1]) {
>
>      for(j in 1:m) {
>
> path1 <- c("NA")
>
> A <- cbind(xy[m,1],xy[m,2])
> B <- cbind(xy[j,1],xy[j,2])
>
>
> path1 <- try(shortestPath(tr1, A, B, output="SpatialLines"), silent = TRUE)
>
> #try function in cases where individuals have the same location and thus
> the shortestPath function does not work
> if (class(path1)=="try-error") { sumdis <- 0 } else {
>
> #I get the coordinates of the SpatialLines object created
>    coo <- as.data.frame(coordinates(path1))
>    dis <- c()
> #I compute the distance between each successive paires of coordinates
>    for (k in 1:c(dim(coo)[1]-1)) {
>    dis <- c(dis, dist(rbind(coo[k,],coo[k+1,])))
> }
> #I sum the distances to get the along-path distance in meters
> sumdis <- sum(dis)
>
> }
> #I fill the matrix
> APD[m,j] <- sumdis
>
> }
> }
>
>
>
>
>
> 2013/11/6 Jeremy Larroque <larroque.jeremy at gmail.com>
>
>> Hi,
>>
>>
>>
>> setting all the connection to 1 give me the straight line distance between
>> points, not the distance along the least-cost distance path.
>>
>> Maybe a way to get the least-cost path in meters would be to count the
>> number of transitions and multiply it by the resolution of the raster?
>>
>>
>>
>> Thanks,
>>
>>
>>
>> Jeremy
>>
>>
>> 2013/10/24 Jacob van Etten <jacobvanetten at yahoo.com>
>>
>>> Hi Jeremy,
>>>
>>> Here goes a complete example. The function geoCorrection does the work.
>>> It divides any value in the transition matrix by the distance between those
>>> cells (e.g. cell centres). By setting all connections to 1, we get
>>> conductance=1/distance
>>>
>>> ###
>>>
>>> library("gdistance")
>>>
>>> r <- raster(system.file("external/maungawhau.grd",
>>>                          package="gdistance"))
>>> plot(r)
>>> r #units are in meters
>>> tr1 <- transition(r,function(x){1},8) #gives all connection value 1
>>> tr1 <- geoCorrection(tr1) #conductance=1/distance is equivalent to
>>> resistance=distance
>>>
>>> tr2 <- transition(r,function(x){1},16) #more precision
>>> tr2 <- geoCorrection(tr2)
>>>
>>> A <- c(2667670,6479000)
>>> B <- c(2667800,6479400)
>>> cc <- rbind(A,B)
>>> points(cc)
>>>
>>> costDistance(tr1, cc) #454
>>> costDistance(tr2, cc) #431
>>>
>>> #Pythagoras to check
>>> sqrt((B[1] - A[1])^2 + (B[2] - A[2])^2) #421
>>>
>>> ###
>>>
>>> Using more connections (16 instead of 8) gives a better result in this
>>> case.
>>>
>>> The method also works for latlon grids. See ?geoCorrection.
>>>
>>> Jacob.
>>>
>>>
>>>    On Thursday, 24 October 2013, 7:09, Jeremy Larroque <
>>> larroque.jeremy at gmail.com> wrote:
>>>   Hi everyone,
>>>
>>>
>>>
>>> I'm using the gdistance package to compute least-cost distance, i.e. the
>>> cumulative cost of the least-cost path, on a raster file between points
>>> using the costDistance function.
>>>
>>> But does anyone have an idea of how to calculate the distance in meter
>>> along the least-cost path?
>>>
>>>
>>>
>>> Thanks,
>>>
>>>
>>> Jeremy
>>>
>>>      [[alternative HTML version deleted]]
>>>
>>> _______________________________________________
>>> R-sig-Geo mailing list
>>> R-sig-Geo at r-project.org
>>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>>>
>>>
>>>
>>
>
> 	[[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>

--
Rafael Wüest
rafael.wueest at gmail.com
http://www.rowueest.net

```