Search in sources :

Example 1 with ImageTile

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());
}
Also used : TileMatrixSet(org.geotoolkit.storage.multires.TileMatrixSet) DefaultImageTile(org.geotoolkit.storage.coverage.DefaultImageTile) ImageTile(org.geotoolkit.storage.coverage.ImageTile) DefaultImageTile(org.geotoolkit.storage.coverage.DefaultImageTile) TileMatrix(org.geotoolkit.storage.multires.TileMatrix) BufferedImage(java.awt.image.BufferedImage) Test(org.junit.Test)

Example 2 with ImageTile

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();
}
Also used : TileMatrixSet(org.geotoolkit.storage.multires.TileMatrixSet) DefaultImageTile(org.geotoolkit.storage.coverage.DefaultImageTile) ImageTile(org.geotoolkit.storage.coverage.ImageTile) ArrayList(java.util.ArrayList) DefaultImageTile(org.geotoolkit.storage.coverage.DefaultImageTile) StoreEvent(org.apache.sis.storage.event.StoreEvent) TileMatrix(org.geotoolkit.storage.multires.TileMatrix) BufferedImage(java.awt.image.BufferedImage) Test(org.junit.Test)

Example 3 with ImageTile

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());
}
Also used : TileMatrixSet(org.geotoolkit.storage.multires.TileMatrixSet) DefaultImageTile(org.geotoolkit.storage.coverage.DefaultImageTile) ImageTile(org.geotoolkit.storage.coverage.ImageTile) DefaultImageTile(org.geotoolkit.storage.coverage.DefaultImageTile) TileMatrix(org.geotoolkit.storage.multires.TileMatrix) BufferedImage(java.awt.image.BufferedImage) Test(org.junit.Test)

Example 4 with ImageTile

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());
}
Also used : GeneralDirectPosition(org.apache.sis.geometry.GeneralDirectPosition) DefiningCoverageResource(org.geotoolkit.storage.coverage.DefiningCoverageResource) DefiningMosaic(org.geotoolkit.data.multires.DefiningMosaic) DefaultImageTile(org.geotoolkit.storage.coverage.DefaultImageTile) SampleDimension(org.apache.sis.coverage.SampleDimension) Dimension(java.awt.Dimension) Point(java.awt.Point) Pyramid(org.geotoolkit.data.multires.Pyramid) DefiningPyramid(org.geotoolkit.data.multires.DefiningPyramid) GenericName(org.opengis.util.GenericName) DefaultImageTile(org.geotoolkit.storage.coverage.DefaultImageTile) ImageTile(org.geotoolkit.storage.coverage.ImageTile) PyramidalCoverageResource(org.geotoolkit.storage.coverage.PyramidalCoverageResource) DefiningPyramid(org.geotoolkit.data.multires.DefiningPyramid) RenderedImage(java.awt.image.RenderedImage) DefiningMosaic(org.geotoolkit.data.multires.DefiningMosaic) Mosaic(org.geotoolkit.data.multires.Mosaic) Test(org.junit.Test)

Example 5 with ImageTile

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

ImageTile (org.geotoolkit.storage.coverage.ImageTile)6 DefaultImageTile (org.geotoolkit.storage.coverage.DefaultImageTile)5 Test (org.junit.Test)4 Point (java.awt.Point)3 BufferedImage (java.awt.image.BufferedImage)3 TileMatrix (org.geotoolkit.storage.multires.TileMatrix)3 TileMatrixSet (org.geotoolkit.storage.multires.TileMatrixSet)3 RenderedImage (java.awt.image.RenderedImage)2 Path (java.nio.file.Path)2 Dimension (java.awt.Dimension)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 ArrayList (java.util.ArrayList)1 GZIPOutputStream (java.util.zip.GZIPOutputStream)1 IIOImage (javax.imageio.IIOImage)1