@DescribeParameter(name = "transparent", min = 0, description = "Force output to have transparent background", defaultValue = "true") Boolean transparent,
@DescribeParameter(name = "store", min = 0, description = "Indicates whether to keep the output file after processing", defaultValue = "false") Boolean store,
@DescribeParameter(name = "outputPath", min = 0, description = "Pathname where the output file is stored") String outputPath)
throws IOException {
GeoTiffReader reader = null;
List<File> removeFiles = new ArrayList<File>();
String location = null;
try {
File tempFolder = config.getTempFolder();
File loggingFolder = config.getLoggingFolder();
// do we have to add the alpha channel?
boolean forceTransparent = false;
if (transparent == null) {
transparent = true;
}
ColorModel cm = coverage.getRenderedImage().getColorModel();
if (cm.getTransparency() == Transparency.OPAQUE && transparent) {
forceTransparent = true;
}
// //
//
// STEP 1: Getting the dataset to be georectified
//
// //
final Object fileSource = coverage.getProperty(GridCoverage2DReader.FILE_SOURCE_PROPERTY);
if (fileSource != null && fileSource instanceof String) {
location = (String) fileSource;
}
if (location == null) {
RenderedImage image = coverage.getRenderedImage();
if (forceTransparent) {
ImageWorker iw = new ImageWorker(image);
iw.forceComponentColorModel();
final ImageLayout tempLayout = new ImageLayout(image);
tempLayout.unsetValid(ImageLayout.COLOR_MODEL_MASK).unsetValid(
ImageLayout.SAMPLE_MODEL_MASK);
RenderedImage alpha = ConstantDescriptor.create(
Float.valueOf(image.getWidth()), Float.valueOf(image.getHeight()),
new Byte[] { Byte.valueOf((byte) 255) }, new RenderingHints(
JAI.KEY_IMAGE_LAYOUT, tempLayout));
iw.addBand(alpha, false);
image = iw.getRenderedImage();
cm = image.getColorModel();
}
File storedImageFile = storeImage(image, tempFolder);
location = storedImageFile.getAbsolutePath();
removeFiles.add(storedImageFile);
}
// //
//
// STEP 2: Adding Ground Control Points
//
// //
final int gcpNum[] = new int[1];
final String gcp = parseGcps(gcps, gcpNum);
File vrtFile = addGroundControlPoints(location, gcp,
config.getGdalTranslateParameters());
if (vrtFile == null || !vrtFile.exists() || !vrtFile.canRead()) {
throw new IOException(
"Unable to get a valid file with attached Ground Control Points");
}
removeFiles.add(vrtFile);
// //
//
// STEP 3: Warping
//
// //
File warpedFile = warpFile(vrtFile, bbox, crs, width, height, warpOrder, tempFolder,
loggingFolder, config.getExecutionTimeout(), config.getGdalWarpingParameters());
if (warpedFile == null || !warpedFile.exists() || !warpedFile.canRead()) {
throw new IOException("Unable to get a valid georectified file");
}
boolean expand = false;
if (cm instanceof IndexColorModel) {
expand = true;
} else if (cm instanceof ComponentColorModel && cm.getNumComponents() == 1
&& cm.getComponentSize()[0] == 1) {
expand = true;
}
if (expand) {
removeFiles.add(warpedFile);
warpedFile = expandRgba(warpedFile.getAbsolutePath());
}
// if we have the output path move the final file there
if(Boolean.TRUE.equals(store) && outputPath != null) {
File output = new File(outputPath);
if(output.exists()) {
if(!output.delete()) {
throw new WPSException("Output file " + outputPath + " exists but cannot be overwritten");
}
} else {
File parent = output.getParentFile();
if(!parent.exists()) {
if(!parent.mkdirs()) {
throw new WPSException("Output file parent directory "
+ parent.getAbsolutePath() + " does not exist and cannot be created");
}
}
}
if(!warpedFile.renameTo(output)) {
throw new WPSException("Could not move "
+ warpedFile.getAbsolutePath() + " to " + outputPath
+ ", it's likely a permission issue");
}
warpedFile = output;
}
// mark the output file for deletion at the end of request
if (resourceManager != null && !Boolean.TRUE.equals(store)) {
resourceManager.addResource(new WPSFileResource(warpedFile));
}
// //
//
// FINAL STEP: Returning the warped gridcoverage
//
// //
reader = new GeoTiffReader(warpedFile);
GridCoverage2D cov = addLocationProperty(reader.read(null), warpedFile);
Map<String, Object> result = new HashMap<String, Object>();
result.put("result", cov);
result.put("path", warpedFile.getAbsolutePath());
return result;
} finally {
if (reader != null) {
try {
reader.dispose();
} catch (Throwable t) {
// Does nothing
}
}