Sunday, 1 January 2017

jts topology suite - Check if line crosses a polygon



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.


example



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:


enter image description here


and


enter image description here


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



enter image description here


2) the boundary of the polygon in red (LinearRing)


enter image description here!enter image description here


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


enter image description hereenter image description here


And the resulting DE-9IM matrix change:


enter image description here enter image description here


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...