Skip to content

Commit

Permalink
Unify how to read geometries
Browse files Browse the repository at this point in the history
  • Loading branch information
inigo-cobian committed Oct 2, 2024
1 parent 742263b commit 3624061
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 70 deletions.
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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<String> 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<Double> 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");
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<String> 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<Date> 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());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -48,15 +47,7 @@ public Builder validate(String urn, URL url, IParameters<String> 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);

Expand Down

0 comments on commit 3624061

Please sign in to comment.