Commit 0ab9dd2f authored by Andrea Aime's avatar Andrea Aime
Browse files

[GEOS-7314] GeoTiffPPIO can return the source file of a processed coverage

parent 43b2b719
/* (c) 2014 Open Source Geospatial Foundation - all rights reserved
/* (c) 2014 - 2015 Open Source Geospatial Foundation - all rights reserved
* (c) 2001 - 2013 OpenPlans
* This code is licensed under the GPL 2.0 license, available at the root
* application directory.
......@@ -15,6 +15,8 @@ import java.io.InputStream;
import java.io.OutputStream;
import javax.media.jai.JAI;
import javax.media.jai.OpImage;
import javax.media.jai.RenderedOp;
import org.apache.commons.io.IOUtils;
import org.geoserver.wps.WPSException;
......@@ -28,8 +30,11 @@ import org.geotools.coverage.grid.io.imageio.GeoToolsWriteParams;
import org.geotools.gce.geotiff.GeoTiffFormat;
import org.geotools.gce.geotiff.GeoTiffReader;
import org.geotools.gce.geotiff.GeoTiffWriteParams;
import org.geotools.geometry.GeneralEnvelope;
import org.geotools.image.ImageWorker;
import org.geotools.process.ProcessException;
import org.geotools.resources.image.ImageUtilities;
import org.opengis.geometry.Envelope;
import org.opengis.parameter.GeneralParameterValue;
import org.opengis.parameter.ParameterValueGroup;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
......@@ -91,28 +96,23 @@ public class GeoTiffPPIO extends BinaryPPIO {
// did we get lucky and all we need to do is to copy a file over?
final Object fileSource = coverage.getProperty(AbstractGridCoverage2DReader.FILE_SOURCE_PROPERTY);
if (fileSource != null && fileSource instanceof String) {
if (fileSource != null && fileSource instanceof String && isUnprocessed(coverage)) {
File file = new File((String) fileSource);
if(file.exists()) {
GeoTiffReader reader = null;
FileInputStream fis = null;
try {
// geotiff reader won't read unreferenced tiffs unless we tell it to
if(unreferenced) {
// just check if it has the proper extension for the moment, until
// we get a more reliable way to check if it's a tiff
String name = file.getName().toLowerCase();
if(!name.endsWith(".tiff") && !name.endsWith(".tif")) {
throw new IOException("Not a tiff");
}
} else {
reader = new GeoTiffReader(file);
reader.read(null);
reader = new GeoTiffReader(file);
GeneralEnvelope originalEnvelope = reader.getOriginalEnvelope();
Envelope envelope = coverage.getEnvelope();
if(originalEnvelope.equals(envelope, 1e-9, false)) {
GridCoverage2D test = reader.read(null);
ImageUtilities.disposeImage(test.getRenderedImage());
// ooh, a geotiff already!
fis = new FileInputStream(file);
IOUtils.copyLarge(fis, os);
return;
}
// ooh, a geotiff already!
fis = new FileInputStream(file);
IOUtils.copyLarge(fis, os);
return;
} catch(Exception e) {
// ok, not a geotiff!
} finally {
......@@ -177,6 +177,24 @@ public class GeoTiffPPIO extends BinaryPPIO {
}
}
/**
* Returns true if the coverage has not been processed in any way since it has been read
*
* @param coverage
* @return
*/
private boolean isUnprocessed(GridCoverage2D coverage) {
RenderedImage ri = coverage.getRenderedImage();
if(ri instanceof RenderedOp) {
RenderedOp op = (RenderedOp) ri;
return op.getOperationName().startsWith("ImageRead");
} else if(ri instanceof OpImage) {
return ri.getClass().getSimpleName().startsWith("ImageRead");
} else {
return true;
}
}
@Override
public String getFileExtension() {
return "tiff";
......
/* (c) 2014 - 2015 Open Source Geospatial Foundation - all rights reserved
* (c) 2014 OpenPlans
* This code is licensed under the GPL 2.0 license, available at the root
* application directory.
*/
package org.geoserver.wps.ppio;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.io.FileUtils;
import org.geoserver.data.test.SystemTestData;
import org.geotools.coverage.grid.GridCoverage2D;
import org.geotools.coverage.grid.GridCoverageFactory;
import org.geotools.coverage.grid.io.AbstractGridCoverage2DReader;
import org.geotools.gce.geotiff.GeoTiffReader;
import org.geotools.geometry.jts.JTS;
import org.geotools.geometry.jts.ReferencedEnvelope;
import org.geotools.process.raster.CropCoverage;
import org.geotools.resources.image.ImageUtilities;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class GeotiffPPIOTest {
File geotiff = new File("./target/test.tiff");
File target = new File("./target/target.tiff");
GeoTiffReader reader;
GridCoverage2D coverage;
@Before
public void prepareGeoTiff() throws IOException {
try(InputStream is = SystemTestData.class.getResourceAsStream("tazbm.tiff")) {
FileUtils.copyInputStreamToFile(is, geotiff);
}
reader = new GeoTiffReader(geotiff);
}
@After
public void cleanup() {
if(coverage != null) {
ImageUtilities.disposeImage(coverage.getRenderedImage());
}
if(reader != null) {
reader.dispose();
}
}
private GridCoverage2D getCoverage() throws IOException {
coverage = reader.read(null);
Map properties = new HashMap<>(coverage.getProperties());
properties.put(AbstractGridCoverage2DReader.FILE_SOURCE_PROPERTY, geotiff.getCanonicalPath());
return new GridCoverageFactory().create(coverage.getName(), coverage.getRenderedImage(), coverage.getEnvelope(), coverage.getSampleDimensions(), null, properties);
}
@Test
public void testRawCopy() throws Exception {
GridCoverage2D coverage = getCoverage();
GeoTiffPPIO ppio = new GeoTiffPPIO();
try(FileOutputStream fos = new FileOutputStream(target)) {
ppio.encode(coverage, fos);
}
// was a straight copy (a re-encoding would change the size as the input
// is compressed, the output is not)
assertEquals(geotiff.length(), target.length());
}
@Test
public void testCropped() throws Exception {
GridCoverage2D cov = getCoverage();
ReferencedEnvelope re = ReferencedEnvelope.reference(coverage.getEnvelope2D());
re.expandBy(-0.1);
this.coverage = new CropCoverage().execute(coverage, JTS.toGeometry(re), null);
GeoTiffPPIO ppio = new GeoTiffPPIO();
try(FileOutputStream fos = new FileOutputStream(target)) {
ppio.encode(coverage, fos);
}
// not a straight copy, size is different
assertNotEquals(geotiff.length(), target.length());
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment