Search in sources :

Example 1 with Assets

use of org.geotoolkit.storage.Assets in project geotoolkit by Geomatys.

the class URITileMatrix method writeTile.

/**
 * Note : we don't start by deleting the previous tile, the end replace operation will do it.
 * @param tile
 * @throws DataStoreException
 */
@Override
protected void writeTile(Tile tile) throws DataStoreException {
    ArgumentChecks.ensureNonNull("tile", tile);
    final int tileX = tile.getPosition().x;
    final int tileY = tile.getPosition().y;
    final URI tileUri = getTilePath(tileX, tileY);
    final Path tilePath = toPath(tileUri);
    if (tilePath == null) {
        throw new DataStoreException("Tile " + tileUri + " is not on the file system, writing is not supported.");
    }
    final Path tempTilePath = tilePath.resolveSibling(tilePath.getFileName().toString() + "_" + Thread.currentThread().getId());
    try {
        Files.createDirectories(tilePath.getParent());
    } catch (IOException ex) {
        throw new DataStoreException(ex.getMessage(), ex);
    }
    if (format.isImage()) {
        if (tile instanceof ImageTile) {
            try {
                final RenderedImage image = ((ImageTile) tile).getImage();
                final ImageWriterSpi writerSpi = XImageIO.getImageWriterSpi(format.getImageSpi());
                final ImageWriter writer = writerSpi.createWriterInstance();
                final File output = tempTilePath.toFile();
                output.delete();
                final ImageOutputStream stream = createImageOutputStream(output);
                try {
                    ImageWriteParam writeParam = writer.getDefaultWriteParam();
                    if (format.getImageCompression() != null) {
                        writeParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
                        writeParam.setCompressionType(format.getImageCompression());
                    }
                    writer.setOutput(stream);
                    writer.write(null, new IIOImage(image, null, null), writeParam);
                    writer.dispose();
                } finally {
                    stream.close();
                }
            } catch (IOException ex) {
                throw new DataStoreException(ex.getMessage(), ex);
            }
        } else {
            throw new DataStoreException("Invalid tile class " + tile.getClass() + ". " + "TileMatrixSet format is declared as image, an ImageTile instance is expected.");
        }
    } else {
        final DataStoreProvider provider = format.getStoreProvider();
        final StorageConnector connector = new StorageConnector(tempTilePath);
        try (final DataStore store = provider.open(connector)) {
            Resource tileData = tile;
            if (tile instanceof DeferredTile) {
                tileData = ((DeferredTile) tile).open();
            }
            if (tileData instanceof Assets) {
                final Assets source = (Assets) tile;
                if (store instanceof Assets) {
                    final Assets target = (Assets) store;
                    for (Data data : source.getDatas()) {
                        target.addData(data);
                    }
                } else {
                    throw new DataStoreException("No procedure found to copy from " + tile.getClass() + " to " + store.getClass());
                }
            } else if (tileData instanceof BandedCoverageResource && store instanceof WritableBandedCoverageResource) {
                final BandedCoverage bandedCoverage = ((BandedCoverageResource) tileData).read(null);
                ((WritableBandedCoverageResource) store).write(bandedCoverage);
            } else {
                throw new DataStoreException("No procedure found to copy from " + tile.getClass() + " to " + store.getClass());
            }
        } finally {
            connector.closeAllExcept(null);
        }
    }
    // compress produced tile
    final URITileFormat.Compression compression = format.getCompression();
    switch(compression.name()) {
        case "GZ":
            {
                // read and compress datas
                byte[] datas;
                try (BufferedInputStream in = new BufferedInputStream(Files.newInputStream(tempTilePath))) {
                    final ByteArrayOutputStream out = new ByteArrayOutputStream();
                    final GZIPOutputStream gout = new GZIPOutputStream(out);
                    IOUtilities.copy(in, gout);
                    gout.finish();
                    gout.close();
                    datas = out.toByteArray();
                } catch (IOException ex) {
                    throw new DataStoreException(ex.getMessage(), ex);
                }
                // write result
                try (OutputStream fout = new BufferedOutputStream(Files.newOutputStream(tempTilePath))) {
                    fout.write(datas);
                } catch (IOException ex) {
                    throw new DataStoreException(ex.getMessage(), ex);
                }
            }
            break;
        case "LZ4":
            {
                // read and compress datas
                byte[] datas;
                try (BufferedInputStream in = new BufferedInputStream(Files.newInputStream(tempTilePath))) {
                    final ByteArrayOutputStream out = new ByteArrayOutputStream();
                    final FramedLZ4CompressorOutputStream gout = new FramedLZ4CompressorOutputStream(out);
                    IOUtilities.copy(in, gout);
                    gout.finish();
                    gout.close();
                    datas = out.toByteArray();
                } catch (IOException ex) {
                    throw new DataStoreException(ex.getMessage(), ex);
                }
                // write result
                try (OutputStream fout = new BufferedOutputStream(Files.newOutputStream(tempTilePath))) {
                    fout.write(datas);
                } catch (IOException ex) {
                    throw new DataStoreException(ex.getMessage(), ex);
                }
            }
            break;
    }
    try {
        // replace tile file by new tile.
        Files.move(tempTilePath, tilePath, StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.ATOMIC_MOVE);
    } catch (IOException ex) {
        throw new DataStoreException(ex.getMessage(), ex);
    }
}
Also used : StorageConnector(org.apache.sis.storage.StorageConnector) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ImageIO.createImageOutputStream(javax.imageio.ImageIO.createImageOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) FramedLZ4CompressorOutputStream(org.apache.commons.compress.compressors.lz4.FramedLZ4CompressorOutputStream) ImageOutputStream(javax.imageio.stream.ImageOutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) ImageWriter(javax.imageio.ImageWriter) URI(java.net.URI) IIOImage(javax.imageio.IIOImage) ImageTile(org.geotoolkit.storage.coverage.ImageTile) BufferedInputStream(java.io.BufferedInputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) Assets(org.geotoolkit.storage.Assets) DataStore(org.apache.sis.storage.DataStore) BufferedOutputStream(java.io.BufferedOutputStream) ImageIO.createImageOutputStream(javax.imageio.ImageIO.createImageOutputStream) ImageOutputStream(javax.imageio.stream.ImageOutputStream) Path(java.nio.file.Path) DataStoreException(org.apache.sis.storage.DataStoreException) WritableBandedCoverageResource(org.geotoolkit.storage.coverage.WritableBandedCoverageResource) DataStoreProvider(org.apache.sis.storage.DataStoreProvider) BandedCoverageResource(org.geotoolkit.storage.coverage.BandedCoverageResource) Resource(org.apache.sis.storage.Resource) WritableBandedCoverageResource(org.geotoolkit.storage.coverage.WritableBandedCoverageResource) BandedCoverageResource(org.geotoolkit.storage.coverage.BandedCoverageResource) WritableBandedCoverageResource(org.geotoolkit.storage.coverage.WritableBandedCoverageResource) Data(org.geotoolkit.storage.Assets.Data) BandedCoverage(org.apache.sis.coverage.BandedCoverage) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ImageWriteParam(javax.imageio.ImageWriteParam) Point(java.awt.Point) DeferredTile(org.geotoolkit.storage.multires.DeferredTile) RenderedImage(java.awt.image.RenderedImage) ImageWriterSpi(javax.imageio.spi.ImageWriterSpi) File(java.io.File) FramedLZ4CompressorOutputStream(org.apache.commons.compress.compressors.lz4.FramedLZ4CompressorOutputStream)

Aggregations

Point (java.awt.Point)1 RenderedImage (java.awt.image.RenderedImage)1 BufferedInputStream (java.io.BufferedInputStream)1 BufferedOutputStream (java.io.BufferedOutputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 URI (java.net.URI)1 Path (java.nio.file.Path)1 GZIPOutputStream (java.util.zip.GZIPOutputStream)1 IIOImage (javax.imageio.IIOImage)1 ImageIO.createImageOutputStream (javax.imageio.ImageIO.createImageOutputStream)1 ImageWriteParam (javax.imageio.ImageWriteParam)1 ImageWriter (javax.imageio.ImageWriter)1 ImageWriterSpi (javax.imageio.spi.ImageWriterSpi)1 ImageOutputStream (javax.imageio.stream.ImageOutputStream)1 FramedLZ4CompressorOutputStream (org.apache.commons.compress.compressors.lz4.FramedLZ4CompressorOutputStream)1 BandedCoverage (org.apache.sis.coverage.BandedCoverage)1 DataStore (org.apache.sis.storage.DataStore)1