【问题标题】:Finding euclidean distance in R{spatstat} between points, confined by an irregular polygon window在 R{spatstat} 中查找点之间的欧几里德距离,由不规则多边形窗口限制
【发布时间】:2014-05-21 18:19:53
【问题描述】:

我试图找到两点之间的欧式距离,由不规则多边形限制。 (即,必须将距离计算为通过给定窗口的路线)

这是一个可重现的例子:

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") 来查看它)应该会给你一个好的开始。

标签: r polygon euclidean-distance spatstat


【解决方案1】:

好的,这是我昨天在 cmets 中提到的基于 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))

【讨论】:

  • 使用 gdistance 的好例子!我也试图找出一种使用凸包的方法,但这要好得多!谢谢!
  • 一切都按需要工作,只是查看所选路径的绘图给了我以下错误消息:&gt; SL12 &lt;- 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"
  • 如果没有您面前的空间数据对象,我将无法为您提供太多帮助,只能建议您仔细检查 tr1Pts 附带的投影元数据,使用proj4string()crs()identicalCRS() 等。如果它们不匹配,您可能需要将您的点投影到栅格的 CRS 或采取其他一些繁琐的步骤来使它们匹配。祝你好运!
猜你喜欢
  • 1970-01-01
  • 2011-01-29
  • 2019-07-10
  • 1970-01-01
  • 2012-07-03
  • 1970-01-01
  • 1970-01-01
  • 2018-09-23
  • 1970-01-01
相关资源
最近更新 更多