【发布时间】:2023-04-03 15:13:02
【问题描述】:
raster 包中 flowPath 函数返回的路径由平行于 x 轴和 y 轴的线段组成。
从 rasterVis 文档 (https://oscarperpinan.github.io/rastervis/) 中的 Vector Field Plots 示例开始,我尝试从表面上的起点找到流路,但路径输出不正确。
library(raster)
library(rasterVis)
proj <- CRS('+proj=longlat +datum=WGS84')
df <- expand.grid(x = seq(-2, 2, .01), y = seq(-2, 2, .01))
df$z <- with(df, (3*x^2 + y)*exp(-x^2-y^2))
r <- rasterFromXYZ(df, crs=proj)
# Up to this point we follow the example in the rasterVis documentation
# Now attempt to find the path from a point on the surface
contour(r$z)
r.fd<-terrain(r,opt='flowdir')
r.c<-cellFromXY(r,cbind(-1,0))
r.p<-flowPath(r.fd,r.c)
p.xy<-xyFromCell(r.fd,r.p)
lines(p.xy,col='green')
Flow path from point (-1,0) depicting undesired behavior.
正如您在上面看到的,流动路径通过向 +x 移动,然后向 -y 移动,在大约 (0,-.8) 处达到最小值。我一直无法构建一个不存在此问题的数据集。但是:flowPath 文档中包含的示例(在 raster 包中,使用 volcano 数据)产生了人们可能期望的输出,但没有表现出这个问题。
我做错了什么,无法扩展 rasterVis 文档中的示例?
附录:我质疑输出的原因可能更多是对 flowPath 应该返回的内容的误解。我预计液滴在下坡时可能会遵循什么样的路径。像这样:
这是使用简单的最速下降步行计算得出的。但是,如果(如受访者 Hijmans 所述)flowPath 按预期工作,那么我可能需要找到另一个函数来提供液滴沿着下坡移动的路径。
【问题讨论】: