Search in sources :

Example 1 with Pyramid

use of org.geotoolkit.data.multires.Pyramid in project geotoolkit by Geomatys.

the class PGCoverageResource method createModel.

@Override
public MultiResolutionModel createModel(MultiResolutionModel template) throws DataStoreException {
    if (template instanceof Pyramid) {
        final Pyramid p = (Pyramid) template;
        final Pyramid n = newPyramid(p.getCoordinateReferenceSystem());
        Pyramids.copyStructure(p, n);
        return n;
    } else {
        throw new DataStoreException("Unsupported template : " + template);
    }
}
Also used : Pyramid(org.geotoolkit.data.multires.Pyramid) DataStoreException(org.apache.sis.storage.DataStoreException)

Example 2 with Pyramid

use of org.geotoolkit.data.multires.Pyramid in project geotoolkit by Geomatys.

the class PGCoverageResource method newPyramid.

private Pyramid newPyramid(final CoordinateReferenceSystem crs) throws DataStoreException {
    String pyramidId = "";
    Connection cnx = null;
    Statement stmt = null;
    ResultSet rs = null;
    try {
        cnx = pgstore.getDataSource().getConnection();
        cnx.setReadOnly(false);
        // find or insert coordinate reference system
        final PGEPSGWriter writer = new PGEPSGWriter(pgstore);
        final String epsgCode = String.valueOf(writer.getOrCreateCoordinateReferenceSystem(crs));
        stmt = cnx.createStatement();
        final int layerId = pgstore.getLayerId(cnx, getIdentifier().get().tip().toString());
        StringBuilder query = new StringBuilder();
        query.append("INSERT INTO ");
        query.append(pgstore.encodeTableName("Pyramid"));
        query.append("(\"layerId\",\"epsg\") VALUES (");
        query.append(layerId);
        query.append(",'");
        query.append(epsgCode);
        query.append("')");
        stmt.executeUpdate(query.toString(), new String[] { "id" });
        rs = stmt.getGeneratedKeys();
        if (rs.next()) {
            pyramidId = String.valueOf(rs.getInt(1));
        }
        // write version
        if (version != null && !version.getLabel().equals(PGVersionControl.UNSET)) {
            query.setLength(0);
            query.append("INSERT INTO ");
            query.append(pgstore.encodeTableName("PyramidProperty"));
            query.append("(\"pyramidId\",\"key\",\"type\",\"value\") VALUES (");
            query.append(pyramidId);
            query.append(",'version','date','");
            query.append(TemporalUtilities.toISO8601Z(version.getDate(), TimeZone.getTimeZone("GMT+0")));
            query.append("')");
            stmt.executeUpdate(query.toString());
        }
    } catch (FactoryException ex) {
        throw new DataStoreException(ex);
    } catch (SQLException ex) {
        throw new DataStoreException(ex);
    } finally {
        pgstore.closeSafe(cnx, stmt, rs);
    }
    mustUpdate();
    final CoverageStoreManagementEvent event = firePyramidAdded(pyramidId);
    getOriginator().forwardEvent(event);
    for (Pyramid p : getPyramids()) {
        if (p.getIdentifier().equals(pyramidId)) {
            return p;
        }
    }
    // should not happen
    throw new DataStoreException("Generated pyramid not found.");
}
Also used : PGEPSGWriter(org.geotoolkit.coverage.postgresql.epsg.PGEPSGWriter) Pyramid(org.geotoolkit.data.multires.Pyramid) DataStoreException(org.apache.sis.storage.DataStoreException) FactoryException(org.opengis.util.FactoryException) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) CoverageStoreManagementEvent(org.geotoolkit.storage.coverage.CoverageStoreManagementEvent) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) Point(java.awt.Point)

Example 3 with Pyramid

use of org.geotoolkit.data.multires.Pyramid in project geotoolkit by Geomatys.

the class PGPyramidTest method testSampleDimensions.

@Test
public void testSampleDimensions() throws DataStoreException, VersioningException, IOException, TransformException {
    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;
    BufferedImage image;
    final GenericName name = NamesExt.create(null, "sampleTestLayer");
    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());
    final List<SampleDimension> dimensions = new LinkedList<>();
    // dim 1
    SampleDimension.Builder b = new SampleDimension.Builder();
    b.addQuantitative("data", NumberRange.create(1, true, 100, true), MeasurementRange.create(-50.0, true, 45.6, true, Units.CELSIUS));
    b.addQualitative(Vocabulary.formatInternational(Vocabulary.Keys.Nodata), Double.NaN);
    final SampleDimension dim1 = b.setName("dim0").build();
    dimensions.add(0, dim1);
    // dim 2
    b.clear();
    b.addQuantitative("data", 1, 55, 2.0, 0.0, Units.METRE);
    b.addQualitative(null, 0);
    final SampleDimension dim2 = b.setName("dim1").build();
    dimensions.add(1, dim2);
    // test create SampleDimensions
    cref.setSampleDimensions(dimensions);
    List<SampleDimension> resultSamples = cref.getSampleDimensions();
    assertNotNull(resultSamples);
    assertEquals(2, resultSamples.size());
    SampleDimension resultDim1 = resultSamples.get(0);
    SampleDimension resultDim2 = resultSamples.get(1);
    assertNotNull(resultDim1);
    assertNotNull(resultDim2);
    assertEquals("dim0", resultDim1.getName().toString());
    assertEquals("dim1", resultDim2.getName().toString());
    assertEquals(Units.CELSIUS, resultDim1.getUnits());
    assertEquals(Units.METRE, resultDim2.getUnits());
    List<Category> resultCat1 = resultDim1.getCategories();
    List<Category> resultCat2 = resultDim2.getCategories();
    assertEquals(2, resultCat1.size());
    assertEquals(2, resultCat2.size());
// assertCategoryEquals(dataCat, resultCat1);
// assertCategoryEquals(nodataCat, resultCat1);
// assertCategoryEquals(dataCat2, resultCat2);
// assertCategoryEquals(nodataCat2, resultCat2);
}
Also used : GeneralDirectPosition(org.apache.sis.geometry.GeneralDirectPosition) Category(org.apache.sis.coverage.Category) DefiningCoverageResource(org.geotoolkit.storage.coverage.DefiningCoverageResource) SampleDimension(org.apache.sis.coverage.SampleDimension) Dimension(java.awt.Dimension) SampleDimension(org.apache.sis.coverage.SampleDimension) BufferedImage(java.awt.image.BufferedImage) Pyramid(org.geotoolkit.data.multires.Pyramid) DefiningPyramid(org.geotoolkit.data.multires.DefiningPyramid) GenericName(org.opengis.util.GenericName) PyramidalCoverageResource(org.geotoolkit.storage.coverage.PyramidalCoverageResource) DefiningPyramid(org.geotoolkit.data.multires.DefiningPyramid) DefiningMosaic(org.geotoolkit.data.multires.DefiningMosaic) Mosaic(org.geotoolkit.data.multires.Mosaic) Test(org.junit.Test)

Example 4 with Pyramid

use of org.geotoolkit.data.multires.Pyramid 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 Pyramid

use of org.geotoolkit.data.multires.Pyramid in project geotoolkit by Geomatys.

the class PGPyramid method createMosaic.

@Override
public Mosaic createMosaic(Mosaic template) throws DataStoreException {
    final Dimension gridSize = template.getGridSize();
    final Dimension tilePixelSize = template.getTileSize();
    final DirectPosition upperleft = template.getUpperLeftCorner();
    final double pixelscale = template.getScale();
    final PGCoverageStore pgstore = ref.getOriginator();
    long mosaicId = 0;
    Connection cnx = null;
    Statement stmt = null;
    ResultSet rs = null;
    try {
        cnx = pgstore.getDataSource().getConnection();
        cnx.setReadOnly(false);
        stmt = cnx.createStatement();
        StringBuilder query = new StringBuilder();
        query.append("INSERT INTO ");
        query.append(pgstore.encodeTableName("Mosaic"));
        query.append("(\"pyramidId\",\"upperCornerX\",\"upperCornerY\",\"gridWidth\",\"gridHeight\",\"scale\",\"tileWidth\",\"tileHeight\") VALUES (");
        query.append(id).append(',');
        query.append(upperleft.getOrdinate(0)).append(',');
        query.append(upperleft.getOrdinate(1)).append(',');
        query.append(gridSize.width).append(',');
        query.append(gridSize.height).append(',');
        query.append(pixelscale).append(',');
        query.append(tilePixelSize.width).append(',');
        query.append(tilePixelSize.height);
        query.append(")");
        stmt.executeUpdate(query.toString(), new String[] { "id" });
        rs = stmt.getGeneratedKeys();
        if (rs.next()) {
            mosaicId = rs.getLong(1);
        }
        final CoordinateReferenceSystem crs = upperleft.getCoordinateReferenceSystem();
        final CoordinateSystem cs = crs.getCoordinateSystem();
        for (int i = 2, n = cs.getDimension(); i < n; i++) {
            final double value = upperleft.getOrdinate(i);
            query = new StringBuilder();
            query.append("INSERT INTO ");
            query.append(pgstore.encodeTableName("MosaicAxis"));
            query.append("(\"mosaicId\",\"indice\",\"value\") VALUES (");
            query.append(mosaicId).append(',');
            query.append(i).append(',');
            query.append(value);
            query.append(")");
            stmt.executeUpdate(query.toString());
        }
    } catch (SQLException ex) {
        throw new DataStoreException(ex.getMessage(), ex);
    } finally {
        pgstore.closeSafe(cnx, stmt, rs);
    }
    ref.mustUpdate();
    final CoverageStoreManagementEvent event = ref.fireMosaicAdded(getIdentifier(), String.valueOf(mosaicId));
    pgstore.forwardEvent(event);
    for (final Pyramid p : ref.getPyramids()) {
        if (p.getIdentifier().equals(getIdentifier())) {
            for (Mosaic mosaic : p.getMosaics()) {
                if (((PGGridMosaic) mosaic).getDatabaseId() == mosaicId) {
                    return mosaic;
                }
            }
        }
    }
    // should not happen
    throw new DataStoreException("Generated mosaic not found.");
}
Also used : DirectPosition(org.opengis.geometry.DirectPosition) DataStoreException(org.apache.sis.storage.DataStoreException) SQLException(java.sql.SQLException) Statement(java.sql.Statement) CoordinateSystem(org.opengis.referencing.cs.CoordinateSystem) CoverageStoreManagementEvent(org.geotoolkit.storage.coverage.CoverageStoreManagementEvent) Connection(java.sql.Connection) Dimension(java.awt.Dimension) Pyramid(org.geotoolkit.data.multires.Pyramid) AbstractPyramid(org.geotoolkit.data.multires.AbstractPyramid) ResultSet(java.sql.ResultSet) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) Mosaic(org.geotoolkit.data.multires.Mosaic)

Aggregations

Pyramid (org.geotoolkit.data.multires.Pyramid)7 Dimension (java.awt.Dimension)4 DataStoreException (org.apache.sis.storage.DataStoreException)4 Mosaic (org.geotoolkit.data.multires.Mosaic)4 PyramidalCoverageResource (org.geotoolkit.storage.coverage.PyramidalCoverageResource)4 Point (java.awt.Point)3 GeneralDirectPosition (org.apache.sis.geometry.GeneralDirectPosition)3 DefiningMosaic (org.geotoolkit.data.multires.DefiningMosaic)3 DefiningPyramid (org.geotoolkit.data.multires.DefiningPyramid)3 DefiningCoverageResource (org.geotoolkit.storage.coverage.DefiningCoverageResource)3 Test (org.junit.Test)3 GenericName (org.opengis.util.GenericName)3 Connection (java.sql.Connection)2 ResultSet (java.sql.ResultSet)2 SQLException (java.sql.SQLException)2 Statement (java.sql.Statement)2 SampleDimension (org.apache.sis.coverage.SampleDimension)2 CoverageStoreManagementEvent (org.geotoolkit.storage.coverage.CoverageStoreManagementEvent)2 DefaultImageTile (org.geotoolkit.storage.coverage.DefaultImageTile)2 BufferedImage (java.awt.image.BufferedImage)1