0

我有一组受复杂多边形约束的点(纬度和经度)。但是,有些点不在多边形的范围内,我想从原始数据框(不是 ppp 对象,如下所述)中对这些点进行子集化。

#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))
df <- data.frame(x=c(0.5, 2.5, 4.5, 2.5), y=c(4,1,4, 4))

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

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

#plotting example, to show the one out of the bound owin object
plot(bound)
points(df$x, df$y)

正如预期的那样,错误消息1 point was rejected as lying outside the specified window出现了。我将如何对原始数据框进行子集化df以找出哪些点被拒绝?

4

2 回答 2

4

生成边界和点列表

ex.poly <- data.frame(x=c(0,5,5,2.5,0), y=c(0,0,5,2.5,5))
df <- data.frame(x=c(0.5, 2.5, 4.5, 2.5), y=c(4,1,4, 4))

使用包 spatstat 识别和绘制多边形内部和外部的点

library(spatstat)
bound <- owin(poly=data.frame(x=ex.poly$x, y=ex.poly$y))
isin<-inside.owin(x=df$x,y=df$y,w=bound)
point_in <- df[isin,]
point_out <- df[!isin,]
plot(bound)
points(df)
points(point_out,col="red",cex = 3 )
points(point_in,col="green",cex = 3 )

多边形外的spatstat

或者,使用包装 splancs

library(splancs)
pts<-as.matrix(df)
poly<-as.matrix(ex.poly)
point_in<-pip(pts,poly,out=FALSE)
point_out<-pip(pts,poly,out=TRUE)
plot(pts, ylim=c(0,5), xlim=c(0,5))
polygon (poly)
points(point_out,col="red",cex = 3 )
points(point_in,col="green",cex = 3 )

PointsInOutOfPolygon

于 2014-10-12T15:34:21.867 回答
0

如果您的主要目标是“找出哪些点被拒绝”,您可以简单地访问rejects您已经创建的 test.ppp 的属性:

exterior_points <- attr(test.ppp, "rejects")

exterior_points 现在是一个单独的 ppp,它仅包含您在创建 test.ppp 时指定的窗口之外的点。

于 2014-11-14T05:19:23.957 回答