背景:该项目涉及在GIS环境中映射区域。我需要找到一组多边形与另一组多边形的交集。我通过将与地图兼容的多边形转换为Polygon2d(http://geom-java.sourceforge.net/api/math/geom2d/polygon/package-summary.html)并使用交集方法查找交叉多边形的顶点列表来实现此目的。
问题:除非在相同的两个多边形之间存在多个重叠区域,否则此方法有效。顶点列表是正确的,但我需要将列表分成每个单独的多边形。
第一张图片应该是它的样子,第二张是实际生成的图片:
(忽略顶部的区域。这是一个不同的错误的结果)
如何检测并纠正这种情况?
答案 0 :(得分:1)
简单的代码示例:
// 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)
答案 1 :(得分: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;
}