Skip to content

Commit

Permalink
simple geometry conversion shortcut methods
Browse files Browse the repository at this point in the history
  • Loading branch information
bosborn committed Jan 9, 2024
1 parent 313df52 commit 697c9cb
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 5 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
6 changes: 2 additions & 4 deletions src/main/java/mil/nga/sf/geojson/Feature.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,9 @@ public void setProperties(Map<String, Object> 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;
}
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/mil/nga/sf/geojson/FeatureCollection.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

import mil.nga.sf.GeometryCollection;

/**
* Feature Collection
*
Expand Down Expand Up @@ -122,6 +124,27 @@ public Iterator<Feature> iterator() {
return features.iterator();
}

/**
* {@inheritDoc}
*/
@Override
public mil.nga.sf.Geometry getSimpleGeometry() {
mil.nga.sf.Geometry geometry = null;
if (features != null && !features.isEmpty()) {
GeometryCollection<mil.nga.sf.Geometry> 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
*
Expand Down
52 changes: 52 additions & 0 deletions src/main/java/mil/nga/sf/geojson/FeatureConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/mil/nga/sf/geojson/GeoJsonObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
*
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/mil/nga/sf/geojson/Geometry.java
Original file line number Diff line number Diff line change
Expand Up @@ -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}
*/
Expand Down
8 changes: 8 additions & 0 deletions src/test/java/mil/nga/sf/geojson/FeatureCollectionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,14 @@ public void toStringValue() {
featureCollectionGeoJsonObject.getFeature(i));
}

Geometry geometry = FeatureConverter.toSimpleGeometry(stringValue);
assertEquals(geoJsonObjectFromString.getSimpleGeometry(), geometry);
GeometryCollection<Geometry> geomCollection = new GeometryCollection<>();
for (Feature feature : featureCollection.getFeatures()) {
geomCollection.addGeometry(feature.getSimpleGeometry());
}
assertEquals(geomCollection, geometry);

}

@Test
Expand Down
7 changes: 7 additions & 0 deletions src/test/java/mil/nga/sf/geojson/FeatureTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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() {
Expand Down
7 changes: 7 additions & 0 deletions src/test/java/mil/nga/sf/geojson/TestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));

}

}

0 comments on commit 697c9cb

Please # to comment.