我试图用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)

发布于 2018-12-16 15:58:18
我发现GRASS (在R中使用rgrass7访问)、r.drain函数或raster::flowPath实现了我在上述问题中试图做的事情。
https://stackoverflow.com/questions/53547179
复制相似问题