I have numerous routes, each defined by its many points which in turn are defined by their corresponding Gcs coordinates (latitude,longitude). I was trying to find an efficient Java implementation to determine which lines are self-intersecting and I was referred to JTS.
This is what I was trying to do:
public boolean intersects(List<GcsNode> nodes) {
Coordinate[] sequence = new Coordinate[route.size()];
for (int i = 0; i < nodes.size(); i++) {
sequence[i] = toCartesian(nodes.get(i).latitude(), nodes.get(i).longitude());
}
GeometryFactory factory = new GeometryFactory();
return !factory.createLineString(sequence).isSimple();
}
public static final double DIST = 6371.0;
public Coordinate toCartesian(double latitude, double longitude) {
return new Coordinate(DIST * Math.cos(latitude) * Math.cos(longitude),
DIST * Math.cos(latitude) * Math.sin(longitude));
}
Unfortunately, the intersects method always returns true, no matter if the routes are self-intersecting or not. Does anybody know what I am doing wrong?