2014-10-02 92 views
0

我有一個座標,我想在地圖上放置一個指針。 問題是如何計算這個指針在vb.Net中的位置。查找最佳標籤位置座標/多邊形組

我可以有3個sitiations:

  1. 單點
  2. 甲方式點,2個或更多個點
  3. 的多邊形,多點

所有點的緯度和經度。現在我想爲這些情況設置一個標記。

  1. 簡單,標記位置是點
  2. 標記應在線路
  3. 中間的多邊形是最難的。我可以找到它的質心(重心),但它可能在多邊形之外。所以這對標記沒有用處。

我不知道如何計算這些點(exept爲nr.1 ;-))

回答

0

假設多邊形不自相交,你可以:

  1. 三角測量多邊形不知何故。 This can be done in O(n log n) time.
  2. 選擇其中一個三角形作爲「代表」。我的建議是:面積最大的三角形,或最小角度最大的三角形,或質心最接近點質心的三角形。
  3. 使用此三角形的質心,它必須位於三角形內部,因此位於多邊形內部。
0

我發現,做所有的魔法庫:NetTopologySuite 您可以安裝它作爲一個NuGet包和文檔here 獲得包很簡單了。

中心多邊形內部:

首先創建,加入所有的緯度和經度,以一個座標數組多邊形。

'The first point needs to be the last point so it becomes a closed shape 
'Becouse I loop through all my coordinates I set a flag for the first point 
'so I can add it at the end 
Dim firstPoint As GeoAPI.Geometries.Coordinate = Nothing 

'create an coordinatearray 
Dim coordinates() As GeoAPI.Geometries.Coordinate = {} 

'Loop trough all the coordinates you have (you can do a dataset loop etc) 
'For the example the coordinates are in a pointF array 
For Each Point As PointF In points 

    'Save the first point so we can add it in the end 
    If firstPoint Is Nothing Then 
      firstPoint = New GeoAPI.Geometries.Coordinate(Point.X, Point.Y) 
    End If 

    'Create a coordinate so we can add it to the coordinate array 
    Dim coordinate As New GeoAPI.Geometries.Coordinate(Point.X, Point.Y) 

    'Adding it to the array 
    Array.Resize(coordinates, coordinates.Length + 1) 
    coordinates(coordinates.Length - 1) = coordinate 
Next 

'Now all the coordinates are in the array we need to add the first one at the end 
Array.Resize(coordinates, coordinates.Length + 1) 
coordinates(coordinates.Length - 1) = firstPoint 


'Now we create a linearRing with these coordinates 
Dim ring As New NetTopologySuite.Geometries.LinearRing(coordinates) 

'And use the ring to get a center point inside it 
Dim insidePoint As IPoint = ring.InteriorPoint 

'If you want a centroid you can do the following 
Dim polygon As New NetTopologySuite.Geometries.Polygon(ring) 
Dim centroidPoint As IPoint = polygon.centroid