diff --git a/CHANGELOG.md b/CHANGELOG.md index 10efe37..8a0732b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,8 @@ Adheres to [Semantic Versioning](http://semver.org/). ## 3.3.3 (TBD) -* TBD +* GeoJsonObject simple geometry retrieval method +* FeatureConverter conversion to simple geometry utility methods ## [3.3.2](https://github.com/ngageoint/simple-features-geojson-java/releases/tag/3.3.2) (01-19-2023) diff --git a/src/main/java/mil/nga/sf/geojson/Feature.java b/src/main/java/mil/nga/sf/geojson/Feature.java index 1ffb899..a902649 100755 --- a/src/main/java/mil/nga/sf/geojson/Feature.java +++ b/src/main/java/mil/nga/sf/geojson/Feature.java @@ -112,11 +112,9 @@ public void setProperties(Map properties) { } /** - * Get the simple feature geometry - * - * @return simple feature geometry + * {@inheritDoc} */ - @JsonIgnore + @Override public mil.nga.sf.Geometry getSimpleGeometry() { return geometry != null ? geometry.getGeometry() : null; } diff --git a/src/main/java/mil/nga/sf/geojson/FeatureCollection.java b/src/main/java/mil/nga/sf/geojson/FeatureCollection.java index 5189626..75ce587 100755 --- a/src/main/java/mil/nga/sf/geojson/FeatureCollection.java +++ b/src/main/java/mil/nga/sf/geojson/FeatureCollection.java @@ -10,6 +10,8 @@ import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonPropertyOrder; +import mil.nga.sf.GeometryCollection; + /** * Feature Collection * @@ -122,6 +124,27 @@ public Iterator iterator() { return features.iterator(); } + /** + * {@inheritDoc} + */ + @Override + public mil.nga.sf.Geometry getSimpleGeometry() { + mil.nga.sf.Geometry geometry = null; + if (features != null && !features.isEmpty()) { + GeometryCollection geomCollection = new GeometryCollection<>(); + for (Feature feature : features) { + mil.nga.sf.Geometry geom = feature.getSimpleGeometry(); + if (geom != null) { + geomCollection.addGeometry(geom); + } + } + if (!geomCollection.isEmpty()) { + geometry = geomCollection; + } + } + return geometry; + } + /** * Get the geometry type * diff --git a/src/main/java/mil/nga/sf/geojson/FeatureConverter.java b/src/main/java/mil/nga/sf/geojson/FeatureConverter.java index 25cebfe..2111ed9 100644 --- a/src/main/java/mil/nga/sf/geojson/FeatureConverter.java +++ b/src/main/java/mil/nga/sf/geojson/FeatureConverter.java @@ -252,6 +252,58 @@ public static GeoJsonObject toGeoJsonObject(JsonNode tree) { return toTypedObject(GeoJsonObject.class, tree); } + /** + * Convert the string content to a simple geometry + * + * @param content + * string content + * @return simple geometry + * @since 3.3.3 + */ + public static mil.nga.sf.Geometry toSimpleGeometry(String content) { + return toSimpleGeometry(toGeoJsonObject(content)); + } + + /** + * Convert the object value to a simple geometry + * + * @param value + * object value + * @return simple geometry + * @since 3.3.3 + */ + public static mil.nga.sf.Geometry toSimpleGeometry(Object value) { + return toSimpleGeometry(toGeoJsonObject(value)); + } + + /** + * Convert the JSON tree to a simple geometry + * + * @param tree + * tree node + * @return simple geometry + * @since 3.3.3 + */ + public static mil.nga.sf.Geometry toSimpleGeometry(JsonNode tree) { + return toSimpleGeometry(toGeoJsonObject(tree)); + } + + /** + * Convert the GeoJSON object to a simple geometry + * + * @param geoJson + * GeoJSON object + * @return simple geometry + * @since 3.3.3 + */ + public static mil.nga.sf.Geometry toSimpleGeometry(GeoJsonObject geoJson) { + mil.nga.sf.Geometry geometry = null; + if (geoJson != null) { + geometry = geoJson.getSimpleGeometry(); + } + return geometry; + } + /** * Convert the GeoJSON object to an object map * diff --git a/src/main/java/mil/nga/sf/geojson/GeoJsonObject.java b/src/main/java/mil/nga/sf/geojson/GeoJsonObject.java index 5f22da2..458b735 100755 --- a/src/main/java/mil/nga/sf/geojson/GeoJsonObject.java +++ b/src/main/java/mil/nga/sf/geojson/GeoJsonObject.java @@ -7,6 +7,7 @@ import com.fasterxml.jackson.annotation.JsonAnyGetter; import com.fasterxml.jackson.annotation.JsonAnySetter; +import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonInclude.Include; import com.fasterxml.jackson.annotation.JsonSubTypes; @@ -66,6 +67,15 @@ public void setBbox(double[] bbox) { */ public abstract String getType(); + /** + * Get the simple feature geometry + * + * @return simple feature geometry + * @since 3.3.3 + */ + @JsonIgnore + public abstract mil.nga.sf.Geometry getSimpleGeometry(); + /** * Get the foreign members * diff --git a/src/main/java/mil/nga/sf/geojson/Geometry.java b/src/main/java/mil/nga/sf/geojson/Geometry.java index fdc8b00..c4bb495 100644 --- a/src/main/java/mil/nga/sf/geojson/Geometry.java +++ b/src/main/java/mil/nga/sf/geojson/Geometry.java @@ -39,6 +39,14 @@ public abstract class Geometry extends GeoJsonObject { @JsonIgnore public abstract mil.nga.sf.Geometry getGeometry(); + /** + * {@inheritDoc} + */ + @Override + public mil.nga.sf.Geometry getSimpleGeometry() { + return getGeometry(); + } + /** * {@inheritDoc} */ diff --git a/src/test/java/mil/nga/sf/geojson/FeatureCollectionTest.java b/src/test/java/mil/nga/sf/geojson/FeatureCollectionTest.java index 9a8a6cf..b488627 100755 --- a/src/test/java/mil/nga/sf/geojson/FeatureCollectionTest.java +++ b/src/test/java/mil/nga/sf/geojson/FeatureCollectionTest.java @@ -210,6 +210,14 @@ public void toStringValue() { featureCollectionGeoJsonObject.getFeature(i)); } + Geometry geometry = FeatureConverter.toSimpleGeometry(stringValue); + assertEquals(geoJsonObjectFromString.getSimpleGeometry(), geometry); + GeometryCollection geomCollection = new GeometryCollection<>(); + for (Feature feature : featureCollection.getFeatures()) { + geomCollection.addGeometry(feature.getSimpleGeometry()); + } + assertEquals(geomCollection, geometry); + } @Test diff --git a/src/test/java/mil/nga/sf/geojson/FeatureTest.java b/src/test/java/mil/nga/sf/geojson/FeatureTest.java index 337b0be..72303ed 100755 --- a/src/test/java/mil/nga/sf/geojson/FeatureTest.java +++ b/src/test/java/mil/nga/sf/geojson/FeatureTest.java @@ -12,6 +12,8 @@ import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.node.ObjectNode; +import mil.nga.sf.Geometry; + public class FeatureTest { @Test @@ -139,6 +141,11 @@ public void toStringValue() { assertEquals(feature, featureGeoJsonObject); assertEquals(feature.getSimpleGeometry(), featureGeoJsonObject.getSimpleGeometry()); + + Geometry geometry = FeatureConverter.toSimpleGeometry(stringValue); + assertEquals(geoJsonObjectFromString.getSimpleGeometry(), geometry); + assertEquals(feature.getSimpleGeometry(), geometry); + } private Feature getTestFeature() { diff --git a/src/test/java/mil/nga/sf/geojson/TestUtils.java b/src/test/java/mil/nga/sf/geojson/TestUtils.java index 22e0b9f..9271b9d 100755 --- a/src/test/java/mil/nga/sf/geojson/TestUtils.java +++ b/src/test/java/mil/nga/sf/geojson/TestUtils.java @@ -189,6 +189,13 @@ public static void toStringValue(mil.nga.sf.Geometry simpleGeometry) { assertEquals(geometry, geometryGeoJsonObject); assertEquals(geometry.getGeometry(), geometryGeoJsonObject.getGeometry()); + + assertEquals(simpleGeometry, + geoJsonObjectFromString.getSimpleGeometry()); + assertEquals(simpleGeometry, geomteryFromString.getSimpleGeometry()); + assertEquals(simpleGeometry, + FeatureConverter.toSimpleGeometry(stringValue)); + } }