1

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.

Community
  • 1
  • 1
michael
  • 3,835
  • 14
  • 53
  • 90
  • You will need to recast your problem as Points and LineStrings and then tell us where abouts your lines are on the globe before we can attempt to answer this . – Ian Turton Sep 14 '15 at 11:37
  • @iant 1. The points are within 200m radius (no big circle etc. Issues if this is what you mean). 2. I'm not looking for code solution rather then for algorithmic solution like: convert pojos into GeoTools objects, then... Thanks, – michael Sep 14 '15 at 11:49
  • @iant I posted this small question as preliminary for this: http://stackoverflow.com/questions/32565298/geotools-how-to-build-a-point-imports-issue – michael Sep 14 '15 at 12:53

1 Answers1

2

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());
}
Lars
  • 2,315
  • 2
  • 24
  • 29