繁体   English   中英

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

[英]Obtaining the subset of points which are outside a specified irregular polygon using {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进行子集化以找出哪些点被拒绝?

生成边界和点列表

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超出多边形

或者,使用包平面图

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

如果您的主要目标是“找出哪些点被拒绝”,则只需访问已创建的test.ppp的rejects属性即可:

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM