From 362406109a248876cab924255a5079cf07e60384 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=8D=C3=B1igo=20Cobi=C3=A1n?= Date: Wed, 2 Oct 2024 14:58:53 +0200 Subject: [PATCH] Unify how to read geometries --- .../klab/stac/STACCollectionParser.java | 31 +++++++------ .../klab/stac/STACService.java | 46 ------------------- .../klab/stac/STACValidator.java | 11 +---- 3 files changed, 18 insertions(+), 70 deletions(-) diff --git a/adapters/klab.ogc/src/main/java/org/integratedmodelling/klab/stac/STACCollectionParser.java b/adapters/klab.ogc/src/main/java/org/integratedmodelling/klab/stac/STACCollectionParser.java index b170568b8..53173c7fc 100644 --- a/adapters/klab.ogc/src/main/java/org/integratedmodelling/klab/stac/STACCollectionParser.java +++ b/adapters/klab.ogc/src/main/java/org/integratedmodelling/klab/stac/STACCollectionParser.java @@ -1,14 +1,13 @@ package org.integratedmodelling.klab.stac; -import java.time.LocalDateTime; -import java.time.ZoneId; -import java.time.format.DateTimeFormatter; +import java.time.Instant; import java.util.List; import java.util.Optional; import org.integratedmodelling.klab.api.data.IGeometry; import org.integratedmodelling.klab.common.Geometry; import org.integratedmodelling.klab.common.GeometryBuilder; +import org.integratedmodelling.klab.components.geospace.extents.Projection; import org.integratedmodelling.klab.exceptions.KlabResourceAccessException; import org.integratedmodelling.klab.exceptions.KlabResourceNotFoundException; @@ -30,26 +29,30 @@ private static JSONObject readItemAssets(JSONObject collection) { return collection.getJSONObject("item_assets"); } + /** + * Obtains the geometry from the collection data. + * Currently, only available for dynamic collections. + * @param parameters + * @return geometry + */ public static IGeometry readGeometry(JSONObject collection) { GeometryBuilder gBuilder = Geometry.builder(); - DateTimeFormatter filterTimestampFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'"); - + JSONObject extent = collection.getJSONObject("extent"); - List interval = extent.getJSONObject("temporal").getJSONArray("interval").getJSONArray(0).toList(); + List bbox = extent.getJSONObject("spatial").getJSONArray("bbox").getJSONArray(0).toList(); + gBuilder.space().boundingBox(Double.valueOf(bbox.get(0).toString()), Double.valueOf(bbox.get(1).toString()), + Double.valueOf(bbox.get(2).toString()), Double.valueOf(bbox.get(3).toString())); + List interval = extent.getJSONObject("temporal").getJSONArray("interval").getJSONArray(0).toList(); if (interval.get(0) != null) { - LocalDateTime start = LocalDateTime.parse(interval.get(0), filterTimestampFormatter); - gBuilder.time().start(start.atZone(ZoneId.of("UTC")).toInstant().toEpochMilli()); + gBuilder.time().start(Instant.parse(interval.get(0).toString()).toEpochMilli()); } if (interval.size() > 1 && interval.get(1) != null) { - LocalDateTime start = LocalDateTime.parse(interval.get(1), filterTimestampFormatter); - gBuilder.time().start(start.atZone(ZoneId.of("UTC")).toInstant().toEpochMilli()); + gBuilder.time().end(Instant.parse(interval.get(1).toString()).toEpochMilli()); } - List bbox = extent.getJSONObject("spatial").getJSONArray("bbox").getJSONArray(0).toList(); - - gBuilder.space().boundingBox(bbox.get(0), bbox.get(1), bbox.get(2), bbox.get(3)); - return gBuilder.build(); + return gBuilder.build().withProjection(Projection.DEFAULT_PROJECTION_CODE) + .withTimeType("grid"); } /** diff --git a/adapters/klab.ogc/src/main/java/org/integratedmodelling/klab/stac/STACService.java b/adapters/klab.ogc/src/main/java/org/integratedmodelling/klab/stac/STACService.java index 5cda314dd..a90f10f56 100644 --- a/adapters/klab.ogc/src/main/java/org/integratedmodelling/klab/stac/STACService.java +++ b/adapters/klab.ogc/src/main/java/org/integratedmodelling/klab/stac/STACService.java @@ -1,17 +1,8 @@ package org.integratedmodelling.klab.stac; -import java.util.Date; -import java.util.List; - -import org.geotools.geometry.jts.ReferencedEnvelope; import org.hortonmachine.gears.io.stac.HMStacCollection; import org.hortonmachine.gears.io.stac.HMStacManager; import org.hortonmachine.gears.libs.monitor.LogProgressMonitor; -import org.integratedmodelling.kim.api.IParameters; -import org.integratedmodelling.klab.api.data.IGeometry; -import org.integratedmodelling.klab.common.Geometry; -import org.integratedmodelling.klab.common.GeometryBuilder; -import org.integratedmodelling.klab.components.geospace.extents.Projection; import org.integratedmodelling.klab.exceptions.KlabInternalErrorException; import kong.unirest.json.JSONObject; @@ -46,41 +37,4 @@ public STACService(String collectionUrl) { public HMStacCollection getCollection() { return collection; } - - /** - * Obtains the geometry from the collection data. - * Currently, only available for dynamic collections. - * @param parameters - * @return geometry - */ - public IGeometry getGeometry(IParameters parameters) { - GeometryBuilder gBuilder = Geometry.builder(); - buildSpatialContext(gBuilder); - buildTemporalInterval(gBuilder); - - Geometry ret = gBuilder.build().withProjection(Projection.DEFAULT_PROJECTION_CODE) - .withTimeType("grid"); - return ret; - } - - private void buildSpatialContext(GeometryBuilder gBuilder) { - ReferencedEnvelope envelope = collection.getSpatialBounds(); - double[] upperCorner = {envelope.getMaxX(), envelope.getMaxY()}; - double[] lowerCorner = {envelope.getMinX(), envelope.getMinY()}; - - gBuilder.space().boundingBox(lowerCorner[0], upperCorner[0], lowerCorner[1], upperCorner[1]); - } - - private void buildTemporalInterval(GeometryBuilder gBuilder) { - List interval = collection.getTemporalBounds(); - if (interval.isEmpty()) { - return; - } - if (interval.get(0) != null) { - gBuilder.time().start(interval.get(0).getTime()); - } - if (interval.size() > 1 && interval.get(1) != null) { - gBuilder.time().end(interval.get(1).getTime()); - } - } } diff --git a/adapters/klab.ogc/src/main/java/org/integratedmodelling/klab/stac/STACValidator.java b/adapters/klab.ogc/src/main/java/org/integratedmodelling/klab/stac/STACValidator.java index 8a74d082f..b7231b9f7 100644 --- a/adapters/klab.ogc/src/main/java/org/integratedmodelling/klab/stac/STACValidator.java +++ b/adapters/klab.ogc/src/main/java/org/integratedmodelling/klab/stac/STACValidator.java @@ -21,7 +21,6 @@ import org.integratedmodelling.klab.data.resources.Resource; import org.integratedmodelling.klab.data.resources.ResourceBuilder; import org.integratedmodelling.klab.exceptions.KlabUnimplementedException; -import org.integratedmodelling.klab.ogc.STACAdapter; import org.integratedmodelling.klab.rest.CodelistReference; import org.integratedmodelling.klab.rest.MappingReference; import org.integratedmodelling.klab.rest.ResourceCRUDRequest; @@ -48,15 +47,7 @@ public Builder validate(String urn, URL url, IParameters userData, IMoni collectionId = collectionData.getString("id"); userData.put("collectionId", collectionId); } - STACService service = STACAdapter.getService(collectionUrl); - - String catalogUrl = STACUtils.getCatalogUrl(collectionData); - JSONObject catalogData = STACUtils.requestMetadata(catalogUrl, "catalog"); - - boolean hasSearchOption = STACUtils.containsLinkTo(catalogData, "search"); - IGeometry geometry = hasSearchOption - ? service.getGeometry(userData) - : STACCollectionParser.readGeometry(collectionData); + IGeometry geometry = STACCollectionParser.readGeometry(collectionData); Builder builder = new ResourceBuilder(urn).withParameters(userData).withGeometry(geometry);