Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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<Geometry> readGeometry(Object input) {
return self.readCachedGeometry(input);
}

/**
* Please use this function as it contains the parser with enough decimal to make it work.
*
Expand All @@ -235,8 +239,7 @@ public Optional<Geometry> readCachedGeometry(Object input) {
j = (String) input;
}
return Optional.of(json.read(j));
}
catch (IOException e) {
} catch (IOException e) {
// Do nothing
}
return Optional.empty();
Expand Down Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ protected GeometryCollection convertToGeometryCollection(
List<Geometry> geometries = new ArrayList<>();

// Iterate through the FeatureCollection and extract geometries
try(FeatureIterator<SimpleFeature> features = featureCollection.features()) {
while(features.hasNext()) {
Geometry geometry = (Geometry)(features.next()).getDefaultGeometry();
try (FeatureIterator<SimpleFeature> features = featureCollection.features()) {
while (features.hasNext()) {
Geometry geometry = (Geometry) (features.next()).getDefaultGeometry();
geometries.add(geometry);
}
}
Expand All @@ -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
Expand All @@ -58,8 +60,8 @@ public void verifyCentroidCorrect() throws IOException {

List<Coordinate> 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
Expand Down Expand Up @@ -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");
}

}