[GIS] How to tell if a line intersects a polygon not only at endpoints

jts-topology-suitenettopologysuite

How do I do this in .net topology suite or jts? Please see image.

Any other library that can I run on .NET is fine, or even a general algorithm if there is no option.

For example this answer is only for endpoints:
Check if line crosses a polygon

polygon-line-intersect

Right now I'm randomly experimenting and if i intersect and check

if (intersection.Boundary.ToString().Contains("EMPTY"))

it seems to work, but… yeah no idea why or if it's all cases.

Best Answer

From your drawings and the comments I think your requirements are that the line

  • intersects with the interior of the polygon
  • does not just touch the boundary of the polygon

Just combine the requirements. This is the method using JTS that should answer your question:

public static boolean lineReallyIntersectsPolygon(LineString ls, Polygon p){
    if(ls.intersects(p) && !ls.touches(p)){
        return true;
    } else {
        return false;
    }
}