Skip to content

Commit 8a3ec3a

Browse files
authored
Merge pull request #219 from aodn/feature/7708-map-selection-4codata
add key in feature properties
2 parents 952d06b + fc68ba6 commit 8a3ec3a

2 files changed

Lines changed: 60 additions & 3 deletions

File tree

server/src/main/java/au/org/aodn/ogcapi/server/core/service/ElasticSearch.java

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -650,7 +650,40 @@ public SearchResult<FeatureGeoJSON> searchFeatureSummary(String collectionId, Li
650650
for (var hit : response.hits().hits()) {
651651
EsFeatureCollectionModel hitFeatureCollection = hit.source();
652652
if (hitFeatureCollection != null && hitFeatureCollection.getFeatures() != null) {
653-
features.addAll(hitFeatureCollection.toFeatureCollectionGeoJSON().getFeatures());
653+
// A collectionID may map to several dataset key. So we need to identify features with dataset keys. TO get a dataset key which sits in hit.properties.key. For example:
654+
// "properties": {
655+
// "date": "2011-04",
656+
// "collection": "4d3d4aca-472e-4616-88a5-df0f5ab401ba",
657+
// "key": "mooring_acidification_realtime_qc.parquet"
658+
// }
659+
String datasetKey = null;
660+
if (hitFeatureCollection.getProperties() != null) {
661+
Object keyObj = hitFeatureCollection.getProperties().get("key");
662+
if (keyObj != null) {
663+
datasetKey = keyObj.toString();
664+
}
665+
}
666+
667+
List<FeatureGeoJSON> documentFeatures =
668+
hitFeatureCollection.toFeatureCollectionGeoJSON().getFeatures();
669+
670+
for (FeatureGeoJSON feature : documentFeatures) {
671+
// add key in property field for each feature
672+
if (datasetKey != null) {
673+
Object featurePropsObj = feature.getProperties();
674+
675+
if (featurePropsObj instanceof Map) {
676+
@SuppressWarnings("unchecked")
677+
Map<String, Object> featurePropsMap = (Map<String, Object>) featurePropsObj;
678+
featurePropsMap.put("key", datasetKey);
679+
} else {
680+
Map<String, Object> newPropsMap = new HashMap<>();
681+
newPropsMap.put("key", datasetKey);
682+
feature.setProperties(newPropsMap);
683+
}
684+
}
685+
features.add(feature);
686+
}
654687
}
655688
}
656689

server/src/test/java/au/org/aodn/ogcapi/server/service/ElasticSearchTest.java

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
import static org.junit.jupiter.api.Assertions.assertEquals;
2727
import static org.junit.jupiter.api.Assertions.assertNotNull;
28+
import static org.junit.jupiter.api.Assertions.assertTrue;
2829
import static org.mockito.Mockito.*;
2930

3031
public class ElasticSearchTest {
@@ -61,10 +62,22 @@ public void searchFeatureSummaryTest() throws IOException {
6162
featureCollectionProperties.put("key", "satellite_ghrsst_l4_gamssa_1day_multi_sensor_world.zarr");
6263
esFeatureCollection.setProperties(featureCollectionProperties);
6364
var coords = new ArrayList<List<List<BigDecimal>>>();
65+
var esFeature = new EsFeatureModel();
66+
67+
// mock a single point [147.338884, -43.190779]
68+
List<List<BigDecimal>> ring = new ArrayList<>();
69+
List<BigDecimal> point = List.of(
70+
new BigDecimal("147.338884"),
71+
new BigDecimal("-43.190779")
72+
);
73+
ring.add(point);
74+
coords.add(ring);
75+
6476
var polygon = new EsPolygonModel();
6577
polygon.setCoordinates(coords);
66-
var esFeature = new EsFeatureModel();
78+
6779
esFeature.setGeometry(polygon);
80+
6881
esFeatureCollection.setFeatures(List.of(esFeature));
6982

7083
when(hit.source()).thenReturn(esFeatureCollection);
@@ -88,7 +101,18 @@ public void searchFeatureSummaryTest() throws IOException {
88101
assertNotNull(result);
89102
assertEquals(1, result.getCollections().size());
90103
assertEquals(1L, result.getTotal());
91-
assertEquals(esFeature.toFeatureGeoJSON(), result.getCollections().get(0));
104+
// validate geometry keeps same after adding key property
105+
assertEquals(esFeature.toFeatureGeoJSON().getGeometry(),
106+
result.getCollections().get(0).getGeometry());
107+
108+
// validate key is in properties
109+
FeatureGeoJSON returnedFeature = result.getCollections().get(0);
110+
@SuppressWarnings("unchecked")
111+
Map<String, Object> featureProps = (Map<String, Object>) returnedFeature.getProperties();
112+
113+
assertTrue(featureProps.containsKey("key"));
114+
assertEquals("satellite_ghrsst_l4_gamssa_1day_multi_sensor_world.zarr",
115+
featureProps.get("key"));
92116
}
93117

94118

0 commit comments

Comments
 (0)