首页 > 解决方案 > 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)

结果 <code>shortestPath</code> 输出

标签: rgeospatialspatialr-rastersp

解决方案


我找到了 GRASS GIS(在 R 中使用rgrass7) r.drain 函数或raster::flowPath实现了我在上述问题中尝试做的事情。


推荐阅读