Search in sources :

Example 1 with ColumnValue

use of mil.nga.geopackage.user.ColumnValue in project geopackage-java by ngageoint.

the class AttributesUtils method testRead.

/**
 * Test read
 *
 * @param geoPackage
 *            GeoPackage
 * @throws SQLException
 *             upon error
 */
public static void testRead(GeoPackage geoPackage) throws SQLException {
    List<String> tables = geoPackage.getAttributesTables();
    if (!tables.isEmpty()) {
        for (String tableName : tables) {
            // Test the get attributes DAO methods
            ContentsDao contentsDao = geoPackage.getContentsDao();
            Contents contents = contentsDao.queryForId(tableName);
            AttributesDao dao = geoPackage.getAttributesDao(contents);
            TestCase.assertNotNull(dao);
            dao = geoPackage.getAttributesDao(tableName);
            TestCase.assertNotNull(dao);
            TestCase.assertNotNull(dao.getDb());
            TestCase.assertEquals(tableName, dao.getTableName());
            AttributesTable attributesTable = dao.getTable();
            String[] columns = attributesTable.getColumnNames();
            // Query for all
            AttributesResultSet attributesResultSet = dao.queryForAll();
            int count = attributesResultSet.getCount();
            int manualCount = 0;
            while (attributesResultSet.moveToNext()) {
                AttributesRow attributesRow = attributesResultSet.getRow();
                validateAttributesRow(columns, attributesRow);
                manualCount++;
            }
            TestCase.assertEquals(count, manualCount);
            attributesResultSet.close();
            // Manually query for all and compare
            Connection connection = dao.getConnection();
            String sql = SQLiteQueryBuilder.buildQueryString(false, dao.getTableName(), null, null, null, null, null, null, null);
            ResultSet resultSet = SQLUtils.query(connection, sql, null);
            int resultSetCount = SQLUtils.count(connection, sql, null);
            attributesResultSet = new AttributesResultSet(attributesTable, resultSet, resultSetCount);
            count = attributesResultSet.getCount();
            manualCount = 0;
            while (attributesResultSet.moveToNext()) {
                manualCount++;
            }
            TestCase.assertEquals(count, manualCount);
            TestCase.assertTrue("No attributes to test", count > 0);
            attributesResultSet.close();
            resultSet = SQLUtils.query(connection, sql, null);
            resultSetCount = SQLUtils.count(connection, sql, null);
            attributesResultSet = new AttributesResultSet(attributesTable, resultSet, resultSetCount);
            // Choose random attribute
            int random = (int) (Math.random() * count);
            attributesResultSet.moveToPosition(random);
            AttributesRow attributesRow = attributesResultSet.getRow();
            attributesResultSet.close();
            // Query by id
            AttributesRow queryAttributesRow = dao.queryForIdRow(attributesRow.getId());
            TestCase.assertNotNull(queryAttributesRow);
            TestCase.assertEquals(attributesRow.getId(), queryAttributesRow.getId());
            // Find two non id columns
            AttributesColumn column1 = null;
            AttributesColumn column2 = null;
            for (AttributesColumn column : attributesRow.getTable().getColumns()) {
                if (!column.isPrimaryKey() && column.getDataType() != GeoPackageDataType.BLOB) {
                    if (column1 == null) {
                        column1 = column;
                    } else {
                        column2 = column;
                        break;
                    }
                }
            }
            // Query for equal
            if (column1 != null) {
                Object column1Value = attributesRow.getValue(column1.getName());
                Class<?> column1ClassType = column1.getDataType().getClassType();
                boolean column1Decimal = column1ClassType == Double.class || column1ClassType == Float.class;
                ColumnValue column1AttributesValue;
                if (column1Decimal) {
                    column1AttributesValue = new ColumnValue(column1Value, .000001);
                } else if (column1Value instanceof Date) {
                    column1AttributesValue = new ColumnValue(DateConverter.converter(column1.getDataType()).stringValue((Date) column1Value));
                } else {
                    column1AttributesValue = new ColumnValue(column1Value);
                }
                attributesResultSet = dao.queryForEq(column1.getName(), column1AttributesValue);
                TestCase.assertTrue(attributesResultSet.getCount() > 0);
                boolean found = false;
                while (attributesResultSet.moveToNext()) {
                    queryAttributesRow = attributesResultSet.getRow();
                    TestCase.assertEquals(column1Value, queryAttributesRow.getValue(column1.getName()));
                    if (!found) {
                        found = attributesRow.getId() == queryAttributesRow.getId();
                    }
                }
                TestCase.assertTrue(found);
                attributesResultSet.close();
                // Query for field values
                Map<String, ColumnValue> fieldValues = new HashMap<String, ColumnValue>();
                fieldValues.put(column1.getName(), column1AttributesValue);
                Object column2Value = null;
                ColumnValue column2AttributesValue;
                if (column2 != null) {
                    column2Value = attributesRow.getValue(column2.getName());
                    Class<?> column2ClassType = column2.getDataType().getClassType();
                    boolean column2Decimal = column2ClassType == Double.class || column2ClassType == Float.class;
                    if (column2Decimal) {
                        column2AttributesValue = new ColumnValue(column2Value, .000001);
                    } else if (column2Value instanceof Date) {
                        column2AttributesValue = new ColumnValue(DateConverter.converter(column2.getDataType()).stringValue((Date) column2Value));
                    } else {
                        column2AttributesValue = new ColumnValue(column2Value);
                    }
                    fieldValues.put(column2.getName(), column2AttributesValue);
                }
                attributesResultSet = dao.queryForValueFieldValues(fieldValues);
                TestCase.assertTrue(attributesResultSet.getCount() > 0);
                found = false;
                while (attributesResultSet.moveToNext()) {
                    queryAttributesRow = attributesResultSet.getRow();
                    TestCase.assertEquals(column1Value, queryAttributesRow.getValue(column1.getName()));
                    if (column2 != null) {
                        TestCase.assertEquals(column2Value, queryAttributesRow.getValue(column2.getName()));
                    }
                    if (!found) {
                        found = attributesRow.getId() == queryAttributesRow.getId();
                    }
                }
                TestCase.assertTrue(found);
                attributesResultSet.close();
            }
            MetadataReferenceDao referenceDao = MetadataExtension.getMetadataReferenceDao(geoPackage);
            List<MetadataReference> references = referenceDao.queryForEq(MetadataReference.COLUMN_TABLE_NAME, attributesTable.getTableName());
            if (references != null && !references.isEmpty()) {
                Metadata metadata = references.get(0).getMetadata();
                TestCase.assertEquals(MetadataScopeType.ATTRIBUTE_TYPE, metadata.getMetadataScope());
                for (MetadataReference reference : references) {
                    TestCase.assertTrue(reference.getReferenceScope() == ReferenceScopeType.ROW || reference.getReferenceScope() == ReferenceScopeType.ROW_COL);
                    Long rowId = reference.getRowIdValue();
                    TestCase.assertNotNull(rowId);
                    AttributesRow queryRow = dao.queryForIdRow(rowId);
                    TestCase.assertNotNull(queryRow);
                    TestCase.assertNotNull(queryRow.getTable());
                    TestCase.assertEquals(attributesTable.getTableName(), queryRow.getTable().getTableName());
                }
            }
            String previousColumn = null;
            for (String column : columns) {
                int expectedDistinctCount = dao.querySingleTypedResult("SELECT COUNT(DISTINCT " + column + ") FROM " + dao.getTableName(), null);
                int distinctCount = dao.count(true, column);
                TestCase.assertEquals(expectedDistinctCount, distinctCount);
                if (dao.count(column + " IS NULL") > 0) {
                    distinctCount++;
                }
                AttributesResultSet expectedResultSet = dao.rawQuery("SELECT DISTINCT " + column + " FROM " + dao.getTableName(), null);
                int expectedDistinctResultSetCount = expectedResultSet.getCount();
                int expectedDistinctManualResultSetCount = 0;
                while (expectedResultSet.moveToNext()) {
                    expectedDistinctManualResultSetCount++;
                }
                expectedResultSet.close();
                TestCase.assertEquals(expectedDistinctManualResultSetCount, expectedDistinctResultSetCount);
                attributesResultSet = dao.query(true, new String[] { column });
                TestCase.assertEquals(1, attributesResultSet.getColumnCount());
                TestCase.assertEquals(expectedDistinctResultSetCount, attributesResultSet.getCount());
                TestCase.assertEquals(distinctCount, attributesResultSet.getCount());
                attributesResultSet.close();
                attributesResultSet = dao.query(new String[] { column });
                TestCase.assertEquals(1, attributesResultSet.getColumnCount());
                TestCase.assertEquals(count, attributesResultSet.getCount());
                Set<Object> distinctValues = new HashSet<>();
                while (attributesResultSet.moveToNext()) {
                    Object value = attributesResultSet.getValue(column);
                    distinctValues.add(value);
                }
                attributesResultSet.close();
                TestCase.assertEquals(distinctCount, distinctValues.size());
                if (previousColumn != null) {
                    attributesResultSet = dao.query(true, new String[] { previousColumn, column });
                    TestCase.assertEquals(2, attributesResultSet.getColumnCount());
                    distinctCount = attributesResultSet.getCount();
                    if (distinctCount < 0) {
                        distinctCount = 0;
                        while (attributesResultSet.moveToNext()) {
                            distinctCount++;
                        }
                    }
                    attributesResultSet.close();
                    attributesResultSet = dao.query(new String[] { previousColumn, column });
                    TestCase.assertEquals(2, attributesResultSet.getColumnCount());
                    TestCase.assertEquals(count, attributesResultSet.getCount());
                    Map<Object, Set<Object>> distinctPairs = new HashMap<>();
                    while (attributesResultSet.moveToNext()) {
                        Object previousValue = attributesResultSet.getValue(previousColumn);
                        Object value = attributesResultSet.getValue(column);
                        distinctValues = distinctPairs.get(previousValue);
                        if (distinctValues == null) {
                            distinctValues = new HashSet<>();
                            distinctPairs.put(previousValue, distinctValues);
                        }
                        distinctValues.add(value);
                    }
                    attributesResultSet.close();
                    int distinctPairsCount = 0;
                    for (Set<Object> values : distinctPairs.values()) {
                        distinctPairsCount += values.size();
                    }
                    TestCase.assertEquals(distinctCount, distinctPairsCount);
                }
                previousColumn = column;
            }
        }
    }
}
Also used : MetadataReferenceDao(mil.nga.geopackage.extension.metadata.reference.MetadataReferenceDao) HashSet(java.util.HashSet) ResultSet(java.sql.ResultSet) Set(java.util.Set) HashMap(java.util.HashMap) Metadata(mil.nga.geopackage.extension.metadata.Metadata) ResultSet(java.sql.ResultSet) HashSet(java.util.HashSet) MetadataReference(mil.nga.geopackage.extension.metadata.reference.MetadataReference) Connection(java.sql.Connection) Date(java.util.Date) Contents(mil.nga.geopackage.contents.Contents) ColumnValue(mil.nga.geopackage.user.ColumnValue) ContentsDao(mil.nga.geopackage.contents.ContentsDao)

Example 2 with ColumnValue

use of mil.nga.geopackage.user.ColumnValue in project joa by sebastianfrey.

the class GeoPackageService method createFeature.

public Feature createFeature(FeatureRow featureRow) {
    Feature feature = null;
    GeoPackageGeometryData geometryData = featureRow.getGeometry();
    if (geometryData != null && !geometryData.isEmpty()) {
        feature = FeatureConverter.toFeature(geometryData.getGeometry());
        feature.setProperties(new HashMap<>());
        for (Map.Entry<String, ColumnValue> entry : featureRow.getAsMap()) {
            ColumnValue value = entry.getValue();
            String key = entry.getKey();
            if (!key.equals(featureRow.getGeometryColumnName())) {
                feature.getProperties().put(key, value.getValue());
            }
        }
        feature.setId(String.valueOf(featureRow.getId()));
    }
    return feature;
}
Also used : GeoPackageGeometryData(mil.nga.geopackage.geom.GeoPackageGeometryData) ColumnValue(mil.nga.geopackage.user.ColumnValue) Feature(mil.nga.sf.geojson.Feature) Map(java.util.Map) HashMap(java.util.HashMap)

Example 3 with ColumnValue

use of mil.nga.geopackage.user.ColumnValue in project geopackage-java by ngageoint.

the class FeatureUtils method testRead.

/**
 * Test read
 *
 * @param geoPackage
 *            GeoPackage
 * @throws SQLException
 *             upon error
 * @throws IOException
 *             upon error
 */
public static void testRead(GeoPackage geoPackage) throws SQLException, IOException {
    GeometryColumnsDao geometryColumnsDao = geoPackage.getGeometryColumnsDao();
    if (geometryColumnsDao.isTableExists()) {
        List<GeometryColumns> results = geometryColumnsDao.queryForAll();
        for (GeometryColumns geometryColumns : results) {
            // Test the get feature DAO methods
            FeatureDao dao = geoPackage.getFeatureDao(geometryColumns);
            TestCase.assertNotNull(dao);
            dao = geoPackage.getFeatureDao(geometryColumns.getContents());
            TestCase.assertNotNull(dao);
            dao = geoPackage.getFeatureDao(geometryColumns.getTableName());
            TestCase.assertNotNull(dao);
            TestCase.assertNotNull(dao.getDb());
            TestCase.assertEquals(geometryColumns.getId(), dao.getGeometryColumns().getId());
            TestCase.assertEquals(geometryColumns.getTableName(), dao.getTableName());
            TestCase.assertEquals(geometryColumns.getColumnName(), dao.getGeometryColumnName());
            FeatureTable featureTable = dao.getTable();
            String[] columns = featureTable.getColumnNames();
            int geomIndex = featureTable.getGeometryColumnIndex();
            TestCase.assertTrue(geomIndex >= 0 && geomIndex < columns.length);
            TestCase.assertEquals(geometryColumns.getColumnName(), columns[geomIndex]);
            // Query for all
            FeatureResultSet featureResultSet = dao.queryForAll();
            int count = featureResultSet.getCount();
            int manualCount = 0;
            while (featureResultSet.moveToNext()) {
                GeoPackageGeometryData geoPackageGeometryData = featureResultSet.getGeometry();
                if (featureResultSet.getBlob(featureTable.getGeometryColumnIndex()) != null) {
                    TestCase.assertNotNull(geoPackageGeometryData);
                    Geometry geometry = geoPackageGeometryData.getGeometry();
                    GeometryType geometryType = geometryColumns.getGeometryType();
                    validateGeometry(geometryType, geometry);
                    byte[] wkbBytes = geoPackageGeometryData.getWkb();
                    int byteLenth = wkbBytes.length;
                    TestCase.assertTrue(byteLenth > 0);
                    Geometry geometryFromBytes = GeometryReader.readGeometry(wkbBytes);
                    TestCase.assertNotNull(geometryFromBytes);
                    TestCase.assertEquals(geometry.getGeometryType(), geometryFromBytes.getGeometryType());
                    validateGeometry(geometryType, geometryFromBytes);
                    ByteBuffer wkbByteBuffer = geoPackageGeometryData.getWkbBuffer();
                    TestCase.assertEquals(byteLenth, wkbByteBuffer.remaining());
                    byte[] wkbBytes2 = new byte[wkbByteBuffer.remaining()];
                    wkbByteBuffer.get(wkbBytes2);
                    Geometry geometryFromBytes2 = GeometryReader.readGeometry(wkbBytes2);
                    TestCase.assertNotNull(geometryFromBytes2);
                    TestCase.assertEquals(geometry.getGeometryType(), geometryFromBytes2.getGeometryType());
                    validateGeometry(geometryType, geometryFromBytes2);
                }
                FeatureRow featureRow = featureResultSet.getRow();
                validateFeatureRow(columns, featureRow);
                manualCount++;
            }
            TestCase.assertEquals(count, manualCount);
            featureResultSet.close();
            // Manually query for all and compare
            Connection connection = dao.getConnection();
            String sql = SQLiteQueryBuilder.buildQueryString(false, dao.getTableName(), null, null, null, null, null, null, null);
            ResultSet resultSet = SQLUtils.query(connection, sql, null);
            int resultSetCount = SQLUtils.count(connection, sql, null);
            featureResultSet = new FeatureResultSet(featureTable, resultSet, resultSetCount);
            count = featureResultSet.getCount();
            manualCount = 0;
            while (featureResultSet.moveToNext()) {
                GeoPackageGeometryData geometry = featureResultSet.getGeometry();
                if (featureResultSet.getBlob(featureTable.getGeometryColumnIndex()) != null) {
                    TestCase.assertNotNull(geometry);
                }
                manualCount++;
            }
            TestCase.assertEquals(count, manualCount);
            TestCase.assertTrue("No features to test", count > 0);
            featureResultSet.close();
            resultSet = SQLUtils.query(connection, sql, null);
            resultSetCount = SQLUtils.count(connection, sql, null);
            featureResultSet = new FeatureResultSet(featureTable, resultSet, resultSetCount);
            // Choose random feature
            int random = (int) (Math.random() * count);
            featureResultSet.moveToPosition(random);
            FeatureRow featureRow = featureResultSet.getRow();
            featureResultSet.close();
            // Query by id
            FeatureRow queryFeatureRow = dao.queryForIdRow(featureRow.getId());
            TestCase.assertNotNull(queryFeatureRow);
            TestCase.assertEquals(featureRow.getId(), queryFeatureRow.getId());
            // Find two non id non geom columns
            FeatureColumn column1 = null;
            FeatureColumn column2 = null;
            for (FeatureColumn column : featureRow.getTable().getColumns()) {
                if (!column.isPrimaryKey() && !column.isGeometry()) {
                    if (column1 == null) {
                        column1 = column;
                    } else {
                        column2 = column;
                        break;
                    }
                }
            }
            // Query for equal
            if (column1 != null) {
                Object column1Value = featureRow.getValue(column1.getName());
                Class<?> column1ClassType = column1.getDataType().getClassType();
                boolean column1Decimal = column1ClassType == Double.class || column1ClassType == Float.class;
                ColumnValue column1FeatureValue;
                if (column1Decimal) {
                    column1FeatureValue = new ColumnValue(column1Value, .000001);
                } else {
                    column1FeatureValue = new ColumnValue(column1Value);
                }
                featureResultSet = dao.queryForEq(column1.getName(), column1FeatureValue);
                TestCase.assertTrue(featureResultSet.getCount() > 0);
                boolean found = false;
                while (featureResultSet.moveToNext()) {
                    queryFeatureRow = featureResultSet.getRow();
                    TestCase.assertEquals(column1Value, queryFeatureRow.getValue(column1.getName()));
                    if (!found) {
                        found = featureRow.getId() == queryFeatureRow.getId();
                    }
                }
                TestCase.assertTrue(found);
                featureResultSet.close();
                // Query for field values
                Map<String, ColumnValue> fieldValues = new HashMap<String, ColumnValue>();
                fieldValues.put(column1.getName(), column1FeatureValue);
                Object column2Value = null;
                ColumnValue column2FeatureValue;
                if (column2 != null) {
                    column2Value = featureRow.getValue(column2.getName());
                    Class<?> column2ClassType = column2.getDataType().getClassType();
                    boolean column2Decimal = column2ClassType == Double.class || column2ClassType == Float.class;
                    if (column2Decimal) {
                        column2FeatureValue = new ColumnValue(column2Value, .000001);
                    } else {
                        column2FeatureValue = new ColumnValue(column2Value);
                    }
                    fieldValues.put(column2.getName(), column2FeatureValue);
                }
                featureResultSet = dao.queryForValueFieldValues(fieldValues);
                TestCase.assertTrue(featureResultSet.getCount() > 0);
                found = false;
                while (featureResultSet.moveToNext()) {
                    queryFeatureRow = featureResultSet.getRow();
                    TestCase.assertEquals(column1Value, queryFeatureRow.getValue(column1.getName()));
                    if (column2 != null) {
                        TestCase.assertEquals(column2Value, queryFeatureRow.getValue(column2.getName()));
                    }
                    if (!found) {
                        found = featureRow.getId() == queryFeatureRow.getId();
                    }
                }
                TestCase.assertTrue(found);
                featureResultSet.close();
            }
            String previousColumn = null;
            for (String column : columns) {
                int expectedDistinctCount = dao.querySingleTypedResult("SELECT COUNT(DISTINCT " + column + ") FROM " + dao.getTableName(), null);
                int distinctCount = dao.count(true, column);
                TestCase.assertEquals(expectedDistinctCount, distinctCount);
                if (dao.count(column + " IS NULL") > 0) {
                    distinctCount++;
                }
                FeatureResultSet expectedResultSet = dao.rawQuery("SELECT DISTINCT " + column + " FROM " + dao.getTableName(), null);
                int expectedDistinctResultSetCount = expectedResultSet.getCount();
                int expectedDistinctManualResultSetCount = 0;
                while (expectedResultSet.moveToNext()) {
                    expectedDistinctManualResultSetCount++;
                }
                expectedResultSet.close();
                TestCase.assertEquals(expectedDistinctManualResultSetCount, expectedDistinctResultSetCount);
                featureResultSet = dao.query(true, new String[] { column });
                TestCase.assertEquals(1, featureResultSet.getColumnCount());
                TestCase.assertEquals(expectedDistinctResultSetCount, featureResultSet.getCount());
                TestCase.assertEquals(distinctCount, featureResultSet.getCount());
                featureResultSet.close();
                featureResultSet = dao.query(new String[] { column });
                TestCase.assertEquals(1, featureResultSet.getColumnCount());
                TestCase.assertEquals(count, featureResultSet.getCount());
                Set<Object> distinctValues = new HashSet<>();
                while (featureResultSet.moveToNext()) {
                    Object value = featureResultSet.getValue(column);
                    distinctValues.add(value);
                }
                featureResultSet.close();
                if (!column.equals(featureTable.getGeometryColumnName())) {
                    TestCase.assertEquals(distinctCount, distinctValues.size());
                }
                if (previousColumn != null) {
                    featureResultSet = dao.query(true, new String[] { previousColumn, column });
                    TestCase.assertEquals(2, featureResultSet.getColumnCount());
                    distinctCount = featureResultSet.getCount();
                    if (distinctCount < 0) {
                        distinctCount = 0;
                        while (featureResultSet.moveToNext()) {
                            distinctCount++;
                        }
                    }
                    featureResultSet.close();
                    featureResultSet = dao.query(new String[] { previousColumn, column });
                    TestCase.assertEquals(2, featureResultSet.getColumnCount());
                    TestCase.assertEquals(count, featureResultSet.getCount());
                    Map<Object, Set<Object>> distinctPairs = new HashMap<>();
                    while (featureResultSet.moveToNext()) {
                        Object previousValue = featureResultSet.getValue(previousColumn);
                        Object value = featureResultSet.getValue(column);
                        distinctValues = distinctPairs.get(previousValue);
                        if (distinctValues == null) {
                            distinctValues = new HashSet<>();
                            distinctPairs.put(previousValue, distinctValues);
                        }
                        distinctValues.add(value);
                    }
                    featureResultSet.close();
                    int distinctPairsCount = 0;
                    for (Set<Object> values : distinctPairs.values()) {
                        distinctPairsCount += values.size();
                    }
                    TestCase.assertEquals(distinctCount, distinctPairsCount);
                }
                previousColumn = column;
            }
        }
    }
}
Also used : HashSet(java.util.HashSet) ResultSet(java.sql.ResultSet) Set(java.util.Set) HashMap(java.util.HashMap) LineString(mil.nga.sf.LineString) MultiLineString(mil.nga.sf.MultiLineString) GeometryType(mil.nga.sf.GeometryType) ResultSet(java.sql.ResultSet) HashSet(java.util.HashSet) GeometryColumns(mil.nga.geopackage.features.columns.GeometryColumns) GeoPackageGeometryData(mil.nga.geopackage.geom.GeoPackageGeometryData) Connection(java.sql.Connection) ByteBuffer(java.nio.ByteBuffer) MultiPoint(mil.nga.sf.MultiPoint) Point(mil.nga.sf.Point) Geometry(mil.nga.sf.Geometry) ColumnValue(mil.nga.geopackage.user.ColumnValue) GeometryColumnsDao(mil.nga.geopackage.features.columns.GeometryColumnsDao)

Example 4 with ColumnValue

use of mil.nga.geopackage.user.ColumnValue in project geopackage-java by ngageoint.

the class TileUtils method testRead.

/**
 * Test read
 *
 * @param geoPackage
 *            GeoPackage
 * @throws SQLException
 *             upon error
 */
public static void testRead(GeoPackage geoPackage) throws SQLException {
    TileMatrixSetDao tileMatrixSetDao = geoPackage.getTileMatrixSetDao();
    if (tileMatrixSetDao.isTableExists()) {
        List<TileMatrixSet> results = tileMatrixSetDao.queryForAll();
        for (TileMatrixSet tileMatrixSet : results) {
            // Test the get tile DAO methods
            TileDao dao = geoPackage.getTileDao(tileMatrixSet);
            TestCase.assertNotNull(dao);
            dao = geoPackage.getTileDao(tileMatrixSet.getContents());
            TestCase.assertNotNull(dao);
            dao = geoPackage.getTileDao(tileMatrixSet.getTableName());
            TestCase.assertNotNull(dao);
            TestCase.assertNotNull(dao.getDb());
            TestCase.assertEquals(tileMatrixSet.getId(), dao.getTileMatrixSet().getId());
            TestCase.assertEquals(tileMatrixSet.getTableName(), dao.getTableName());
            TestCase.assertFalse(dao.getTileMatrices().isEmpty());
            TileTable tileTable = dao.getTable();
            String[] columns = tileTable.getColumnNames();
            int zoomLevelIndex = tileTable.getZoomLevelColumnIndex();
            TestCase.assertTrue(zoomLevelIndex >= 0 && zoomLevelIndex < columns.length);
            TestCase.assertEquals(TileTable.COLUMN_ZOOM_LEVEL, columns[zoomLevelIndex]);
            int tileColumnIndex = tileTable.getTileColumnColumnIndex();
            TestCase.assertTrue(tileColumnIndex >= 0 && tileColumnIndex < columns.length);
            TestCase.assertEquals(TileTable.COLUMN_TILE_COLUMN, columns[tileColumnIndex]);
            int tileRowIndex = tileTable.getTileRowColumnIndex();
            TestCase.assertTrue(tileRowIndex >= 0 && tileRowIndex < columns.length);
            TestCase.assertEquals(TileTable.COLUMN_TILE_ROW, columns[tileRowIndex]);
            int tileDataIndex = tileTable.getTileDataColumnIndex();
            TestCase.assertTrue(tileDataIndex >= 0 && tileDataIndex < columns.length);
            TestCase.assertEquals(TileTable.COLUMN_TILE_DATA, columns[tileDataIndex]);
            // Query for all
            TileResultSet tileResultSet = dao.queryForAll();
            int count = tileResultSet.getCount();
            int manualCount = 0;
            for (TileRow tileRow : tileResultSet) {
                validateTileRow(dao, columns, tileRow);
                manualCount++;
            }
            TestCase.assertEquals(count, manualCount);
            tileResultSet.close();
            // Manually query for all and compare
            Connection connection = dao.getConnection();
            String sql = SQLiteQueryBuilder.buildQueryString(false, dao.getTableName(), null, null, null, null, null, null, null);
            ResultSet resultSet = SQLUtils.query(connection, sql, null);
            int resultSetCount = SQLUtils.count(connection, sql, null);
            tileResultSet = new TileResultSet(tileTable, resultSet, resultSetCount);
            count = tileResultSet.getCount();
            manualCount = 0;
            while (tileResultSet.moveToNext()) {
                manualCount++;
            }
            TestCase.assertEquals(count, manualCount);
            TestCase.assertTrue("No tiles to test", count > 0);
            tileResultSet.close();
            resultSet = SQLUtils.query(connection, sql, null);
            resultSetCount = SQLUtils.count(connection, sql, null);
            tileResultSet = new TileResultSet(tileTable, resultSet, resultSetCount);
            // Choose random tile
            int random = (int) (Math.random() * count);
            tileResultSet.moveToPosition(random);
            TileRow tileRow = tileResultSet.getRow();
            tileResultSet.close();
            // Query by id
            TileRow queryTileRow = dao.queryForIdRow(tileRow.getId());
            TestCase.assertNotNull(queryTileRow);
            TestCase.assertEquals(tileRow.getId(), queryTileRow.getId());
            // Find two non id columns
            TileColumn column1 = null;
            TileColumn column2 = null;
            for (TileColumn column : tileRow.getTable().getColumns()) {
                if (!column.isPrimaryKey()) {
                    if (column1 == null) {
                        column1 = column;
                    } else {
                        column2 = column;
                        break;
                    }
                }
            }
            // Query for equal
            if (column1 != null) {
                Object column1Value = tileRow.getValue(column1.getName());
                Class<?> column1ClassType = column1.getDataType().getClassType();
                boolean column1Decimal = column1ClassType == Double.class || column1ClassType == Float.class;
                ColumnValue column1TileValue;
                if (column1Decimal) {
                    column1TileValue = new ColumnValue(column1Value, .000001);
                } else {
                    column1TileValue = new ColumnValue(column1Value);
                }
                tileResultSet = dao.queryForEq(column1.getName(), column1TileValue);
                TestCase.assertTrue(tileResultSet.getCount() > 0);
                boolean found = false;
                while (tileResultSet.moveToNext()) {
                    queryTileRow = tileResultSet.getRow();
                    TestCase.assertEquals(column1Value, queryTileRow.getValue(column1.getName()));
                    if (!found) {
                        found = tileRow.getId() == queryTileRow.getId();
                    }
                }
                TestCase.assertTrue(found);
                tileResultSet.close();
                // Query for field values
                Map<String, ColumnValue> fieldValues = new HashMap<String, ColumnValue>();
                fieldValues.put(column1.getName(), column1TileValue);
                Object column2Value = null;
                ColumnValue column2TileValue;
                if (column2 != null) {
                    column2Value = tileRow.getValue(column2.getName());
                    Class<?> column2ClassType = column2.getDataType().getClassType();
                    boolean column2Decimal = column2ClassType == Double.class || column2ClassType == Float.class;
                    if (column2Decimal) {
                        column2TileValue = new ColumnValue(column2Value, .000001);
                    } else {
                        column2TileValue = new ColumnValue(column2Value);
                    }
                    fieldValues.put(column2.getName(), column2TileValue);
                }
                tileResultSet = dao.queryForValueFieldValues(fieldValues);
                TestCase.assertTrue(tileResultSet.getCount() > 0);
                found = false;
                while (tileResultSet.moveToNext()) {
                    queryTileRow = tileResultSet.getRow();
                    TestCase.assertEquals(column1Value, queryTileRow.getValue(column1.getName()));
                    if (column2 != null) {
                        TestCase.assertEquals(column2Value, queryTileRow.getValue(column2.getName()));
                    }
                    if (!found) {
                        found = tileRow.getId() == queryTileRow.getId();
                    }
                }
                TestCase.assertTrue(found);
                tileResultSet.close();
            }
            String previousColumn = null;
            for (String column : columns) {
                int expectedDistinctCount = dao.querySingleTypedResult("SELECT COUNT(DISTINCT " + column + ") FROM " + dao.getTableName(), null);
                int distinctCount = dao.count(true, column);
                TestCase.assertEquals(expectedDistinctCount, distinctCount);
                if (dao.count(column + " IS NULL") > 0) {
                    distinctCount++;
                }
                TileResultSet expectedResultSet = dao.rawQuery("SELECT DISTINCT " + column + " FROM " + dao.getTableName(), null);
                int expectedDistinctResultSetCount = expectedResultSet.getCount();
                int expectedDistinctManualResultSetCount = 0;
                while (expectedResultSet.moveToNext()) {
                    expectedDistinctManualResultSetCount++;
                }
                expectedResultSet.close();
                TestCase.assertEquals(expectedDistinctManualResultSetCount, expectedDistinctResultSetCount);
                tileResultSet = dao.query(true, new String[] { column });
                TestCase.assertEquals(1, tileResultSet.getColumnCount());
                TestCase.assertEquals(expectedDistinctResultSetCount, tileResultSet.getCount());
                TestCase.assertEquals(distinctCount, tileResultSet.getCount());
                tileResultSet.close();
                tileResultSet = dao.query(new String[] { column });
                TestCase.assertEquals(1, tileResultSet.getColumnCount());
                TestCase.assertEquals(count, tileResultSet.getCount());
                Set<Object> distinctValues = new HashSet<>();
                while (tileResultSet.moveToNext()) {
                    Object value = tileResultSet.getValue(column);
                    distinctValues.add(value);
                }
                tileResultSet.close();
                if (!column.equals(tileTable.getTileDataColumn().getName())) {
                    TestCase.assertEquals(distinctCount, distinctValues.size());
                }
                if (previousColumn != null) {
                    tileResultSet = dao.query(true, new String[] { previousColumn, column });
                    TestCase.assertEquals(2, tileResultSet.getColumnCount());
                    distinctCount = tileResultSet.getCount();
                    if (distinctCount < 0) {
                        distinctCount = 0;
                        while (tileResultSet.moveToNext()) {
                            distinctCount++;
                        }
                    }
                    tileResultSet.close();
                    tileResultSet = dao.query(new String[] { previousColumn, column });
                    TestCase.assertEquals(2, tileResultSet.getColumnCount());
                    TestCase.assertEquals(count, tileResultSet.getCount());
                    Map<Object, Set<Object>> distinctPairs = new HashMap<>();
                    while (tileResultSet.moveToNext()) {
                        Object previousValue = tileResultSet.getValue(previousColumn);
                        Object value = tileResultSet.getValue(column);
                        distinctValues = distinctPairs.get(previousValue);
                        if (distinctValues == null) {
                            distinctValues = new HashSet<>();
                            distinctPairs.put(previousValue, distinctValues);
                        }
                        distinctValues.add(value);
                    }
                    tileResultSet.close();
                    int distinctPairsCount = 0;
                    for (Set<Object> values : distinctPairs.values()) {
                        distinctPairsCount += values.size();
                    }
                    if (!previousColumn.equals(tileTable.getTileDataColumn().getName()) && !column.equals(tileTable.getTileDataColumn().getName())) {
                        TestCase.assertEquals(distinctCount, distinctPairsCount);
                    }
                }
                previousColumn = column;
            }
        }
    }
}
Also used : HashSet(java.util.HashSet) ResultSet(java.sql.ResultSet) Set(java.util.Set) TileMatrixSet(mil.nga.geopackage.tiles.matrixset.TileMatrixSet) HashMap(java.util.HashMap) ResultSet(java.sql.ResultSet) HashSet(java.util.HashSet) TileMatrixSet(mil.nga.geopackage.tiles.matrixset.TileMatrixSet) Connection(java.sql.Connection) ColumnValue(mil.nga.geopackage.user.ColumnValue) TileMatrixSetDao(mil.nga.geopackage.tiles.matrixset.TileMatrixSetDao)

Aggregations

HashMap (java.util.HashMap)4 ColumnValue (mil.nga.geopackage.user.ColumnValue)4 Connection (java.sql.Connection)3 ResultSet (java.sql.ResultSet)3 HashSet (java.util.HashSet)3 Set (java.util.Set)3 GeoPackageGeometryData (mil.nga.geopackage.geom.GeoPackageGeometryData)2 ByteBuffer (java.nio.ByteBuffer)1 Date (java.util.Date)1 Map (java.util.Map)1 Contents (mil.nga.geopackage.contents.Contents)1 ContentsDao (mil.nga.geopackage.contents.ContentsDao)1 Metadata (mil.nga.geopackage.extension.metadata.Metadata)1 MetadataReference (mil.nga.geopackage.extension.metadata.reference.MetadataReference)1 MetadataReferenceDao (mil.nga.geopackage.extension.metadata.reference.MetadataReferenceDao)1 GeometryColumns (mil.nga.geopackage.features.columns.GeometryColumns)1 GeometryColumnsDao (mil.nga.geopackage.features.columns.GeometryColumnsDao)1 TileMatrixSet (mil.nga.geopackage.tiles.matrixset.TileMatrixSet)1 TileMatrixSetDao (mil.nga.geopackage.tiles.matrixset.TileMatrixSetDao)1 Geometry (mil.nga.sf.Geometry)1