R: gdistance accCost 和 costDistance 的不同结果
R: gdistance different results for accCost and costDistance
R gdistance 中的 accCost() 和 costDistance() 函数在从源坐标 A 到目标坐标 B 时产生不同的值。B 处的成本累积值不应该等于给定的从 A 到 B 的 costDistance 值一个等效的各向异性过渡矩阵,并且两个函数都使用 Dijkstra 算法?
如果不是,那么计算之间的根本区别是什么?如果是这样,是什么解释了从下面给出的代码中得出的不同值?在示例中,A 到 B 的 costDistance=0.13 小时,accCost=0.11 小时,在 B 点。我的其他测试表明 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)
应该没有区别。当前版本的 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)
}
)
此问题已在 gdistance 1.2-1 中得到解决。
R gdistance 中的 accCost() 和 costDistance() 函数在从源坐标 A 到目标坐标 B 时产生不同的值。B 处的成本累积值不应该等于给定的从 A 到 B 的 costDistance 值一个等效的各向异性过渡矩阵,并且两个函数都使用 Dijkstra 算法?
如果不是,那么计算之间的根本区别是什么?如果是这样,是什么解释了从下面给出的代码中得出的不同值?在示例中,A 到 B 的 costDistance=0.13 小时,accCost=0.11 小时,在 B 点。我的其他测试表明 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)
应该没有区别。当前版本的 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)
}
)
此问题已在 gdistance 1.2-1 中得到解决。