[英]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 )
或者,使用包平面圖
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 )
如果您的主要目標是“找出哪些點被拒絕”,則只需訪問已創建的test.ppp的rejects
屬性即可:
exterior_points <- attr(test.ppp, "rejects")
exterior_points現在是一個單獨的ppp,僅包含在創建test.ppp時指定的窗口之外的點。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.