Search in sources :

Example 1 with GridCoverageWriter

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);
}
Also used : GridCoverage(org.opengis.coverage.grid.GridCoverage) Hints(org.geotools.util.factory.Hints) AbstractGridFormat(org.geotools.coverage.grid.io.AbstractGridFormat) GridCoverage(org.opengis.coverage.grid.GridCoverage) Coverage(org.opengis.coverage.Coverage) GridCoverageWriter(org.opengis.coverage.grid.GridCoverageWriter) File(java.io.File)

Aggregations

File (java.io.File)1 AbstractGridFormat (org.geotools.coverage.grid.io.AbstractGridFormat)1 Hints (org.geotools.util.factory.Hints)1 Coverage (org.opengis.coverage.Coverage)1 GridCoverage (org.opengis.coverage.grid.GridCoverage)1 GridCoverageWriter (org.opengis.coverage.grid.GridCoverageWriter)1