r - R - 用 gdistance 模拟地表水流的过渡函数
问题描述
gdistance
shortestPath
我正在尝试使用该函数对从指定原点到单个下坡目标点的陆上(地表)水流进行建模。我需要帮助来定义合适transitionFunction
的,因为我需要确保成本最低的路径只允许水沿着路径流动到与前一个单元格相等或小于值的高程单元格。在下面transitionFunction
的示例中,选择了最小高程单元格,但根据transitionFunction
我定义的,此值可能仍大于先前的单元格值。
我意识到,当上述定义为我想要的时,路径可能会在到达目标点之前终止。这很好,尽管如果可能的话,我希望能够保留从原点到它终止的任何地方的路径。
另外,如果有人知道能够对这种东西进行建模的不同 R 包,请告诉我。
library(gdistance)
library(raster)
library(elevatr)
library(sp)
#load example DEM raster
data(lake)
elevation <- get_elev_raster(lake, z = 9)
#remove negative elevation values from raster
elevation[elevation < 0] <- NA
#create origin and goal points with same projection as elevation raster
origin <- SpatialPoints(cbind(1790000, 640000), proj4string = CRS("+proj=aea +lat_1=20 +lat_2=60 +lat_0=40 +lon_0=-96 +x_0=0 +y_0=0 +datum=NAD83 +units=m +no_defs +ellps=GRS80 +towgs84=0,0,0"))
goal <- SpatialPoints(cbind(1820000, 540000), proj4string = CRS("+proj=aea +lat_1=20 +lat_2=60 +lat_0=40 +lon_0=-96 +x_0=0 +y_0=0 +datum=NAD83 +units=m +no_defs +ellps=GRS80 +towgs84=0,0,0"))
#create df data and convert to SpatialPointsDataFrame
odf <- data.frame("flowreg" = 1)
gdf <- data.frame("flowreg" = 2)
origindf <- SpatialPointsDataFrame(origin, odf)
goaldf <- SpatialPointsDataFrame(goal, gdf)
trCost1 <- transition(elevation, transitionFunction=function(x) 1/min(x), directions=8)
trCost1gc <- geoCorrection(trCost1, type="c")
plot(raster(trCost1))
sPath1 <- shortestPath(trCost1, origin, goal,
output="SpatialLines")
plot(elevation)
plot(origindf, add = TRUE, col='red', cex = 5)
plot(goaldf, add = TRUE, col='green', cex = 5)
lines(sPath1)
解决方案
我找到了 GRASS GIS(在 R 中使用rgrass7
) r.drain 函数或raster::flowPath
实现了我在上述问题中尝试做的事情。
推荐阅读
- locust - 如何在 Locust 中配置日志记录?
- elasticsearch - Elasticsearch 应该使用 must_not - 非嵌套
- javascript - 欢迎留言 Discord.js
- python - 检查字符串是否包含连续子字符串的最佳方法?
- java - 如何使用映射器翻译变量类型?(DTO 到 DAO)
- python - 尝试将两个浮点数相加时,我得到一个“TypeError:+= 不支持的操作数类型:'float' 和 'list'”?
- javascript - 在 React 中导出和导入 const
- python - 如何将列表作为一组打印?
- c++ - C++ 中的扩展内联汇编:是否有必要保留易失性寄存器?
- python - 使用 networkx 和 numpy 绘制累积分布