Skip to content

Commit

Permalink
Import static catalogs reading assets from link items
Browse files Browse the repository at this point in the history
  • Loading branch information
inigo-cobian committed Dec 19, 2024
1 parent aeb6d60 commit 9af651f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import org.integratedmodelling.klab.exceptions.KlabResourceAccessException;
import org.integratedmodelling.klab.exceptions.KlabResourceNotFoundException;

import com.fasterxml.jackson.module.jsonSchema.types.ArraySchema.Items;

import kong.unirest.HttpResponse;
import kong.unirest.JsonNode;
import kong.unirest.Unirest;
Expand Down Expand Up @@ -65,8 +67,8 @@ public static IGeometry readGeometry(JSONObject collection) {
* @throws KlabResourceAccessException
*/
public static JSONObject readAssetsFromCollection(String collectionUrl, JSONObject collection) throws KlabResourceAccessException {
String catalogUrl = STACUtils.getCatalogUrl(collection);
String collectonId = collection.getString("id");
String collectionId = collection.getString("id");
String catalogUrl = STACUtils.getCatalogUrl(collectionUrl, collectionId, collection);
JSONObject catalogData = STACUtils.requestMetadata(catalogUrl, "catalog");

Optional<String> searchEndpoint = STACUtils.containsLinkTo(catalogData, "search")
Expand All @@ -76,10 +78,23 @@ public static JSONObject readAssetsFromCollection(String collectionUrl, JSONObje
// Static catalogs should have their assets on the Collection
if (searchEndpoint.isEmpty()) {
// Check the assets
if (!collection.has("assets")) {
throw new KlabResourceNotFoundException("Static STAC collection \"" + collectionUrl + "\" has no assets");
if (collection.has("assets")) {
return collection.getJSONObject("assets");
}
// Try to get the assets from a link that has type `item`
Optional<String> itemHref = STACUtils.getLinkTo(collection, "item");
if (itemHref.isEmpty()) {
throw new KlabResourceNotFoundException("Cannot find items at STAC collection \"" + collectionUrl + "\"");
}
String itemUrl = itemHref.get().startsWith(".")
? collectionUrl.replace("collection.json", "") + itemHref.get().replace("./", "")
: itemHref.get();
// TODO get assets from the item
JSONObject itemData = STACUtils.requestMetadata(itemUrl, "feature");
if (itemData.has("assets")) {
return itemData.getJSONObject("assets");
}
return collection.getJSONObject("assets");
throw new KlabResourceNotFoundException("Cannot find assets at STAC collection \"" + collectionUrl + "\"");
}

// item_assets is a shortcut for obtaining information about the assets
Expand All @@ -89,7 +104,7 @@ public static JSONObject readAssetsFromCollection(String collectionUrl, JSONObje
}

// TODO Move the query to another place.
String parameters = "?collections=" + collectonId + "&limit=1";
String parameters = "?collections=" + collectionId + "&limit=1";
HttpResponse<JsonNode> response = Unirest.get(searchEndpoint.get() + parameters).asJson();

if (!response.isSuccess()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,8 @@ public void getEncodedData(IResource resource, Map<String, String> urnParameters
IContextualizationScope scope) {
String collectionUrl = resource.getParameters().get("collection", String.class);
JSONObject collectionData = STACUtils.requestMetadata(collectionUrl, "collection");
String catalogUrl = STACUtils.getCatalogUrl(collectionData);
String collectionId = collectionData.getString("id");
String catalogUrl = STACUtils.getCatalogUrl(collectionUrl, collectionId, collectionData);
JSONObject catalogData = STACUtils.requestMetadata(catalogUrl, "catalog");

boolean hasSearchOption = STACUtils.containsLinkTo(catalogData, "search");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public static Type inferValueType(String key) {
* @param collectionData
* @return url of the catalog
*/
public static String getCatalogUrl(JSONObject collectionData) {
public static String getCatalogUrl(String collectionUrl, String collectionId, JSONObject collectionData) {
// The URL of the catalog is the root
if (!collectionData.has("links")) {
throw new KlabResourceAccessException("STAC collection is missing links. It is not fully complaiant and cannot be accessed by the adapter.");
Expand All @@ -124,6 +124,10 @@ public static String getCatalogUrl(JSONObject collectionData) {
if (rootLink.isEmpty()) {
throw new KlabResourceAccessException("STAC collection is missing a relationship to the root catalog");
}
return rootLink.get().getString("href");
String href = rootLink.get().getString("href");
if (href.startsWith("..")) {
return collectionUrl.replace("/collection.json", "").replace(collectionId, "") + href.replace("../", "");
}
return href;
}
}

0 comments on commit 9af651f

Please sign in to comment.