R:gdistance accCost和costDistance的不同结果

时间:2016-04-09 22:55:19

标签: r gis distance

当从源坐标A到目标坐标B时,来自R gdistance的

accCost()和costDistance()函数产生不同的值。不应该将B处的成本累加值等于给定的A到B的costDistance值一个等效的各向异性转移矩阵,两个函数都使用Dijkstra算法?

如果没有,那么计算之间的根本区别是什么?如果是这样,那么从下面给出的代码中得出的不同值是什么?在该示例中,A到B的costDistance = 0.13小时,并且在B点的accCost = 0.11小时。我的其他测试表明,accCost始终低于costDistance,并且在长距离上也是如此。该代码基于accCost文档中提供的示例。

require(gdistance)
r <- raster(system.file("external/maungawhau.grd", package="gdistance"))
altDiff <- function(x){x[2] - x[1]}
hd <- transition(r, altDiff, 8, symm=FALSE)
slope <- geoCorrection(hd)
adj <- adjacent(r, cells=1:ncell(r), pairs=TRUE, directions=8)
speed <- slope
speed[adj] <- 6 * 1000 * exp(-3.5 * abs(slope[adj] + 0.05))#1000 to convert to a common spatial unit of meters
Conductance <- geoCorrection(speed)
A <- matrix(c(2667670, 6479000),ncol=2)
B <- matrix(c(2667800, 6479400),ncol=2)
ca <- accCost(Conductance,fromCoords=A)
extract(ca,B)
costDistance(Conductance,fromCoords=A,toCoords=B)

2 个答案:

答案 0 :(得分:1)

应该没有区别。当前版本的accCost有一个小错误,它是由igraph包的变化引起的。

目前,请查看此功能是否解决了问题。

setMethod("accCost", signature(x = "TransitionLayer", fromCoords = "Coords"),
 def = function(x, fromCoords)
  {
    fromCoords <- .coordsToMatrix(fromCoords) 
    fromCells <- cellFromXY(x, fromCoords)
    if(!all(!is.na(fromCells))){
        warning("some coordinates not found and omitted")
        fromCells <- fromCells[!is.na(fromCells)]
    }
    tr <- transitionMatrix(x)
    tr <- rBind(tr,rep(0,nrow(tr)))
    tr <- cBind(tr,rep(0,nrow(tr)))

    startNode <- nrow(tr) #extra node to serve as origin
    adjP <- cbind(rep(startNode, times=length(fromCells)), fromCells)

    tr[adjP] <- Inf

    adjacencyGraph <- graph.adjacency(tr, mode="directed", weighted=TRUE)
    E(adjacencyGraph)$weight <- 1/E(adjacencyGraph)$weight      

    shortestPaths <- shortest.paths(adjacencyGraph, v=startNode, mode="out")[-startNode]

    result <- as(x, "RasterLayer")
    result <- setValues(result, shortestPaths)  
    return(result)
  }
)

答案 1 :(得分:0)

此问题已在gdistance 1.2-1中解决。