gpt4 book ai didi

java - (JTS) 线串是否正在进入或退出多边形

转载 作者:行者123 更新时间:2023-12-01 09:35:51 25 4
gpt4 key购买 nike

如果我在 JTS(或一般某种开放折线)中有一个 linestring ,其方向由起点定义,是否有一些智能方法可以判断与闭合 的交叉点Polygon linestring 是否“进入”多边形或退出多边形:

  • 在 JRS 中(我在文档中找不到方法),只有直线和闭合形状与交集相交的坐标
  • 一些通用的聪明方法。我目前已经通过测试一个非常小的距离的点来完成它,沿着polygon边缘两侧的linestring并测试哪个是“in”,哪个是“out” 。如果多边形有(不太可能)非常尖锐的内部边缘,则可以想象,这可能会返回错误的结果。

最佳答案

检查线段的起点是在多边形内部还是外部,以确定它是进入还是退出多边形。简单代码示例:

// 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!");
}

此代码并未涵盖所有可能性。例如。如果单个线段多次与多边形相交(进入+退出),则本例中不涉及这一点。在这种情况下,只需计算交叉点的数量并在交叉点之间创建相应数量的线串。

关于java - (JTS) 线串是否正在进入或退出多边形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38939874/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com