diff --git a/server/src/main/java/au/org/aodn/ogcapi/server/core/util/GeometryUtils.java b/server/src/main/java/au/org/aodn/ogcapi/server/core/util/GeometryUtils.java index f9748070..1d629f6e 100644 --- a/server/src/main/java/au/org/aodn/ogcapi/server/core/util/GeometryUtils.java +++ b/server/src/main/java/au/org/aodn/ogcapi/server/core/util/GeometryUtils.java @@ -33,6 +33,7 @@ public class GeometryUtils { protected static final int PRECISION = 15; + public static final String NON_SPECIFIED_MULTIPOLYGON = "non-specified"; @Getter protected static GeometryFactory factory = new GeometryFactory(new PrecisionModel(), 4326); @@ -210,15 +211,18 @@ public static String convertToGeoJson(LiteralExpressionImpl literalExpression, C return r; } } + /** * Many code require static access to this function, hence we will init it somewhere as bean and then * set it to the static instance for sharing + * * @param input - A geometry text input * @return - The converted geometry */ public static Optional readGeometry(Object input) { return self.readCachedGeometry(input); } + /** * Please use this function as it contains the parser with enough decimal to make it work. * @@ -235,8 +239,7 @@ public Optional readCachedGeometry(Object input) { j = (String) input; } return Optional.of(json.read(j)); - } - catch (IOException e) { + } catch (IOException e) { // Do nothing } return Optional.empty(); @@ -295,7 +298,7 @@ public static ReferencedEnvelope toReferencedEnvelope(Coordinate[] coordinates, * @throws IllegalArgumentException if the input is not valid GeoJSON */ public static String convertToWkt(Object geoJsonGeometry) { - if (geoJsonGeometry == null) { + if (geoJsonGeometry == null || geoJsonGeometry.toString().equals(NON_SPECIFIED_MULTIPOLYGON)) { return null; } diff --git a/server/src/test/java/au/org/aodn/ogcapi/server/core/util/GeometryUtilsTest.java b/server/src/test/java/au/org/aodn/ogcapi/server/core/util/GeometryUtilsTest.java index 5bc3ac08..022ea40f 100644 --- a/server/src/test/java/au/org/aodn/ogcapi/server/core/util/GeometryUtilsTest.java +++ b/server/src/test/java/au/org/aodn/ogcapi/server/core/util/GeometryUtilsTest.java @@ -28,9 +28,9 @@ protected GeometryCollection convertToGeometryCollection( List geometries = new ArrayList<>(); // Iterate through the FeatureCollection and extract geometries - try(FeatureIterator features = featureCollection.features()) { - while(features.hasNext()) { - Geometry geometry = (Geometry)(features.next()).getDefaultGeometry(); + try (FeatureIterator features = featureCollection.features()) { + while (features.hasNext()) { + Geometry geometry = (Geometry) (features.next()).getDefaultGeometry(); geometries.add(geometry); } } @@ -41,8 +41,10 @@ protected GeometryCollection convertToGeometryCollection( // Create and return a GeometryCollection from the array of geometries return geometryFactory.createGeometryCollection(geometryArray); } + /** * Given a irregular geojson, with hole the centroid point is still inside the polygon + * * @throws IOException - Not expected to throw this */ @Test @@ -58,8 +60,8 @@ public void verifyCentroidCorrect() throws IOException { List point = GeometryUtils.calculateCollectionCentroid(convertToGeometryCollection(feature)); assertEquals(1, point.size(), "One item"); - assertEquals(2.805438932281021, point.get(0).getX(),"X"); - assertEquals( 2.0556251797475227, point.get(0).getY(), "Y"); + assertEquals(2.805438932281021, point.get(0).getX(), "X"); + assertEquals(2.0556251797475227, point.get(0).getY(), "Y"); } @Test @@ -102,4 +104,18 @@ public void verifyCreateGirdPolygonWithTooBigCellSize() { Assertions.assertTrue(gridPolygons.get(0).equalsExact(polygon), "Get back itself"); } + @Test + public void verifyConvertToWktWithNullGeometry() { + // Test with null input + String result = GeometryUtils.convertToWkt(null); + Assertions.assertNull(result, "Expected null for null input"); + } + + @Test + public void verifyConvertToWktWithNonSpecifiedMultiPolygon() { + // Test with NON_SPECIFIED_MULTIPOLYGON constant value + String result = GeometryUtils.convertToWkt(GeometryUtils.NON_SPECIFIED_MULTIPOLYGON); + Assertions.assertNull(result, "Expected null for non-specified multipolygon"); + } + }