【问题标题】:Having trouble with paths returned by flowPath in raster R-package光栅 R 包中 flowPath 返回的路径有问题
【发布时间】: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 应该返回的内容的误解。我预计液滴在下坡时可能会遵循什么样的路径。像这样:

Expected flowPath

这是使用简单的最速下降步行计算得出的。但是,如果(如受访者 Hijmans 所述)flowPath 按预期工作,那么我可能需要找到另一个函数来提供液滴沿着下坡移动的路径。

【问题讨论】:

    标签: r r-raster


    【解决方案1】:

    为什么路径不正确?对我来说看起来不错。通过聚合和标记来说明。

    library(raster)
    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)
    r <- aggregate(r, 25) * 10
    
    r.fd <- terrain(r, opt='flowdir')
    r.p <- flowPath(r.fd, cbind(-1,0))
    p.xy <- xyFromCell(r.fd,r.p)
    
    plot(r)
    lines(p.xy,col='green', lwd=2)
    text(r)
    

    添加一些噪音以获得更曲折的路径

    set.seed(01234)
    r <- rasterFromXYZ(df, crs=proj)
    r <- aggregate(r, 10) * 10
    r <- r + runif(ncell(r), 1, 2)
    r.fd <- terrain(r, opt='flowdir')
    r.p <- flowPath(r.fd, cbind(-1,0))
    p.xy <- xyFromCell(r.fd,r.p)
    
    plot(r)
    lines(p.xy,col='green', lwd=2)
    

    【讨论】:

    • 感谢您的及时回复,我希望有一条路径遵循 rasterVis 文档 (q.v.) 中矢量图输出中的流线型。在这种情况下,我希望 flowPath 更像我添加的那个我上面的问题。
    猜你喜欢
    • 2011-05-11
    • 1970-01-01
    • 2020-03-05
    • 1970-01-01
    • 2012-12-23
    • 1970-01-01
    • 2011-08-01
    • 2021-04-16
    • 1970-01-01
    相关资源
    最近更新 更多