[英]Removing points inside several polygons
數據:我有兩個長度分別為100,000的變量a
和b
的坐標,並且我有一個文本文件,其中包含多個多邊形的坐標。
我現在想刪除不同多邊形內的所有a和b點。
為此,我正在嘗試使用此答案在STACKOVERFLOW中的代碼,該代碼可對一個點和一個多邊形進行處理。
我想出的方法來解決幾個點和幾個多邊形的問題是這樣的:
a and b
所有100,000個點運行該函數, 如果它們在內部,則將它們附加到列表中,以后可以用來與原始a和b比較 現在我面臨兩個問題,我不知道該如何處理。
包含多邊形坐標的文本文件如下所示:
020241-041200 4 30.83 -3.69 30.82 -3.69 30.82 -3.73 30.83 -3.73
020241-041200 12 30.91 -4.03 30.89 -4.03 30.85 -4.05 30.83 -4.07 30.82 -4.09 30.84 -4.16 30.89 -4.19 30.96 -4.16 30.97 -4.13 30.97 -4.08 30.95 -4.05 30.93 -4.04
這里(020241-041200)是多邊形的ID ,而(4)是多邊形具有的角數, 30.83是第一個角的X坐標, -3.69是 第一個角 的Y坐標等等。
我想跳過前兩列,以便僅考慮多邊形的X,Y坐標。 我怎么做?
多邊形的形狀不同 ,如您所見, 第二個多邊形具有12個角 ,而第一個多邊形為4個。
a and b
100,000點a and b
如果除了我上面給出的解決方案之外,還有任何方便的方法,它也將很有用。
我只想要a和b在多邊形之外的那些點。
當您說“ a和b的那些在多邊形之外的點”時,是指在所有多邊形之外還是在任何多邊形之外?
這是:
point_in_poly
函數一起使用的point_in_poly
。 這是從文件中讀取多邊形點的例程:
def readPolygons(path):
polygons = []
with open(path) as f:
for line in f: # (1)
words = [ float(y) for y in (line.split())[2:] ] # (2)
poly = zip (words[::2], words[1::2]) # (3)
if len(poly): # (4)
polygons.append(poly)
return polygons
每個多邊形都表示為成對的浮點列表,並且例程返回一個多邊形列表。
筆記:
[2:]
刪除前兩個單詞,然后將每個單詞轉換為浮點數。 這是檢查點是否在任何多邊形中的例程:
def inAnyPolygon(x,y,polygons):
for p in polygons:
if point_in_poly(x,y,p):
return True
return False
如果您的條件是“在所有多邊形中”,則使用:
def inAllPolygons(x,y,polygons):
for p in polygons:
if not point_in_poly(x,y,p):
return False
return True
更新:如果你有一個點列表points
,你可以創建一個包含那些不以任何與多邊形的點另一個列表:
outliers = []
for p in points:
(x,y) = p
if not inAnyPolygons(x,y,polygons):
outliers.append(p)
return outliers
如果a
和b
是分別表示100000點的x和y坐標的數字列表,則下面是查找異常值的代碼:
outliers = []
for (x,y) in zip(a,b):
if not inAnyPolygons(x,y,polygons):
outliers.append((x,y))
return outliers
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.