use of org.opengis.coverage.grid.GridCoverageWriter in project georchestra by georchestra.
the class WcsCoverageReader method geotoolsTranformation.
private void geotoolsTranformation(final File sourceFile, final File file, final WcsReaderRequest request, final CoordinateReferenceSystem original) throws IOException {
LOG.info("using Geotools libraries to tranform the coverage");
CoverageTransformation<Object> transformation = new CoverageTransformation<Object>() {
@Override
public Object transform(final GridCoverage coverage) throws IOException, FactoryException {
boolean writeToTmp = sourceFile.equals(file);
Hints hints = new Hints(GeoTools.getDefaultHints());
hints.put(Hints.LENIENT_DATUM_SHIFT, Boolean.TRUE);
GeoTools.init(hints);
Coverage transformed = Operations.DEFAULT.resample(coverage, original);
AbstractGridFormat format = Formats.getFormat(request.format);
if (writeToTmp) {
File tmpDir = FileUtils.createTempDirectory();
try {
File tmpFile = new File(tmpDir, file.getName());
// write must be to tmpFile because Geotools does not always
// load coverages into memory but reads off disk
GridCoverageWriter writer = format.getWriter(tmpFile);
file.delete();
ParameterValue<String> formatParam = FORMAT.createValue();
formatParam.setValue(request.format);
writer.write((GridCoverage) transformed, new GeneralParameterValue[] { formatParam });
// so move all files in the tmpDir
for (File f : tmpDir.listFiles()) {
File dest = new File(file.getParentFile(), f.getName());
FileUtils.moveFile(f, dest);
}
} finally {
FileUtils.delete(tmpDir);
}
} else {
GridCoverageWriter writer = format.getWriter(file);
writer.write((GridCoverage) transformed, null);
}
LOG.debug("Finished reprojecting output");
return null;
}
};
CoverageTransformation.perform(sourceFile, transformation);
}
Aggregations