栅格中两个最远点之间的距离

Distance between two farthest points in a raster

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

冒着浪费时间的风险,因为没有可重现的例子来检查需要什么以及答案是否正确,这里有一些 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)