使用{spatstat}获取指定不规则多边形之外的点子集

时间:2014-10-12 03:37:48

标签: r spatstat

我有一大堆由复杂多边形约束的点(纬度和经度)。但是有些点不在多边形的范围内,我想从原始数据框(而不是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进行子集以找出哪个点被拒绝?

2 个答案:

答案 0 :(得分: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 in out of polygon

或者,使用包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

答案 1 :(得分:0)

如果您的主要目标是&#34;找出哪个点被拒绝&#34;,您只需访问您已创建的test.ppp的rejects属性:

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

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

相关问题