[R-sig-Geo] gdistance: costDistance with barriers

Stachelek, Joseph jstachel at sfwmd.gov
Thu Mar 12 21:12:03 CET 2015


Hi Karl,

I think the underlying shortest.paths algorithm is wrapping around the x dimension of the raster. I was able to fix by remove vertices in the underlying adjacency matrix.

library(gdistance)
r <- raster(nrows=18, ncols=36)
r[] <- 1
t <- transition(r,function(x) 1/mean(x),4)
p <- cbind(seq(-120,120,by=40),rep(0,7))
costDistance(t,p)


#my code
source("gdistance_source/internal-functions.R")
x<-t
fromCoords<-toCoords<-p

fromCoords <- .coordsToMatrix(fromCoords)
toCoords <- .coordsToMatrix(toCoords)

fromCells <- cellFromXY(x, fromCoords)
toCells <- cellFromXY(x, toCoords)

##
costDist <- matrix(NA, nrow=length(fromCoords[,1]),ncol=length(toCoords[,1]))
rownames(costDist) <- rownames(fromCoords)
colnames(costDist) <- rownames(toCoords)
y <- transitionMatrix(x)
#if(isSymmetric(y)) {m <- "undirected"} else{m <- "directed"}
adjacencyGraph <- graph.adjacency(y, mode="directed")
E(adjacencyGraph)$weight <- 1/E(adjacencyGraph)$weight

uniqueFromCells <- unique(fromCells)
uniqueToCells <- unique(toCells)

extent(r)
removepnts <- cbind(rep(180,181),seq(-90,90,by=40))
removecells <- cellFromXY(x, removepnts)
adjacencyGraph<-delete.vertices(adjacencyGraph,removecells)

shortest.paths(adjacencyGraph, v=uniqueFromCells, to=uniqueToCells, mode="out", algorithm="dijkstra")








-----Original Message-----
From: R-sig-Geo [mailto:r-sig-geo-bounces at r-project.org] On Behalf Of karljarvis
Sent: Thursday, March 12, 2015 2:36 PM
To: r-sig-geo at r-project.org
Subject: [R-sig-Geo] gdistance: costDistance with barriers

Hi all,
I am seeing confusing behavior from the costDistance function in gdistance.
In general, when cost is constant, cost distance increases linearly with
actual distance. However, in this example, it is not doing that for the
longest few distances. Am I missing something?
Karl

r <- raster(nrows=18, ncols=36)
r[] <- 1
t <- transition(r,function(x) 1/mean(x),4)
p <- cbind(seq(-120,120,by=40),rep(0,7))
costDistance(t,p)

> costDistance(t,p)
   1  2  3  4  5  6
2  4
3  8  4
4 12  8  4
5 16 12  8  4
6 16 16 12  8  4
7 12 16 16 12  8  4



--
View this message in context: http://r-sig-geo.2731867.n2.nabble.com/gdistance-costDistance-with-barriers-tp7587891.html
Sent from the R-sig-geo mailing list archive at Nabble.com.

_______________________________________________
R-sig-Geo mailing list
R-sig-Geo at r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-geo


We value your opinion. Please take a few minutes to share your comments on the service you received from the District by clicking on this link<http://my.sfwmd.gov/portal/page/portal/pg_grp_surveysystem/survey%20ext?pid=1653>.



More information about the R-sig-Geo mailing list