Skip to content

Commit

Permalink
Fix IsValid for LinearRings (#737)
Browse files Browse the repository at this point in the history
Signed-off-by: Martin Davis <mtnclimb@gmail.com>
  • Loading branch information
dr-jts committed Jun 4, 2021
1 parent 93d804d commit 0f7f19f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -407,11 +407,15 @@ private void checkNoSelfIntersectingRings(GeometryGraph graph)
private void checkNoSelfIntersectingRing(EdgeIntersectionList eiList)
{
Set nodeSet = new TreeSet();
boolean isFirst = true;
for (Iterator i = eiList.iterator(); i.hasNext(); ) {
EdgeIntersection ei = (EdgeIntersection) i.next();
if (isFirst) {
isFirst = false;
/**
* Do not count start point, so start/end node is not counted as a self-intersection.
* Another segment with a node in same location will still trigger an invalid error.
* (Note that the edgeIntersectionList may not contain the start/end node,
* due to noding short-circuiting.)
*/
if (isStartNode(ei)) {
continue;
}
if (nodeSet.contains(ei.coord)) {
Expand All @@ -426,6 +430,10 @@ private void checkNoSelfIntersectingRing(EdgeIntersectionList eiList)
}
}

private static boolean isStartNode(EdgeIntersection ei) {
return ei.getSegmentIndex() == 0 && ei.getDistance() == 0.0;
}

/**
* Tests that each hole is inside the polygon shell.
* This routine assumes that the holes have previously been tested
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,23 @@ public void testLineString() throws Exception {
g.isValid();
assertTrue(true); //No exception thrown [Jon Aquino]
}

public void testLinearRingTriangle() throws Exception {
Geometry g = reader.read(
"LINEARRING (100 100, 150 200, 200 100, 100 100)");
assertTrue(g.isValid());
}

public void testLinearRingSelfCrossing() throws Exception {
Geometry g = reader.read(
"LINEARRING (150 100, 300 300, 100 300, 350 100, 150 100)");
assertTrue(! g.isValid());
}

public void testLinearRingSelfCrossing2() throws Exception {
Geometry g = reader.read(
"LINEARRING (0 0, 100 100, 100 0, 0 100, 0 0)");
assertTrue(! g.isValid());
}

}

0 comments on commit 0f7f19f

Please sign in to comment.