在R {spatstat}中找到点之间的欧几里得距离,该距离受不规则多边形窗口限制 [英] Finding euclidean distance in R{spatstat} between points, confined by an irregular polygon window

查看:132
本文介绍了在R {spatstat}中找到点之间的欧几里得距离,该距离受不规则多边形窗口限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找到两点之间的欧几里得距离,该距离受不规则多边形限制. (即距离必须计算为通过给定窗口的路线)

I'm trying to find the euclidean distance between two points, confined by an irregular polygon. (ie. the distance would have to be calculated as a route through the window given)

以下是可重现的示例:

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

但是我们仅从绘制点就知道

However we know just from plotting the points

plot(test.ppp)

将路线限制为多边形时的距离应更大(在这种情况下为5.00).

that the distance when the route is confined to the polygon should be greater (in this case, 5.00).

在{spatstat}中我不知道还有另一个函数可以执行此操作吗?还是有人对其他可以做到这一点的软件包有其他建议?

Is there another function that I am not aware of in {spatstat} that would do this? Or does anybody have any other suggestions for another package that could do this?

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

I'm trying to find the distance between two points in a water body, so the irregular polygon in my actual data is more complex.

任何帮助将不胜感激!

欢呼

推荐答案

好的,这是我昨天在评论中提到的基于 gdistance 的方法.这不是完美的,因为它所计算的路径段都被约束为出现在棋盘上的16个方向之一(国王的举动和骑士的举动).就是说,对于示例中的三个成对距离,每个距离的正确值都在正确值的2%之内(总是略有高估).

OK, here's the gdistance-based approach I mentioned in comments yesterday. It's not perfect, since the segments of the paths it computes are all constrained to occur in one of 16 directions on a chessboard (king's moves plus knight's moves). That said, it gets within 2% of the correct values (always slightly overestimating) for each of the three pairwise distances in your example.

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))

这篇关于在R {spatstat}中找到点之间的欧几里得距离,该距离受不规则多边形窗口限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆