在R{spatstat}中,如何计算被不规则多边形窗口限制的点之间的欧几里得距离?

5

我正在尝试计算两个点之间的欧几里德距离,但这两个点被一个不规则多边形所限制(也就是说,距离必须按照给定窗口的路径计算)。

以下是一个可重现的示例:

library(spatstat)

#Simple example of a polygon and points.
ex.poly <- data.frame(x=c(0,5,5,2.5,0), y=c(0,0,5,2.5,5))
points <- data.frame(x=c(0.5, 2.5, 4.5), y=c(4,1,4))

bound <- owin(poly=data.frame(x=ex.poly$x, y=ex.poly$y))

test.ppp <- ppp(x=points$x, y=points$y, window=bound)

pairdist.ppp(test.ppp)#distance between every point
#The distance result from this function between point 1 and point 3, is given as 4.0

然而,仅从绘制这些点来看,我们就能知道
plot(test.ppp)

如果路径限制在多边形内,则距离应更大(在这种情况下为5.00)。

是否有我不知道的 {spatstat} 中的另一个函数可以做到这一点?或者有没有其他建议可以使用另一个软件包来完成这个问题?

我正在尝试找到水体中两点之间的距离,因此我的实际数据中的不规则多边形更加复杂。

非常感谢任何帮助!

谢谢!


有趣的问题。我建议将 bound 转换为 RasterLayer 对象(可能首先使用 maptools 提供 as(bound, "SpatialPolygons")as(test.ppp, "SpatialPoints")),然后使用 gdistance 包计算点之间的“最小成本距离”,其中 bound 外所有网格点的摩擦或成本设置为无穷大。gdistance 附带了一个很好的文档(运行 vignette("gdistance") 查看),这应该会给你一个良好的开端。 - Josh O'Brien
1个回答

7

好的,在昨天的评论中我提到了一种基于gdistance的方法。由于它计算的路径段都受限于棋盘上16个方向之一(国王走法和马走法),所以它不是完美的。尽管如此,对于您示例中的三个配对距离,它的结果与正确值只有2%的误差(总是稍微高估)。

library(maptools)  ## To convert spatstat objects to sp objects
library(gdistance) ## Loads raster and provides cost-surface functions

## Convert *.ppp points to SpatialPoints object
Pts <- as(test.ppp, "SpatialPoints")

## Convert the lake's boundary to a raster, with values of 1 for
## cells within the lake and values of 0 for cells on land
Poly <- as(bound, "SpatialPolygons")           ## 1st to SpatialPolygons-object
R <- raster(extent(Poly), nrow=100,  ncol=100) ## 2nd to RasterLayer ...
RR <- rasterize(Poly, R)                       ## ...
RR[is.na(RR)]<-0                               ## Set cells on land to "0"

## gdistance requires that you 1st prepare a sparse "transition matrix"
## whose values give the "conductance" of movement between pairs of
## adjacent and next-to-adjacent cells (when using directions=16)
tr1 <- transition(RR, transitionFunction=mean, directions=16)
tr1 <- geoCorrection(tr1,type="c")

## Compute a matrix of pairwise distances between points
## (These should be 5.00 and 3.605; all are within 2% of actual value).  
costDistance(tr1, Pts)
##          1        2
## 2 3.650282         
## 3 5.005259 3.650282

## View the selected paths
plot(RR)
plot(Pts, pch=16, col="gold", cex=1.5, add=TRUE)
SL12 <- shortestPath(tr1, Pts[1,], Pts[2,], output="SpatialLines")
SL13 <- shortestPath(tr1, Pts[1,], Pts[3,], output="SpatialLines")
SL23 <- shortestPath(tr1, Pts[2,], Pts[3,], output="SpatialLines")
lapply(list(SL12, SL13, SL23), function(X) plot(X, col="red", add=TRUE, lwd=2))

enter image description here


很棒的使用gdistance的例子!我也在尝试使用凸包,但这个方法更好!谢谢! - user3389288
一切都按照需要的方式运行,只是绘制以查看所选路径时出现了以下错误消息:> SL12 <- shortestPath(tr1, Pts[1,], Pts[2,], output="SpatialLines") Error in validObject(.Object) : invalid class “CRS” object: invalid object for slot "projargs" in class "CRS": got class "logical", should be or extend class "character" - user3389288
如果没有你的空间数据对象在我面前,我将无法提供太多帮助,只能建议你仔细检查tr1Pts附加的投影元数据,使用proj4string()crs()identicalCRS()等函数。如果它们不匹配,你可能需要将你的点投影到栅格的CRS或采取其他繁琐的步骤来使它们匹配。祝你好运! - Josh O'Brien

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接