【问题标题】:Calculating intersection between lines with GeoTools (java)使用 GeoTools (java) 计算线之间的交点
【发布时间】:2015-09-14 11:22:56
【问题描述】:

当:

public class Segment {
    Node vertex_1;
    Node vertex_2;
}

public class Node {
    double latitude;
    double longitude;
}

还有一个静态点——原点。如何使用GeoTools 以获得ArrayList<boolean>(大小N),其中每个值对于问题都是真/假:

从原点到线段两边的两条线是否在途中与其他线段相交?注意:这些段彼此足够接近,所以这里不是Great-circle的问题

例如这里的结果是 {true, false, false},因为从原点到路段 1 的第二条边的红线与她的路段 3 相交。

这个问题与this Stackoverflow Q 类似,但不同之处在于,我想在这里使用 GeoTools,而不是实现涉及将地理测量单位(纬度/经度)转换为极平面并执行一些数学计算的算法,例如跨产品 - 不难但有潜在的错误,如果已经有现成的开源库,最好使用它。

因为这个问题涉及到GIS解决方案,所以也在gis stackexchange中提问。

【问题讨论】:

  • 您需要将您的问题改写为点和线串,然后告诉我们您的线在地球上的位置,然后我们才能尝试回答这个问题。
  • @iant 1. 点在 200m 半径范围内(没有大圆圈等。如果这是您的意思,则问题)。 2. 我不是在寻找代码解决方案,而是在寻找算法解决方案,例如:将 pojos 转换为 GeoTools 对象,然后...谢谢,
  • @iant 我发布了这个小问题作为初步问题:stackoverflow.com/questions/32565298/…

标签: java algorithm gis geotools osgeo


【解决方案1】:

您可以使用Coordinate(您的Node)和LineString(您的Segment)对象来解决问题:

// origin point
Coordinate origin = new Coordinate(5, 0);
// segments
ArrayList<LineString> segmentList = new ArrayList();
LineString segmentA = new GeometryFactory().createLineString(new Coordinate[]{new Coordinate(0, 5), new Coordinate(5, 5)});
segmentList.add(segmentA);
LineString segmentB = new GeometryFactory().createLineString(new Coordinate[]{new Coordinate(4, 3), new Coordinate(6, 3)});
segmentList.add(segmentB);
LineString segmentC = new GeometryFactory().createLineString(new Coordinate[]{new Coordinate(7, 4), new Coordinate(10, 4)});
segmentList.add(segmentC);
// result list
ArrayList<Boolean> resultList = new ArrayList();
for(int i = 0; i < segmentList.size(); i++){
    // flag to indicate intersection
    boolean intersectionResult = false;
    // get current segment
    LineString currentSegment = segmentList.get(i);
    // get segments from segment pooints to origin
    LineString startSegment = new GeometryFactory().createLineString(new Coordinate[]{origin, currentSegment.getStartPoint().getCoordinate()});
    LineString endSegment = new GeometryFactory().createLineString(new Coordinate[]{origin, currentSegment.getEndPoint().getCoordinate()});
    // iterate over sections
    for(int j = 0; j < segmentList.size(); j++){
        // ignore same section
        if(i != j){
            // check for intersections between segments
            if(startSegment.intersects(segmentList.get(j)) || endSegment.intersects(segmentList.get(j))){
                intersectionResult = true;
                continue;
            }
        }
    }
    // no intersection found
    resultList.add(intersectionResult);
}

// print results
for(Boolean b : resultList){
    System.out.println("intersection of segment -> " + b.booleanValue());
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-25
    • 2015-02-28
    相关资源
    最近更新 更多