1

在以下情况下,我有ArratList<Segment>(N 个段 - 数组大小为 N):

public class Segment {
    Node vertex_1;
    Node vertex_2;
}

public class Node {
    double latitude;
    double longitude;
}

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

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

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

在此处输入图像描述

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

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

4

1 回答 1

2

您可以使用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());
}
于 2016-07-17T22:22:31.280 回答