This is an automated email from the ASF dual-hosted git repository. asf-gitbox-commits pushed a commit to branch geoapi-4.0 in repository https://gitbox.apache.org/repos/asf/sis.git
commit 1f31a4cf921a7839627da22310a763ffe33a279a Author: jsorel <[email protected]> AuthorDate: Mon Nov 30 16:50:20 2026 +0100 test(Geometry): move several test cases to parameterized junit 5 files --- .../geometries/operation/GeometryProcessor.java | 44 ++++++ .../sis/geometries/adapter/ShapeConverterTest.java | 9 +- .../sis/geometries/operation/AsciiDocTestCase.java | 70 +++++++++ .../sis/geometries/operation/BufferTest.adoc | 61 ++++++++ .../sis/geometries/operation/BufferTest.java | 67 +++------ .../sis/geometries/operation/ConvexHullTest.adoc | 83 +++++++++++ .../sis/geometries/operation/ConvexHullTest.java | 124 +++------------- .../sis/geometries/operation/DifferenceTest.adoc | 104 ++++++++++++++ .../sis/geometries/operation/DifferenceTest.java | 78 +++------- .../sis/geometries/operation/DistanceTest.adoc | 79 ++++++++++ .../sis/geometries/operation/DistanceTest.java | 109 +++----------- .../sis/geometries/operation/IntersectionTest.adoc | 160 +++++++++++++++++++++ .../sis/geometries/operation/IntersectionTest.java | 88 +++--------- .../sis/geometries/operation/LocateAlongTest.adoc | 30 ++++ .../sis/geometries/operation/LocateAlongTest.java | 65 ++++----- .../geometries/operation/LocateBetweenTest.adoc | 32 +++++ .../geometries/operation/LocateBetweenTest.java | 68 ++++----- .../geometries/operation/SymDifferenceTest.adoc | 96 +++++++++++++ .../geometries/operation/SymDifferenceTest.java | 96 +++---------- .../sis/geometries/operation/TransformTest.adoc | 20 +++ .../sis/geometries/operation/TransformTest.java | 52 +++---- .../apache/sis/geometries/operation/UnionTest.adoc | 96 +++++++++++++ .../apache/sis/geometries/operation/UnionTest.java | 94 +++--------- .../geometries/operation/WithinDistanceTest.adoc | 114 +++++++++++++++ .../geometries/operation/WithinDistanceTest.java | 102 +++---------- .../apache/sis/geometries/surface/PolygonTest.java | 16 +++ 26 files changed, 1246 insertions(+), 711 deletions(-) diff --git a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/operation/GeometryProcessor.java b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/operation/GeometryProcessor.java index a788922fd5..10865ae7e7 100644 --- a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/operation/GeometryProcessor.java +++ b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/operation/GeometryProcessor.java @@ -1044,8 +1044,52 @@ public final class GeometryProcessor { * @return the result as a geometry of this package. */ private static Geometry fromJTS(org.locationtech.jts.geom.Geometry result, Geometry source) { + result = oriented(result); result.setUserData(source.getCoordinateReferenceSystem()); return Geometries.fromJTS(result, true); } + /** + * Returns the given JTS geometry with the rings of its polygons oriented as {@link Polygon} requires: + * exterior rings counter-clockwise and interior rings clockwise. JTS overlays build exterior rings clockwise. + * + * @param geometry the geometry computed by JTS. + * @return the geometry with oriented rings. + */ + private static org.locationtech.jts.geom.Geometry oriented(final org.locationtech.jts.geom.Geometry geometry) { + final org.locationtech.jts.geom.GeometryFactory factory = geometry.getFactory(); + if (geometry instanceof org.locationtech.jts.geom.Polygon polygon) { + if (polygon.isEmpty()) { + return polygon; + } + final org.locationtech.jts.geom.LinearRing[] holes = new org.locationtech.jts.geom.LinearRing[polygon.getNumInteriorRing()]; + for (int i = 0; i < holes.length; i++) { + holes[i] = oriented(polygon.getInteriorRingN(i), false); + } + return factory.createPolygon(oriented(polygon.getExteriorRing(), true), holes); + } + if (geometry instanceof org.locationtech.jts.geom.MultiPolygon multi) { + final org.locationtech.jts.geom.Polygon[] parts = new org.locationtech.jts.geom.Polygon[multi.getNumGeometries()]; + for (int i = 0; i < parts.length; i++) { + parts[i] = (org.locationtech.jts.geom.Polygon) oriented(multi.getGeometryN(i)); + } + return factory.createMultiPolygon(parts); + } + if (geometry.getClass() == org.locationtech.jts.geom.GeometryCollection.class) { + final org.locationtech.jts.geom.Geometry[] parts = new org.locationtech.jts.geom.Geometry[geometry.getNumGeometries()]; + for (int i = 0; i < parts.length; i++) { + parts[i] = oriented(geometry.getGeometryN(i)); + } + return factory.createGeometryCollection(parts); + } + return geometry; + } + + /** + * Returns the given ring, reversed if needed for being counter-clockwise or clockwise. + */ + private static org.locationtech.jts.geom.LinearRing oriented(final org.locationtech.jts.geom.LinearRing ring, final boolean ccw) { + return (org.locationtech.jts.algorithm.Orientation.isCCW(ring.getCoordinateSequence()) == ccw) ? ring : ring.reverse(); + } + } diff --git a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/adapter/ShapeConverterTest.java b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/adapter/ShapeConverterTest.java index f7daca7790..6d56b54b1f 100644 --- a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/adapter/ShapeConverterTest.java +++ b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/adapter/ShapeConverterTest.java @@ -164,18 +164,19 @@ public final class ShapeConverterTest { final Polygon polygon = assertInstanceOf(Polygon.class, geometry); assertEquals(1, polygon.getNumInteriorRing()); + // Exterior ring counter-clockwise, hole clockwise, as required by Polygon. assertCoordinatesEqual(polygon.getExteriorRing(), LinearRing.class, Vector.of(1.0, 2.0), - Vector.of(1.0, 22.0), - Vector.of(11.0, 22.0), Vector.of(11.0, 2.0), + Vector.of(11.0, 22.0), + Vector.of(1.0, 22.0), Vector.of(1.0, 2.0)); assertCoordinatesEqual(polygon.getInteriorRingN(0), LinearRing.class, Vector.of(7.0, 6.0), - Vector.of(7.0, 9.0), - Vector.of(5.0, 9.0), Vector.of(5.0, 6.0), + Vector.of(5.0, 9.0), + Vector.of(7.0, 9.0), Vector.of(7.0, 6.0)); } diff --git a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/AsciiDocTestCase.java b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/AsciiDocTestCase.java index 8db5b25d74..55cf3a17e2 100644 --- a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/AsciiDocTestCase.java +++ b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/AsciiDocTestCase.java @@ -29,15 +29,24 @@ import java.util.List; import java.util.Map; import java.util.Set; import java.util.function.BiPredicate; +import java.util.function.Supplier; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Stream; +import javax.measure.Quantity; +import javax.measure.Unit; +import org.opengis.referencing.crs.CoordinateReferenceSystem; +import org.opengis.util.FactoryException; import org.apache.sis.geometries.Geometry; import org.apache.sis.geometries.adapter.WellKnownText3; +import org.apache.sis.measure.Quantities; +import org.apache.sis.measure.Units; +import org.apache.sis.referencing.CRS; // Test dependencies import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.fail; import org.junit.jupiter.api.function.Executable; @@ -164,6 +173,39 @@ record AsciiDocTestCase(String id, String description, Map<String,String> values return new WellKnownText3().decode(value(key)); } + /** + * Returns a required number. + */ + double number(final String key) { + return Double.parseDouble(value(key)); + } + + /** + * Returns a required quantity, written as a number optionally followed by a unit symbol: + * {@code 10 m}, {@code 1 deg}, {@code inf}. A quantity without unit is dimensionless. + */ + Quantity<?> quantity(final String key) { + final String[] parts = value(key).strip().split("\\s+", 2); + final double amount = switch (parts[0]) { + case "inf", "+inf" -> Double.POSITIVE_INFINITY; + case "-inf" -> Double.NEGATIVE_INFINITY; + default -> Double.parseDouble(parts[0]); + }; + final Unit<?> unit = (parts.length > 1) ? Units.valueOf(parts[1]) : Units.UNITY; + return Quantities.create(amount, unit); + } + + /** + * Returns a required coordinate reference system, given by its authority code. + */ + CoordinateReferenceSystem crs(final String key) { + try { + return CRS.forCode(value(key)); + } catch (FactoryException e) { + throw new AssertionError("Test " + id + " has an invalid CRS.", e); + } + } + /** * Returns the expected boolean result. */ @@ -213,6 +255,34 @@ record AsciiDocTestCase(String id, String description, Map<String,String> values } } + /** + * Verifies an operation returning a geometry: its result against the {@code result} geometry, + * or its failure against the {@code error} value. The {@code compare} value selects the comparison: + * + * <ul> + * <li>{@code exact} (the default): the Well-Known Text 3 of the two geometries are equal.</li> + * <li>{@code normalized}: the same after {@link Geometry#normalized()}, which ignores the + * first vertex of the rings and the order of the parts.</li> + * <li>{@code emptiness}: the result is empty, whatever its type.</li> + * </ul> + * + * @param operation the operation to test, which shall not decode the geometries itself. + */ + void assertGeometry(final Supplier<Geometry> operation) { + if (expectsError()) { + assertError(operation::get); + return; + } + final Geometry actual = operation.get(); + final WellKnownText3 codec = new WellKnownText3(); + switch (values.getOrDefault("compare", "exact")) { + case "exact" -> assertEquals(codec.encode(geometry("result")), codec.encode(actual), description); + case "normalized" -> assertEquals(codec.encode(geometry("result").normalized()), codec.encode(actual.normalized()), description); + case "emptiness" -> assertTrue(actual.isEmpty(), description); + default -> fail("Test " + id + " has an unknown comparison: " + values.get("compare")); + } + } + /** * Returns the identifier, which is the JUnit display name of the test case. */ diff --git a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/BufferTest.adoc b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/BufferTest.adoc new file mode 100644 index 0000000000..41cd86cc37 --- /dev/null +++ b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/BufferTest.adoc @@ -0,0 +1,61 @@ += Buffer +:description: Test cases of GeometryProcessor.buffer(Geometry, Quantity). + +Each level-2 section is one test. Its title is the test id, the paragraph below it +describes the case, and the labeled list gives the operands in Well-Known Text 3 +and the expected result or error. + +A geometry result is compared with the `result` geometry by their Well-Known Text 3, +unless a `compare` value says otherwise: `normalized` compares them after `Geometry.normalized()`, +ignoring the first vertex of the rings and the order of the parts, and `emptiness` only checks +that the result is empty. + +The `radius` is a quantity: a number followed by a unit symbol, or a plain number for a dimensionless radius, +which is interpreted in the units of the coordinate system. + +include::RelationShapes.adoc[] + +// ─── The empty set ───────────────────────────────────────────────────────────── + +== empty-positive-radius + +There is no position to grow a buffer around, so the buffer of the empty set is the empty set. + +input:: `G_GEOMETRY EMPTY` +radius:: `10 m` +result:: `G_GEOMETRY EMPTY` + +== empty-zero-radius + +The buffer of the empty set with a radius of zero is the empty set. + +input:: `G_GEOMETRY EMPTY` +radius:: `0 m` +result:: `G_GEOMETRY EMPTY` + +== empty-negative-radius + +A negative radius shrinks a geometry: the empty set stays empty. + +input:: `G_GEOMETRY EMPTY` +radius:: `-10 m` +result:: `G_GEOMETRY EMPTY` + +== empty-dimensionless-radius + +A dimensionless radius is in the units of the coordinate system: the empty set stays empty. + +input:: `G_GEOMETRY EMPTY` +radius:: `10` +result:: `G_GEOMETRY EMPTY` + +// ─── Points ──────────────────────────────────────────────────────────────────── + +== point-zero-radius + +The buffer of a point with a radius of zero is empty. + +input:: `G_POINT [10 5]` +radius:: `0 m` +result:: `G_GEOMETRY EMPTY` +compare:: `emptiness` diff --git a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/BufferTest.java b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/BufferTest.java index e602b83f56..1b3796a21a 100644 --- a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/BufferTest.java +++ b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/BufferTest.java @@ -16,6 +16,7 @@ */ package org.apache.sis.geometries.operation; +import java.util.stream.Stream; import javax.measure.Quantity; import org.apache.sis.geometries.Geometry; import org.apache.sis.geometries.Surface; @@ -23,12 +24,12 @@ import org.apache.sis.measure.Quantities; import org.apache.sis.measure.Units; // Test dependencies -import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertInstanceOf; -import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertTrue; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; /** @@ -38,51 +39,26 @@ import org.junit.jupiter.api.Test; */ public class BufferTest extends AbstractD9IMTest { /** - * The inputs and expected result of a single test of {@code buffer(Geometry, Quantity)}. + * Returns the test cases declared in {@code BufferTest.adoc}. + * This method is public because JUnit cannot open the package of a named module. * - * @param input the geometry on which the operation is invoked. - * @param radius the buffer radius. - * @param expected the expected result, or {@code null} if an exception is expected. - * @param error the type of the expected exception, or {@code null} if the operation should succeed. + * @return the test cases. */ - private record TestCase(Geometry input, - Quantity<?> radius, - Geometry expected, - Class<? extends Exception> error) - { + public static Stream<AsciiDocTestCase> cases() { + return AsciiDocTestCase.read(BufferTest.class, "BufferTest.adoc"); } /** - * All test cases of {@code buffer(Geometry, Quantity)}. - */ - private static final TestCase[] RADIUS_ENTRIES = { - /* - * There is no position to grow a buffer around, therefore the buffer of an empty geometry - * is the empty geometry. This holds for a negative radius, which shrinks a geometry, and - * for a dimensionless radius, which is interpreted in the units of the coordinate system. - */ - new TestCase(EMPTY_1, Quantities.create( 10, Units.METRE), EMPTY_1, null), - new TestCase(EMPTY_1, Quantities.create( 0, Units.METRE), EMPTY_1, null), - new TestCase(EMPTY_1, Quantities.create(-10, Units.METRE), EMPTY_1, null), - new TestCase(EMPTY_1, Quantities.create( 10, Units.UNITY), EMPTY_1, null) - }; - - /** - * Tests {@code buffer(Geometry, Length)} on all declared test cases. + * Tests {@code buffer(Geometry, Quantity)} on one test case of {@code BufferTest.adoc}. + * + * @param test the test case. */ - @Test - public void testBufferByRadius() { - for (final TestCase entry : RADIUS_ENTRIES) { - try { - final Geometry result = new GeometryProcessor().buffer(entry.input(), entry.radius()); - assertNull(entry.error(), "An exception was expected."); - assertEquals(entry.expected(), result); - } catch (Exception ex) { - if (entry.error() == null || !entry.error().isInstance(ex)) { - throw new AssertionError("Unexpected exception for " + entry, ex); - } - } - } + @ParameterizedTest(name = "{0}") + @MethodSource("cases") + public void testBuffer(final AsciiDocTestCase test) { + final Geometry input = test.geometry("input"); + final Quantity<?> radius = test.quantity("radius"); + test.assertGeometry(() -> new GeometryProcessor().buffer(input, radius)); } /** @@ -106,13 +82,4 @@ public class BufferTest extends AbstractD9IMTest { assertTrue(area >= 0.95 * disk, () -> "Area " + area + " is too far from the disk area " + disk); } - /** - * Tests that a buffer of radius zero around a point gives an empty geometry: - * a point has no surface to preserve. - */ - @Test - public void testBufferOfPointWithZeroRadius() { - final Geometry result = new GeometryProcessor().buffer(POINT_A, Quantities.create( 0, Units.METRE)); - assertTrue(result.isEmpty(), "The buffer of a point with a radius of zero is empty."); - } } diff --git a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/ConvexHullTest.adoc b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/ConvexHullTest.adoc new file mode 100644 index 0000000000..8f350af15c --- /dev/null +++ b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/ConvexHullTest.adoc @@ -0,0 +1,83 @@ += Convex hull +:description: Test cases of GeometryProcessor.convexHull(Geometry). + +Each level-2 section is one test. Its title is the test id, the paragraph below it +describes the case, and the labeled list gives the operands in Well-Known Text 3 +and the expected result or error. + +A geometry result is compared with the `result` geometry by their Well-Known Text 3, +unless a `compare` value says otherwise: `normalized` compares them after `Geometry.normalized()`, +ignoring the first vertex of the rings and the order of the parts, and `emptiness` only checks +that the result is empty. + +include::RelationShapes.adoc[] + +// ─── Degenerate inputs ───────────────────────────────────────────────────────── + +== empty + +The convex hull of the empty set is the empty set. + +input:: `G_GEOMETRY EMPTY` +result:: `G_GEOMETRY EMPTY` + +== point + +A point is its own convex hull. + +input:: `G_POINT [10 5]` +result:: `G_POINT [10 5]` + +== points-same + +Two points at the same position have that point as convex hull. + +input:: `G_COLLECTION POINT [1 1, 1 1]` +result:: `G_POINT [1 1]` + +== points-aligned + +The convex hull of two distinct points is the segment joining them. + +input:: `G_COLLECTION POINT [5 5, 20 20]` +result:: `G_LINESTRING [5 5, 20 20]` + +// ─── Polygonal hulls ─────────────────────────────────────────────────────────── + +== line-self-crossing + +The convex hull of a curve visiting the four corners of a square is that square, counter-clockwise from its first vertex. + +input:: `G_LINESTRING [0 0, 10 10, 10 0, 0 10]` +result:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0])` + +== attributes + +The vertices of the hull keep their attributes, and the inner vertex is dropped with its attribute. + +input:: `G_LINESTRING <P:2, TEST:1> [0 0, 2 1, 4 0, 2 4] [1, 2, 3, 4]` +result:: `G_POLYGON <P:2, TEST:1> ([0 0, 4 0, 2 4, 0 0] [1, 3, 4, 1])` + +== square + +A square is convex, so it is its own hull. + +input:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0])` +result:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0])` +compare:: `normalized` + +== square-hole + +The hull of a square with a hole is the square, the hole filled. + +input:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0], [4 4, 4 6, 6 6, 6 4, 4 4])` +result:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0])` +compare:: `normalized` + +== squares-diagonal + +The hull of two squares placed along a diagonal is a hexagon, its exterior ring counter-clockwise. + +input:: `G_COLLECTION POLYGON (G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0]), G_POLYGON ([20 20, 30 20, 30 30, 20 30, 20 20]))` +result:: `G_POLYGON ([0 0, 10 0, 30 20, 30 30, 20 30, 0 10, 0 0])` +compare:: `normalized` diff --git a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/ConvexHullTest.java b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/ConvexHullTest.java index 2d60ca7364..c289e93ede 100644 --- a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/ConvexHullTest.java +++ b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/ConvexHullTest.java @@ -16,126 +16,46 @@ */ package org.apache.sis.geometries.operation; -import java.util.LinkedHashMap; -import java.util.Map; -import org.apache.sis.geometries.DataPointsType; +import java.util.stream.Stream; import org.apache.sis.geometries.Geometry; -import org.apache.sis.geometries.GeometryFactory; -import org.apache.sis.geometries.Point; -import org.apache.sis.geometries.curve.LineString; -import org.apache.sis.geometries.surface.Polygon; -import org.apache.sis.maths.Array; -import org.apache.sis.maths.NDArrays; // Test dependencies -import static org.junit.jupiter.api.Assertions.assertArrayEquals; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertInstanceOf; -import static org.junit.jupiter.api.Assertions.assertNull; -import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; /** - * Tests the {@code convexHull} operations of {@link GeometryProcessor}. + * Tests the {@code convexHull} operation of {@link GeometryProcessor}. + * The test cases are declared in {@code ConvexHullTest.adoc}. * * @author Johann Sorel (Geomatys) */ -public class ConvexHullTest extends AbstractD9IMTest { +public class ConvexHullTest { /** - * The inputs and expected result of a single test of {@code convexHull(Geometry)}. - * - * @param input the geometry on which the operation is invoked. - * @param expected the expected result, or {@code null} if an exception is expected. - * @param error the type of the expected exception, or {@code null} if the operation should succeed. - */ - private record TestCase(Geometry input, - Geometry expected, - Class<? extends Exception> error) - { - } - - /** - * All test cases of {@code convexHull(Geometry)}. - */ - private static final TestCase[] ENTRIES = { - // The convex hull of the empty set is empty. - new TestCase(EMPTY_1, EMPTY_1, null), - // points - new TestCase(POINT_A, POINT_A, null) - }; - - /** - * Tests {@code convexHull(Geometry)} on all declared test cases. - */ - @Test - public void testConvexHull() { - for (final TestCase entry : ENTRIES) { - try { - final Geometry result = new GeometryProcessor().convexHull(entry.input()); - assertNull(entry.error(), "An exception was expected."); - assertEquals(entry.expected(), result); - } catch (Exception ex) { - if (entry.error() == null || !entry.error().isInstance(ex)) { - throw new AssertionError("Unexpected exception for " + entry, ex); - } - } - } - } - - /** - * Tests {@code convexHull(Geometry)} on the geometries which are not already convex. - * The hull of a convex surface is that surface, and the hull of a curve crossing itself - * is the smallest convex surface enclosing all of its positions. + * Creates a new test case. */ - @Test - public void testConvexHullOfShapes() { - final GeometryProcessor processor = new GeometryProcessor(); - assertEquals(100, areaOf(processor.convexHull(SQUARE)), TOLERANCE, "A square is already convex."); - assertEquals(100, areaOf(processor.convexHull(SQUARE_WITH_HOLE)), TOLERANCE, "The hull of a square with a hole fills that hole."); - /* - * The four positions of that curve are the four corners of a square of 10 × 10, - * whatever the order in which the curve visits them. - */ - assertEquals(100, areaOf(processor.convexHull(LINE_SELF_CROSSING)), TOLERANCE, "The hull of a curve visiting the four corners of a square is that square."); - /* - * The hull of the two squares [0…10]² and [20…30]² is the hexagon - * (0 0), (10 0), (30 20), (30 30), (20 30), (0 10), of area 500. - */ - assertEquals(500, areaOf(processor.convexHull(MULTI_POLYGON)), TOLERANCE, "The hull of two squares placed along a diagonal is a hexagon."); - } - - private static double[] positions(final Geometry geometry) { - return geometry.getDataPoints().getAttributeArray(DataPointsType.ATT_POSITION).toArrayDouble(); + public ConvexHullTest() { } /** - * The hull of positions in 2 dimensions is computed natively: its ring is counterclockwise - * and starts at the lowest position. + * Returns the test cases declared in {@code ConvexHullTest.adoc}. + * This method is public because JUnit cannot open the package of a named module. + * + * @return the test cases. */ - @Test - public void testConvexHullVertices() { - final GeometryProcessor processor = new GeometryProcessor(); - final Polygon hull = assertInstanceOf(Polygon.class, processor.convexHull(LINE_SELF_CROSSING)); - assertArrayEquals(new double[] {0,0, 10,0, 10,10, 0,10, 0,0}, positions(hull)); - - final LineString aligned = assertInstanceOf(LineString.class, processor.convexHull(MULTI_POINT_SPREAD)); - assertArrayEquals(new double[] {5,5, 20,20}, positions(aligned)); - - final Point same = assertInstanceOf(Point.class, processor.convexHull(wkt("MULTIPOINT ((1 1), (1 1))"))); - assertArrayEquals(new double[] {1, 1}, same.getPosition().toArrayDouble()); + public static Stream<AsciiDocTestCase> cases() { + return AsciiDocTestCase.read(ConvexHullTest.class, "ConvexHullTest.adoc"); } /** - * The vertices of the hull keep their attributes. + * Tests {@code convexHull(Geometry)} on one test case of {@code ConvexHullTest.adoc}. + * + * @param test the test case. */ - @Test - public void testConvexHullAttributes() { - final Map<String,Array> attributes = new LinkedHashMap<>(); - attributes.put(DataPointsType.ATT_POSITION, NDArrays.of(CRS_2D, 0, 0, 2, 1, 4, 0, 2, 4)); - attributes.put("TEST", NDArrays.of(1, 1.0, 2, 3, 4)); - final LineString line = GeometryFactory.DEFAULT.createLineString(GeometryFactory.DEFAULT.createDataPoints(attributes)); - final Polygon hull = assertInstanceOf(Polygon.class, new GeometryProcessor().convexHull(line)); - assertArrayEquals(new double[] {0,0, 4,0, 2,4, 0,0}, positions(hull)); - assertArrayEquals(new double[] {1, 3, 4, 1}, hull.getDataPoints().getAttributeArray("TEST").toArrayDouble()); + @ParameterizedTest(name = "{0}") + @MethodSource("cases") + public void testConvexHull(final AsciiDocTestCase test) { + final Geometry input = test.geometry("input"); + test.assertGeometry(() -> new GeometryProcessor().convexHull(input)); } } diff --git a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/DifferenceTest.adoc b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/DifferenceTest.adoc new file mode 100644 index 0000000000..b61c5ec941 --- /dev/null +++ b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/DifferenceTest.adoc @@ -0,0 +1,104 @@ += Difference +:description: Test cases of GeometryProcessor.difference(Geometry, Geometry). + +Each level-2 section is one test. Its title is the test id, the paragraph below it +describes the case, and the labeled list gives the operands in Well-Known Text 3 +and the expected result or error. + +A geometry result is compared with the `result` geometry by their Well-Known Text 3, +unless a `compare` value says otherwise: `normalized` compares them after `Geometry.normalized()`, +ignoring the first vertex of the rings and the order of the parts, and `emptiness` only checks +that the result is empty. + +include::RelationShapes.adoc[] + +// ─── The empty set ───────────────────────────────────────────────────────────── + +== empty-point + +The empty set less anything is the empty set. + +input:: `G_GEOMETRY EMPTY` +other:: `G_POINT [10 5]` +result:: `G_GEOMETRY EMPTY` + +== point-empty + +A geometry less the empty set is that geometry. + +input:: `G_POINT [10 5]` +other:: `G_GEOMETRY EMPTY` +result:: `G_POINT [10 5]` + +== empty-empty + +The empty set less the empty set is the empty set. + +input:: `G_GEOMETRY EMPTY` +other:: `G_GEOMETRY EMPTY` +result:: `G_GEOMETRY EMPTY` + +// ─── Points ──────────────────────────────────────────────────────────────────── + +== point-other-position + +Removing a point at another position leaves the point unchanged. + +input:: `G_POINT [10 5]` +other:: `G_POINT [20 15]` +result:: `G_POINT [10 5]` + +== point-same-position + +Removing a point at the same position leaves nothing. + +input:: `G_POINT [10 5]` +other:: `G_POINT [10 5]` +result:: `G_GEOMETRY EMPTY` + +// ─── Surfaces ────────────────────────────────────────────────────────────────── + +== square-same-position + +Removing a square from itself leaves nothing. + +input:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0])` +other:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0])` +result:: `G_GEOMETRY EMPTY` +compare:: `emptiness` + +== square-overlap + +Removing a square which covers a quarter of the first one leaves a concave polygon, its exterior ring counter-clockwise. + +input:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0])` +other:: `G_POLYGON ([5 5, 15 5, 15 15, 5 15, 5 5])` +result:: `G_POLYGON ([0 0, 10 0, 10 5, 5 5, 5 10, 0 10, 0 0])` +compare:: `normalized` + +== square-inner + +Removing a square strictly inside the first one digs a hole, its ring clockwise. + +input:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0])` +other:: `G_POLYGON ([2 2, 4 2, 4 4, 2 4, 2 2])` +result:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0], [2 2, 2 4, 4 4, 4 2, 2 2])` +compare:: `normalized` + +== square-disjoint + +Removing a disjoint square leaves the first one unchanged. + +input:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0])` +other:: `G_POLYGON ([20 20, 30 20, 30 30, 20 30, 20 20])` +result:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0])` +compare:: `normalized` + +== square-touching + +Removing a square sharing only an edge removes no area, so the first square is unchanged. + +input:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0])` +other:: `G_POLYGON ([10 0, 20 0, 20 10, 10 10, 10 0])` +result:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0])` +compare:: `normalized` diff --git a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/DifferenceTest.java b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/DifferenceTest.java index 8d767d85eb..ebec0603ee 100644 --- a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/DifferenceTest.java +++ b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/DifferenceTest.java @@ -16,79 +16,47 @@ */ package org.apache.sis.geometries.operation; +import java.util.stream.Stream; import org.apache.sis.geometries.Geometry; // Test dependencies -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNull; -import static org.junit.jupiter.api.Assertions.assertTrue; -import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; /** - * Tests the {@code difference} operations of {@link GeometryProcessor}. + * Tests the {@code difference} operation of {@link GeometryProcessor}. + * The test cases are declared in {@code DifferenceTest.adoc}. * * @author Johann Sorel (Geomatys) */ -public class DifferenceTest extends AbstractD9IMTest { +public class DifferenceTest { /** - * The inputs and expected result of a single test of {@code difference(Geometry, Geometry)}. - * - * @param input the geometry on which the operation is invoked. - * @param other the other operand. - * @param expected the expected result, or {@code null} if an exception is expected. - * @param error the type of the expected exception, or {@code null} if the operation should succeed. + * Creates a new test case. */ - private record TestCase(Geometry input, - Geometry other, - Geometry expected, - Class<? extends Exception> error) - { + public DifferenceTest() { } /** - * All test cases of {@code difference(Geometry, Geometry)}. - */ - private static final TestCase[] ENTRIES = { - // ∅ − A = ∅ and A − ∅ = A, which are both the first operand. - new TestCase(EMPTY_1, NON_EMPTY, EMPTY_1, null), - new TestCase(NON_EMPTY, EMPTY_1, NON_EMPTY, null), - new TestCase(EMPTY_1, EMPTY_2, EMPTY_1, null), - // points - new TestCase(POINT_A, POINT_B, POINT_A, null), - new TestCase(POINT_A, POINT_A_BIS, EMPTY_RESULT, null) - }; - - /** - * Tests {@code difference(Geometry, Geometry)} on all declared test cases. + * Returns the test cases declared in {@code DifferenceTest.adoc}. + * This method is public because JUnit cannot open the package of a named module. + * + * @return the test cases. */ - @Test - public void testDifference() { - for (final TestCase entry : ENTRIES) { - try { - final Geometry result = new GeometryProcessor().difference(entry.input(), entry.other()); - assertNull(entry.error(), "An exception was expected."); - assertEquals(entry.expected(), result); - } catch (Exception ex) { - if (entry.error() == null || !entry.error().isInstance(ex)) { - throw new AssertionError("Unexpected exception for " + entry, ex); - } - } - } + public static Stream<AsciiDocTestCase> cases() { + return AsciiDocTestCase.read(DifferenceTest.class, "DifferenceTest.adoc"); } /** - * Tests {@code difference(Geometry, Geometry)} on two surfaces. Removing a surface from - * another one leaves the part of the first one which the second does not cover. + * Tests {@code difference(Geometry, Geometry)} on one test case of {@code DifferenceTest.adoc}. + * + * @param test the test case. */ - @Test - public void testDifferenceOfSurfaces() { - final GeometryProcessor processor = new GeometryProcessor(); - assertEquals(75, areaOf(processor.difference(SQUARE, SQUARE_OVERLAP)), TOLERANCE, "A square of area 100 less the area of 25 it shares with another one."); - assertEquals(96, areaOf(processor.difference(SQUARE, SQUARE_INNER)), TOLERANCE, "A square of area 100 less an inner square of area 4, which digs a hole."); - assertEquals(100, areaOf(processor.difference(SQUARE, SQUARE_DISJOINT)), TOLERANCE, "Removing a disjoint surface changes nothing."); - assertEquals(100, areaOf(processor.difference(SQUARE, SQUARE_TOUCHING)), TOLERANCE, "Removing a surface sharing only an edge changes no area."); - assertTrue(processor.difference(SQUARE, SQUARE_BIS).isEmpty(), "Removing a square from itself leaves nothing."); + @ParameterizedTest(name = "{0}") + @MethodSource("cases") + public void testDifference(final AsciiDocTestCase test) { + final Geometry input = test.geometry("input"); + final Geometry other = test.geometry("other"); + test.assertGeometry(() -> new GeometryProcessor().difference(input, other)); } - } diff --git a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/DistanceTest.adoc b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/DistanceTest.adoc new file mode 100644 index 0000000000..2f309dbda4 --- /dev/null +++ b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/DistanceTest.adoc @@ -0,0 +1,79 @@ += Distance +:description: Test cases of GeometryProcessor.distance(Geometry, Geometry). + +Each level-2 section is one test. Its title is the test id, the paragraph below it +describes the case, and the labeled list gives the operands in Well-Known Text 3 +and the expected result or error. + +The `result` is a quantity: a number followed by a unit symbol, or `inf` for a dimensionless infinity. +The distance is expressed in the unit of the axes of the coordinate reference system. + +include::RelationShapes.adoc[] + +// ─── Points ──────────────────────────────────────────────────────────────────── + +== point-same-position + +Two points at the same position are at a distance of zero. + +input:: `G_POINT {P:crs='EPSG:4326'} [10 5]` +other:: `G_POINT {P:crs='EPSG:4326'} [10 5]` +result:: `0 deg` + +== point-one-degree + +Two points one degree apart along the second axis. + +input:: `G_POINT {P:crs='EPSG:4326'} [10 5]` +other:: `G_POINT {P:crs='EPSG:4326'} [10 6]` +result:: `1 deg` + +== point-one-degree-reversed + +The distance is symmetric. + +input:: `G_POINT {P:crs='EPSG:4326'} [10 6]` +other:: `G_POINT {P:crs='EPSG:4326'} [10 5]` +result:: `1 deg` + +== point-3d-vertical + +Two points differing only by their height are at a horizontal distance of zero. + +input:: `G_POINT <P:3> {P:crs='EPSG:4979'} [10 5 0]` +other:: `G_POINT <P:3> {P:crs='EPSG:4979'} [10 5 100]` +result:: `0 deg` + +== crs-mismatch + +The distance between geometries in different reference systems is not computed. + +input:: `G_POINT {P:crs='EPSG:4326'} [0 0]` +other:: `G_POINT {P:crs='CRS:84'} [0 0]` +error:: `OperationException` + +// ─── The empty set ───────────────────────────────────────────────────────────── + +== empty-point + +The empty set is at an infinite distance from anything, with no unit to report. + +input:: `G_GEOMETRY {P:crs='EPSG:4326'} EMPTY` +other:: `G_POINT {P:crs='EPSG:4326'} [10 5]` +result:: `inf` + +== point-empty + +Anything is at an infinite distance from the empty set. + +input:: `G_POINT {P:crs='EPSG:4326'} [10 5]` +other:: `G_GEOMETRY {P:crs='EPSG:4326'} EMPTY` +result:: `inf` + +== empty-empty + +Two empty sets are at an infinite distance from each other. + +input:: `G_GEOMETRY {P:crs='EPSG:4326'} EMPTY` +other:: `G_GEOMETRY {P:crs='EPSG:4326'} EMPTY` +result:: `inf` diff --git a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/DistanceTest.java b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/DistanceTest.java index 233a52be81..be4db03c2c 100644 --- a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/DistanceTest.java +++ b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/DistanceTest.java @@ -18,6 +18,7 @@ package org.apache.sis.geometries.operation; import java.util.Arrays; import java.util.List; +import java.util.stream.Stream; import javax.measure.Quantity; import org.apache.sis.geometries.Geometry; import org.apache.sis.geometries.GeometryFactory; @@ -26,20 +27,18 @@ import org.apache.sis.geometries.Point; import org.apache.sis.geometries.surface.Capsule; import org.apache.sis.geometries.surface.Sphere; import org.apache.sis.geometries.surface.Triangle; -import org.apache.sis.maths.SampleSystem; import org.apache.sis.maths.Vector; import org.apache.sis.maths.DataType; import org.apache.sis.maths.Vectors; -import org.apache.sis.measure.Quantities; import org.apache.sis.measure.Units; -import org.apache.sis.referencing.CommonCRS; // Test dependencies import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertThrows; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; /** @@ -48,98 +47,30 @@ import org.junit.jupiter.api.Test; */ public class DistanceTest extends AbstractD9IMTest { - private static final SampleSystem CRS2D = SampleSystem.of(CommonCRS.WGS84.geographic()); - - /** - * Two points at the same position, and a third one at a distance of 1 along the second axis. - */ - private static final Point POINT_10_5 = GeometryFactory.DEFAULT.createPoint(CRS2D, 10.0, 5.0); - private static final Point POINT_10_5_BIS = GeometryFactory.DEFAULT.createPoint(CRS2D, 10.0, 5.0); - private static final Point POINT_10_6 = GeometryFactory.DEFAULT.createPoint(CRS2D, 10.0, 6.0); - - /** - * Two points in a three-dimensional coordinate reference system, at the same horizontal - * position but at different heights. - */ - private static final SampleSystem CRS3D = SampleSystem.of(CommonCRS.WGS84.geographic3D()); - private static final Point POINT_3D_LOW = GeometryFactory.DEFAULT.createPoint(CRS3D, 10.0, 5.0, 0.0); - private static final Point POINT_3D_HIGH = GeometryFactory.DEFAULT.createPoint(CRS3D, 10.0, 5.0, 100.0); - /** - * Two points using coordinate reference systems which differ by their axis order. - */ - private static final Point POINT_GEOGRAPHIC = GeometryFactory.DEFAULT.createPoint(CommonCRS.WGS84.geographic()); - private static final Point POINT_NORMALIZED = GeometryFactory.DEFAULT.createPoint(CommonCRS.WGS84.normalizedGeographic()); - - /** - * The distance from an empty geometry to any geometry, including itself, is infinite. - * The quantity is dimensionless, the empty set having no measurement unit to report. - */ - private static final Quantity<?> INFINITY = Quantities.create(Double.POSITIVE_INFINITY, Units.UNITY); - - /** - * The inputs and expected result of a single test of {@code distance(Geometry, Geometry)}. + * Returns the test cases declared in {@code DistanceTest.adoc}. + * This method is public because JUnit cannot open the package of a named module. * - * @param input the geometry on which the operation is invoked. - * @param other the other operand. - * @param expected the expected result, or {@code null} if an exception is expected. - * @param error the type of the expected exception, or {@code null} if the operation should succeed. + * @return the test cases. */ - private record TestCase(Geometry input, - Geometry other, - Quantity<?> expected, - Class<? extends Exception> error) - { + public static Stream<AsciiDocTestCase> cases() { + return AsciiDocTestCase.read(DistanceTest.class, "DistanceTest.adoc"); } /** - * All test cases of {@code distance(Geometry, Geometry)}. - */ - private static final TestCase[] ENTRIES = { - /* - * Point to point. The distance is zero when the two points are at the same position, and is - * otherwise the Pythagorean distance computed in the units of the coordinate system axes. - * The unit reported is metre in both cases; see the limitation documented on - * `GeometryProcessor.distance(Geometry, Geometry)`. - */ - new TestCase(POINT_10_5, POINT_10_5_BIS, Quantities.create(0.0, Units.DEGREE), null), - new TestCase(POINT_10_5, POINT_10_6, Quantities.create(1.0, Units.DEGREE), null), - new TestCase(POINT_10_6, POINT_10_5, Quantities.create(1.0, Units.DEGREE), null), - /* - * TODO / Limitation: only the first two axes are taken in account, therefore two points - * at the same horizontal position are reported at a distance of zero whatever the - * difference of their heights. - */ - new TestCase(POINT_3D_LOW, POINT_3D_HIGH, Quantities.create(0.0, Units.DEGREE), null), - /* - * The operation computes in the coordinate reference system of the first geometry, - * and does not transform the second one. - */ - new TestCase(POINT_GEOGRAPHIC, POINT_NORMALIZED, null, OperationException.class), - /* - * The distance from an empty geometry to any geometry, including itself, is infinite. - */ - new TestCase(EMPTY_1, NON_EMPTY, INFINITY, null), - new TestCase(NON_EMPTY, EMPTY_1, INFINITY, null), - new TestCase(EMPTY_1, EMPTY_1, INFINITY, null), - new TestCase(EMPTY_1, EMPTY_2, INFINITY, null) - }; - - /** - * Tests {@code distance(Geometry, Geometry)} on all declared test cases. + * Tests {@code distance(Geometry, Geometry)} on one test case of {@code DistanceTest.adoc}. + * + * @param test the test case. */ - @Test - public void testDistance() { - for (final TestCase entry : ENTRIES) { - try { - final Quantity<?> result = new GeometryProcessor().distance(entry.input(), entry.other()); - assertNull(entry.error(), "An exception was expected."); - assertEquals(entry.expected(), result); - } catch (Exception ex) { - if (entry.error() == null || !entry.error().isInstance(ex)) { - throw new AssertionError("Unexpected exception for " + entry, ex); - } - } + @ParameterizedTest(name = "{0}") + @MethodSource("cases") + public void testDistance(final AsciiDocTestCase test) { + final Geometry input = test.geometry("input"); + final Geometry other = test.geometry("other"); + if (test.expectsError()) { + test.assertError(() -> new GeometryProcessor().distance(input, other)); + } else { + assertEquals(test.quantity("result"), new GeometryProcessor().distance(input, other), test.description()); } } diff --git a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/IntersectionTest.adoc b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/IntersectionTest.adoc new file mode 100644 index 0000000000..8964045c84 --- /dev/null +++ b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/IntersectionTest.adoc @@ -0,0 +1,160 @@ += Intersection +:description: Test cases of GeometryProcessor.intersection(Geometry, Geometry). + +Each level-2 section is one test. Its title is the test id, the paragraph below it +describes the case, and the labeled list gives the operands in Well-Known Text 3 +and the expected result or error. + +A geometry result is compared with the `result` geometry by their Well-Known Text 3, +unless a `compare` value says otherwise: `normalized` compares them after `Geometry.normalized()`, +ignoring the first vertex of the rings and the order of the parts, and `emptiness` only checks +that the result is empty. + +include::RelationShapes.adoc[] + +// ─── The empty set ───────────────────────────────────────────────────────────── + +== empty-point + +The intersection with the empty set is the empty set. + +input:: `G_GEOMETRY EMPTY` +other:: `G_POINT [10 5]` +result:: `G_GEOMETRY EMPTY` + +== point-empty + +The intersection with the empty set is the empty set, whatever the operand order. + +input:: `G_POINT [10 5]` +other:: `G_GEOMETRY EMPTY` +result:: `G_GEOMETRY EMPTY` + +== empty-empty + +The intersection of two empty sets is the empty set. + +input:: `G_GEOMETRY EMPTY` +other:: `G_GEOMETRY EMPTY` +result:: `G_GEOMETRY EMPTY` + +// ─── Points ──────────────────────────────────────────────────────────────────── + +== point-same-position + +Two points at the same position intersect at that point. + +input:: `G_POINT [10 5]` +other:: `G_POINT [10 5]` +result:: `G_POINT [10 5]` + +== point-other-position + +Two points at different positions have nothing in common. + +input:: `G_POINT [10 5]` +other:: `G_POINT [20 15]` +result:: `G_GEOMETRY EMPTY` + +// ─── Surfaces ────────────────────────────────────────────────────────────────── + +== square-disjoint + +Two disjoint squares have nothing in common. + +input:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0])` +other:: `G_POLYGON ([20 20, 30 20, 30 30, 20 30, 20 20])` +result:: `G_GEOMETRY EMPTY` +compare:: `emptiness` + +== square-overlap + +Two squares offset by 5 along both axes have in common a square of 5 × 5, its exterior ring counter-clockwise. + +input:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0])` +other:: `G_POLYGON ([5 5, 15 5, 15 15, 5 15, 5 5])` +result:: `G_POLYGON ([5 5, 10 5, 10 10, 5 10, 5 5])` +compare:: `normalized` + +== square-inner + +What a square has in common with a square it contains is the contained square. + +input:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0])` +other:: `G_POLYGON ([2 2, 4 2, 4 4, 2 4, 2 2])` +result:: `G_POLYGON ([2 2, 4 2, 4 4, 2 4, 2 2])` +compare:: `normalized` + +== square-touching + +Two squares sharing only an edge have that edge in common: a curve, of a lower dimension than the operands. + +input:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0])` +other:: `G_POLYGON ([10 0, 20 0, 20 10, 10 10, 10 0])` +result:: `G_LINESTRING [10 0, 10 10]` +compare:: `normalized` + +// ─── Three-dimensional geometries ────────────────────────────────────────────── + +== ray-box-beside + +A horizontal ray passing beside the box has nothing in common with it. + +input:: `G_RAY <P:3 ; DIRECTION:3> [-1 3 1] [1 0 0]` +other:: `G_BBOX <P:3> [0 0 0, 2 2 2]` +result:: `G_GEOMETRY EMPTY` +compare:: `emptiness` + +== ray-triangle-outside + +A vertical ray passing outside the triangle has nothing in common with it. + +input:: `G_RAY <P:3 ; DIRECTION:3> [3 3 0] [0 0 1]` +other:: `G_TRIANGLE <P:3, TEST:1> ([0 0 5, 4 0 5, 0 4 5, 0 0 5] [10, 20, 30, 10])` +result:: `G_GEOMETRY EMPTY` +compare:: `emptiness` + +== triangle-plane-beside + +The plane x = 9 is beside the triangle. + +input:: `G_TRIANGLE <P:3, TEST:1> ([0 0 5, 4 0 5, 0 4 5, 0 0 5] [10, 20, 30, 10])` +other:: `G_PLANE <P:3 ; NORMAL:3> [9 0 0] [1 0 0]` +result:: `G_GEOMETRY EMPTY` +compare:: `emptiness` + +== ray-plane-away + +A ray going down never reaches the plane z = 5. + +input:: `G_RAY <P:3 ; DIRECTION:3> [1 2 0] [0 0 -1]` +other:: `G_HYPERPLANE <P:3 ; NORMAL:3> [0 0 5] [0 0 1]` +result:: `G_GEOMETRY EMPTY` +compare:: `emptiness` + +== ray-ring-above + +A horizontal ray above the plane of the 3D ring misses it. + +input:: `G_RAY <P:3 ; DIRECTION:3> [-1 2 2] [1 0 0]` +other:: `G_LINESTRING <P:3, TEST:1> [0 0 1, 4 0 1, 4 4 1, 0 4 1, 0 0 1] [0, 10, 20, 40, 0]` +result:: `G_GEOMETRY EMPTY` +compare:: `emptiness` + +// ─── Unsupported operands ────────────────────────────────────────────────────── + +== ray-square-unsupported + +The intersection of a 3D ray with a 2D polygon is not supported. + +input:: `G_RAY <P:3 ; DIRECTION:3> [1 2 0] [0 0 1]` +other:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0])` +error:: `UnsupportedOperationException` + +== square-ray-unsupported + +The intersection of a 2D polygon with a 3D ray is not supported. + +input:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0])` +other:: `G_RAY <P:3 ; DIRECTION:3> [1 2 0] [0 0 1]` +error:: `UnsupportedOperationException` diff --git a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/IntersectionTest.java b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/IntersectionTest.java index 0b2f6d02d7..a3bb5b68c8 100644 --- a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/IntersectionTest.java +++ b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/IntersectionTest.java @@ -16,6 +16,7 @@ */ package org.apache.sis.geometries.operation; +import java.util.stream.Stream; import org.opengis.geometry.Envelope; import org.apache.sis.geometries.Geometry; import org.apache.sis.geometries.GeometryCollection; @@ -40,11 +41,12 @@ import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertInstanceOf; -import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertSame; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; /** @@ -162,68 +164,26 @@ public class IntersectionTest extends AbstractD9IMTest { } /** - * The inputs and expected result of a single test of {@code intersection(Geometry, Geometry)}. + * Returns the test cases declared in {@code IntersectionTest.adoc}. + * This method is public because JUnit cannot open the package of a named module. * - * @param input the geometry on which the operation is invoked. - * @param other the other operand. - * @param expected the expected result, or {@code null} if an exception is expected. - * @param error the type of the expected exception, or {@code null} if the operation should succeed. + * @return the test cases. */ - private record TestCase(Geometry input, - Geometry other, - Geometry expected, - Class<? extends Exception> error) - { + public static Stream<AsciiDocTestCase> cases() { + return AsciiDocTestCase.read(IntersectionTest.class, "IntersectionTest.adoc"); } /** - * All test cases of {@code intersection(Geometry, Geometry)}. - */ - private static final TestCase[] ENTRIES = { - // ∅ ∩ A = ∅ and A ∩ ∅ = ∅: the result is the operand which is already empty. - new TestCase(EMPTY_1, NON_EMPTY, EMPTY_1, null), - new TestCase(NON_EMPTY, EMPTY_1, EMPTY_1, null), - new TestCase(EMPTY_1, EMPTY_2, EMPTY_1, null), - // points - new TestCase(POINT_A, POINT_A_BIS, POINT_A, null), - new TestCase(POINT_A, POINT_B, EMPTY_RESULT, null) - }; - - /** - * Tests {@code intersection(Geometry, Geometry)} on all declared test cases. - */ - @Test - public void testIntersection() { - for (final TestCase entry : ENTRIES) { - try { - final Geometry result = new GeometryProcessor().intersection(entry.input(), entry.other()); - assertNull(entry.error(), "An exception was expected."); - assertEquals(entry.expected(), result); - } catch (Exception ex) { - if (entry.error() == null || !entry.error().isInstance(ex)) { - throw new AssertionError("Unexpected exception for " + entry, ex); - } - } - } - } - - /** - * Tests {@code intersection(Geometry, Geometry)} on two surfaces. - * What two surfaces have in common is the surface covered by both of them. + * Tests {@code intersection(Geometry, Geometry)} on one test case of {@code IntersectionTest.adoc}. + * + * @param test the test case. */ - @Test - public void testIntersectionOfSurfaces() { - final GeometryProcessor processor = new GeometryProcessor(); - assertEquals(25, areaOf(processor.intersection(SQUARE, SQUARE_OVERLAP)), TOLERANCE, "Two squares of 10 × 10 offset by 5 along both axes."); - assertEquals(4, areaOf(processor.intersection(SQUARE, SQUARE_INNER)), TOLERANCE, "A square contained in another one."); - assertTrue(processor.intersection(SQUARE, SQUARE_DISJOINT).isEmpty(), "Two disjoint squares have nothing in common."); - /* - * Two surfaces sharing only an edge have a curve in common, not a surface: - * the dimension of the result is lower than the dimension of the operands. - */ - final Geometry edge = processor.intersection(SQUARE, SQUARE_TOUCHING); - assertFalse(edge.isEmpty(), "Two surfaces sharing an edge have that edge in common."); - assertEquals(1, edge.getTopologicDimension(), "What two surfaces sharing only an edge have in common is a curve, not a surface."); + @ParameterizedTest(name = "{0}") + @MethodSource("cases") + public void testIntersection(final AsciiDocTestCase test) { + final Geometry input = test.geometry("input"); + final Geometry other = test.geometry("other"); + test.assertGeometry(() -> new GeometryProcessor().intersection(input, other)); } private static Vector<?> v(final double... coordinates) { @@ -358,7 +318,6 @@ public class IntersectionTest extends AbstractD9IMTest { final LineString inside = assertInstanceOf(LineString.class, processor.intersection(ray(-1, 1, 1, 1, 0, 0), BOX_3D)); assertArrayEquals(new double[] {0, 1, 1}, inside.getPointN(0).getPosition().toArrayDouble(), TOLERANCE); assertArrayEquals(new double[] {2, 1, 1}, inside.getPointN(1).getPosition().toArrayDouble(), TOLERANCE); - assertTrue(processor.intersection(ray(-1, 3, 1, 1, 0, 0), BOX_3D).isEmpty()); } /** @@ -416,7 +375,6 @@ public class IntersectionTest extends AbstractD9IMTest { assertAttributedPoint(1, 0, 5, 12.5, cut.getPointN(0)); assertAttributedPoint(1, 3, 5, 27.5, cut.getPointN(1)); assertSame(TRIANGLE_3D, processor.intersection(PLANE_Z5, TRIANGLE_3D)); - assertTrue(processor.intersection(TRIANGLE_3D, new Plane(CRS_3D, v(9, 0, 0), v(1, 0, 0))).isEmpty()); } /** @@ -463,7 +421,6 @@ public class IntersectionTest extends AbstractD9IMTest { final GeometryProcessor processor = new GeometryProcessor(); assertAttributedPoint(1, 2, 5, 22.5, processor.intersection(ray(1, 2, 0, 0, 0, 1), TRIANGLE_3D)); assertAttributedPoint(1, 2, 5, 22.5, processor.intersection(TRIANGLE_3D, ray(1, 2, 0, 0, 0, 1))); - assertTrue(processor.intersection(ray(3, 3, 0, 0, 0, 1), TRIANGLE_3D).isEmpty()); final LineString inside = assertInstanceOf(LineString.class, processor.intersection(ray(-1, 1, 5, 1, 0, 0), TRIANGLE_3D)); assertEquals(2, inside.getNumPoints()); @@ -480,7 +437,6 @@ public class IntersectionTest extends AbstractD9IMTest { final Point point = assertInstanceOf(Point.class, processor.intersection(ray(1, 2, 0, 0, 1, 1), PLANE_Z5)); assertArrayEquals(new double[] {1, 7, 5}, point.getPosition().toArrayDouble(), TOLERANCE); assertInstanceOf(Ray.class, processor.intersection(ray(1, 2, 5, 1, 0, 0), PLANE_Z5)); - assertTrue(processor.intersection(ray(1, 2, 0, 0, 0, -1), PLANE_Z5).isEmpty()); final Plane plane = new Plane(CRS_3D, v(0, 0, 5), v(0, 0, 1)); final Point onPlane = assertInstanceOf(Point.class, processor.intersection(ray(1, 2, 0, 0, 0, 1), plane)); @@ -510,16 +466,6 @@ public class IntersectionTest extends AbstractD9IMTest { assertAttributedPoint(0, 0, 1, 0, along.getPointN(0)); assertAttributedPoint(4, 0, 1, 10, along.getPointN(1)); - assertTrue(processor.intersection(ray(-1, 2, 2, 1, 0, 0), RING_3D).isEmpty()); } - /** - * The intersection of a ray with a geometry type which is not supported throws an exception. - */ - @Test - public void testIntersectionOfRayAndUnsupported() { - final GeometryProcessor processor = new GeometryProcessor(); - assertThrows(UnsupportedOperationException.class, () -> processor.intersection(ray(1, 2, 0, 0, 0, 1), SQUARE)); - assertThrows(UnsupportedOperationException.class, () -> processor.intersection(SQUARE, ray(1, 2, 0, 0, 0, 1))); - } } diff --git a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/LocateAlongTest.adoc b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/LocateAlongTest.adoc new file mode 100644 index 0000000000..5fea602c22 --- /dev/null +++ b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/LocateAlongTest.adoc @@ -0,0 +1,30 @@ += Locate along +:description: Test cases of GeometryProcessor.locateAlong(Geometry, String, Number). + +Each level-2 section is one test. Its title is the test id, the paragraph below it +describes the case, and the labeled list gives the operands in Well-Known Text 3 +and the expected result or error. + +The operation is not implemented yet: these cases verify that it fails cleanly. + +include::RelationShapes.adoc[] + +// ─── Not supported ───────────────────────────────────────────────────────────── + +== empty-unsupported + +Locating a measure along the empty set is not supported yet. + +input:: `G_GEOMETRY EMPTY` +attribute:: `M` +measure:: `1` +error:: `UnsupportedOperationException` + +== point-unsupported + +Locating a measure along a point is not supported yet. + +input:: `G_POINT [10 5]` +attribute:: `M` +measure:: `1` +error:: `UnsupportedOperationException` diff --git a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/LocateAlongTest.java b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/LocateAlongTest.java index 5e6268f49b..903a433f43 100644 --- a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/LocateAlongTest.java +++ b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/LocateAlongTest.java @@ -16,63 +16,48 @@ */ package org.apache.sis.geometries.operation; +import java.util.stream.Stream; import org.apache.sis.geometries.Geometry; -import org.apache.sis.geometries.DataPointsType; // Test dependencies -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNull; -import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; /** - * Tests the {@code locateAlong} operations of {@link GeometryProcessor}. + * Tests the {@code locateAlong} operation of {@link GeometryProcessor}. + * The test cases are declared in {@code LocateAlongTest.adoc}. * * @author Johann Sorel (Geomatys) */ -public class LocateAlongTest extends AbstractD9IMTest { +public class LocateAlongTest { /** - * The inputs and expected result of a single test of {@code locateAlong(Geometry, double)}. - * - * @param input the geometry on which the operation is invoked. - * @param mValue the measure value to locate. - * @param expected the expected result, or {@code null} if an exception is expected. - * @param error the type of the expected exception, or {@code null} if the operation should succeed. + * Creates a new test case. */ - private record TestCase(Geometry input, - String attName, - double mValue, - Geometry expected, - Class<? extends Exception> error) - { + public LocateAlongTest() { } /** - * All test cases of {@code locateAlong(Geometry, double)}. + * Returns the test cases declared in {@code LocateAlongTest.adoc}. + * This method is public because JUnit cannot open the package of a named module. + * + * @return the test cases. */ - private static final TestCase[] ENTRIES = { - /* - * TODO - */ - new TestCase(EMPTY_1, DataPointsType.ATT_M, 1, null, UnsupportedOperationException.class), - new TestCase(POINT_A, DataPointsType.ATT_M, 1, null, UnsupportedOperationException.class) - }; + public static Stream<AsciiDocTestCase> cases() { + return AsciiDocTestCase.read(LocateAlongTest.class, "LocateAlongTest.adoc"); + } /** - * Tests {@code locateAlong(Geometry, double)} on all declared test cases. + * Tests {@code locateAlong(Geometry, String, Number)} on one test case of {@code LocateAlongTest.adoc}. + * + * @param test the test case. */ - @Test - public void testLocateAlong() { - for (final TestCase entry : ENTRIES) { - try { - final Geometry result = new GeometryProcessor().locateAlong(entry.input(), entry.attName(), entry.mValue()); - assertNull(entry.error(), "An exception was expected."); - assertEquals(entry.expected(), result); - } catch (Exception ex) { - if (entry.error() == null || !entry.error().isInstance(ex)) { - throw new AssertionError("Unexpected exception for " + entry, ex); - } - } - } + @ParameterizedTest(name = "{0}") + @MethodSource("cases") + public void testLocateAlong(final AsciiDocTestCase test) { + final Geometry input = test.geometry("input"); + final String attribute = test.value("attribute"); + final double measure = test.number("measure"); + test.assertGeometry(() -> new GeometryProcessor().locateAlong(input, attribute, measure)); } } diff --git a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/LocateBetweenTest.adoc b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/LocateBetweenTest.adoc new file mode 100644 index 0000000000..25c6d975e7 --- /dev/null +++ b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/LocateBetweenTest.adoc @@ -0,0 +1,32 @@ += Locate between +:description: Test cases of GeometryProcessor.locateBetween(Geometry, String, Number, Number). + +Each level-2 section is one test. Its title is the test id, the paragraph below it +describes the case, and the labeled list gives the operands in Well-Known Text 3 +and the expected result or error. + +The operation is not implemented yet: these cases verify that it fails cleanly. + +include::RelationShapes.adoc[] + +// ─── Not supported ───────────────────────────────────────────────────────────── + +== empty-unsupported + +Locating a range of measures along the empty set is not supported yet. + +input:: `G_GEOMETRY EMPTY` +attribute:: `M` +start:: `1` +end:: `2` +error:: `UnsupportedOperationException` + +== point-unsupported + +Locating a range of measures along a point is not supported yet. + +input:: `G_POINT [10 5]` +attribute:: `M` +start:: `1` +end:: `2` +error:: `UnsupportedOperationException` diff --git a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/LocateBetweenTest.java b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/LocateBetweenTest.java index c643c45bf4..3253b18963 100644 --- a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/LocateBetweenTest.java +++ b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/LocateBetweenTest.java @@ -16,65 +16,49 @@ */ package org.apache.sis.geometries.operation; +import java.util.stream.Stream; import org.apache.sis.geometries.Geometry; -import org.apache.sis.geometries.DataPointsType; // Test dependencies -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNull; -import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; /** - * Tests the {@code locateBetween} operations of {@link GeometryProcessor}. + * Tests the {@code locateBetween} operation of {@link GeometryProcessor}. + * The test cases are declared in {@code LocateBetweenTest.adoc}. * * @author Johann Sorel (Geomatys) */ -public class LocateBetweenTest extends AbstractD9IMTest { +public class LocateBetweenTest { /** - * The inputs and expected result of a single test of {@code locateBetween(Geometry, double, double)}. - * - * @param input the geometry on which the operation is invoked. - * @param mStart the first measure value. - * @param mEnd the last measure value. - * @param expected the expected result, or {@code null} if an exception is expected. - * @param error the type of the expected exception, or {@code null} if the operation should succeed. + * Creates a new test case. */ - private record TestCase(Geometry input, - String attName, - double mStart, - double mEnd, - Geometry expected, - Class<? extends Exception> error) - { + public LocateBetweenTest() { } /** - * All test cases of {@code locateBetween(Geometry, double, double)}. + * Returns the test cases declared in {@code LocateBetweenTest.adoc}. + * This method is public because JUnit cannot open the package of a named module. + * + * @return the test cases. */ - private static final TestCase[] ENTRIES = { - /* - * TODO - */ - new TestCase(EMPTY_1, DataPointsType.ATT_M, 1, 2, null, UnsupportedOperationException.class), - new TestCase(POINT_A, DataPointsType.ATT_M, 1, 2, null, UnsupportedOperationException.class) - }; + public static Stream<AsciiDocTestCase> cases() { + return AsciiDocTestCase.read(LocateBetweenTest.class, "LocateBetweenTest.adoc"); + } /** - * Tests {@code locateBetween(Geometry, double, double)} on all declared test cases. + * Tests {@code locateBetween(Geometry, String, Number, Number)} on one test case of {@code LocateBetweenTest.adoc}. + * + * @param test the test case. */ - @Test - public void testLocateBetween() { - for (final TestCase entry : ENTRIES) { - try { - final Geometry result = new GeometryProcessor().locateBetween(entry.input(), entry.attName(), entry.mStart(), entry.mEnd()); - assertNull(entry.error(), "An exception was expected."); - assertEquals(entry.expected(), result); - } catch (Exception ex) { - if (entry.error() == null || !entry.error().isInstance(ex)) { - throw new AssertionError("Unexpected exception for " + entry, ex); - } - } - } + @ParameterizedTest(name = "{0}") + @MethodSource("cases") + public void testLocateBetween(final AsciiDocTestCase test) { + final Geometry input = test.geometry("input"); + final String attribute = test.value("attribute"); + final double start = test.number("start"); + final double end = test.number("end"); + test.assertGeometry(() -> new GeometryProcessor().locateBetween(input, attribute, start, end)); } } diff --git a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/SymDifferenceTest.adoc b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/SymDifferenceTest.adoc new file mode 100644 index 0000000000..5a26ccc827 --- /dev/null +++ b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/SymDifferenceTest.adoc @@ -0,0 +1,96 @@ += Symmetric difference +:description: Test cases of GeometryProcessor.symDifference(Geometry, Geometry). + +Each level-2 section is one test. Its title is the test id, the paragraph below it +describes the case, and the labeled list gives the operands in Well-Known Text 3 +and the expected result or error. + +A geometry result is compared with the `result` geometry by their Well-Known Text 3, +unless a `compare` value says otherwise: `normalized` compares them after `Geometry.normalized()`, +ignoring the first vertex of the rings and the order of the parts, and `emptiness` only checks +that the result is empty. + +include::RelationShapes.adoc[] + +// ─── The empty set ───────────────────────────────────────────────────────────── + +== empty-point + +The symmetric difference with the empty set is the other operand. + +input:: `G_GEOMETRY EMPTY` +other:: `G_POINT [10 5]` +result:: `G_POINT [10 5]` + +== point-empty + +The symmetric difference with the empty set is the other operand, whatever the operand order. + +input:: `G_POINT [10 5]` +other:: `G_GEOMETRY EMPTY` +result:: `G_POINT [10 5]` + +== empty-empty + +The symmetric difference of two empty sets is the empty set. + +input:: `G_GEOMETRY EMPTY` +other:: `G_GEOMETRY EMPTY` +result:: `G_GEOMETRY EMPTY` + +// ─── Points ──────────────────────────────────────────────────────────────────── + +== point-same-position + +Two points at the same position share their only position, so nothing remains. + +input:: `G_POINT [10 5]` +other:: `G_POINT [10 5]` +result:: `G_GEOMETRY EMPTY` + +== points-distinct + +Two distinct points share no position, so both remain, in any order, in the coordinate reference system of the operands. + +input:: `G_POINT {P:crs='EPSG:4326'} [10 5]` +other:: `G_POINT {P:crs='EPSG:4326'} [20 15]` +result:: `G_COLLECTION POINT {P:crs='EPSG:4326'} [10 5, 20 15]` +compare:: `normalized` + +// ─── Surfaces ────────────────────────────────────────────────────────────────── + +== square-same-position + +Every position of a square belongs to both operands when they are equal, so nothing remains. + +input:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0])` +other:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0])` +result:: `G_GEOMETRY EMPTY` +compare:: `emptiness` + +== square-overlap + +Two squares sharing a quarter of their area leave two concave polygons touching at two corners, each with its exterior ring counter-clockwise. + +input:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0])` +other:: `G_POLYGON ([5 5, 15 5, 15 15, 5 15, 5 5])` +result:: `G_COLLECTION POLYGON (G_POLYGON ([0 0, 10 0, 10 5, 5 5, 5 10, 0 10, 0 0]), G_POLYGON ([10 5, 15 5, 15 15, 5 15, 5 10, 10 10, 10 5]))` +compare:: `normalized` + +== square-disjoint + +Two disjoint squares share no position, so both remain as two polygons. + +input:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0])` +other:: `G_POLYGON ([20 20, 30 20, 30 30, 20 30, 20 20])` +result:: `G_COLLECTION POLYGON (G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0]), G_POLYGON ([20 20, 30 20, 30 30, 20 30, 20 20]))` +compare:: `normalized` + +== square-inner + +A square less the square it contains is a square with a hole, its ring clockwise. + +input:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0])` +other:: `G_POLYGON ([2 2, 4 2, 4 4, 2 4, 2 2])` +result:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0], [2 2, 2 4, 4 4, 4 2, 2 2])` +compare:: `normalized` diff --git a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/SymDifferenceTest.java b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/SymDifferenceTest.java index e1c464bc97..e9992e422b 100644 --- a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/SymDifferenceTest.java +++ b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/SymDifferenceTest.java @@ -16,97 +16,47 @@ */ package org.apache.sis.geometries.operation; +import java.util.stream.Stream; import org.apache.sis.geometries.Geometry; -import org.apache.sis.geometries.GeometryCollection; -import org.apache.sis.geometries.GeometryType; -import org.apache.sis.geometries.Point; // Test dependencies -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertInstanceOf; -import static org.junit.jupiter.api.Assertions.assertNull; -import static org.junit.jupiter.api.Assertions.assertTrue; -import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; /** - * Tests the {@code symDifference} operations of {@link GeometryProcessor}. + * Tests the {@code symDifference} operation of {@link GeometryProcessor}. + * The test cases are declared in {@code SymDifferenceTest.adoc}. * * @author Johann Sorel (Geomatys) */ -public class SymDifferenceTest extends AbstractD9IMTest { +public class SymDifferenceTest { /** - * The inputs and expected result of a single test of {@code symDifference(Geometry, Geometry)}. - * - * @param input the geometry on which the operation is invoked. - * @param other the other operand. - * @param expected the expected result, or {@code null} if an exception is expected. - * @param error the type of the expected exception, or {@code null} if the operation should succeed. + * Creates a new test case. */ - private record TestCase(Geometry input, - Geometry other, - Geometry expected, - Class<? extends Exception> error) - { + public SymDifferenceTest() { } /** - * All test cases of {@code symDifference(Geometry, Geometry)}. - */ - private static final TestCase[] ENTRIES = { - // (∅ − A) ∪ (A − ∅) = A: the result is the operand which is not empty. - new TestCase(EMPTY_1, NON_EMPTY, NON_EMPTY, null), - new TestCase(NON_EMPTY, EMPTY_1, NON_EMPTY, null), - new TestCase(EMPTY_1, EMPTY_2, EMPTY_2, null), - new TestCase(POINT_A, POINT_A_BIS, EMPTY_RESULT, null) - }; - - /** - * Tests {@code symDifference(Geometry, Geometry)} on all declared test cases. - */ - @Test - public void testSymDifference() { - for (final TestCase entry : ENTRIES) { - try { - final Geometry result = new GeometryProcessor().symDifference(entry.input(), entry.other()); - assertNull(entry.error(), "An exception was expected."); - assertEquals(entry.expected(), result); - } catch (Exception ex) { - if (entry.error() == null || !entry.error().isInstance(ex)) { - throw new AssertionError("Unexpected exception for " + entry, ex); - } - } - } - } - - /** - * Tests {@code symDifference(Geometry, Geometry)} on two points at different positions. - * No position belongs to both points, therefore the result shall contain both of them, - * in an unspecified order. + * Returns the test cases declared in {@code SymDifferenceTest.adoc}. + * This method is public because JUnit cannot open the package of a named module. + * + * @return the test cases. */ - @Test - public void testDistinctPoints() { - final Geometry result = new GeometryProcessor().symDifference(POINT_A, POINT_B); - assertInstanceOf(GeometryCollection.class, result, "No position is shared by the two points."); - final GeometryCollection<Point> points = (GeometryCollection<Point>) result; - assertEquals(GeometryType.POINT, points.getElementType()); - assertEquals(CRS_2D, points.getCoordinateReferenceSystem()); - assertEquals(2, points.getNumGeometries()); - assertPositionsEqual(points, POINT_A, POINT_B); + public static Stream<AsciiDocTestCase> cases() { + return AsciiDocTestCase.read(SymDifferenceTest.class, "SymDifferenceTest.adoc"); } /** - * Tests {@code symDifference(Geometry, Geometry)} on two surfaces. The symmetric difference - * keeps what belongs to exactly one of the two surfaces, which is their union less what they - * have in common. + * Tests {@code symDifference(Geometry, Geometry)} on one test case of {@code SymDifferenceTest.adoc}. + * + * @param test the test case. */ - @Test - public void testSymDifferenceOfSurfaces() { - final GeometryProcessor processor = new GeometryProcessor(); - assertEquals(150, areaOf(processor.symDifference(SQUARE, SQUARE_OVERLAP)), TOLERANCE, "Two squares of area 100 sharing an area of 25: 175 of union less 25 in common."); - assertEquals(200, areaOf(processor.symDifference(SQUARE, SQUARE_DISJOINT)), TOLERANCE, "Two disjoint squares belong to exactly one of the two operands."); - assertEquals(96, areaOf(processor.symDifference(SQUARE, SQUARE_INNER)), TOLERANCE, "A square less the square of area 4 it contains."); - assertTrue(processor.symDifference(SQUARE, SQUARE_BIS).isEmpty(), "Every position of a square belongs to both operands when they are equal."); + @ParameterizedTest(name = "{0}") + @MethodSource("cases") + public void testSymDifference(final AsciiDocTestCase test) { + final Geometry input = test.geometry("input"); + final Geometry other = test.geometry("other"); + test.assertGeometry(() -> new GeometryProcessor().symDifference(input, other)); } - } diff --git a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/TransformTest.adoc b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/TransformTest.adoc new file mode 100644 index 0000000000..b6f174240c --- /dev/null +++ b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/TransformTest.adoc @@ -0,0 +1,20 @@ += Transform +:description: Test cases of GeometryProcessor.transform(Geometry, CoordinateReferenceSystem, MathTransform). + +Each level-2 section is one test. Its title is the test id, the paragraph below it +describes the case, and the labeled list gives the operands in Well-Known Text 3 +and the expected result or error. + +The target `crs` is given by its authority code. No math transform is given: the operation finds it. + +include::RelationShapes.adoc[] + +// ─── The empty set ───────────────────────────────────────────────────────────── + +== empty-unsupported + +Transforming the empty set is not supported. + +input:: `G_GEOMETRY {P:crs='EPSG:4326'} EMPTY` +crs:: `EPSG:4978` +error:: `UnsupportedOperationException` diff --git a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/TransformTest.java b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/TransformTest.java index c4da19d966..6f88000df7 100644 --- a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/TransformTest.java +++ b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/TransformTest.java @@ -16,6 +16,7 @@ */ package org.apache.sis.geometries.operation; +import java.util.stream.Stream; import org.opengis.referencing.crs.CoordinateReferenceSystem; import org.opengis.referencing.operation.MathTransform; import org.opengis.referencing.operation.TransformException; @@ -36,6 +37,8 @@ import org.apache.sis.referencing.operation.transform.MathTransforms; // Test dependencies import static org.junit.jupiter.api.Assertions.*; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; /** @@ -135,29 +138,27 @@ public class TransformTest extends AbstractD9IMTest { } /** - * The inputs and expected result of a single test of {@code transform(Geometry, CoordinateReferenceSystem, MathTransform)}. + * Returns the test cases declared in {@code TransformTest.adoc}. + * This method is public because JUnit cannot open the package of a named module. * - * @param input the geometry on which the operation is invoked. - * @param crs the target coordinate reference system. - * @param expected the expected result, or {@code null} if an exception is expected. - * @param error the type of the expected exception, or {@code null} if the operation should succeed. + * @return the test cases. */ - private record TestCase(Geometry input, - CoordinateReferenceSystem crs, - Geometry expected, - Class<? extends Exception> error) - { + public static Stream<AsciiDocTestCase> cases() { + return AsciiDocTestCase.read(TransformTest.class, "TransformTest.adoc"); } /** - * All test cases of {@code transform(Geometry, CoordinateReferenceSystem, MathTransform)}. + * Tests {@code transform(Geometry, CoordinateReferenceSystem, MathTransform)} on one test case of {@code TransformTest.adoc}. + * + * @param test the test case. */ - private static final TestCase[] ENTRIES = { - /* - * TODO: the transform of an empty geometry should be that geometry in the target reference system. - */ - new TestCase(EMPTY_1, CRS_TARGET, null, UnsupportedOperationException.class) - }; + @ParameterizedTest(name = "{0}") + @MethodSource("cases") + public void testTransform(final AsciiDocTestCase test) { + final Geometry input = test.geometry("input"); + final CoordinateReferenceSystem crs = test.crs("crs"); + test.assertGeometry(() -> new GeometryProcessor().transform(input, crs, null)); + } /** * Tests the transform of a point when no math transform is given: @@ -195,21 +196,4 @@ public class TransformTest extends AbstractD9IMTest { } } - /** - * Tests {@code transform(Geometry, CoordinateReferenceSystem, MathTransform)} on all declared test cases. - */ - @Test - public void testTransform() { - for (final TestCase entry : ENTRIES) { - try { - final Geometry result = new GeometryProcessor().transform(entry.input(), entry.crs(), null); - assertNull(entry.error(), "An exception was expected."); - assertEquals(entry.expected(), result); - } catch (Exception ex) { - if (entry.error() == null || !entry.error().isInstance(ex)) { - throw new AssertionError("Unexpected exception for " + entry, ex); - } - } - } - } } diff --git a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/UnionTest.adoc b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/UnionTest.adoc new file mode 100644 index 0000000000..c652f7e2bf --- /dev/null +++ b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/UnionTest.adoc @@ -0,0 +1,96 @@ += Union +:description: Test cases of GeometryProcessor.union(Geometry, Geometry). + +Each level-2 section is one test. Its title is the test id, the paragraph below it +describes the case, and the labeled list gives the operands in Well-Known Text 3 +and the expected result or error. + +A geometry result is compared with the `result` geometry by their Well-Known Text 3, +unless a `compare` value says otherwise: `normalized` compares them after `Geometry.normalized()`, +ignoring the first vertex of the rings and the order of the parts, and `emptiness` only checks +that the result is empty. + +include::RelationShapes.adoc[] + +// ─── The empty set ───────────────────────────────────────────────────────────── + +== empty-point + +The union with the empty set is the other operand. + +input:: `G_GEOMETRY EMPTY` +other:: `G_POINT [10 5]` +result:: `G_POINT [10 5]` + +== point-empty + +The union with the empty set is the other operand, whatever the operand order. + +input:: `G_POINT [10 5]` +other:: `G_GEOMETRY EMPTY` +result:: `G_POINT [10 5]` + +== empty-empty + +The union of two empty sets is the empty set. + +input:: `G_GEOMETRY EMPTY` +other:: `G_GEOMETRY EMPTY` +result:: `G_GEOMETRY EMPTY` + +// ─── Points ──────────────────────────────────────────────────────────────────── + +== point-same-position + +The union of two points at the same position is that point. + +input:: `G_POINT [10 5]` +other:: `G_POINT [10 5]` +result:: `G_POINT [10 5]` + +== points-distinct + +The union of two distinct points is a set of the two points, in any order, in the coordinate reference system of the operands. + +input:: `G_POINT {P:crs='EPSG:4326'} [10 5]` +other:: `G_POINT {P:crs='EPSG:4326'} [20 15]` +result:: `G_COLLECTION POINT {P:crs='EPSG:4326'} [10 5, 20 15]` +compare:: `normalized` + +// ─── Surfaces ──────────────────────────────────────────────────────────────── + +== square-overlap + +Two squares sharing a quarter of their area merge into one concave polygon, its exterior ring counter-clockwise. + +input:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0])` +other:: `G_POLYGON ([5 5, 15 5, 15 15, 5 15, 5 5])` +result:: `G_POLYGON ([0 0, 10 0, 10 5, 15 5, 15 15, 5 15, 5 10, 0 10, 0 0])` +compare:: `normalized` + +== square-disjoint + +Two disjoint squares stay two polygons, each with its exterior ring counter-clockwise. + +input:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0])` +other:: `G_POLYGON ([20 20, 30 20, 30 30, 20 30, 20 20])` +result:: `G_COLLECTION POLYGON (G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0]), G_POLYGON ([20 20, 30 20, 30 30, 20 30, 20 20]))` +compare:: `normalized` + +== square-inner + +A square absorbs a square it contains. + +input:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0])` +other:: `G_POLYGON ([2 2, 4 2, 4 4, 2 4, 2 2])` +result:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0])` +compare:: `normalized` + +== square-same-position + +The union of a square with itself is that square. + +input:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0])` +other:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0])` +result:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0])` +compare:: `normalized` diff --git a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/UnionTest.java b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/UnionTest.java index 852ca24b53..a803b18811 100644 --- a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/UnionTest.java +++ b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/UnionTest.java @@ -16,95 +16,47 @@ */ package org.apache.sis.geometries.operation; +import java.util.stream.Stream; import org.apache.sis.geometries.Geometry; -import org.apache.sis.geometries.GeometryCollection; -import org.apache.sis.geometries.GeometryType; -import org.apache.sis.geometries.Point; // Test dependencies -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertInstanceOf; -import static org.junit.jupiter.api.Assertions.assertNull; -import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; /** - * Tests the {@code union} operations of {@link GeometryProcessor}. + * Tests the {@code union} operation of {@link GeometryProcessor}. + * The test cases are declared in {@code UnionTest.adoc}. * * @author Johann Sorel (Geomatys) */ -public class UnionTest extends AbstractD9IMTest { +public class UnionTest { /** - * The inputs and expected result of a single test of {@code union(Geometry, Geometry)}. - * - * @param input the geometry on which the operation is invoked. - * @param other the other operand. - * @param expected the expected result, or {@code null} if an exception is expected. - * @param error the type of the expected exception, or {@code null} if the operation should succeed. + * Creates a new test case. */ - private record TestCase(Geometry input, - Geometry other, - Geometry expected, - Class<? extends Exception> error) - { + public UnionTest() { } /** - * All test cases of {@code union(Geometry, Geometry)}. - */ - private static final TestCase[] ENTRIES = { - // ∅ ∪ A = A and A ∪ ∅ = A: the result is the operand which is not empty. - new TestCase(EMPTY_1, NON_EMPTY, NON_EMPTY, null), - new TestCase(NON_EMPTY, EMPTY_1, NON_EMPTY, null), - new TestCase(EMPTY_1, EMPTY_2, EMPTY_2, null), - new TestCase(POINT_A, POINT_A_BIS, POINT_A, null) - }; - - /** - * Tests {@code union(Geometry, Geometry)} on all declared test cases. - */ - @Test - public void testUnion() { - for (final TestCase entry : ENTRIES) { - try { - final Geometry result = new GeometryProcessor().union(entry.input(), entry.other()); - assertNull(entry.error(), "An exception was expected."); - assertEquals(entry.expected(), result); - } catch (Exception ex) { - if (entry.error() == null || !entry.error().isInstance(ex)) { - throw new AssertionError("Unexpected exception for " + entry, ex); - } - } - } - } - - /** - * Tests {@code union(Geometry, Geometry)} on two points at different positions. - * The result shall contain both positions, in an unspecified order. + * Returns the test cases declared in {@code UnionTest.adoc}. + * This method is public because JUnit cannot open the package of a named module. + * + * @return the test cases. */ - @Test - public void testDistinctPoints() { - final Geometry result = new GeometryProcessor().union(POINT_A, POINT_B); - assertInstanceOf(GeometryCollection.class, result, "The union of two distinct points is a set of two points."); - final GeometryCollection<Point> points = (GeometryCollection<Point>) result; - assertEquals(GeometryType.POINT, points.getElementType()); - assertEquals(CRS_2D, points.getCoordinateReferenceSystem()); - assertEquals(2, points.getNumGeometries()); - assertPositionsEqual(points, POINT_A, POINT_B); + public static Stream<AsciiDocTestCase> cases() { + return AsciiDocTestCase.read(UnionTest.class, "UnionTest.adoc"); } /** - * Tests {@code union(Geometry, Geometry)} on two surfaces. The area of the union of two - * overlapping surfaces is the sum of their areas less the area they have in common, and - * the union of two disjoint surfaces keeps both of them. + * Tests {@code union(Geometry, Geometry)} on one test case of {@code UnionTest.adoc}. + * + * @param test the test case. */ - @Test - public void testUnionOfSurfaces() { - final GeometryProcessor processor = new GeometryProcessor(); - assertEquals(175, areaOf(processor.union(SQUARE, SQUARE_OVERLAP)), TOLERANCE, "Two squares of area 100 sharing an area of 25."); - assertEquals(200, areaOf(processor.union(SQUARE, SQUARE_DISJOINT)), TOLERANCE, "Two disjoint squares of area 100."); - assertEquals(100, areaOf(processor.union(SQUARE, SQUARE_INNER)), TOLERANCE, "A square absorbs a square it contains."); - assertEquals(100, areaOf(processor.union(SQUARE, SQUARE_BIS)), TOLERANCE, "The union of a square with itself is that square."); + @ParameterizedTest(name = "{0}") + @MethodSource("cases") + public void testUnion(final AsciiDocTestCase test) { + final Geometry input = test.geometry("input"); + final Geometry other = test.geometry("other"); + test.assertGeometry(() -> new GeometryProcessor().union(input, other)); } - } diff --git a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/WithinDistanceTest.adoc b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/WithinDistanceTest.adoc new file mode 100644 index 0000000000..0c5b1e2bf4 --- /dev/null +++ b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/WithinDistanceTest.adoc @@ -0,0 +1,114 @@ += Within distance +:description: Test cases of GeometryProcessor.withinDistance(Geometry, Geometry, Quantity). + +Each level-2 section is one test. Its title is the test id, the paragraph below it +describes the case, and the labeled list gives the operands in Well-Known Text 3 +and the expected result or error. + +`input` is within `distance` of `other` when the shortest distance between them does not exceed it, +the limit included. The geometries are in a geographic reference system whose axes are in degrees. + +include::RelationShapes.adoc[] + +// ─── Points ──────────────────────────────────────────────────────────────────── + +== empty-point + +The empty set is at no distance of anything. + +input:: `G_GEOMETRY {P:crs='EPSG:4326'} EMPTY` +other:: `G_POINT {P:crs='EPSG:4326'} [10 5]` +distance:: `1 deg` +result:: `false` + +== point-empty + +Nothing is at a finite distance of the empty set. + +input:: `G_POINT {P:crs='EPSG:4326'} [10 5]` +other:: `G_GEOMETRY {P:crs='EPSG:4326'} EMPTY` +distance:: `1 deg` +result:: `false` + +== point-same-position + +Two points at the same position are within any distance. + +input:: `G_POINT {P:crs='EPSG:4326'} [10 5]` +other:: `G_POINT {P:crs='EPSG:4326'} [10 5]` +distance:: `1 deg` +result:: `true` + +== point-other-position + +Two points about 14 degrees apart are not within 1 degree. + +input:: `G_POINT {P:crs='EPSG:4326'} [10 5]` +other:: `G_POINT {P:crs='EPSG:4326'} [20 15]` +distance:: `1 deg` +result:: `false` + +// ─── Shapes ──────────────────────────────────────────────────────────────────── + +== square-line-far-above-limit + +The far line is 10 degrees above the square, within 11 degrees. + +input:: `G_POLYGON {P:crs='EPSG:4326'} ([0 0, 10 0, 10 10, 0 10, 0 0])` +other:: `G_LINESTRING {P:crs='EPSG:4326'} [0 20, 10 20]` +distance:: `11 deg` +result:: `true` + +== square-line-far-at-limit + +The far line is exactly 10 degrees above the square: the limit is included. + +input:: `G_POLYGON {P:crs='EPSG:4326'} ([0 0, 10 0, 10 10, 0 10, 0 0])` +other:: `G_LINESTRING {P:crs='EPSG:4326'} [0 20, 10 20]` +distance:: `10 deg` +result:: `true` + +== square-line-far-below-limit + +The far line is 10 degrees above the square, not within 9 degrees. + +input:: `G_POLYGON {P:crs='EPSG:4326'} ([0 0, 10 0, 10 10, 0 10, 0 0])` +other:: `G_LINESTRING {P:crs='EPSG:4326'} [0 20, 10 20]` +distance:: `9 deg` +result:: `false` + +== square-disjoint-above-limit + +The two squares are 10√2 degrees apart, which is less than 15. + +input:: `G_POLYGON {P:crs='EPSG:4326'} ([0 0, 10 0, 10 10, 0 10, 0 0])` +other:: `G_POLYGON {P:crs='EPSG:4326'} ([20 20, 30 20, 30 30, 20 30, 20 20])` +distance:: `15 deg` +result:: `true` + +== square-disjoint-below-limit + +The two squares are 10√2 degrees apart, which is more than 14. + +input:: `G_POLYGON {P:crs='EPSG:4326'} ([0 0, 10 0, 10 10, 0 10, 0 0])` +other:: `G_POLYGON {P:crs='EPSG:4326'} ([20 20, 30 20, 30 30, 20 30, 20 20])` +distance:: `14 deg` +result:: `false` + +== square-touching-zero + +Two squares sharing an edge are at a distance of zero. + +input:: `G_POLYGON {P:crs='EPSG:4326'} ([0 0, 10 0, 10 10, 0 10, 0 0])` +other:: `G_POLYGON {P:crs='EPSG:4326'} ([10 0, 20 0, 20 10, 10 10, 10 0])` +distance:: `0 deg` +result:: `true` + +== square-point-center-zero + +A position inside a square is at a distance of zero from it. + +input:: `G_POLYGON {P:crs='EPSG:4326'} ([0 0, 10 0, 10 10, 0 10, 0 0])` +other:: `G_POINT {P:crs='EPSG:4326'} [5 5]` +distance:: `0 deg` +result:: `true` diff --git a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/WithinDistanceTest.java b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/WithinDistanceTest.java index bf1f9cff14..2c2c5bf72d 100644 --- a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/WithinDistanceTest.java +++ b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/WithinDistanceTest.java @@ -16,104 +16,46 @@ */ package org.apache.sis.geometries.operation; +import java.util.stream.Stream; import javax.measure.Quantity; -import org.apache.sis.geometries.Geometry; -import org.apache.sis.measure.Quantities; -import org.apache.sis.measure.Units; // Test dependencies -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNull; -import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; /** - * Tests the {@code withinDistance} operations of {@link GeometryProcessor}. + * Tests the {@code withinDistance} operation of {@link GeometryProcessor}. + * The test cases are declared in {@code WithinDistanceTest.adoc}. * * @author Johann Sorel (Geomatys) */ -public class WithinDistanceTest extends AbstractD9IMTest { +public class WithinDistanceTest { /** - * An arbitrary radius, smaller than the distance between the two points of the test cases. + * Creates a new test case. */ - private static final Quantity<?> RADIUS = Quantities.create(1, Units.DEGREE); - - /** - * The inputs and expected result of a single test of {@code withinDistance(Geometry, Geometry, Length)}. - * - * @param input the geometry on which the operation is invoked. - * @param other the other operand. - * @param distance the maximal distance. - * @param expected the expected result, or {@code null} if an exception is expected. - * @param error the type of the expected exception, or {@code null} if the operation should succeed. - */ - private record TestCase(Geometry input, - Geometry other, - Quantity<?> distance, - Boolean expected, - Class<? extends Exception> error) - { - } - - /** - * All test cases of {@code withinDistance(Geometry, Geometry, Quantity)}. - */ - private static final TestCase[] ENTRIES = { - new TestCase(EMPTY_1, POINT_A, RADIUS, false, null), - new TestCase(POINT_A, EMPTY_1, RADIUS, false, null), - new TestCase(POINT_A, POINT_A_BIS, RADIUS, true, null), - new TestCase(POINT_A, POINT_B, RADIUS, false, null) - }; - - /** - * Tests {@code withinDistance(Geometry, Geometry, Length)} on all declared test cases. - */ - @Test - public void testWithinDistance() { - for (final TestCase entry : ENTRIES) { - try { - final boolean result = new GeometryProcessor().withinDistance(entry.input(), entry.other(), entry.distance()); - assertNull(entry.error(), "An exception was expected."); - assertEquals(entry.expected(), result); - } catch (Exception ex) { - if (entry.error() == null || !entry.error().isInstance(ex)) { - throw new AssertionError("Unexpected exception for " + entry, ex); - } - } - } + public WithinDistanceTest() { } /** - * Tests {@code withinDistance(Geometry, Geometry, Quantity)} between geometries which are - * not positions. The answer is {@code true} as soon as the shortest distance between the - * two geometries does not exceed the given one, the limit case included. + * Returns the test cases declared in {@code WithinDistanceTest.adoc}. + * This method is public because JUnit cannot open the package of a named module. + * + * @return the test cases. */ - @Test - public void testWithinDistanceBetweenShapes() { - final GeometryProcessor processor = new GeometryProcessor(); - assertTrue (processor.withinDistance(SQUARE, LINE_FAR, degrees(11)), "The distance is 10."); - assertTrue (processor.withinDistance(SQUARE, LINE_FAR, degrees(10)), "The distance is exactly the limit."); - assertFalse(processor.withinDistance(SQUARE, LINE_FAR, degrees(9)), "The distance is 10."); - /* - * The two squares are 10√2 apart, which is between 14 and 15. - */ - assertTrue (processor.withinDistance(SQUARE, SQUARE_DISJOINT, degrees(15)), "The distance is 10√2."); - assertFalse(processor.withinDistance(SQUARE, SQUARE_DISJOINT, degrees(14)), "The distance is 10√2."); - /* - * Geometries which meet are within any distance, including a distance of zero. - */ - assertTrue(processor.withinDistance(SQUARE, SQUARE_TOUCHING, degrees(0)), "Two squares sharing an edge are at a distance of zero."); - assertTrue(processor.withinDistance(SQUARE, POINT_CENTER, degrees(0)), "A position inside a square is at a distance of zero from it."); + public static Stream<AsciiDocTestCase> cases() { + return AsciiDocTestCase.read(WithinDistanceTest.class, "WithinDistanceTest.adoc"); } /** - * Returns the given amount of degrees, the unit of the axes of the reference system - * used by the geometries of these tests. + * Tests {@code withinDistance(Geometry, Geometry, Quantity)} on one test case of {@code WithinDistanceTest.adoc}. + * + * @param test the test case. */ - private static Quantity<?> degrees(final double value) { - return Quantities.create(value, Units.DEGREE); + @ParameterizedTest(name = "{0}") + @MethodSource("cases") + public void testWithinDistance(final AsciiDocTestCase test) { + final Quantity<?> distance = test.quantity("distance"); + test.assertRelation((input, other) -> new GeometryProcessor().withinDistance(input, other, distance)); } - } diff --git a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/surface/PolygonTest.java b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/surface/PolygonTest.java index 2bff1d86e1..fb3ead2543 100644 --- a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/surface/PolygonTest.java +++ b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/surface/PolygonTest.java @@ -46,6 +46,18 @@ public abstract class PolygonTest extends GeometryTest { */ private static final double[] HOLE = {4,4, 6,4, 6,6, 4,6, 4,4}; + /** + * The ring of a concave polygon covering {@link #SQUARE} and a square of 10 × 10 offset by 5 + * along both axes, therefore of area 100 + 100 − 25 = 175. + */ + private static final double[] OVERLAPPING_SQUARES = {0,0, 10,0, 10,5, 15,5, 15,15, 5,15, 5,10, 0,10, 0,0}; + + /** + * The ring of a convex hexagon with oblique edges, which is a square of 30 × 30 less two + * corner triangles of area 200, therefore of area 900 − 400 = 500. + */ + private static final double[] HEXAGON = {0,0, 10,0, 30,20, 30,30, 20,30, 0,10, 0,0}; + /** * A ring crossing itself at (5 5), which makes the surface it bounds invalid. */ @@ -123,6 +135,10 @@ public abstract class PolygonTest extends GeometryTest { "The area shall use the square of the unit of the axes."); assertEquals(96, createPolygon(CRS_2D, SQUARE, HOLE).getArea().getValue().doubleValue(), TOLERANCE, "A square of 10 × 10 less a hole of 2 × 2."); + assertEquals(175, createPolygon(CRS_2D, OVERLAPPING_SQUARES).getArea().getValue().doubleValue(), + TOLERANCE, "Two squares of 10 × 10 overlapping on 5 × 5, as a concave polygon."); + assertEquals(500, createPolygon(CRS_2D, HEXAGON).getArea().getValue().doubleValue(), + TOLERANCE, "A convex hexagon with oblique edges."); } /**
