2015-01-14 52 views
3

背景:該項目涉及在GIS環境中映射區域。我需要找到一組多邊形與另一組多邊形的交集。我通過將我的地圖兼容多邊形投射爲Polygon2d(http://geom-java.sourceforge.net/api/math/geom2d/polygon/package-summary.html)並使用交集方法來查找相交多邊形的頂點列表來實現此目的。檢測多個多邊形交叉點(java)

問題:除了相同的兩個多邊形之間存在多個重疊區域時,這適用。頂點列表是正確的,但我需要將列表分隔到每個單獨的多邊形中。

第一個圖像是它應該是什麼樣子,第二個是實際產生:(頂部忽略的區域那是一個不同的錯誤的結果)

如何檢測並糾正這種情況?

+0

我建議張貼thi s在http://gis.stackexchange.com/ - 你可能會得到更好的反饋。 –

回答

0

嗯,我想通了。我將交點多邊形的頂點列表(藍色)迭代通過每對點,將兩個原始多邊形中的每個線段都投影爲LineSegment2D對象,並使用.contains方法檢查每對點是否在其中一個點線段。如果每對點不包含在1行內,則出現錯誤。

繼承人的方法,但請記住有幾個專有類參與。

public static boolean noErrors(SurfacePolygonX p1, SurfacePolygonX p2, List<LatLon> list) { 
    boolean allPointsInSamePolygon = true; 

    //for each latlon jl in locations, cast jl and jl2 as points  
    for (int j = 0; j < list.size(); j++) { 
     LatLon jl = list.get(j); 
     LatLon jl2 = list.get((j == list.size() - 1) ? 0 : j + 1); 
     Point2D pt = new Point2D(jl.longitude.degrees, jl.latitude.degrees); 
     Point2D pt2 = new Point2D(jl2.longitude.degrees, jl2.latitude.degrees); 

     List<LatLon> corners = p1.getCorners(); 

     boolean bothPointsInSameSegment = false; 

     //for each latlon k selectedShape cast k and k+1 as 2lineseg 
     for (int k = 0; k < corners.size(); k++) { 
      LatLon kl = corners.get(k); 
      LatLon kl2 = corners.get((k == corners.size() - 1) ? 0 : k + 1); 

      LineSegment2D segment = new LineSegment2D(kl.longitude.degrees, kl.latitude.degrees, kl2.longitude.degrees, kl2.latitude.degrees); 

      boolean segContainsP1 = segment.contains(pt); 
      boolean segContainsP2 = segment.contains(pt2); 

      System.out.println("selectedShape: segment "+k+" contains p"+j+":("+segContainsP1+") and p"+(j+1)+":("+segContainsP2+")"); 

      //check if each line contains the points. 
      if (segContainsP1 && segContainsP2) 
      { 
       bothPointsInSameSegment = true; 
      } 

     } 

     corners = p2.getCorners(); 
     //for each latlon k tempShape cast k and k+1 as 2lineseg 
     for (int k = 0; k < corners.size(); k++) { 
      LatLon kl = corners.get(k); 
      LatLon kl2 = corners.get((k == corners.size() - 1) ? 0 : k + 1); 

      LineSegment2D segment = new LineSegment2D(kl.longitude.degrees, kl.latitude.degrees, kl2.longitude.degrees, kl2.latitude.degrees); 


      boolean segContainsP1 = segment.contains(pt); 
      boolean segContainsP2 = segment.contains(pt2); 

      System.out.println("intersectingShape: segment "+k+" contains p"+j+":("+segContainsP1+") and p"+(j+1)+":("+segContainsP2+")"); 

      //check if each line contains the points. 
      if (segContainsP1 && segContainsP2) 
      { 
       bothPointsInSameSegment = true; 
      } 

     } 

     if (!bothPointsInSameSegment) allPointsInSamePolygon = false; 

    } 

    //if both points are not in the same line, then theres a conflict 
    return allPointsInSamePolygon; 
} 
1

您可以使用JTS

  1. 創建使用LinearRing
  2. 使用intersection方法的多邊形

簡單的代碼示例:

// build polygon p1 
LinearRing p1 = new GeometryFactory().createLinearRing(new Coordinate[]{new Coordinate(0,0), new Coordinate(0,10), new Coordinate(10,10), new Coordinate(10,0), new Coordinate(0,0)}); 
// build polygon p2 
LinearRing p2 = new GeometryFactory().createLinearRing(new Coordinate[]{new Coordinate(5,5), new Coordinate(15,5), new Coordinate(15,15), new Coordinate(5,15), new Coordinate(5,5)}); 
// calculate intersecting points 
Geometry intersectingPoints = p1.intersection(p2); 
// print result 
for(Coordinate c : intersectingPoints.getCoordinates()){ 
    System.out.println(c.toString()); 
} 

輸出是(像預期):

(5.0, 10.0, NaN) 
(10.0, 5.0, NaN)