use of mil.nga.geopackage.extension.metadata.MetadataExtension 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();
}
use of mil.nga.geopackage.extension.metadata.MetadataExtension 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);
}
}
Aggregations