Search in sources :

Example 1 with TestGeoPackageProgress

use of mil.nga.geopackage.io.TestGeoPackageProgress in project geopackage-java by ngageoint.

the class FeatureIndexManagerUtils method testIndex.

private static void testIndex(GeoPackage geoPackage, FeatureIndexType type, boolean includeEmpty) throws SQLException {
    // Test indexing each feature table
    List<String> featureTables = geoPackage.getFeatureTables();
    for (String featureTable : featureTables) {
        FeatureDao featureDao = geoPackage.getFeatureDao(featureTable);
        FeatureIndexManager featureIndexManager = new FeatureIndexManager(geoPackage, featureDao);
        featureIndexManager.setContinueOnError(false);
        featureIndexManager.setIndexLocation(type);
        featureIndexManager.deleteAllIndexes();
        // Determine how many features have geometry envelopes or geometries
        int expectedCount = 0;
        FeatureRow testFeatureRow = null;
        FeatureResultSet featureResultSet = featureDao.query();
        while (featureResultSet.moveToNext()) {
            FeatureRow featureRow = featureResultSet.getRow();
            if (featureRow.getGeometryEnvelope() != null) {
                expectedCount++;
                // queries later
                if (testFeatureRow == null) {
                    testFeatureRow = featureRow;
                } else if (Math.random() < (1.0 / featureResultSet.getCount())) {
                    testFeatureRow = featureRow;
                }
            } else if (includeEmpty) {
                expectedCount++;
            }
        }
        featureResultSet.close();
        TestCase.assertFalse(featureIndexManager.isIndexed());
        TestCase.assertNull(featureIndexManager.getLastIndexed());
        Date currentDate = new Date();
        // Test indexing
        TestGeoPackageProgress progress = new TestGeoPackageProgress();
        featureIndexManager.setProgress(progress);
        int indexCount = featureIndexManager.index();
        TestCase.assertEquals(expectedCount, indexCount);
        TestCase.assertEquals(featureDao.count(), progress.getProgress());
        TestCase.assertNotNull(featureIndexManager.getLastIndexed());
        Date lastIndexed = featureIndexManager.getLastIndexed();
        TestCase.assertTrue(lastIndexed.getTime() > currentDate.getTime());
        TestCase.assertTrue(featureIndexManager.isIndexed());
        TestCase.assertEquals(expectedCount, featureIndexManager.count());
        // Test re-indexing, both ignored and forced
        TestCase.assertEquals(0, featureIndexManager.index());
        TestCase.assertEquals(expectedCount, featureIndexManager.index(true));
        TestCase.assertTrue(featureIndexManager.getLastIndexed().getTime() > lastIndexed.getTime());
        // Query for all indexed geometries
        int resultCount = 0;
        FeatureIndexResults featureIndexResults = featureIndexManager.query();
        for (FeatureRow featureRow : featureIndexResults) {
            validateFeatureRow(featureIndexManager, featureRow, null, includeEmpty);
            resultCount++;
        }
        featureIndexResults.close();
        TestCase.assertEquals(expectedCount, resultCount);
        // Query for all indexed geometries with columns
        resultCount = 0;
        featureIndexResults = featureIndexManager.query(featureDao.getIdAndGeometryColumnNames());
        for (FeatureRow featureRow : featureIndexResults) {
            validateFeatureRow(featureIndexManager, featureRow, null, includeEmpty);
            resultCount++;
        }
        featureIndexResults.close();
        TestCase.assertEquals(expectedCount, resultCount);
        // Test the query by envelope
        GeometryEnvelope envelope = testFeatureRow.getGeometryEnvelope();
        final double difference = .000001;
        envelope.setMinX(envelope.getMinX() - difference);
        envelope.setMaxX(envelope.getMaxX() + difference);
        envelope.setMinY(envelope.getMinY() - difference);
        envelope.setMaxY(envelope.getMaxY() + difference);
        if (envelope.hasZ()) {
            envelope.setMinZ(envelope.getMinZ() - difference);
            envelope.setMaxZ(envelope.getMaxZ() + difference);
        }
        if (envelope.hasM()) {
            envelope.setMinM(envelope.getMinM() - difference);
            envelope.setMaxM(envelope.getMaxM() + difference);
        }
        resultCount = 0;
        boolean featureFound = false;
        TestCase.assertTrue(featureIndexManager.count(envelope) >= 1);
        featureIndexResults = featureIndexManager.query(envelope);
        for (FeatureRow featureRow : featureIndexResults) {
            validateFeatureRow(featureIndexManager, featureRow, envelope, includeEmpty);
            if (featureRow.getId() == testFeatureRow.getId()) {
                featureFound = true;
            }
            resultCount++;
        }
        featureIndexResults.close();
        TestCase.assertTrue(featureFound);
        TestCase.assertTrue(resultCount >= 1);
        resultCount = 0;
        featureFound = false;
        featureIndexResults = featureIndexManager.query(featureDao.getIdAndGeometryColumnNames(), envelope);
        for (FeatureRow featureRow : featureIndexResults) {
            validateFeatureRow(featureIndexManager, featureRow, envelope, includeEmpty);
            if (featureRow.getId() == testFeatureRow.getId()) {
                featureFound = true;
            }
            resultCount++;
        }
        featureIndexResults.close();
        TestCase.assertTrue(featureFound);
        TestCase.assertTrue(resultCount >= 1);
        // Test the query by envelope with id iteration
        resultCount = 0;
        featureFound = false;
        TestCase.assertTrue(featureIndexManager.count(envelope) >= 1);
        featureIndexResults = featureIndexManager.query(envelope);
        for (long featureRowId : featureIndexResults.ids()) {
            FeatureRow featureRow = featureDao.queryForIdRow(featureRowId);
            validateFeatureRow(featureIndexManager, featureRow, envelope, includeEmpty);
            if (featureRowId == testFeatureRow.getId()) {
                featureFound = true;
            }
            resultCount++;
        }
        featureIndexResults.close();
        TestCase.assertTrue(featureFound);
        TestCase.assertTrue(resultCount >= 1);
        // Pick a projection different from the feature dao and project the
        // bounding box
        BoundingBox boundingBox = new BoundingBox(envelope.getMinX() - 1, envelope.getMinY() - 1, envelope.getMaxX() + 1, envelope.getMaxY() + 1);
        Projection projection = null;
        if (!featureDao.getProjection().equals(ProjectionConstants.AUTHORITY_EPSG, ProjectionConstants.EPSG_WORLD_GEODETIC_SYSTEM)) {
            projection = ProjectionFactory.getProjection(ProjectionConstants.EPSG_WORLD_GEODETIC_SYSTEM);
        } else {
            projection = ProjectionFactory.getProjection(ProjectionConstants.EPSG_WEB_MERCATOR);
        }
        GeometryTransform transform = GeometryTransform.create(featureDao.getProjection(), projection);
        BoundingBox transformedBoundingBox = boundingBox.transform(transform);
        // Test the query by projected bounding box
        resultCount = 0;
        featureFound = false;
        TestCase.assertTrue(featureIndexManager.count(transformedBoundingBox, projection) >= 1);
        featureIndexResults = featureIndexManager.query(transformedBoundingBox, projection);
        for (FeatureRow featureRow : featureIndexResults) {
            validateFeatureRow(featureIndexManager, featureRow, boundingBox.buildEnvelope(), includeEmpty);
            if (featureRow.getId() == testFeatureRow.getId()) {
                featureFound = true;
            }
            resultCount++;
        }
        featureIndexResults.close();
        TestCase.assertTrue(featureFound);
        TestCase.assertTrue(resultCount >= 1);
        resultCount = 0;
        featureFound = false;
        featureIndexResults = featureIndexManager.query(featureDao.getIdAndGeometryColumnNames(), transformedBoundingBox, projection);
        for (FeatureRow featureRow : featureIndexResults) {
            validateFeatureRow(featureIndexManager, featureRow, boundingBox.buildEnvelope(), includeEmpty);
            if (featureRow.getId() == testFeatureRow.getId()) {
                featureFound = true;
            }
            resultCount++;
        }
        featureIndexResults.close();
        TestCase.assertTrue(featureFound);
        TestCase.assertTrue(resultCount >= 1);
        // Test query by criteria
        FeatureTable table = featureDao.getTable();
        List<FeatureColumn> columns = table.getColumns();
        Map<String, Number> numbers = new HashMap<>();
        Map<String, String> strings = new HashMap<>();
        for (FeatureColumn column : columns) {
            if (column.isPrimaryKey() || column.isGeometry()) {
                continue;
            }
            GeoPackageDataType dataType = column.getDataType();
            switch(dataType) {
                case DOUBLE:
                case FLOAT:
                case INT:
                case INTEGER:
                case TINYINT:
                case SMALLINT:
                case MEDIUMINT:
                case REAL:
                    numbers.put(column.getName(), null);
                    break;
                case TEXT:
                    strings.put(column.getName(), null);
                    break;
                default:
            }
        }
        for (String number : numbers.keySet()) {
            Object value = testFeatureRow.getValue(number);
            numbers.put(number, (Number) value);
        }
        for (String string : strings.keySet()) {
            String value = testFeatureRow.getValueString(string);
            strings.put(string, value);
        }
        for (Entry<String, Number> number : numbers.entrySet()) {
            String column = number.getKey();
            double value = number.getValue().doubleValue();
            String where = column + " >= ? AND " + column + " <= ?";
            String[] whereArgs = new String[] { String.valueOf(value - 0.001), String.valueOf(value + 0.001) };
            long count = featureIndexManager.count(where, whereArgs);
            TestCase.assertTrue(count >= 1);
            featureIndexResults = featureIndexManager.query(where, whereArgs);
            TestCase.assertEquals(count, featureIndexResults.count());
            for (FeatureRow featureRow : featureIndexResults) {
                TestCase.assertEquals(value, ((Number) featureRow.getValue(column)).doubleValue());
            }
            featureIndexResults.close();
            featureIndexResults = featureIndexManager.query(new String[] { column }, where, whereArgs);
            TestCase.assertEquals(count, featureIndexResults.count());
            for (FeatureRow featureRow : featureIndexResults) {
                TestCase.assertEquals(value, ((Number) featureRow.getValue(column)).doubleValue());
            }
            featureIndexResults.close();
            resultCount = 0;
            featureFound = false;
            count = featureIndexManager.count(transformedBoundingBox, projection, where, whereArgs);
            TestCase.assertTrue(count >= 1);
            featureIndexResults = featureIndexManager.query(transformedBoundingBox, projection, where, whereArgs);
            TestCase.assertEquals(count, featureIndexResults.count());
            for (FeatureRow featureRow : featureIndexResults) {
                TestCase.assertEquals(value, ((Number) featureRow.getValue(column)).doubleValue());
                validateFeatureRow(featureIndexManager, featureRow, boundingBox.buildEnvelope(), includeEmpty);
                if (featureRow.getId() == testFeatureRow.getId()) {
                    featureFound = true;
                }
                resultCount++;
            }
            featureIndexResults.close();
            TestCase.assertTrue(featureFound);
            TestCase.assertTrue(resultCount >= 1);
            resultCount = 0;
            featureFound = false;
            featureIndexResults = featureIndexManager.query(new String[] { featureDao.getGeometryColumnName(), column, featureDao.getIdColumnName() }, transformedBoundingBox, projection, where, whereArgs);
            TestCase.assertEquals(count, featureIndexResults.count());
            for (FeatureRow featureRow : featureIndexResults) {
                TestCase.assertEquals(value, ((Number) featureRow.getValue(column)).doubleValue());
                validateFeatureRow(featureIndexManager, featureRow, boundingBox.buildEnvelope(), includeEmpty);
                if (featureRow.getId() == testFeatureRow.getId()) {
                    featureFound = true;
                }
                resultCount++;
            }
            featureIndexResults.close();
            TestCase.assertTrue(featureFound);
            TestCase.assertTrue(resultCount >= 1);
        }
        for (Entry<String, String> string : strings.entrySet()) {
            String column = string.getKey();
            String value = string.getValue();
            Map<String, Object> fieldValues = new HashMap<>();
            fieldValues.put(column, value);
            long count = featureIndexManager.count(fieldValues);
            TestCase.assertTrue(count >= 1);
            featureIndexResults = featureIndexManager.query(fieldValues);
            TestCase.assertEquals(count, featureIndexResults.count());
            for (FeatureRow featureRow : featureIndexResults) {
                TestCase.assertEquals(value, featureRow.getValueString(column));
            }
            featureIndexResults.close();
            featureIndexResults = featureIndexManager.query(new String[] { column }, fieldValues);
            TestCase.assertEquals(count, featureIndexResults.count());
            for (FeatureRow featureRow : featureIndexResults) {
                TestCase.assertEquals(value, featureRow.getValueString(column));
            }
            featureIndexResults.close();
            resultCount = 0;
            featureFound = false;
            count = featureIndexManager.count(transformedBoundingBox, projection, fieldValues);
            TestCase.assertTrue(count >= 1);
            featureIndexResults = featureIndexManager.query(transformedBoundingBox, projection, fieldValues);
            TestCase.assertEquals(count, featureIndexResults.count());
            for (FeatureRow featureRow : featureIndexResults) {
                TestCase.assertEquals(value, featureRow.getValueString(column));
                validateFeatureRow(featureIndexManager, featureRow, boundingBox.buildEnvelope(), includeEmpty);
                if (featureRow.getId() == testFeatureRow.getId()) {
                    featureFound = true;
                }
                resultCount++;
            }
            featureIndexResults.close();
            TestCase.assertTrue(featureFound);
            TestCase.assertTrue(resultCount >= 1);
            resultCount = 0;
            featureFound = false;
            featureIndexResults = featureIndexManager.query(new String[] { column, featureDao.getIdColumnName(), featureDao.getGeometryColumnName() }, transformedBoundingBox, projection, fieldValues);
            TestCase.assertEquals(count, featureIndexResults.count());
            for (FeatureRow featureRow : featureIndexResults) {
                TestCase.assertEquals(value, featureRow.getValueString(column));
                validateFeatureRow(featureIndexManager, featureRow, boundingBox.buildEnvelope(), includeEmpty);
                if (featureRow.getId() == testFeatureRow.getId()) {
                    featureFound = true;
                }
                resultCount++;
            }
            featureIndexResults.close();
            TestCase.assertTrue(featureFound);
            TestCase.assertTrue(resultCount >= 1);
        }
        // Update a Geometry and update the index of a single feature row
        Point point = new Point(5, 5);
        GeoPackageGeometryData geometryData = GeoPackageGeometryData.create(featureDao.getSrsId(), point);
        testFeatureRow.setGeometry(geometryData);
        TestCase.assertEquals(1, featureDao.update(testFeatureRow));
        Date lastIndexedBefore = featureIndexManager.getLastIndexed();
        try {
            Thread.sleep(1);
        } catch (InterruptedException e) {
        }
        TestCase.assertTrue(featureIndexManager.index(testFeatureRow));
        Date lastIndexedAfter = featureIndexManager.getLastIndexed();
        TestCase.assertTrue(lastIndexedAfter.after(lastIndexedBefore));
        // Verify the index was updated for the feature row
        envelope = point.getEnvelope();
        resultCount = 0;
        featureFound = false;
        TestCase.assertTrue(featureIndexManager.count(envelope) >= 1);
        featureIndexResults = featureIndexManager.query(envelope);
        for (FeatureRow featureRow : featureIndexResults) {
            validateFeatureRow(featureIndexManager, featureRow, envelope, includeEmpty);
            if (featureRow.getId() == testFeatureRow.getId()) {
                featureFound = true;
            }
            resultCount++;
        }
        featureIndexResults.close();
        TestCase.assertTrue(featureFound);
        TestCase.assertTrue(resultCount >= 1);
        featureIndexManager.close();
    }
    // Delete the extensions
    boolean everyOther = false;
    for (String featureTable : featureTables) {
        FeatureDao featureDao = geoPackage.getFeatureDao(featureTable);
        FeatureIndexManager featureIndexManager = new FeatureIndexManager(geoPackage, featureDao);
        featureIndexManager.setIndexLocation(type);
        TestCase.assertTrue(featureIndexManager.isIndexed());
        // Test deleting a single geometry index
        if (everyOther) {
            FeatureResultSet featureResultSet = featureDao.query();
            while (featureResultSet.moveToNext()) {
                FeatureRow featureRow = featureResultSet.getRow();
                if (featureRow.getGeometryEnvelope() != null) {
                    featureResultSet.close();
                    TestCase.assertTrue(featureIndexManager.deleteIndex(featureRow));
                    break;
                }
            }
            featureResultSet.close();
        }
        featureIndexManager.deleteIndex();
        TestCase.assertFalse(featureIndexManager.isIndexed());
        everyOther = !everyOther;
        featureIndexManager.close();
    }
}
Also used : HashMap(java.util.HashMap) GeoPackageDataType(mil.nga.geopackage.db.GeoPackageDataType) Projection(mil.nga.proj.Projection) FeatureDao(mil.nga.geopackage.features.user.FeatureDao) TestGeoPackageProgress(mil.nga.geopackage.io.TestGeoPackageProgress) GeometryTransform(mil.nga.sf.proj.GeometryTransform) FeatureColumn(mil.nga.geopackage.features.user.FeatureColumn) FeatureResultSet(mil.nga.geopackage.features.user.FeatureResultSet) BoundingBox(mil.nga.geopackage.BoundingBox) GeoPackageGeometryData(mil.nga.geopackage.geom.GeoPackageGeometryData) FeatureTable(mil.nga.geopackage.features.user.FeatureTable) Point(mil.nga.sf.Point) Point(mil.nga.sf.Point) Date(java.util.Date) FeatureRow(mil.nga.geopackage.features.user.FeatureRow) GeometryEnvelope(mil.nga.sf.GeometryEnvelope)

Example 2 with TestGeoPackageProgress

use of mil.nga.geopackage.io.TestGeoPackageProgress in project geopackage-java by ngageoint.

the class UrlTileGeneratorUtils method testGenerateTiles.

/**
 * Test generating tiles
 *
 * @param tileGenerator
 * @throws SQLException
 * @throws IOException
 */
private static void testGenerateTiles(TileGenerator tileGenerator) throws SQLException, IOException {
    GeoPackage geoPackage = tileGenerator.getGeoPackage();
    String tableName = tileGenerator.getTableName();
    int minZoom = tileGenerator.getMinZoom();
    int maxZoom = tileGenerator.getMaxZoom();
    BoundingBox webMercatorBoundingBox = tileGenerator.getBoundingBox();
    TestGeoPackageProgress progress = new TestGeoPackageProgress();
    tileGenerator.setProgress(progress);
    int count = tileGenerator.generateTiles();
    long expected = expectedTiles(webMercatorBoundingBox, minZoom, maxZoom);
    TestCase.assertEquals(expected, count);
    TestCase.assertEquals(expected, progress.getProgress());
    TileDao tileDao = geoPackage.getTileDao(tableName);
    TestCase.assertEquals(expected, tileDao.count());
    TestCase.assertEquals(minZoom, tileDao.getMinZoom());
    TestCase.assertEquals(maxZoom, tileDao.getMaxZoom());
    BoundingBox tileMatrixSetBoundingBox = tileDao.getBoundingBox();
    for (int zoom = minZoom; zoom <= maxZoom; zoom++) {
        TileGrid expectedTileGrid = TileBoundingBoxUtils.getTileGrid(webMercatorBoundingBox, zoom);
        BoundingBox expectedBoundingBox = TileBoundingBoxUtils.getWebMercatorBoundingBox(expectedTileGrid, zoom);
        BoundingBox zoomBoundingBox = tileDao.getBoundingBox(zoom);
        TestCase.assertEquals(expectedBoundingBox.getMinLongitude(), zoomBoundingBox.getMinLongitude(), .000001);
        TestCase.assertEquals(expectedBoundingBox.getMaxLongitude(), zoomBoundingBox.getMaxLongitude(), .000001);
        TestCase.assertEquals(expectedBoundingBox.getMinLatitude(), zoomBoundingBox.getMinLatitude(), .000001);
        TestCase.assertEquals(expectedBoundingBox.getMaxLatitude(), zoomBoundingBox.getMaxLatitude(), .000001);
        long expectedZoomTiles = expectedTiles(webMercatorBoundingBox, zoom);
        TestCase.assertEquals(expectedZoomTiles, tileDao.count(zoom));
        TileMatrix tileMatrix = tileDao.getTileMatrix(zoom);
        TileGrid tileGrid = TileBoundingBoxUtils.getTileGrid(tileMatrixSetBoundingBox, tileMatrix.getMatrixWidth(), tileMatrix.getMatrixHeight(), zoomBoundingBox);
        TestCase.assertTrue(tileGrid.getMinX() >= 0);
        TestCase.assertTrue(tileGrid.getMaxX() < tileMatrix.getMatrixWidth());
        TestCase.assertTrue(tileGrid.getMinY() >= 0);
        TestCase.assertTrue(tileGrid.getMaxY() < tileMatrix.getMatrixHeight());
        TileResultSet resultSet = tileDao.queryForTile(zoom);
        TestCase.assertEquals(expectedZoomTiles, resultSet.getCount());
        int resultCount = 0;
        while (resultSet.moveToNext()) {
            TileRow tileRow = resultSet.getRow();
            resultCount++;
            byte[] tileData = tileRow.getTileData();
            TestCase.assertNotNull(tileData);
            BufferedImage image = tileRow.getTileDataImage();
            TestCase.assertNotNull(image);
            TestCase.assertEquals(tileMatrix.getTileWidth(), image.getWidth());
            TestCase.assertEquals(tileMatrix.getTileHeight(), image.getHeight());
        }
        TestCase.assertEquals(expectedZoomTiles, resultCount);
    }
}
Also used : GeoPackage(mil.nga.geopackage.GeoPackage) Point(mil.nga.sf.Point) BufferedImage(java.awt.image.BufferedImage) TestGeoPackageProgress(mil.nga.geopackage.io.TestGeoPackageProgress) TileRow(mil.nga.geopackage.tiles.user.TileRow) BoundingBox(mil.nga.geopackage.BoundingBox) TileResultSet(mil.nga.geopackage.tiles.user.TileResultSet) TileDao(mil.nga.geopackage.tiles.user.TileDao) TileMatrix(mil.nga.geopackage.tiles.matrix.TileMatrix)

Example 3 with TestGeoPackageProgress

use of mil.nga.geopackage.io.TestGeoPackageProgress in project geopackage-java by ngageoint.

the class FeatureTableIndexUtils method testIndex.

/**
 * Test index
 *
 * @param geoPackage
 * @throws Exception
 */
public static void testIndex(GeoPackage geoPackage) throws Exception {
    // Test indexing each feature table
    List<String> featureTables = geoPackage.getFeatureTables();
    for (String featureTable : featureTables) {
        FeatureDao featureDao = geoPackage.getFeatureDao(featureTable);
        FeatureTableIndex featureTableIndex = new FeatureTableIndex(geoPackage, featureDao);
        // Determine how many features have geometry envelopes or geometries
        int expectedCount = 0;
        FeatureRow testFeatureRow = null;
        FeatureResultSet featureResultSet = featureDao.queryForAll();
        while (featureResultSet.moveToNext()) {
            FeatureRow featureRow = featureResultSet.getRow();
            if (featureRow.getGeometryEnvelope() != null) {
                expectedCount++;
                // queries later
                if (testFeatureRow == null) {
                    testFeatureRow = featureRow;
                } else if (Math.random() < (1.0 / featureResultSet.getCount())) {
                    testFeatureRow = featureRow;
                }
            }
        }
        featureResultSet.close();
        if (featureTableIndex.isIndexed()) {
            featureTableIndex.deleteIndex();
        }
        TestCase.assertFalse(featureTableIndex.isIndexed());
        TestCase.assertNull(featureTableIndex.getLastIndexed());
        Date currentDate = new Date();
        TestUtils.validateGeoPackage(geoPackage);
        // Test indexing
        TestGeoPackageProgress progress = new TestGeoPackageProgress();
        featureTableIndex.setProgress(progress);
        int indexCount = featureTableIndex.index();
        TestUtils.validateGeoPackage(geoPackage);
        TestCase.assertEquals(expectedCount, indexCount);
        TestCase.assertEquals(featureDao.count(), progress.getProgress());
        TestCase.assertNotNull(featureTableIndex.getLastIndexed());
        Date lastIndexed = featureTableIndex.getLastIndexed();
        TestCase.assertTrue(lastIndexed.getTime() > currentDate.getTime());
        TestCase.assertTrue(featureTableIndex.isIndexed());
        TestCase.assertEquals(expectedCount, featureTableIndex.count());
        // Test re-indexing, both ignored and forced
        TestCase.assertEquals(0, featureTableIndex.index());
        TestCase.assertEquals(expectedCount, featureTableIndex.index(true));
        TestCase.assertTrue(featureTableIndex.getLastIndexed().getTime() > lastIndexed.getTime());
        // Query for all indexed geometries
        int resultCount = 0;
        CloseableIterator<GeometryIndex> featureTableResults = featureTableIndex.query();
        while (featureTableResults.hasNext()) {
            GeometryIndex geometryIndex = featureTableResults.next();
            validateGeometryIndex(featureTableIndex, geometryIndex);
            resultCount++;
        }
        featureTableResults.close();
        TestCase.assertEquals(expectedCount, resultCount);
        // Test the query by envelope
        GeometryEnvelope envelope = testFeatureRow.getGeometryEnvelope();
        envelope.setMinX(envelope.getMinX() - .000001);
        envelope.setMaxX(envelope.getMaxX() + .000001);
        envelope.setMinY(envelope.getMinY() - .000001);
        envelope.setMaxY(envelope.getMaxY() + .000001);
        if (envelope.hasZ()) {
            envelope.setMinZ(envelope.getMinZ() - .000001);
            envelope.setMaxZ(envelope.getMaxZ() + .000001);
        }
        if (envelope.hasM()) {
            envelope.setMinM(envelope.getMinM() - .000001);
            envelope.setMaxM(envelope.getMaxM() + .000001);
        }
        resultCount = 0;
        boolean featureFound = false;
        TestCase.assertTrue(featureTableIndex.count(envelope) >= 1);
        featureTableResults = featureTableIndex.query(envelope);
        while (featureTableResults.hasNext()) {
            GeometryIndex geometryIndex = featureTableResults.next();
            validateGeometryIndex(featureTableIndex, geometryIndex);
            if (geometryIndex.getGeomId() == testFeatureRow.getId()) {
                featureFound = true;
            }
            resultCount++;
        }
        featureTableResults.close();
        TestCase.assertTrue(featureFound);
        TestCase.assertTrue(resultCount >= 1);
        // Pick a projection different from the feature dao and project the
        // bounding box
        BoundingBox boundingBox = new BoundingBox(envelope.getMinX() - 1, envelope.getMinY() - 1, envelope.getMaxX() + 1, envelope.getMaxY() + 1);
        Projection projection = null;
        if (!featureDao.getProjection().equals(ProjectionConstants.AUTHORITY_EPSG, ProjectionConstants.EPSG_WORLD_GEODETIC_SYSTEM)) {
            projection = ProjectionFactory.getProjection(ProjectionConstants.EPSG_WORLD_GEODETIC_SYSTEM);
        } else {
            projection = ProjectionFactory.getProjection(ProjectionConstants.EPSG_WEB_MERCATOR);
        }
        GeometryTransform transform = GeometryTransform.create(featureDao.getProjection(), projection);
        BoundingBox transformedBoundingBox = boundingBox.transform(transform);
        // Test the query by projected bounding box
        resultCount = 0;
        featureFound = false;
        TestCase.assertTrue(featureTableIndex.count(transformedBoundingBox, projection) >= 1);
        featureTableResults = featureTableIndex.query(transformedBoundingBox, projection);
        while (featureTableResults.hasNext()) {
            GeometryIndex geometryIndex = featureTableResults.next();
            validateGeometryIndex(featureTableIndex, geometryIndex);
            if (geometryIndex.getGeomId() == testFeatureRow.getId()) {
                featureFound = true;
            }
            resultCount++;
        }
        featureTableResults.close();
        TestCase.assertTrue(featureFound);
        TestCase.assertTrue(resultCount >= 1);
        // Update a Geometry and update the index of a single feature row
        Point point = new Point(5, 5);
        GeoPackageGeometryData geometryData = GeoPackageGeometryData.create(featureDao.getSrsId(), point);
        testFeatureRow.setGeometry(geometryData);
        TestCase.assertEquals(1, featureDao.update(testFeatureRow));
        Date lastIndexedBefore = featureTableIndex.getLastIndexed();
        TestCase.assertTrue(featureTableIndex.index(testFeatureRow));
        Date lastIndexedAfter = featureTableIndex.getLastIndexed();
        TestCase.assertTrue(lastIndexedAfter.after(lastIndexedBefore));
        // Verify the index was updated for the feature row
        envelope = point.getEnvelope();
        resultCount = 0;
        featureFound = false;
        TestCase.assertTrue(featureTableIndex.count(envelope) >= 1);
        featureTableResults = featureTableIndex.query(envelope);
        while (featureTableResults.hasNext()) {
            GeometryIndex geometryIndex = featureTableResults.next();
            validateGeometryIndex(featureTableIndex, geometryIndex);
            if (geometryIndex.getGeomId() == testFeatureRow.getId()) {
                featureFound = true;
            }
            resultCount++;
        }
        featureTableResults.close();
        TestCase.assertTrue(featureFound);
        TestCase.assertTrue(resultCount >= 1);
    }
    ExtensionsDao extensionsDao = geoPackage.getExtensionsDao();
    GeometryIndexDao geometryIndexDao = FeatureTableIndex.getGeometryIndexDao(geoPackage);
    TableIndexDao tableIndexDao = FeatureTableIndex.getTableIndexDao(geoPackage);
    // Delete the extensions for the first half of the feature tables
    boolean everyOther = false;
    for (String featureTable : featureTables.subList(0, (int) Math.ceil(featureTables.size() * .5))) {
        FeatureDao featureDao = geoPackage.getFeatureDao(featureTable);
        int geometryCount = geometryIndexDao.queryForTableName(featureTable).size();
        TestCase.assertTrue(geometryCount > 0);
        TestCase.assertNotNull(tableIndexDao.queryForId(featureTable));
        Extensions extensions = extensionsDao.queryByExtension(FeatureTableIndex.EXTENSION_NAME, featureTable, featureDao.getGeometryColumnName());
        TestCase.assertNotNull(extensions);
        TestCase.assertEquals(extensions.getTableName(), featureTable);
        TestCase.assertEquals(extensions.getColumnName(), featureDao.getGeometryColumnName());
        TestCase.assertEquals(extensions.getExtensionName(), FeatureTableIndex.EXTENSION_NAME);
        TestCase.assertEquals(extensions.getAuthor(), FeatureTableIndex.EXTENSION_AUTHOR);
        TestCase.assertEquals(extensions.getExtensionNameNoAuthor(), FeatureTableIndex.EXTENSION_NAME_NO_AUTHOR);
        TestCase.assertEquals(extensions.getDefinition(), FeatureTableIndex.EXTENSION_DEFINITION);
        TestCase.assertEquals(extensions.getScope(), ExtensionScopeType.READ_WRITE);
        FeatureTableIndex featureTableIndex = new FeatureTableIndex(geoPackage, featureDao);
        TestCase.assertTrue(featureTableIndex.isIndexed());
        TestCase.assertEquals(geometryCount, featureTableIndex.count());
        // Test deleting a single geometry index
        if (everyOther) {
            FeatureResultSet featureResultSet = featureDao.queryForAll();
            while (featureResultSet.moveToNext()) {
                FeatureRow featureRow = featureResultSet.getRow();
                GeoPackageGeometryData geometryData = featureRow.getGeometry();
                if (geometryData != null && (geometryData.getEnvelope() != null || geometryData.getGeometry() != null)) {
                    featureResultSet.close();
                    TestCase.assertEquals(1, featureTableIndex.deleteIndex(featureRow));
                    TestCase.assertEquals(geometryCount - 1, featureTableIndex.count());
                    break;
                }
            }
            featureResultSet.close();
        }
        geoPackage.getExtensionManager().deleteTableExtensions(featureTable);
        TestCase.assertFalse(featureTableIndex.isIndexed());
        TestCase.assertEquals(0, geometryIndexDao.queryForTableName(featureTable).size());
        TestCase.assertNull(tableIndexDao.queryForId(featureTable));
        extensions = extensionsDao.queryByExtension(FeatureTableIndex.EXTENSION_NAME, featureTable, featureDao.getGeometryColumnName());
        TestCase.assertNull(extensions);
        everyOther = !everyOther;
    }
    TestCase.assertTrue(geometryIndexDao.isTableExists());
    TestCase.assertTrue(tableIndexDao.isTableExists());
    TestCase.assertTrue(extensionsDao.queryByExtension(FeatureTableIndex.EXTENSION_NAME).size() > 0);
    // Test deleting all NGA extensions
    geoPackage.getExtensionManager().deleteExtensions();
    TestCase.assertFalse(geometryIndexDao.isTableExists());
    TestCase.assertFalse(tableIndexDao.isTableExists());
    TestCase.assertFalse(extensionsDao.isTableExists());
}
Also used : GeoPackageGeometryData(mil.nga.geopackage.geom.GeoPackageGeometryData) ExtensionsDao(mil.nga.geopackage.extension.ExtensionsDao) Projection(mil.nga.proj.Projection) FeatureDao(mil.nga.geopackage.features.user.FeatureDao) Point(mil.nga.sf.Point) Extensions(mil.nga.geopackage.extension.Extensions) Point(mil.nga.sf.Point) Date(java.util.Date) TestGeoPackageProgress(mil.nga.geopackage.io.TestGeoPackageProgress) GeometryTransform(mil.nga.sf.proj.GeometryTransform) FeatureRow(mil.nga.geopackage.features.user.FeatureRow) FeatureResultSet(mil.nga.geopackage.features.user.FeatureResultSet) BoundingBox(mil.nga.geopackage.BoundingBox) GeometryEnvelope(mil.nga.sf.GeometryEnvelope)

Example 4 with TestGeoPackageProgress

use of mil.nga.geopackage.io.TestGeoPackageProgress in project geopackage-java by ngageoint.

the class TileReprojectionUtils method testReprojectCancel.

/**
 * Test reproject cancel
 *
 * @param geoPackage
 *            GeoPackage
 */
public static void testReprojectCancel(GeoPackage geoPackage) {
    String table = randomTileTables(geoPackage).get(0);
    String reprojectTable = table + "_reproject";
    Projection projection = geoPackage.getProjection(table);
    Projection reprojectProjection = alternateProjection(projection);
    TileReprojection tileReprojection = TileReprojection.create(geoPackage, table, reprojectTable, reprojectProjection);
    TestGeoPackageProgress progress = new TestGeoPackageProgress();
    tileReprojection.setProgress(progress);
    progress.cancel();
    int tiles = tileReprojection.reproject();
    assertEquals(0, tiles);
    assertEquals(0, progress.getProgress());
}
Also used : TestGeoPackageProgress(mil.nga.geopackage.io.TestGeoPackageProgress) Projection(mil.nga.proj.Projection)

Example 5 with TestGeoPackageProgress

use of mil.nga.geopackage.io.TestGeoPackageProgress in project geopackage-java by ngageoint.

the class TileReprojectionUtils method testReprojectOverwrite.

/**
 * Test reproject of overwriting a table
 *
 * @param geoPackage
 *            GeoPackage
 * @throws SQLException
 *             upon error
 */
public static void testReprojectOverwrite(GeoPackage geoPackage) throws SQLException {
    for (String table : randomTileTables(geoPackage)) {
        String reprojectTable = table + "_reproject";
        Projection projection = geoPackage.getProjection(table);
        Projection reprojectProjection = alternateProjection(projection);
        TileDao tileDao = geoPackage.getTileDao(table);
        int count = tileDao.count();
        Map<Long, Integer> counts = zoomCounts(tileDao);
        int tiles = TileReprojection.reproject(geoPackage, table, reprojectTable, reprojectProjection);
        assertEquals(count > 0, tiles > 0);
        assertTrue(projection.equals(geoPackage.getProjection(table)));
        assertTrue(reprojectProjection.equals(geoPackage.getProjection(reprojectTable)));
        tileDao = geoPackage.getTileDao(table);
        compareZoomCounts(count, counts, tileDao);
        TileDao reprojectTileDao = geoPackage.getTileDao(reprojectTable);
        checkZoomCounts(count, counts, reprojectTileDao, tiles);
        int tiles2 = TileReprojection.reproject(geoPackage, table, reprojectTable, reprojectProjection);
        assertEquals(tiles, tiles2);
        TileMatrixSet tileMatrixSet = reprojectTileDao.getTileMatrixSet();
        double multiplier = 0.5;
        tileMatrixSet.setMinX(tileMatrixSet.getMinX() * multiplier);
        tileMatrixSet.setMinY(tileMatrixSet.getMinY() * multiplier);
        tileMatrixSet.setMaxX(tileMatrixSet.getMaxX() * multiplier);
        tileMatrixSet.setMaxY(tileMatrixSet.getMaxY() * multiplier);
        reprojectTileDao.getTileMatrixSetDao().update(tileMatrixSet);
        TileReprojection tileReprojection = TileReprojection.create(geoPackage, table, reprojectTable, reprojectProjection);
        try {
            tileReprojection.reproject();
            fail("Reprojection of existing table with new geographic properties did not fail");
        } catch (Exception e) {
        // expected
        }
        tileReprojection.setOverwrite(true);
        tileReprojection.reproject();
        tileMatrixSet.setMinX(tileMatrixSet.getMinX() / multiplier);
        tileMatrixSet.setMinY(tileMatrixSet.getMinY() / multiplier);
        tileMatrixSet.setMaxX(tileMatrixSet.getMaxX() / multiplier);
        tileMatrixSet.setMaxY(tileMatrixSet.getMaxY() / multiplier);
        reprojectTileDao.getTileMatrixSetDao().update(tileMatrixSet);
        tileReprojection = TileReprojection.create(geoPackage, table, reprojectTable, reprojectProjection);
        try {
            tileReprojection.reproject();
            fail("Reprojection of existing table with new geographic properties did not fail");
        } catch (Exception e) {
        // expected
        }
        tileReprojection.setOverwrite(true);
        TestGeoPackageProgress progress = new TestGeoPackageProgress();
        tileReprojection.setProgress(progress);
        int tiles3 = tileReprojection.reproject();
        assertEquals(tiles3, progress.getProgress());
        assertEquals(tiles, tiles3);
        Set<Long> zoomLevels = new HashSet<>(tileDao.getZoomLevels());
        Set<Long> reprojectZoomLevels = reprojectTileDao.getZoomLevels();
        zoomLevels.removeAll(reprojectZoomLevels);
        assertEquals(0, zoomLevels.size());
        compareBoundingBox(geoPackage.getBoundingBox(reprojectProjection, table), geoPackage.getContentsBoundingBox(reprojectTable), .0000001);
    }
}
Also used : TileMatrixSet(mil.nga.geopackage.tiles.matrixset.TileMatrixSet) Projection(mil.nga.proj.Projection) SQLException(java.sql.SQLException) IOException(java.io.IOException) TestGeoPackageProgress(mil.nga.geopackage.io.TestGeoPackageProgress) TileDao(mil.nga.geopackage.tiles.user.TileDao) HashSet(java.util.HashSet)

Aggregations

TestGeoPackageProgress (mil.nga.geopackage.io.TestGeoPackageProgress)5 Projection (mil.nga.proj.Projection)4 BoundingBox (mil.nga.geopackage.BoundingBox)3 Point (mil.nga.sf.Point)3 Date (java.util.Date)2 FeatureDao (mil.nga.geopackage.features.user.FeatureDao)2 FeatureResultSet (mil.nga.geopackage.features.user.FeatureResultSet)2 FeatureRow (mil.nga.geopackage.features.user.FeatureRow)2 GeoPackageGeometryData (mil.nga.geopackage.geom.GeoPackageGeometryData)2 TileDao (mil.nga.geopackage.tiles.user.TileDao)2 GeometryEnvelope (mil.nga.sf.GeometryEnvelope)2 GeometryTransform (mil.nga.sf.proj.GeometryTransform)2 BufferedImage (java.awt.image.BufferedImage)1 IOException (java.io.IOException)1 SQLException (java.sql.SQLException)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 GeoPackage (mil.nga.geopackage.GeoPackage)1 GeoPackageDataType (mil.nga.geopackage.db.GeoPackageDataType)1 Extensions (mil.nga.geopackage.extension.Extensions)1