Skip to content

Commit

Permalink
move GDAL GeoTiff reader to adapter so that it can be used for comman…
Browse files Browse the repository at this point in the history
…dline ingest
  • Loading branch information
rfecher committed Jul 21, 2016
1 parent c02e5bb commit 37af37b
Show file tree
Hide file tree
Showing 9 changed files with 209 additions and 34 deletions.
27 changes: 26 additions & 1 deletion extensions/adapters/raster/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,32 @@
<artifactId>geowave-adapter-raster</artifactId>
<name>Geowave Raster Adapter</name>
<description>Geowave Data Adapter for Raster Data</description>
<dependencies>
<dependencies><dependency>
<groupId>it.geosolutions.imageio-ext</groupId>
<artifactId>imageio-ext-gdalgeotiff</artifactId>
<version>1.1.13</version>
<exclusions>
<exclusion>
<groupId>com.vividsolutions</groupId>
<artifactId>jts</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-imageio-ext-gdal</artifactId>
<version>${geotools.version}</version>
<exclusions>
<exclusion>
<groupId>com.vividsolutions</groupId>
<artifactId>jts</artifactId>
</exclusion>
<exclusion>
<artifactId>gt-epsg-hsql</artifactId>
<groupId>org.geotools</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package mil.nga.giat.geowave.format.landsat8;
package mil.nga.giat.geowave.adapter.raster.plugin;

import java.util.Collections;
import java.util.HashMap;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package mil.nga.giat.geowave.adapter.raster.plugin;

import java.awt.RenderingHints.Key;
import java.util.Map;

import org.geotools.coverage.grid.io.AbstractGridFormat;
import org.geotools.coverageio.BaseGridFormatFactorySPI;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import it.geosolutions.imageio.plugins.geotiff.GeoTiffImageReaderSpi;

public class GDALGeoTiffFormatFactory extends
BaseGridFormatFactorySPI
{
private final static Logger LOGGER = LoggerFactory.getLogger(GDALGeoTiffFormatFactory.class);

@Override
public boolean isAvailable() {
boolean available = true;

// if these classes are here, then the runtime environment has
// access to JAI and the JAI ImageI/O toolbox.
try {
Class.forName("it.geosolutions.imageio.plugins.geotiff.GeoTiffImageReaderSpi");
available = new GeoTiffImageReaderSpi().isAvailable();

}
catch (final ClassNotFoundException cnf) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("GDALGeoTiffFormatFactory is not availaible.");
}

available = false;
}

return available;
}

@Override
public AbstractGridFormat createFormat() {
return new GDALGeoTiffFormat();
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package mil.nga.giat.geowave.format.landsat8;
package mil.nga.giat.geowave.adapter.raster.plugin;

import org.geotools.coverageio.gdal.BaseGDALGridCoverage2DReader;
import org.geotools.data.DataSourceException;
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
mil.nga.giat.geowave.adapter.raster.plugin.GeoWaveGTRasterFormatFactory
mil.nga.giat.geowave.adapter.raster.plugin.GDALGeoTiffFormatFactory
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public GeoServerConfig() {
}

public String getUrl() {
if (!url.contains("//")) {
if (url.contains("//")) {
// assume exact URL
return url;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import mil.nga.giat.geowave.adapter.raster.RasterUtils;
import mil.nga.giat.geowave.adapter.raster.adapter.RasterDataAdapter;
import mil.nga.giat.geowave.adapter.raster.adapter.merge.nodata.NoDataMergeStrategy;
import mil.nga.giat.geowave.adapter.raster.plugin.GDALGeoTiffReader;
import mil.nga.giat.geowave.adapter.raster.plugin.GeoWaveGTRasterFormat;
import mil.nga.giat.geowave.adapter.vector.plugin.ExtractGeometryFilterVisitor;
import mil.nga.giat.geowave.adapter.vector.plugin.GeoWaveGTDataStore;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,20 @@
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.log4j.Logger;
import org.geotools.coverage.grid.GridCoverage2D;
import org.geotools.coverage.grid.io.AbstractGridFormat;
import org.geotools.coverage.grid.io.GridCoverage2DReader;
import org.geotools.coverage.grid.io.GridFormatFinder;
import org.geotools.factory.Hints;
import org.geotools.referencing.CRS;
import org.opengis.coverage.grid.GridCoverage;
import org.opengis.referencing.crs.CoordinateReferenceSystem;

import mil.nga.giat.geowave.adapter.raster.RasterUtils;
import mil.nga.giat.geowave.adapter.raster.adapter.RasterDataAdapter;
Expand All @@ -38,7 +43,8 @@
public class GeoToolsRasterDataStoreIngestPlugin implements
LocalFileIngestPlugin<GridCoverage>
{
private final static Logger LOGGER = Logger.getLogger(GeoToolsRasterDataStoreIngestPlugin.class);
private final static Logger LOGGER = Logger.getLogger(
GeoToolsRasterDataStoreIngestPlugin.class);
private final RasterOptionProvider optionProvider;

public GeoToolsRasterDataStoreIngestPlugin() {
Expand All @@ -63,48 +69,134 @@ public void init(
@Override
public boolean supportsFile(
final File file ) {

final AbstractGridFormat format = GridFormatFinder.findFormat(file);
AbstractGridFormat format = null;
try {
format = GridFormatFinder.findFormat(
file);
}
catch (final Exception e) {
LOGGER.info(
"Unable to support as raster file",
e);
}
// the null check is enough and we don't need to check the format
// accepts this file because the finder should have previously validated
// this
return (format != null);
}

private static AbstractGridFormat prioritizedFindFormat(
final File input ) {
final AbstractGridFormat format = null;
try {
final Set<AbstractGridFormat> formats = GridFormatFinder.findFormats(
input);
if ((formats == null) || formats.isEmpty()) {
LOGGER.warn(
"Unable to support raster file " + input.getAbsolutePath());
return null;
}
// world image and geotiff can both open tif files, give
// priority to gdalgeotiff, followed by geotiff
for (final AbstractGridFormat f : formats) {
if ("GDALGeoTiff".equals(
f.getName())) {
return f;
}
}
for (final AbstractGridFormat f : formats) {
if ("GeoTIFF".equals(
f.getName())) {
return f;
}
}

// otherwise just pick the first
final Iterator<AbstractGridFormat> it = formats.iterator();
if (it.hasNext()) {
return it.next();
}
}
catch (final Exception e) {
LOGGER.warn(
"Error while trying read raster file",
e);
return null;
}
return format;
}

@Override
public CloseableIterator<GeoWaveData<GridCoverage>> toGeoWaveData(
final File input,
final Collection<ByteArrayId> primaryIndexIds,
final String globalVisibility ) {

final AbstractGridFormat format = GridFormatFinder.findFormat(input);
final GridCoverage2DReader reader = format.getReader(input);
final AbstractGridFormat format = prioritizedFindFormat(
input);
if (format == null) {
return new Wrapper(
Collections.emptyIterator());
}
Hints hints = null;
if ((optionProvider.getCrs() != null) && !optionProvider.getCrs().trim().isEmpty()) {
try {
final CoordinateReferenceSystem crs = CRS.decode(
optionProvider.getCrs());
if (crs != null) {
hints = new Hints();
hints.put(
Hints.DEFAULT_COORDINATE_REFERENCE_SYSTEM,
crs);
}
}
catch (final Exception e) {
LOGGER.warn(
"Unable to find coordinate reference system, continuing without hint",
e);
}
}
final GridCoverage2DReader reader = format.getReader(
input, hints);
if (reader == null) {
LOGGER.error("Unable to get reader instance, getReader returned null");
LOGGER.error(
"Unable to get reader instance, getReader returned null");
return new Wrapper(
Collections.emptyIterator());
}
try {
final GridCoverage2D coverage = reader.read(null);
final GridCoverage2D coverage = reader.read(
null);
if (coverage != null) {
final Map<String, String> metadata = new HashMap<String, String>();
final String coverageName = coverage.getName().toString();
final String[] mdNames = reader.getMetadataNames(coverageName);
if ((mdNames != null) && (mdNames.length > 0)) {
for (final String mdName : mdNames) {
metadata.put(
mdName,
reader.getMetadataValue(
coverageName,
mdName));
try {
// wrapping with try-catch block because often the reader
// does not support operations on coverage name
// if not, we just don't have metadata, and continue
final String[] mdNames = reader.getMetadataNames(
coverageName);
if ((mdNames != null) && (mdNames.length > 0)) {
for (final String mdName : mdNames) {
metadata.put(
mdName,
reader.getMetadataValue(
coverageName,
mdName));
}
}
}
catch (final Exception e) {
LOGGER.debug(
"Unable to find metadata from coverage reader",
e);
}
final List<GeoWaveData<GridCoverage>> coverages = new ArrayList<GeoWaveData<GridCoverage>>();

if (optionProvider.isSeparateBands() && (coverage.getNumSampleDimensions() > 1)) {
final String baseName = optionProvider.getCoverageName() != null ? optionProvider.getCoverageName()
: input.getName();
final double[][] nodata = optionProvider.getNodata(coverage.getNumSampleDimensions());
final double[][] nodata = optionProvider.getNodata(
coverage.getNumSampleDimensions());
for (int b = 0; b < coverage.getNumSampleDimensions(); b++) {
final RasterDataAdapter adapter = new RasterDataAdapter(
baseName + "_B" + b,
Expand All @@ -120,10 +212,11 @@ public CloseableIterator<GeoWaveData<GridCoverage>> toGeoWaveData(
new double[][] {
nodata[b]
});
coverages.add(new GeoWaveData<GridCoverage>(
adapter,
primaryIndexIds,
coverage));
coverages.add(
new GeoWaveData<GridCoverage>(
adapter,
primaryIndexIds,
coverage));
}
}
else {
Expand All @@ -135,11 +228,13 @@ public CloseableIterator<GeoWaveData<GridCoverage>> toGeoWaveData(
optionProvider.getTileSize(),
optionProvider.isBuildPyramid(),
optionProvider.isBuildHistogream(),
optionProvider.getNodata(coverage.getNumSampleDimensions()));
coverages.add(new GeoWaveData<GridCoverage>(
adapter,
primaryIndexIds,
coverage));
optionProvider.getNodata(
coverage.getNumSampleDimensions()));
coverages.add(
new GeoWaveData<GridCoverage>(
adapter,
primaryIndexIds,
coverage));
}
return new Wrapper(
coverages.iterator()) {
Expand All @@ -152,8 +247,9 @@ public void close()
};
}
else {
LOGGER.warn("Null grid coverage from file '" + input.getAbsolutePath()
+ "' for discovered geotools format '" + format.getName() + "'");
LOGGER.warn(
"Null grid coverage from file '" + input.getAbsolutePath()
+ "' for discovered geotools format '" + format.getName() + "'");
}
}
catch (final IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@ public class RasterOptionProvider implements
{

@Parameter(names = "--pyramid", description = "Build an image pyramid on ingest for quick reduced resolution query")
private boolean buildPyramid = RasterDataAdapter.DEFAULT_BUILD_PYRAMID;
private boolean buildPyramid = false;

@Parameter(names = "--crs", description = "A CRS override for the provided raster file")
private String crs = null;

@Parameter(names = "--histogram", description = "Build a histogram of samples per band on ingest for performing band equalization")
private boolean buildHistogream = RasterDataAdapter.DEFAULT_BUILD_HISTOGRAM;
private boolean buildHistogream = false;

@Parameter(names = "--tileSize", description = "Optional parameter to set the tile size stored (default is 256)")
private int tileSize = RasterDataAdapter.DEFAULT_TILE_SIZE;
Expand Down Expand Up @@ -45,6 +48,10 @@ public boolean isSeparateBands() {
return separateBands;
}

public String getCrs() {
return crs;
}

public String getCoverageName() {
if ((coverageName == null) || coverageName.trim().isEmpty()) {
return null;
Expand Down

0 comments on commit 37af37b

Please sign in to comment.