Skip to content

Commit

Permalink
fix stac reverting changes
Browse files Browse the repository at this point in the history
  • Loading branch information
moovida committed Oct 31, 2024
1 parent b2c9031 commit 8c2c2ec
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 229 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
package org.hortonmachine.gears.io.stac;

import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.model.S3Object;
import com.fasterxml.jackson.databind.JsonNode;
import it.geosolutions.imageio.core.BasicAuthURI;
import it.geosolutions.imageio.plugins.cog.CogImageReadParam;
import it.geosolutions.imageioimpl.plugins.cog.*;
import java.util.Iterator;

import org.geotools.coverage.grid.GridCoverage2D;
import org.geotools.gce.geotiff.GeoTiffReader;
import org.hortonmachine.gears.libs.modules.HMConstants;
Expand All @@ -14,13 +10,18 @@
import org.opengis.parameter.GeneralParameterValue;
import org.opengis.referencing.crs.CoordinateReferenceSystem;

import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import com.fasterxml.jackson.databind.JsonNode;

import it.geosolutions.imageio.core.BasicAuthURI;
import it.geosolutions.imageio.plugins.cog.CogImageReadParam;
import it.geosolutions.imageioimpl.plugins.cog.CogImageInputStreamSpi;
import it.geosolutions.imageioimpl.plugins.cog.CogImageReaderSpi;
import it.geosolutions.imageioimpl.plugins.cog.CogSourceSPIProvider;
import it.geosolutions.imageioimpl.plugins.cog.HttpRangeReader;

/**
* An asset from a stac item.
*
*
* @author Andrea Antonello (www.hydrologis.com)
*
*/
Expand Down Expand Up @@ -48,8 +49,9 @@ public HMStacAsset( String id, JsonNode assetNode ) {
}
if (type.toLowerCase().contains("profile=cloud-optimized")) {
JsonNode rasterBandNode = assetNode.get("raster:bands");
assetUrl = assetNode.get("href").textValue();
if (rasterBandNode != null && !rasterBandNode.isEmpty()) {
assetUrl = assetNode.get("href").textValue();

Iterator<JsonNode> rbIterator = rasterBandNode.elements();
while( rbIterator.hasNext() ) {
JsonNode rbNode = rbIterator.next();
Expand All @@ -62,6 +64,9 @@ public HMStacAsset( String id, JsonNode assetNode ) {
resolution = resolNode.asDouble();
}
}
} else {
isValid = false;
nonValidReason = "raster bands metadata missing";
}
} else {
isValid = false;
Expand All @@ -88,29 +93,23 @@ public String toString() {

/**
* Read the asset's coverage into a local raster.
*
*
* @param region and optional region to read from.
* @param user an optional user in case of authentication.
* @param password an optional password in case of authentication.
* @return the read raster from the asset's url.
* @throws Exception
* @return the read raster from the asset's url..
* @throws Exception
*/
public GridCoverage2D readRaster( RegionMap region, String user, String password, AmazonS3 s3Client ) throws Exception {
public GridCoverage2D readRaster( RegionMap region, String user, String password ) throws Exception {
BasicAuthURI cogUri = new BasicAuthURI(assetUrl, false);
if (user != null && password != null) {
cogUri.setUser(user);
cogUri.setPassword(password);
}
GeoTiffReader reader;
if (assetUrl.startsWith("s3://")) {
InputStream inputProvider = readS3Raster(cogUri, s3Client);
reader = new GeoTiffReader(inputProvider);
} else {
RangeReader rangeReader = new HttpRangeReader(cogUri.getUri(), CogImageReadParam.DEFAULT_HEADER_LENGTH);
CogSourceSPIProvider inputProvider = new CogSourceSPIProvider(cogUri, new CogImageReaderSpi(),
new CogImageInputStreamSpi(), rangeReader.getClass().getName());
reader = new GeoTiffReader(inputProvider);
}
HttpRangeReader rangeReader = new HttpRangeReader(cogUri.getUri(), CogImageReadParam.DEFAULT_HEADER_LENGTH);
CogSourceSPIProvider inputProvider = new CogSourceSPIProvider(cogUri, new CogImageReaderSpi(),
new CogImageInputStreamSpi(), rangeReader.getClass().getName());
GeoTiffReader reader = new GeoTiffReader(inputProvider);
CoordinateReferenceSystem crs = reader.getCoordinateReferenceSystem();

GeneralParameterValue[] generalParameter = null;
Expand All @@ -121,18 +120,8 @@ public GridCoverage2D readRaster( RegionMap region, String user, String password
return coverage;
}

public InputStream readS3Raster(BasicAuthURI cogUri, AmazonS3 s3Client ) throws IOException {
String[] bucketAndObject = assetUrl.split("://")[1].split("/", 2);
S3Object object = s3Client.getObject(bucketAndObject[0], bucketAndObject[1]);
return object.getObjectContent();
}

public GridCoverage2D readRaster( RegionMap region ) throws Exception {
return readRaster(region, null, null, null );
}

public GridCoverage2D readRaster( RegionMap region, AmazonS3 s3Client ) throws Exception {
return readRaster(region, null, null, s3Client );
return readRaster(region, null, null);
}

public String getId() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package org.hortonmachine.gears.io.stac;

import com.amazonaws.services.s3.AmazonS3;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import org.geotools.coverage.grid.GridCoverage2D;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.data.simple.SimpleFeatureIterator;
Expand Down Expand Up @@ -32,13 +37,6 @@
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import org.opengis.referencing.operation.MathTransform;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@SuppressWarnings({"rawtypes"})
/**
* A stac collection.
Expand All @@ -51,7 +49,6 @@ public class HMStacCollection {
private Collection collection;
private SearchQuery search;
private IHMProgressMonitor pm;
private AmazonS3 s3Client;

HMStacCollection( STACClient stacClient, Collection collection, IHMProgressMonitor pm ) {
this.stacClient = stacClient;
Expand Down Expand Up @@ -111,26 +108,10 @@ public HMStacCollection setTimestampFilter( Date startTimestamp, Date endTimesta
public HMStacCollection setGeometryFilter( Geometry intersectionGeometry ) {
if (search == null)
search = new SearchQuery();
else if (search.getBbox() != null)
throw new IllegalStateException("Cannot add intersects filter. Only one of either intersects or bbox may be specified");
search.setIntersects(intersectionGeometry);
return this;
}

/**
* Set the geometry bbox filter for search query
* @param bbox
* @return the current collection.
*/
public HMStacCollection setBboxFilter( double[] bbox ) {
if (search == null)
search = new SearchQuery();
else if (search.getIntersects() != null)
throw new IllegalStateException("Cannot add intersects filter. Only one of either intersects or bbox may be specified");
search.setBbox(bbox);
return this;
}

/**
* Set cql filter for search query;
*
Expand All @@ -145,21 +126,12 @@ public HMStacCollection setCqlFilter( String cqlFilter ) throws CQLException {
return this;
}

private SimpleFeatureCollection queryFeatureCollection() throws IOException {
try {
return stacClient.search(search, STACClient.SearchMode.POST);
} catch (IOException e) {
pm.message("POST search query not supported by the endpoint. GET will be tried.");
}
return stacClient.search(search, STACClient.SearchMode.GET);
}

public List<HMStacItem> searchItems() throws Exception {
if (search == null)
search = new SearchQuery();
search.setCollections(Arrays.asList(getId()));

SimpleFeatureCollection fc = queryFeatureCollection();
SimpleFeatureCollection fc = stacClient.search(search, STACClient.SearchMode.GET);
SimpleFeatureIterator iterator = fc.features();
pm.beginTask("Extracting STAC items...", -1);
List<HMStacItem> stacItems = new ArrayList<>();
Expand Down Expand Up @@ -192,7 +164,7 @@ public List<HMStacItem> searchItems() throws Exception {
* @return the final raster.
* @throws Exception
*/
public HMRaster readRasterBandOnRegion( RegionMap latLongRegionMap, String bandName, List<HMStacItem> items,
public static HMRaster readRasterBandOnRegion( RegionMap latLongRegionMap, String bandName, List<HMStacItem> items,
boolean allowTransform, MergeMode mergeMode, IHMProgressMonitor pm ) throws Exception {

if (!allowTransform) {
Expand Down

This file was deleted.

Loading

0 comments on commit 8c2c2ec

Please sign in to comment.