2014-10-12 16 views
0

獲取位於指定不規則多邊形之外的點的子集我有一個由複雜多邊形約束的大量點(經度和緯度)。但是有些點不在多邊形的範圍內,我想從原始數據框(而不是ppp對象,下面描述)中對這些點進行子集劃分。使用{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)) 
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找出哪些點被拒絕?

+0

前段時間我遇到了[this](http://robinlovelace.net/r/2014/07/29/clipping-with-r.html)。此鏈接可能有用。 – jazzurro 2014-10-12 03:43:26

+0

你應該提到你正在使用包'spatstat'。 IIRC該軟件包有工具可以告訴你某個點是否在內部,因此請查看幫助頁面。 – 2014-10-12 12:29:08

+0

要使用的spatstat命令是inside.owin – user3969377 2014-10-13 06:19:56

回答

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

0

如果你的主要目標是「找出點(S)被拒絕」,你可以簡單地訪問你已經創建了test.ppp的rejects屬性:

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

exterior_points現在是一個單獨的ppp,它只包含創建test.ppp時指定窗口之外的點。

相關問題