我Polygons從一個KML檔案中提取了大量的。多邊形代表地球表面上的“塊”。我可以讀取多邊形的坐標并存盤它們的值,以及關于多邊形的其他一些資訊。
我的問題是,我現在有一組點(帶有它們的坐標,我們再次談論地球表面上的點),我需要檢查它們屬于哪個多邊形。
我知道PiP這不是一個微不足道的新問題,所以我不想重新發明輪子!有沒有VB.NET可以幫助我快速解決這個問題的庫?謝謝
編輯
我的多邊形是 n 元組的形狀,例如一個 5 元組(其中一些有更多,所有的第一個和最后一個點都相等):
[( -59.00002600000005,-52.00002600000001,0 ),
( -59.00002600000005,-51.50002600000001,0 ),
( -59.50002600000005,-51.50002600000001,0 ),
( -59.50002600000005,-52.00002600000001,0 ),
( -59.00002600000005,-52.00002600000001,0 ) ]
目前我正在以這種方式讀取 KML 檔案:
'foreach KML Placemark
For Each p As System.XML.Linq.XElement In xdata.Descendants(ns "Placemark")
ID = p.Element(ns "name").Value
'coordinates, every substring has triple x,y,z but i only care about x,y
temp = p.Descendants(ns "coordinates").Value
str = temp.split(" ")
'number of polygon vertexes
lunghezza(ID) = str.length()-1
'polygon vertexes
Dim polygonPoints(str.length()-1) As System.Drawing.PointF
'first substring is empty
for s = 1 to str.length()-1
'i get x,y
x = String2Array(str(s),",",false)(1)
y = String2Array(str(s),",",false)(2)
'cast to double
flt_x = double.Parse(x)
flt_y = double.Parse(y)
'point is made
polygonPoints(s-1) = new System.Drawing.PointF(flt_x, flt_y)
next
'points are associated to the polygon ID
punti(ID) = polygonPoints
Next
在這一點上,我嘗試查看一個點是否在我存盤的這些多邊形之一內:為此,我使用了我在這里找到的演算法
dim ok as boolean = false
dim xinters as double
' flt_y and flt_x contain my test latitude and longitude
'foreach polygon stored
for each id in IDS
if not empty(id)
'get number of points defining the polygon
Dim polygonPoints2(lunghezza(id)) As System.Drawing.PointF
'i get the polygon vertexes
polygonPoints2 = punti(id)
dim p1, p2 as System.Drawing.PointF
'first point
p1 = polygonPoints2(0)
'counter
dim c as integer = 0
for i as integer = 1 to lunghezza(id)
p2 = polygonPoints2(i Mod lunghezza(id))
if ( flt_x > Math.min(p1.x, p2.x) and flt_x <= Math.max(p1.x, p2.x) and flt_y <= Math.max(p1.y, p2.y) and p1.x <> p2.x) then
xinters = (flt_x - p1.x) * (p2.y - p1.y) / (p2.x - p1.x) p1.y
if (p1.y = p2.y or flt_y <= xinters)
c = c 1
end if
end if
p1 = p2
next
if (c mod 2) <> 0
'found one!
ok = true
exit for
end if
end if
next
但我永遠找不到我的觀點屬于哪個多邊形!我究竟做錯了什么?
測驗我使用坐標: x: 44,3034627 y: 7,800283
我確信這些適合這個多邊形:
<Polygon>
<extrude>0</extrude>
<altitudeMode>clampToGround</altitudeMode>
<outerBoundaryIs>
<LinearRing>
<coordinates>
10.99997399999995,45.999974,0
10.99997399999995,46.499974,0
10.49997399999995,46.499974,0
9.999973999999952,46.499974,0
9.499973999999952,46.499974,0
8.999973999999952,46.499974,0
8.499973999999952,46.499974,0
7.999973999999952,46.499974,0
7.999973999999952,45.999974,0
7.499973999999952,45.999974,0
6.999973999999952,45.999974,0
6.999973999999952,45.499974,0
6.999973999999952,44.999974,0
6.999973999999952,44.499974,0
6.999973999999952,43.999974,0
7.499973999999952,43.999974,0
7.999973999999952,43.999974,0
7.999973999999952,44.499974,0
8.499973999999952,44.499974,0
8.999973999999952,44.499974,0
9.499973999999952,44.499974,0
9.999973999999952,44.499974,0
10.49997399999995,44.499974,0
10.49997399999995,43.999974,0
10.99997399999995,43.999974,0
11.49997399999995,43.999974,0
11.99997399999995,43.999974,0
11.99997399999995,44.499974,0
12.49997399999995,44.499974,0
12.49997399999995,44.999974,0
11.99997399999995,44.999974,0
11.49997399999995,44.999974,0
11.49997399999995,45.499974,0
10.99997399999995,45.499974,0
10.99997399999995,45.999974,0
</coordinates>
</LinearRing>
</outerBoundaryIs>
</Polygon>
我開始懷疑這個問題在PiP演算法,更具體的方面<>,并=進行比較Doubles,因此重新標簽
uj5u.com熱心網友回復:
最后,我所有的作業都是正確的,我只是以錯誤的順序讀取坐標,而沒有正確處理輸入字串。以下是讀取 KML 的更正代碼:
'foreach KML Placemark
For Each p As System.XML.Linq.XElement In xdata.Descendants(ns "Placemark")
ID = p.Element(ns "name").Value
'coordinates, every substring has triple x,y,z but i only care about x,y
temp = p.Descendants(ns "coordinates").Value
str = temp.split(" ")
'number of polygon vertexes
lunghezza(ID) = str.length()-1
'polygon vertexes
Dim polygonPoints(str.length()-1) As System.Drawing.PointF
'first substring is empty
for s = 1 to str.length()-1
'i get x,y
y = String2Array(str(s),",",false)(1)
x = String2Array(str(s),",",false)(2)
'cast to double
flt_x = double.Parse(x.replace(".", ","))
flt_y = double.Parse(y.replace(".", ","))
'point is made
polygonPoints(s-1) = new System.Drawing.PointF(flt_x, flt_y)
next
'points are associated to the polygon ID
punti(ID) = polygonPoints
Next
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/314908.html
