Skip to content

Commit

Permalink
Fix OverlayNG strict mode Union bug
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 Sep 16, 2020
1 parent dabe23c commit b0d6f03
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,20 @@
* since the intersection clipping optimization can
* interact with the snapping to alter the result.
* <p>
* There is an option to set the overlay computation to work in strict mode.
* In strict mode results have the following semantics:
* Optionally the overlay computation can process using strict mode
* (via {@link #setStrictMode(boolean)}.
* In strict mode result semantics are:
* <ul>
* <li>Result geometries are homogeneous (all components are of same dimension)
* <li>Lines resulting from topology collapses are not included in the result
* <li>Result geometries are homogeneous (all components are of same dimension),
* except for some cases of symmetricDifference.
* <li>Lines and Points resulting from topology collapses are not included in the result
* </ul>
* Strict mode has the following benefits:
* <ul>
* <li>Results are simpler
* <li>Overlay operations are easily chainable
* </ul>
* The original JTS overlay semantics correspons to non-strict mode.
* The original JTS overlay semantics corresponds to non-strict mode.
*
* @author mdavis
*
Expand Down Expand Up @@ -557,7 +560,8 @@ private Geometry extractResult(int opCode, OverlayGraph graph) {
//--- Build lines
boolean allowResultLines = ! hasResultAreaComponents
|| isAllowMixedIntResult
|| opCode == SYMDIFFERENCE;
|| opCode == SYMDIFFERENCE
|| opCode == UNION;
if ( allowResultLines ) {
LineBuilder lineBuilder = new LineBuilder(inputGeom, graph, hasResultAreaComponents, opCode, geomFact);
lineBuilder.setStrictMode(isStrictMode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,14 @@ public void testPolygonLineSymDifference() {
checkEqual(expected, actual);
}

public void testPolygonLineUnion() {
Geometry a = read("POLYGON ((10 20, 20 20, 20 10, 10 10, 10 20))");
Geometry b = read("LINESTRING (15 15, 25 15)");
Geometry expected = read("GEOMETRYCOLLECTION (POLYGON ((20 20, 20 15, 20 10, 10 10, 10 20, 20 20)), LINESTRING (20 15, 25 15))");
Geometry actual = union(a, b);
checkEqual(expected, actual);
}

/**
* Check that result does not include collapsed line intersection
*/
Expand All @@ -97,27 +105,33 @@ public void testPolygonUnionCollapse() {
checkEqual(expected, actual);
}

public static Geometry intersection(Geometry a, Geometry b) {
OverlayNG ov = new OverlayNG(a, b, INTERSECTION);
ov.setStrictMode(true);
return ov.getResult();
static Geometry intersection(Geometry a, Geometry b) {
return overlay(a, b, INTERSECTION);
}

public static Geometry symDifference(Geometry a, Geometry b) {
OverlayNG ov = new OverlayNG(a, b, SYMDIFFERENCE);
ov.setStrictMode(true);
return ov.getResult();
static Geometry symDifference(Geometry a, Geometry b) {
return overlay(a, b, SYMDIFFERENCE);
}

public static Geometry intersection(Geometry a, Geometry b, double scaleFactor) {
PrecisionModel pm = new PrecisionModel(scaleFactor);
OverlayNG ov = new OverlayNG(a, b, pm, INTERSECTION);
static Geometry union(Geometry a, Geometry b) {
return overlay(a, b, UNION);
}

static Geometry overlay(Geometry a, Geometry b, int opCode) {
OverlayNG ov = new OverlayNG(a, b, opCode);
ov.setStrictMode(true);
return ov.getResult();
}
public static Geometry union(Geometry a, Geometry b, double scaleFactor) {

static Geometry intersection(Geometry a, Geometry b, double scaleFactor) {
return overlay(a, b, scaleFactor, INTERSECTION);
}
static Geometry union(Geometry a, Geometry b, double scaleFactor) {
return overlay(a, b, scaleFactor, UNION);
}
static Geometry overlay(Geometry a, Geometry b, double scaleFactor, int opCode) {
PrecisionModel pm = new PrecisionModel(scaleFactor);
OverlayNG ov = new OverlayNG(a, b, pm, UNION);
OverlayNG ov = new OverlayNG(a, b, pm, opCode);
ov.setStrictMode(true);
return ov.getResult();
}
Expand Down

0 comments on commit b0d6f03

Please sign in to comment.