michael
michael

Reputation: 3945

Calculating intersection between lines with GeoTools (java)

I have ArratList<Segment> (N segments - array size is N) when:

public class Segment {
    Node vertex_1;
    Node vertex_2;
}

public class Node {
    double latitude;
    double longitude;
}

And one static point - origin point. How to work with GeoTools in order to get ArrayList<boolean> (size N) where each value is true/false regarding the question:

Is the 2 lines from origin point to both edges of the segment, intersects some other segments on their way? Note: The segments are close enough to each other, so ain't here issues of Great-circle

For example here the result is {true, false, false} because the red line from origin point to second edge of segment 1 intersects on her way segment 3.

enter image description here

This question is similar to this Stackoverflow Q but the difference is that here I want to work with GeoTools instead of implementing the algorithm which involving translation of Geographic measurement units (lat/lon) into polar plane and performing some math calculation such as cross products - Not difficult but has potential for bugs, and if there is already ready open source library it's better to use it.

Because this question involving GIS solution, it asked also in gis stackexchange.

Upvotes: 1

Views: 1108

Answers (1)

Lars
Lars

Reputation: 2485

You can use Coordinate (your Node) and LineString (your Segment) objects to solve the problem:

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

Upvotes: 2

Related Questions