Commit e8a49db9 authored by Alessio Fabiani's avatar Alessio Fabiani
Browse files

- GEOS-6275 GEOS-6276 Back Port from 2.5.x

parent 4d216d5f
......@@ -5,6 +5,7 @@
package org.geoserver.wps.ppio;
import java.awt.Dimension;
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
......@@ -12,40 +13,40 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.imageio.ImageIO;
import javax.media.jai.JAI;
import org.apache.commons.io.IOUtils;
import org.geoserver.wps.WPSException;
import org.geotools.coverage.grid.GridCoverage2D;
import org.geotools.coverage.grid.io.GridCoverage2DReader;
import org.geotools.coverage.grid.io.AbstractGridCoverage2DReader;
import org.geotools.coverage.grid.io.AbstractGridCoverageWriter;
import org.geotools.coverage.grid.io.AbstractGridFormat;
import org.geotools.coverage.grid.io.GridFormatFinder;
import org.geotools.coverage.grid.io.UnknownFormat;
import org.geotools.coverage.grid.io.imageio.GeoToolsWriteParams;
import org.geotools.factory.Hints;
import org.geotools.gce.geotiff.GeoTiffFormat;
import org.geotools.gce.geotiff.GeoTiffReader;
import org.geotools.gce.geotiff.GeoTiffWriteParams;
import org.geotools.image.ImageWorker;
import org.geotools.process.ProcessException;
import org.opengis.parameter.GeneralParameterValue;
import org.opengis.parameter.ParameterValueGroup;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import org.opengis.referencing.crs.EngineeringCRS;
import com.sun.media.jai.operator.ImageReadDescriptor;
/**
* Decodes/encodes a GeoTIFF file
*
* @author Andrea Aime - OpenGeo
* @author Simone Giannecchini, GeoSolutions
*
*/
public class GeoTiffPPIO extends BinaryPPIO {
private final static GeoTiffWriteParams DEFAULT_WRITE_PARAMS;
private final static GeoTiffFormat TIFF_FORMAT = new GeoTiffFormat();
static {
// setting the write parameters (write out using tiling)
DEFAULT_WRITE_PARAMS = new GeoTiffWriteParams();
......@@ -83,12 +84,12 @@ public class GeoTiffPPIO extends BinaryPPIO {
@Override
public void encode(Object value, OutputStream os) throws Exception {
GridCoverage2D coverage = (GridCoverage2D) value;
CoordinateReferenceSystem crs = coverage.getCoordinateReferenceSystem();
boolean unreferenced = crs == null || crs instanceof EngineeringCRS;
boolean unreferenced = crs == null || crs instanceof EngineeringCRS;
// did we get lucky and all we need to do is to copy a file over?
final Object fileSource = coverage.getProperty(GridCoverage2DReader.FILE_SOURCE_PROPERTY);
final Object fileSource = coverage.getProperty(AbstractGridCoverage2DReader.FILE_SOURCE_PROPERTY);
if (fileSource != null && fileSource instanceof String) {
File file = new File((String) fileSource);
if(file.exists()) {
......@@ -124,23 +125,38 @@ public class GeoTiffPPIO extends BinaryPPIO {
}
}
// tiling
final RenderedImage renderedImage = coverage.getRenderedImage();
final int tileWidth=renderedImage.getTileWidth();
final int tileHeight=renderedImage.getTileHeight();
final boolean tiled= tileWidth!=renderedImage.getWidth()&& tileHeight!=renderedImage.getHeight();
// ok, encode in geotiff
if(unreferenced) {
new ImageWorker(coverage.getRenderedImage()).writeTIFF(os, "LZW", 0.75f, 256, 256);
if(tiled){
new ImageWorker(renderedImage).writeTIFF(os, null, 0.75f, tileWidth, tileHeight);
} else {
final Dimension defaultTileSize = JAI.getDefaultTileSize();
new ImageWorker(renderedImage).writeTIFF(os, null, 0.75f, defaultTileSize.width, defaultTileSize.height);
}
} else {
GeoTiffFormat format = new GeoTiffFormat();
final GeoTiffFormat wformat = new GeoTiffFormat();
final GeoTiffWriteParams wp = new GeoTiffWriteParams();
wp.setCompressionMode(GeoTiffWriteParams.MODE_EXPLICIT);
wp.setCompressionType("LZW");
// tiling
wp.setTilingMode(GeoToolsWriteParams.MODE_EXPLICIT);
wp.setTiling(256, 256);
final ParameterValueGroup wparams = wformat.getWriteParameters();
wparams.parameter(AbstractGridFormat.GEOTOOLS_WRITE_PARAMS.getName().toString())
.setValue(wp);
if(tiled){
wp.setTiling(tileWidth, tileHeight);
} else {
final Dimension defaultTileSize = JAI.getDefaultTileSize();
wp.setTiling(defaultTileSize.width, defaultTileSize.height);
}
final GeneralParameterValue[] wps = (GeneralParameterValue[]) wparams.values().toArray(
new GeneralParameterValue[1]);
final ParameterValueGroup wparams = TIFF_FORMAT.getWriteParameters();
wparams.parameter(AbstractGridFormat.GEOTOOLS_WRITE_PARAMS.getName().toString()).setValue(wp);
final GeneralParameterValue[] wps = (GeneralParameterValue[]) wparams.values().toArray(new GeneralParameterValue[1]);
// write out the coverage
AbstractGridCoverageWriter writer = (AbstractGridCoverageWriter) format.getWriter(os);
if (writer == null)
......@@ -148,6 +164,8 @@ public class GeoTiffPPIO extends BinaryPPIO {
"Could not find the GeoTIFF writer, please check it's in the classpath");
try {
writer.write(coverage, wps);
} catch(IOException e) {
throw new ProcessException(e);
} finally {
try {
writer.dispose();
......
......@@ -142,7 +142,7 @@ public class Coverage2RenderedImageAdapterTest extends WPSTestSupport {
GridCoverage2D src = createTestCoverage(500,500, 0,0 ,10,10);
GridCoverage2D dst = createTestCoverage(250,250, 0,0 ,10,10);
GridCoverage2DRIA cria = GridCoverage2DRIA.create(src, dst, NODATA);
GridCoverage2DRIA cria = GridCoverage2DRIA.create(dst, src, NODATA);
//--- internal points should double coords (no interp on coords)
Point2D psrc = new Point2D.Double(13d,16d); // this is on dst gc
......@@ -173,7 +173,7 @@ public class Coverage2RenderedImageAdapterTest extends WPSTestSupport {
// double nodata[] = src.getSampleDimension(0).getNoDataValues();
GridCoverage2DRIA cria = GridCoverage2DRIA.create(src, dst, NODATA);
GridCoverage2DRIA cria = GridCoverage2DRIA.create(dst, src, NODATA);
//--- internal points should halves coords (no interp on coords)
Point2D psrc = new Point2D.Double(0d,0d);
......@@ -203,7 +203,7 @@ public class Coverage2RenderedImageAdapterTest extends WPSTestSupport {
GridCoverage2D src = createTestCoverage(500,500, 0,0 ,5,5);
GridCoverage2D dst = createTestCoverage(500,500, 2,2 ,5,5);
GridCoverage2DRIA cria = GridCoverage2DRIA.create(src, dst, NODATA);
GridCoverage2DRIA cria = GridCoverage2DRIA.create(dst, src, NODATA);
//--- internal points should halves coords (no interp on coords)
Point2D psrc = new Point2D.Double(0d,499d); // this is on dst gc
......
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