Search in sources :

Example 1 with MetadataDao

use of mil.nga.geopackage.extension.metadata.MetadataDao in project geopackage-android-map by ngageoint.

the class ReadmeTest method testGeoPackage.

/**
 * Test GeoPackage
 *
 * @param context        context
 * @param geoPackageFile GeoPackage file
 * @param map            Google map
 * @throws IOException  upon error
 * @throws SQLException upon error
 */
private void testGeoPackage(Context context, File geoPackageFile, GoogleMap map) throws IOException, SQLException {
    // Context context = ...;
    // File geoPackageFile = ...;
    // GoogleMap map = ...;
    // Get a manager
    GeoPackageManager manager = GeoPackageFactory.getManager(context);
    // Import database
    boolean imported = manager.importGeoPackage(geoPackageFile);
    // Available databases
    List<String> databases = manager.databases();
    // Open database
    GeoPackage geoPackage = manager.open(databases.get(0));
    // GeoPackage Table DAOs
    SpatialReferenceSystemDao srsDao = geoPackage.getSpatialReferenceSystemDao();
    ContentsDao contentsDao = geoPackage.getContentsDao();
    GeometryColumnsDao geomColumnsDao = geoPackage.getGeometryColumnsDao();
    TileMatrixSetDao tileMatrixSetDao = geoPackage.getTileMatrixSetDao();
    TileMatrixDao tileMatrixDao = geoPackage.getTileMatrixDao();
    SchemaExtension schemaExtension = new SchemaExtension(geoPackage);
    DataColumnsDao dao = schemaExtension.getDataColumnsDao();
    DataColumnConstraintsDao dataColumnConstraintsDao = schemaExtension.getDataColumnConstraintsDao();
    MetadataExtension metadataExtension = new MetadataExtension(geoPackage);
    MetadataDao metadataDao = metadataExtension.getMetadataDao();
    MetadataReferenceDao metadataReferenceDao = metadataExtension.getMetadataReferenceDao();
    ExtensionsDao extensionsDao = geoPackage.getExtensionsDao();
    // Feature and tile tables
    List<String> features = geoPackage.getFeatureTables();
    List<String> tiles = geoPackage.getTileTables();
    // Query Features
    String featureTable = features.get(0);
    FeatureDao featureDao = geoPackage.getFeatureDao(featureTable);
    GoogleMapShapeConverter converter = new GoogleMapShapeConverter(featureDao.getProjection());
    FeatureCursor featureCursor = featureDao.queryForAll();
    try {
        for (FeatureRow featureRow : featureCursor) {
            GeoPackageGeometryData geometryData = featureRow.getGeometry();
            if (geometryData != null && !geometryData.isEmpty()) {
                Geometry geometry = geometryData.getGeometry();
                GoogleMapShape shape = converter.toShape(geometry);
                GoogleMapShape mapShape = GoogleMapShapeConverter.addShapeToMap(map, shape);
            // ...
            }
        }
    } finally {
        featureCursor.close();
    }
    // Query Tiles
    String tileTable = tiles.get(0);
    TileDao tileDao = geoPackage.getTileDao(tileTable);
    TileCursor tileCursor = tileDao.queryForAll();
    try {
        for (TileRow tileRow : tileCursor) {
            byte[] tileBytes = tileRow.getTileData();
            Bitmap tileBitmap = tileRow.getTileDataBitmap();
        // ...
        }
    } finally {
        tileCursor.close();
    }
    // Retrieve Tiles by XYZ
    GeoPackageTileRetriever retriever = new GeoPackageTileRetriever(tileDao);
    GeoPackageTile geoPackageTile = retriever.getTile(2, 2, 2);
    if (geoPackageTile != null) {
        byte[] tileBytes = geoPackageTile.getData();
        Bitmap tileBitmap = geoPackageTile.getBitmap();
    // ...
    }
    // Retrieve Tiles by Bounding Box
    TileCreator tileCreator = new TileCreator(tileDao, ProjectionFactory.getProjection(ProjectionConstants.EPSG_WORLD_GEODETIC_SYSTEM));
    GeoPackageTile geoPackageTile2 = tileCreator.getTile(new BoundingBox(-90.0, 0.0, 0.0, 66.513260));
    if (geoPackageTile2 != null) {
        byte[] tileBytes = geoPackageTile2.getData();
        Bitmap tileBitmap = geoPackageTile2.getBitmap();
    // ...
    }
    // Tile Provider (GeoPackage or Google API)
    TileProvider overlay = GeoPackageOverlayFactory.getTileProvider(tileDao);
    TileOverlayOptions overlayOptions = new TileOverlayOptions();
    overlayOptions.tileProvider(overlay);
    overlayOptions.zIndex(-1);
    map.addTileOverlay(overlayOptions);
    BoundingBox boundingBox = BoundingBox.worldWebMercator();
    Projection projection = ProjectionFactory.getProjection(ProjectionConstants.EPSG_WEB_MERCATOR);
    // Index Features
    FeatureIndexManager indexer = new FeatureIndexManager(context, geoPackage, featureDao);
    indexer.setIndexLocation(FeatureIndexType.GEOPACKAGE);
    int indexedCount = indexer.index();
    // Query Indexed Features in paginated chunks
    FeatureIndexResults indexResults = indexer.queryForChunk(boundingBox, projection, 50);
    FeaturePaginatedCursor paginatedCursor = indexer.paginate(indexResults);
    for (FeatureRow featureRow : paginatedCursor) {
        GeoPackageGeometryData geometryData = featureRow.getGeometry();
        if (geometryData != null && !geometryData.isEmpty()) {
            Geometry geometry = geometryData.getGeometry();
        // ...
        }
    }
    // Feature Tile Provider (dynamically draw tiles from features)
    FeatureTiles featureTiles = new DefaultFeatureTiles(context, featureDao, context.getResources().getDisplayMetrics().density);
    // Set max features to draw per tile
    featureTiles.setMaxFeaturesPerTile(1000);
    // Custom feature tile implementation
    NumberFeaturesTile numberFeaturesTile = new NumberFeaturesTile(context);
    // Draw feature count tiles when max features passed
    featureTiles.setMaxFeaturesTileDraw(numberFeaturesTile);
    // Set index manager to query feature indices
    featureTiles.setIndexManager(indexer);
    FeatureOverlay featureOverlay = new FeatureOverlay(featureTiles);
    // Set zoom level to start showing tiles
    featureOverlay.setMinZoom(featureDao.getZoomLevel());
    TileOverlayOptions featureOverlayOptions = new TileOverlayOptions();
    featureOverlayOptions.tileProvider(featureOverlay);
    // Draw the feature tiles behind map markers
    featureOverlayOptions.zIndex(-1);
    map.addTileOverlay(featureOverlayOptions);
    // URL Tile Generator (generate tiles from a URL)
    TileGenerator urlTileGenerator = new UrlTileGenerator(context, geoPackage, "url_tile_table", "http://url/{z}/{x}/{y}.png", 0, 0, boundingBox, projection);
    int urlTileCount = urlTileGenerator.generateTiles();
    // Feature Tile Generator (generate tiles from features)
    TileGenerator featureTileGenerator = new FeatureTileGenerator(context, geoPackage, "tiles_" + featureTable, featureTiles, 1, 2, boundingBox, projection);
    int featureTileCount = featureTileGenerator.generateTiles();
    // Close feature tiles (and indexer)
    featureTiles.close();
    // Close database when done
    geoPackage.close();
}
Also used : MetadataReferenceDao(mil.nga.geopackage.extension.metadata.reference.MetadataReferenceDao) GeoPackageTileRetriever(mil.nga.geopackage.tiles.retriever.GeoPackageTileRetriever) DataColumnsDao(mil.nga.geopackage.extension.schema.columns.DataColumnsDao) Projection(mil.nga.proj.Projection) DefaultFeatureTiles(mil.nga.geopackage.tiles.features.DefaultFeatureTiles) FeatureDao(mil.nga.geopackage.features.user.FeatureDao) GeoPackage(mil.nga.geopackage.GeoPackage) MetadataExtension(mil.nga.geopackage.extension.metadata.MetadataExtension) TileProvider(com.google.android.gms.maps.model.TileProvider) FeatureOverlay(mil.nga.geopackage.map.tiles.overlay.FeatureOverlay) SpatialReferenceSystemDao(mil.nga.geopackage.srs.SpatialReferenceSystemDao) Bitmap(android.graphics.Bitmap) FeaturePaginatedCursor(mil.nga.geopackage.features.user.FeaturePaginatedCursor) DataColumnConstraintsDao(mil.nga.geopackage.extension.schema.constraints.DataColumnConstraintsDao) BoundingBox(mil.nga.geopackage.BoundingBox) TileCreator(mil.nga.geopackage.tiles.retriever.TileCreator) MetadataDao(mil.nga.geopackage.extension.metadata.MetadataDao) FeatureIndexResults(mil.nga.geopackage.features.index.FeatureIndexResults) GeoPackageGeometryData(mil.nga.geopackage.geom.GeoPackageGeometryData) SchemaExtension(mil.nga.geopackage.extension.schema.SchemaExtension) TileOverlayOptions(com.google.android.gms.maps.model.TileOverlayOptions) ExtensionsDao(mil.nga.geopackage.extension.ExtensionsDao) NumberFeaturesTile(mil.nga.geopackage.tiles.features.custom.NumberFeaturesTile) GeoPackageManager(mil.nga.geopackage.GeoPackageManager) UrlTileGenerator(mil.nga.geopackage.tiles.UrlTileGenerator) GoogleMapShapeConverter(mil.nga.geopackage.map.geom.GoogleMapShapeConverter) GoogleMapShape(mil.nga.geopackage.map.geom.GoogleMapShape) Geometry(mil.nga.sf.Geometry) GeoPackageTile(mil.nga.geopackage.tiles.retriever.GeoPackageTile) DefaultFeatureTiles(mil.nga.geopackage.tiles.features.DefaultFeatureTiles) FeatureTiles(mil.nga.geopackage.tiles.features.FeatureTiles) TileMatrixDao(mil.nga.geopackage.tiles.matrix.TileMatrixDao) FeatureRow(mil.nga.geopackage.features.user.FeatureRow) FeatureIndexManager(mil.nga.geopackage.features.index.FeatureIndexManager) TileRow(mil.nga.geopackage.tiles.user.TileRow) TileGenerator(mil.nga.geopackage.tiles.TileGenerator) FeatureTileGenerator(mil.nga.geopackage.tiles.features.FeatureTileGenerator) UrlTileGenerator(mil.nga.geopackage.tiles.UrlTileGenerator) FeatureCursor(mil.nga.geopackage.features.user.FeatureCursor) FeatureTileGenerator(mil.nga.geopackage.tiles.features.FeatureTileGenerator) TileMatrixSetDao(mil.nga.geopackage.tiles.matrixset.TileMatrixSetDao) ContentsDao(mil.nga.geopackage.contents.ContentsDao) TileDao(mil.nga.geopackage.tiles.user.TileDao) TileCursor(mil.nga.geopackage.tiles.user.TileCursor) GeometryColumnsDao(mil.nga.geopackage.features.columns.GeometryColumnsDao)

Example 2 with MetadataDao

use of mil.nga.geopackage.extension.metadata.MetadataDao in project geopackage-android-map by ngageoint.

the class TestSetupTeardown method setUpCreateCommon.

/**
 * Set up create common
 *
 * @param geoPackage
 * @throws SQLException
 */
private static void setUpCreateCommon(GeoPackage geoPackage) throws SQLException {
    // Metadata
    MetadataExtension metadataExtension = new MetadataExtension(geoPackage);
    metadataExtension.createMetadataTable();
    MetadataDao metadataDao = metadataExtension.getMetadataDao();
    Metadata metadata1 = new Metadata();
    metadata1.setMetadataScope(MetadataScopeType.DATASET);
    metadata1.setStandardUri("TEST_URI_1");
    metadata1.setMimeType("text/xml");
    metadata1.setMetadata("TEST METADATA 1");
    metadataDao.create(metadata1);
    Metadata metadata2 = new Metadata();
    metadata2.setMetadataScope(MetadataScopeType.FEATURE_TYPE);
    metadata2.setStandardUri("TEST_URI_2");
    metadata2.setMimeType("text/xml");
    metadata2.setMetadata("TEST METADATA 2");
    metadataDao.create(metadata2);
    Metadata metadata3 = new Metadata();
    metadata3.setMetadataScope(MetadataScopeType.TILE);
    metadata3.setStandardUri("TEST_URI_3");
    metadata3.setMimeType("text/xml");
    metadata3.setMetadata("TEST METADATA 3");
    metadataDao.create(metadata3);
    // Metadata Reference
    metadataExtension.createMetadataReferenceTable();
    MetadataReferenceDao metadataReferenceDao = metadataExtension.getMetadataReferenceDao();
    MetadataReference reference1 = new MetadataReference();
    reference1.setReferenceScope(ReferenceScopeType.GEOPACKAGE);
    reference1.setTimestamp(new Date());
    reference1.setMetadata(metadata1);
    metadataReferenceDao.create(reference1);
    MetadataReference reference2 = new MetadataReference();
    reference2.setReferenceScope(ReferenceScopeType.TABLE);
    reference2.setTableName("TEST_TABLE_NAME_2");
    reference2.setTimestamp(new Date());
    reference2.setMetadata(metadata2);
    reference2.setParentMetadata(metadata1);
    metadataReferenceDao.create(reference2);
    MetadataReference reference3 = new MetadataReference();
    reference3.setReferenceScope(ReferenceScopeType.ROW_COL);
    reference3.setTableName("TEST_TABLE_NAME_3");
    reference3.setColumnName("TEST_COLUMN_NAME_3");
    reference3.setRowIdValue(5L);
    reference3.setTimestamp(new Date());
    reference3.setMetadata(metadata3);
    metadataReferenceDao.create(reference3);
    // Extensions
    geoPackage.createExtensionsTable();
    ExtensionsDao extensionsDao = geoPackage.getExtensionsDao();
    Extensions extensions1 = new Extensions();
    extensions1.setTableName("TEST_TABLE_NAME_1");
    extensions1.setColumnName("TEST_COLUMN_NAME_1");
    extensions1.setExtensionName("nga", "test_extension_1");
    extensions1.setDefinition("TEST DEFINITION 1");
    extensions1.setScope(ExtensionScopeType.READ_WRITE);
    extensionsDao.create(extensions1);
    Extensions extensions2 = new Extensions();
    extensions2.setTableName("TEST_TABLE_NAME_2");
    extensions2.setExtensionName("nga", "test_extension_2");
    extensions2.setDefinition("TEST DEFINITION 2");
    extensions2.setScope(ExtensionScopeType.WRITE_ONLY);
    extensionsDao.create(extensions2);
    Extensions extensions3 = new Extensions();
    extensions3.setExtensionName("nga", "test_extension_3");
    extensions3.setDefinition("TEST DEFINITION 3");
    extensions3.setScope(ExtensionScopeType.READ_WRITE);
    extensionsDao.create(extensions3);
    // Attributes
    List<AttributesColumn> columns = new ArrayList<AttributesColumn>();
    columns.add(AttributesColumn.createColumn(6, "test_text_limited", GeoPackageDataType.TEXT, 5L, false, null));
    columns.add(AttributesColumn.createColumn(7, "test_blob_limited", GeoPackageDataType.BLOB, 7L, false, null));
    columns.add(AttributesColumn.createColumn(1, "test_text", GeoPackageDataType.TEXT, false, ""));
    columns.add(AttributesColumn.createColumn(2, "test_real", GeoPackageDataType.REAL, false, null));
    columns.add(AttributesColumn.createColumn(3, "test_boolean", GeoPackageDataType.BOOLEAN, false, null));
    columns.add(AttributesColumn.createColumn(4, "test_blob", GeoPackageDataType.BLOB, false, null));
    columns.add(AttributesColumn.createColumn(5, "test_integer", GeoPackageDataType.INTEGER, false, null));
    AttributesTable attributesTable = geoPackage.createAttributesTable(AttributesTableMetadata.create("test_attributes", columns));
    TestCase.assertNotNull(attributesTable);
    Contents attributesContents = attributesTable.getContents();
    TestCase.assertNotNull(attributesContents);
    TestCase.assertEquals(ContentsDataType.ATTRIBUTES, attributesContents.getDataType());
    TestCase.assertEquals("test_attributes", attributesContents.getTableName());
    TestCase.assertEquals(attributesContents.getTableName(), attributesTable.getTableName());
    Metadata attributesMetadata = new Metadata();
    attributesMetadata.setMetadataScope(MetadataScopeType.ATTRIBUTE_TYPE);
    attributesMetadata.setStandardUri("ATTRIBUTES_TEST_URI");
    attributesMetadata.setMimeType("text/plain");
    attributesMetadata.setMetadata("ATTRIBUTES METADATA");
    metadataDao.create(attributesMetadata);
    AttributesDao attributesDao = geoPackage.getAttributesDao(attributesTable.getTableName());
    for (int i = 0; i < 10; i++) {
        AttributesRow newRow = attributesDao.newRow();
        for (AttributesColumn column : attributesTable.getColumns()) {
            if (!column.isPrimaryKey()) {
                // Leave nullable columns null 20% of the time
                if (!column.isNotNull()) {
                    if (Math.random() < 0.2) {
                        continue;
                    }
                }
                Object value = null;
                switch(column.getDataType()) {
                    case TEXT:
                        String text = UUID.randomUUID().toString();
                        if (column.getMax() != null && text.length() > column.getMax()) {
                            text = text.substring(0, column.getMax().intValue());
                        }
                        value = text;
                        break;
                    case REAL:
                    case DOUBLE:
                        value = Math.random() * 5000.0;
                        break;
                    case BOOLEAN:
                        value = Math.random() < .5 ? false : true;
                        break;
                    case INTEGER:
                    case INT:
                        value = (int) (Math.random() * 500);
                        break;
                    case BLOB:
                        byte[] blob = UUID.randomUUID().toString().getBytes();
                        if (column.getMax() != null && blob.length > column.getMax()) {
                            byte[] blobLimited = new byte[column.getMax().intValue()];
                            ByteBuffer.wrap(blob, 0, column.getMax().intValue()).get(blobLimited);
                            blob = blobLimited;
                        }
                        value = blob;
                        break;
                    default:
                        throw new UnsupportedOperationException("Not implemented for data type: " + column.getDataType());
                }
                newRow.setValue(column.getName(), value);
            }
        }
        long rowId = attributesDao.create(newRow);
        MetadataReference attributesReference = new MetadataReference();
        attributesReference.setReferenceScope(ReferenceScopeType.ROW);
        attributesReference.setTableName(attributesTable.getTableName());
        attributesReference.setRowIdValue(rowId);
        attributesReference.setTimestamp(new Date());
        attributesReference.setMetadata(attributesMetadata);
        metadataReferenceDao.create(attributesReference);
    }
}
Also used : MetadataReferenceDao(mil.nga.geopackage.extension.metadata.reference.MetadataReferenceDao) ExtensionsDao(mil.nga.geopackage.extension.ExtensionsDao) MetadataReference(mil.nga.geopackage.extension.metadata.reference.MetadataReference) Metadata(mil.nga.geopackage.extension.metadata.Metadata) AttributesTableMetadata(mil.nga.geopackage.attributes.AttributesTableMetadata) ArrayList(java.util.ArrayList) MetadataExtension(mil.nga.geopackage.extension.metadata.MetadataExtension) Extensions(mil.nga.geopackage.extension.Extensions) Date(java.util.Date) AttributesRow(mil.nga.geopackage.attributes.AttributesRow) Contents(mil.nga.geopackage.contents.Contents) AttributesDao(mil.nga.geopackage.attributes.AttributesDao) AttributesColumn(mil.nga.geopackage.attributes.AttributesColumn) AttributesTable(mil.nga.geopackage.attributes.AttributesTable) MetadataDao(mil.nga.geopackage.extension.metadata.MetadataDao)

Aggregations

ExtensionsDao (mil.nga.geopackage.extension.ExtensionsDao)2 MetadataDao (mil.nga.geopackage.extension.metadata.MetadataDao)2 MetadataExtension (mil.nga.geopackage.extension.metadata.MetadataExtension)2 MetadataReferenceDao (mil.nga.geopackage.extension.metadata.reference.MetadataReferenceDao)2 Bitmap (android.graphics.Bitmap)1 TileOverlayOptions (com.google.android.gms.maps.model.TileOverlayOptions)1 TileProvider (com.google.android.gms.maps.model.TileProvider)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 BoundingBox (mil.nga.geopackage.BoundingBox)1 GeoPackage (mil.nga.geopackage.GeoPackage)1 GeoPackageManager (mil.nga.geopackage.GeoPackageManager)1 AttributesColumn (mil.nga.geopackage.attributes.AttributesColumn)1 AttributesDao (mil.nga.geopackage.attributes.AttributesDao)1 AttributesRow (mil.nga.geopackage.attributes.AttributesRow)1 AttributesTable (mil.nga.geopackage.attributes.AttributesTable)1 AttributesTableMetadata (mil.nga.geopackage.attributes.AttributesTableMetadata)1 Contents (mil.nga.geopackage.contents.Contents)1 ContentsDao (mil.nga.geopackage.contents.ContentsDao)1 Extensions (mil.nga.geopackage.extension.Extensions)1