use of org.geotoolkit.storage.coverage.ImageTile in project geotoolkit by Geomatys.
the class CachedTiledGridCoverageResourceTest method testNoBlockingCache.
/**
* Test cache do not ask for the same tile twice in none blocking mode.
*/
@Test
public void testNoBlockingCache() throws DataStoreException, InterruptedException {
final MockTiledGridCoverageResource parent = new MockTiledGridCoverageResource(Names.createLocalName(null, null, "test"));
final TileMatrixSet tileMatrixSet = (TileMatrixSet) parent.createTileMatrixSet(TileMatrices.createWorldWGS84Template(0));
final TileMatrix tileMatrix = tileMatrixSet.getTileMatrices().iterator().next();
final ImageTile it0 = new DefaultImageTile(new BufferedImage(256, 256, BufferedImage.TYPE_INT_ARGB), 0, 0);
final ImageTile it1 = new DefaultImageTile(new BufferedImage(256, 256, BufferedImage.TYPE_INT_ARGB), 1, 0);
tileMatrix.writeTiles(Stream.of(it0, it1), null);
Assert.assertEquals(4, parent.localEvents.size());
Assert.assertEquals(MockTiledGridCoverageResource.EventType.TILE_MATRIX_SET_CREATED, parent.localEvents.get(0).type);
Assert.assertEquals(MockTiledGridCoverageResource.EventType.TILE_MATRIX_CREATED, parent.localEvents.get(1).type);
Assert.assertEquals(MockTiledGridCoverageResource.EventType.TILE_SET, parent.localEvents.get(2).type);
Assert.assertEquals(MockTiledGridCoverageResource.EventType.TILE_SET, parent.localEvents.get(3).type);
parent.localEvents.clear();
final CachedTiledGridCoverageResource r = new CachedTiledGridCoverageResource(parent, 30, 60, true, true);
final TileMatrixSet cacheTms = (TileMatrixSet) r.getTileMatrixSets().iterator().next();
final TileMatrix cacheTm = cacheTms.getTileMatrices().iterator().next();
// get tile a first time, should put in a queue to returned later
cacheTm.getTile(0, 0);
Assert.assertEquals(0, parent.localEvents.size());
// wait a little for the non blocking queue
Thread.sleep(1000);
Assert.assertEquals(1, parent.localEvents.size());
// acces tile again, must be in cache
cacheTm.getTile(0, 0);
Assert.assertEquals(1, parent.localEvents.size());
// get another tile, should put in a queue and returned later
cacheTm.getTile(1, 0);
cacheTm.getTile(1, 0);
cacheTm.getTile(1, 0);
Assert.assertEquals(1, parent.localEvents.size());
// wait a little for the non blocking queue
Thread.sleep(1000);
Assert.assertEquals(2, parent.localEvents.size());
// acces tiles again, must be in cache
cacheTm.getTile(0, 0);
cacheTm.getTile(1, 0);
Assert.assertEquals(2, parent.localEvents.size());
}
use of org.geotoolkit.storage.coverage.ImageTile in project geotoolkit by Geomatys.
the class CachedTiledGridCoverageResourceTest method testUpdate.
/**
* Test cache pyramid updates it's structure and clear it's cache on parent update.
*/
@Test
public void testUpdate() throws DataStoreException {
final InMemoryTiledGridCoverageResource parent = new InMemoryTiledGridCoverageResource(Names.createLocalName(null, null, "test"));
final CachedTiledGridCoverageResource r = new CachedTiledGridCoverageResource(parent, 30, 60, true);
final List<StoreEvent> events = new ArrayList<>();
r.addListener(StoreEvent.class, new StoreListener<StoreEvent>() {
@Override
public void eventOccured(StoreEvent event) {
events.add(event);
}
});
// change parent structure
final TileMatrixSet tileMatrixSet = (TileMatrixSet) parent.createTileMatrixSet(TileMatrices.createWorldWGS84Template(1));
Assert.assertEquals(1, events.size());
events.clear();
// write a new tile
final TileMatrix tileMatrix = tileMatrixSet.getTileMatrices().iterator().next();
final ImageTile it = new DefaultImageTile(new BufferedImage(256, 256, BufferedImage.TYPE_INT_ARGB), 0, 0);
tileMatrix.writeTiles(Stream.of(it), null);
Assert.assertEquals(1, events.size());
events.clear();
// update existing tile
final ImageTile it2 = new DefaultImageTile(new BufferedImage(256, 256, BufferedImage.TYPE_INT_ARGB), 0, 0);
tileMatrix.writeTiles(Stream.of(it2), null);
Assert.assertEquals(1, events.size());
events.clear();
}
use of org.geotoolkit.storage.coverage.ImageTile in project geotoolkit by Geomatys.
the class CachedTiledGridCoverageResourceTest method testBlockingCache.
/**
* Test cache do not ask for the same tile twice.
*/
@Test
public void testBlockingCache() throws DataStoreException {
final MockTiledGridCoverageResource parent = new MockTiledGridCoverageResource(Names.createLocalName(null, null, "test"));
final TileMatrixSet tileMatrixSet = (TileMatrixSet) parent.createTileMatrixSet(TileMatrices.createWorldWGS84Template(0));
final TileMatrix tileMatrix = tileMatrixSet.getTileMatrices().iterator().next();
final ImageTile it0 = new DefaultImageTile(new BufferedImage(256, 256, BufferedImage.TYPE_INT_ARGB), 0, 0);
final ImageTile it1 = new DefaultImageTile(new BufferedImage(256, 256, BufferedImage.TYPE_INT_ARGB), 1, 0);
tileMatrix.writeTiles(Stream.of(it0, it1), null);
Assert.assertEquals(4, parent.localEvents.size());
Assert.assertEquals(MockTiledGridCoverageResource.EventType.TILE_MATRIX_SET_CREATED, parent.localEvents.get(0).type);
Assert.assertEquals(MockTiledGridCoverageResource.EventType.TILE_MATRIX_CREATED, parent.localEvents.get(1).type);
Assert.assertEquals(MockTiledGridCoverageResource.EventType.TILE_SET, parent.localEvents.get(2).type);
Assert.assertEquals(MockTiledGridCoverageResource.EventType.TILE_SET, parent.localEvents.get(3).type);
parent.localEvents.clear();
final CachedTiledGridCoverageResource r = new CachedTiledGridCoverageResource(parent, 30, 60, true);
final TileMatrixSet cacheTms = (TileMatrixSet) r.getTileMatrixSets().iterator().next();
final TileMatrix cacheTm = cacheTms.getTileMatrices().iterator().next();
// get tile a first time, should be grabbed from the main resource
cacheTm.getTile(0, 0);
Assert.assertEquals(1, parent.localEvents.size());
// acces tile again, must be in cache
cacheTm.getTile(0, 0);
Assert.assertEquals(1, parent.localEvents.size());
// get tile a first time, should be grabbed from the main resource
cacheTm.getTile(1, 0);
Assert.assertEquals(2, parent.localEvents.size());
// acces tiles again, must be in cache
cacheTm.getTile(0, 0);
cacheTm.getTile(1, 0);
Assert.assertEquals(2, parent.localEvents.size());
}
use of org.geotoolkit.storage.coverage.ImageTile in project geotoolkit by Geomatys.
the class PGPyramidTest method testInsertUpdateDelete.
@Test
public void testInsertUpdateDelete() throws DataStoreException, VersioningException, IOException {
reload();
final GeneralDirectPosition upperLeft = new GeneralDirectPosition(CommonCRS.WGS84.geographic());
final Dimension dimension = new Dimension(20, 20);
upperLeft.setOrdinate(0, -90);
upperLeft.setOrdinate(1, +180);
PyramidalCoverageResource cref;
Pyramid pyramid;
Mosaic mosaic;
RenderedImage image;
final GenericName name = NamesExt.create(null, "versLayer");
store.add(new DefiningCoverageResource(name));
// create version 1 -----------------------------------------------------
cref = (PyramidalCoverageResource) store.findResource(name.toString());
assertNotNull(cref);
// test create pyramid
pyramid = (Pyramid) cref.createModel(new DefiningPyramid(CommonCRS.WGS84.geographic()));
assertEquals(1, cref.getModels().size());
// test create mosaic
mosaic = pyramid.createMosaic(new DefiningMosaic(null, upperLeft, 1, dimension, new Dimension(1, 4)));
pyramid = Pyramids.getPyramid(cref, pyramid.getIdentifier());
assertEquals(1, pyramid.getMosaics().size());
// test insert tile
mosaic.writeTiles(Stream.of(new DefaultImageTile(createImage(dimension, Color.RED), new Point(0, 0)), new DefaultImageTile(createImage(dimension, Color.GREEN), new Point(0, 1)), new DefaultImageTile(createImage(dimension, Color.BLUE), new Point(0, 2)), new DefaultImageTile(createImage(dimension, Color.YELLOW), new Point(0, 3))), null);
image = ((ImageTile) mosaic.getTile(0, 0)).getImage();
assertImageColor(image, Color.RED);
image = ((ImageTile) mosaic.getTile(0, 1)).getImage();
assertImageColor(image, Color.GREEN);
image = ((ImageTile) mosaic.getTile(0, 2)).getImage();
assertImageColor(image, Color.BLUE);
image = ((ImageTile) mosaic.getTile(0, 3)).getImage();
assertImageColor(image, Color.YELLOW);
// test delete tile
mosaic.deleteTile(0, 1);
assertNotNull(((ImageTile) mosaic.getTile(0, 2)).getInput());
mosaic.deleteTile(0, 2);
assertNull(((ImageTile) mosaic.getTile(0, 2)).getInput());
// test update tile
mosaic.writeTiles(Stream.of(new DefaultImageTile(createImage(dimension, Color.PINK), new Point(0, 3))), null);
image = ((ImageTile) mosaic.getTile(0, 3)).getImageReader().read(3);
assertImageColor(image, Color.PINK);
// test delete mosaic
pyramid.deleteMosaic(mosaic.getIdentifier());
pyramid = Pyramids.getPyramid(cref, pyramid.getIdentifier());
assertTrue(pyramid.getMosaics().isEmpty());
// test delete pyramid
cref.removeModel(pyramid.getIdentifier());
assertTrue(cref.getModels().isEmpty());
}
use of org.geotoolkit.storage.coverage.ImageTile 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