简体   繁体   English

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

[英]Is (JTS) linestring is entering or exiting a polygon

If I have a linestring in JTS (or some sort of open polyline generally) with its direction defined by it start point, is there some smart way to tell at intersections with a closed polygon whether the linestring is 'entering' the polygon or exiting it, either:如果我在 JTS(或一般的某种开放折线)中有一个linestring ,其方向由起点定义,是否有一些聪明的方法可以在与闭合polygon交点处判断linestring是“进入”多边形还是退出多边形,或者:

  • In JRS ( I cant find a way in docs), only the coordinates where the line and closed shape intersect with intersection在 JRS 中(我在文档中找不到方法),只有线和闭合形状与intersection的坐标
  • Some general clever way.一些通用的聪明方法。 I currently have done it by testing a point a very small distance, along linestring on either side of polygon edge and testing which was 'in', and which was 'out'.我目前已经通过测试一个非常小的点,沿着polygon边缘两侧的linestring并测试哪个是“in”,哪个是“out”。 This could conceivably return an incorrect result if the polygon had a (unlikely) REALLY sharp internal edge.如果polygon具有(不太可能)非常尖锐的内部边缘,那么这可能会返回不正确的结果。

Check if the startpoint of a segment of the linestring is inside the polygon or outside to figure out if it is entering or exiting the polygon .检查linestring线段的起点是在多边形内部还是外部,以确定它是进入还是退出polygon Simple code example:简单的代码示例:

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

This code does not cover all possibilities.此代码并未涵盖所有可能性。 Eg if single segments are intersecting the polygon multiple times (entering + exiting) this is not covered in this example.例如,如果单个线段多次与多边形相交(进入 + 退出),则本示例中不包括在内。 In this case just count the number of intersections and create the according number of linestrings between the intersection points.在这种情况下,只需计算交叉点的数量并在交叉点之间创建相应数量的线串。

Answering my own question - for anyone who has similar problem.回答我自己的问题 - 对于任何有类似问题的人。 I ended up writing a bit of code based on number of intersections (since I had these already via JTS).我最终根据交叉点的数量编写了一些代码(因为我已经通过 JTS 获得了这些代码)。 Ideas stemmed on stuff from crossing number algorithm and odd-even rule .想法源于交叉数算法奇偶规则

The 'rules' ( I do not think there are exceptions, probably wrong) are: “规则”(我认为没有例外,可能是错误的)是:

  1. An enter intersection must be followed by an exit, and vice versa.进入交叉路口后必须紧跟出口,反之亦然。
  2. If you start in the closed polygon, the first intersection point is an exit.如果从闭合多边形开始,第一个交点是出口。
  3. If you do not start in the polygon, the first intersection is an enter.如果您不在多边形中开始,则第一个交点是回车。

As pseudocode, something like this:作为伪代码,是这样的:

    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.

Haven't looked at source for JTS intersect and contains methods so might be some doubling in what I am doing and some optimisation there.还没有查看 JTS intersect源代码并contains方法,所以我正在做的事情可能会加倍并在那里进行一些优化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM