【问题标题】:Distance between two farthest points in a raster栅格中两个最远点之间的距离
【发布时间】:2021-07-27 06:49:20
【问题描述】:

我有一个地理区域作为栅格文件,我想恢复栅格单元位置之间的最长距离。换句话说,最长的轨迹。 (位置由单元格的中心表示)。可以在 R 中做吗?

【问题讨论】:

  • 请添加minimal reproducible example。谢谢
  • @ava 我只想计算栅格对象中任意两个像元之间的最大距离,但我不知道该怎么做。
  • 你想要的不是那么清楚(“任意两个单元格”,还是“最远点”?)。请非常具体。并且请添加一个最小的、独立的、可重现的示例 ----就像在 R 帮助文件中一样;来说明你的追求。
  • @RobertHijmans 我编辑了我的帖子我不知道现在是否更清楚了。
  • 非常模糊。一个栅格有很多像元。哪些细胞。请包括一个像r <- raster(ncol=10, nrow=10) 等的例子,然后用它来确切地解释你想做什么以及答案应该是什么。

标签: r raster euclidean-distance


【解决方案1】:

冒着浪费时间的风险,因为没有可重复的示例来检查需要什么以及答案是否正确,这里有一些 R 代码来做我认为需要做的事情。基本上,找到栅格的边缘,因为最大距离将涉及边缘。从那里,找到所有边缘点对之间的距离,以确定全局的最大距离。

library(raster)
library(rgdal)
library(rgeos)

# A reproducible raster file
f <- system.file("external/test.grd", package="raster")
r <- raster(f)
    
# Find the edge of the raster values
b <- boundaries(r)
b[b==0] <- NA
b <- trim(b)
    
# Find the distances between pairs of points 
# using the suggestion from the comments
# so that lon lat data would also work
gd <- pointDistance(rasterToPoints(b)[,1:2], lonlat=FALSE)

# Find the maximum row and column and the points these correspond to
maxpair <- which(gd==max(gd), arr.ind=T)
firstpoint <- df[maxpair[1],]
secondpoint <- df[maxpair[2],]
    
# Plot things
plot(r)
points(firstpoint, col='red', cex=4, pch=10)
points(secondpoint, col='red', cex=4, pch=10)

【讨论】:

  • 非常好。我会使用 gd &lt;- pointDistance(rasterToPoints(b)[,1:2], lonlat=FALSE) 以便它可以适应 lonlat 数据。
  • @RobertHijmans - 好建议。我已经更改了答案以反映它。
猜你喜欢
  • 2010-12-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多