2016-08-14 85 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方法,所以可能是我在做什么一定加倍和一些优化那里。

相关问题