r - 具有多个点的最低成本路径

标签 r gis

我正在尝试将双变量位置点连接在一起以形成一条路线。我的观点是指鱼的位置,所以我需要只穿过水而不是周围土地的路径。基本上,我需要进行多个最低成本路径分析并完全加入它们。我想在 R 中执行此操作,因为我在 R 中拥有比 ArcGIS、python、modelbuilder 等更多的经验。

我使用 ArcGIS 创建了一个成本面,水编码为 0,土地编码为“NoData”,并将其导入到 R 中。

我曾尝试使用 gdistance pkg 中的 shortestPath,但是当我尝试在两点之间运行它时,R 总是对我关闭。例如:

costpath=shortestPath(costtrans,c29924[1,],c29924[2,],output="SpatialLines")

其中我的成本面是“costtrans”,“c29924”是一个 SpatialPointsDataFrame 与我的纬度/经度位置。我计划运行一个循环,以便我可以对数据框中的每一行执行此操作。但是,我不知道为什么 R 不能很好地处理一次迭代。在将我的成本表面转换为第一次争论的过渡对象时,我确实收到以下警告消息:
Warning messages:
1: In array(ans, c(len.a%/%d2, d.ans), if (!all(vapply(dn.ans, is.null,  :
Reached total allocation of 6057Mb: see help(memory.size)
2: In array(ans, c(len.a%/%d2, d.ans), if (!all(vapply(dn.ans, is.null,  :
Reached total allocation of 6057Mb: see help(memory.size)
3: In as.vector(transition.values) :
Reached total allocation of 6057Mb: see help(memory.size)
4: In as.vector(transition.values) :
Reached total allocation of 6057Mb: see help(memory.size)
5: In .TM.repl.i.mat(as(x, "TsparseMatrix"), i = i, value = value) :
number of items to replace is not a multiple of replacement length

任何解决此问题的建议,或实现我最初目标的其他方法,将不胜感激!

最佳答案

可以使用 gdistance 做您想做的事包裹。这可能是您的栅格大小的问题(即它对内存来说太大了),在这种情况下,您可以使用 aggregate() 对其进行放大。来自 raster包裹。如另一条评论中所述,您的陆地和海洋参数化也可能存在问题。

这是我认为您想要实现的示例(如下)。我将土地参数化为高成本障碍(=10000 成本单位),将海洋参数化为无障碍(=1 成本单位)。另请注意,我取逆来生成电导表面。如果您想要位置之间路径的长度,可以使用 costDistance() 来完成。这将为您提供以栅格为单位的地理路径长度的结果。

library(gdistance)

## Create cost surface where "land" exists in the middle
cost <- raster(nrow=100, ncol=100, 
           xmn=0, xmx=100, ymn=0, ymx=100, crs="+proj=utm")
cost[] <- 1
cost[cellFromRowColCombine(cost, 50:55,20:80)] <- 10000

## Produce transition matrices, and correct because 8 directions
trCost <- transition(1/cost, mean, directions=8)
trCost <- geoCorrection(trCost, type="c")

## Create three points (representing three points in time series)
pts <- cbind(x=c(20, 60, 40), y=c(80, 60, 20))

## Display results
plot(cost)
plot(SpatialPoints(pts), add=TRUE, pch=20, col="red")
text(pts[,1]+2, pts[,2]+2, 1:nrow(pts))
plot(shortestPath(trCost, pts[1,], pts[2,], output="SpatialLines"), add=TRUE)
plot(shortestPath(trCost, pts[2,], pts[3,], output="SpatialLines"), add=TRUE)

Example plot of least-cost paths around land

关于r - 具有多个点的最低成本路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9605827/

相关文章:

R 数据帧错误 - 替换有 1 行,数据有 0

r - 如何最好地处理将大型本地数据帧转换为 SparkR 数据帧?

python - 如何通过 PyQGIS 使用 cpt-city 目录中的颜色渐变

javascript - 谷歌地图或其他服务获取指定范围内的地点列表?

r - 使用 knitr --without Rstudio 编译 *.Rnw 文件

r - 如何在 R 中打印双引号 (")

r - ggplot : scales package中的欧元符号

python - 从时间序列中插值单个值

r - 同时通过 coord_sf 更改 geom_sf 和 geom_point 的投影时出错

java - 国家边界坐标数据