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);
}
}
Aggregations