2016-08-14 48 views
0

如果我在JTS中有一個linestring(或者通常是某種開放的多段線),其方向由它的起始點定義,是否有一些智能的方法可以在與一個閉合的交點處告知polygon所述linestring是否「進入」多邊形或離開它,或者:判斷線串是否正在進入或退出多邊形

  • 在JRS(我不能找到在文檔的方式)時,只有其中線和封閉形狀相交intersection
  • 一些一般的座標聰明的方式。我現在通過測試一個非常小的距離點沿着polygon邊緣的linestring和測試哪個是'in',哪個是'out'來做到這一點。如果polygon有一個(不太可能)真正尖銳的內部邊緣,這可能會返回錯誤的結果。

回答

1

檢查是否linestring的區段的起始點是在多邊形的內部或外部,以找出是否它正在進入或離開polygon。簡單代碼示例:

// some demo polygon + line 
Polygon polygon = new GeometryFactory().createPolygon(new Coordinate[]{new Coordinate(1,1), new Coordinate(6,1), new Coordinate(6,6), new Coordinate(1,6), new Coordinate(1,1)}); 
LineString line = new GeometryFactory().createLineString(new Coordinate[]{new Coordinate(0, 0), new Coordinate(5,5), new Coordinate(10,5)}); 

// check for intersection in the first place 
if(line.intersects(polygon)){ 
    System.out.println("line intersects polygon!"); 
    // iterate over all segments of the linestring and check for intersections 
    for(int i = 1; i < line.getNumPoints(); i++){ 
     // create line for current segment 
     LineString currentSegment = new GeometryFactory().createLineString(new Coordinate[]{line.getCoordinates()[i-1], line.getCoordinates()[i]}); 
     // check if line is intersecting with ring 
     if(currentSegment.intersects(polygon)){ 
      // segment is entering the ring if startpoint is outside the ring 
      if(!polygon.contains(currentSegment.getStartPoint())){ 
       System.out.println("This segment is entering the polygon -> "); 
       System.out.println(currentSegment.toText()); 
      // startpoint is inside the ring 
      } 
      if (polygon.contains(currentSegment.getStartPoint())) { 
       System.out.println("This segment is exiting the polygon -> "); 
       System.out.println(currentSegment.toText()); 
      } 
     } 
    } 
} else { 
    System.out.println("line is not intersecting the polygon!"); 
} 

此代碼不涵蓋所有可能性。例如。如果單個線段多次與多邊形相交(輸入+退出),則此示例中未涉及。在這種情況下,只需計算交點的數量並在交點之間創建相應數量的線串。

0

回答我自己的問題 - 對於任何有類似問題的人。 我最終寫了一些基於交叉點的代碼(因爲我已經通過JTS)。想法源自crossing number algorithmodd-even rule

的「規則」(我不認爲有例外,可能是錯的)是:

  1. 的進入路口後必須跟一個出口,反之亦然。
  2. 如果從閉合的多邊形開始,第一個交點就是出口。
  3. 如果您沒有從多邊形開始,那麼第一個交點就是一個回車。

由於僞代碼是這樣的:

Get intersection_points between polyline and closed polygon // using JTS.intersect() 
    Sort intersection_points along chainage of polyline 
    if polyline start_point in polygon // using JTS.contains() 
     first intersect_point is an EXIT, next is an ENTER, EXIT, ENTER and so on alternating along chainage. 
    else //start point not in polygon 
     first intersect_point is an ENTER, next is an EXIT, ENTER, EXIT and so on along chainage. 

沒有看過源JTS intersectcontains方法,所以可能是我在做什麼一定加倍和一些優化那裏。

相關問題