Skip to content

Commit

Permalink
Add Densifer setValidated method (#595)
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 12, 2020
1 parent 4804f76 commit 3e2d9b2
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
* The coordinates created during densification respect the input geometry's
* {@link PrecisionModel}.
* <p>
* By default polygonal results are processed to ensure they are valid.
* This processing is costly, and it is very rare for results to be invalid.
* Validation processing can be disabled by calling the {@link #setValidate(boolean)} method.
* <p>
* <b>Note:</b> At some future point this class will
* offer a variety of densification strategies.
*
Expand Down Expand Up @@ -86,6 +90,11 @@ private static Coordinate[] densifyPoints(Coordinate[] pts,

private double distanceTolerance;

/**
* Indicates whether areas should be topologically validated.
*/
private boolean isValidated = true;

/**
* Creates a new densifier instance.
*
Expand All @@ -110,20 +119,31 @@ public void setDistanceTolerance(double distanceTolerance) {
this.distanceTolerance = distanceTolerance;
}

/**
* Sets whether polygonal results are processed to ensure they are valid.
*
* @param isValidated true if the results should be validated
*/
public void setValidate(boolean isValidated) {
this.isValidated = isValidated;
}

/**
* Gets the densified geometry.
*
* @return the densified geometry
*/
public Geometry getResultGeometry() {
return (new DensifyTransformer(distanceTolerance)).transform(inputGeom);
return (new DensifyTransformer(distanceTolerance, isValidated)).transform(inputGeom);
}

static class DensifyTransformer extends GeometryTransformer {
double distanceTolerance;
private boolean isValidated;

DensifyTransformer(double distanceTolerance) {
DensifyTransformer(double distanceTolerance, boolean isValidated) {
this.distanceTolerance = distanceTolerance;
this.isValidated = isValidated;
}

protected CoordinateSequence transformCoordinates(
Expand Down Expand Up @@ -165,6 +185,7 @@ protected Geometry transformMultiPolygon(MultiPolygon geom, Geometry parent) {
* @return a valid area geometry
*/
private Geometry createValidArea(Geometry roughAreaGeom) {
if (! isValidated) return roughAreaGeom;
return roughAreaGeom.buffer(0.0);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright (c) 2016 Vivid Solutions.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* and Eclipse Distribution License v. 1.0 which accompanies this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v20.html
* and the Eclipse Distribution License is available at
*
* http://www.eclipse.org/org/documents/edl-v10.php.
*/
package org.locationtech.jts.densify;

import org.locationtech.jts.geom.Geometry;

import junit.textui.TestRunner;
import test.jts.GeometryTestCase;

public class DensifierTest extends GeometryTestCase {
private static final double TOLERANCE = 1e-6;

public static void main(String args[]) {
TestRunner.run(DensifierTest.class);
}

public DensifierTest(String name) { super(name); }

public void testLine() {
checkDensify("LINESTRING (0 0, 30 40, 35 35)",
10, "LINESTRING (0 0, 5 6.666666666666668, 10 13.333333333333336, 15 20, 20 26.66666666666667, 25 33.33333333333334, 30 40, 35 35)");
}

public void testBox() {
checkDensify("POLYGON ((10 30, 30 30, 30 10, 10 10, 10 30))",
10, "POLYGON ((10 30, 16.666666666666668 30, 23.333333333333336 30, 30 30, 30 23.333333333333332, 30 16.666666666666664, 30 10, 23.333333333333332 10, 16.666666666666664 10, 10 10, 10 16.666666666666668, 10 23.333333333333336, 10 30))");
}

public void testBoxNoValidate() {
checkDensifyNoValidate("POLYGON ((10 30, 30 30, 30 10, 10 10, 10 30))",
10, "POLYGON ((10 30, 16.666666666666668 30, 23.333333333333336 30, 30 30, 30 23.333333333333332, 30 16.666666666666664, 30 10, 23.333333333333332 10, 16.666666666666664 10, 10 10, 10 16.666666666666668, 10 23.333333333333336, 10 30))");
}

private void checkDensify(String wkt, double distanceTolerance, String wktExpected) {
Geometry geom = read(wkt);
Geometry expected = read(wktExpected);
Geometry actual = Densifier.densify(geom, distanceTolerance);
checkEqual(expected, actual, TOLERANCE);
}

/**
* Note: it's hard to construct a geometry which would actually be invalid when densified.
* This test just checks that the code path executes.
*
* @param wkt
* @param distanceTolerance
* @param wktExpected
*/
private void checkDensifyNoValidate(String wkt, double distanceTolerance, String wktExpected) {
Geometry geom = read(wkt);
Geometry expected = read(wktExpected);
Densifier den = new Densifier(geom);
den.setDistanceTolerance(distanceTolerance);
den.setValidate(false);
Geometry actual = den.getResultGeometry();
checkEqual(expected, actual, TOLERANCE);
}

}

0 comments on commit 3e2d9b2

Please sign in to comment.