What I'm trying to do is with JSTS to check with lineGeom.crosses(polygonGeom)
function if a line is crossing completely a polygon.
The problem is that both cases from the picture returns true. I don't know why. Is there another way to check only the first case with line 1.
Answer
You need to understand the definition of the spatial predicates. What you're really want is to know if a line traverses the polygon (and not simply cross it)
Using the JTS Topology Suite, you can see that in both cases the predicates are the same: the two lines intersects and crosses the polygon:
and
With the JTS Topology Suite, you can use the Dimensionally Extended nine-Intersection Model (DE-9IM) and the Clementini Matrix:
In the first case it is
1 F 2
0 F 1
And in the second, it is
1 0 2
0 F 1
What does that really mean:
- in the second case the result of the intersection of the boundary of the polygon and the boundary of the line has a dimension of 0 (must obligatorily be one Point, and only one)
- in the first case the result of the intersection of the boundary of the polygon and the boundary of the line has a dimension of F (is not important, 2 Point here)
Therefore you are able to discriminate between the two cases.
line.intersection(LinearRing(polygon)) = 1 Point
or
line.intersection(LinearRing(polygon)) = 2 Points
New: if the polygon has holes:
You need to understand the topological definitions of interior, exterior and boundary of a geometry: this is fundamental if you work with JSTS, JTS or GEOS.
1) the polygon
2) the boundary of the polygon in red (LinearRing)
!
3) The result of the intersection of the line and the boundary of the polygon because the holes lie within the interior of the polygon
And the resulting DE-9IM matrix change:
No comments:
Post a Comment