use of mil.nga.geopackage.tiles.matrix.TileMatrixDao 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.tiles.matrix.TileMatrixDao in project geopackage-android-map by ngageoint.
the class TestSetupTeardown method setUpCreateTiles.
/**
* Set up create for tiles test
*
* @param testContext
* @param geoPackage
* @throws SQLException
* @throws IOException
*/
private static void setUpCreateTiles(Context testContext, GeoPackage geoPackage) throws SQLException, IOException {
// Get existing SRS objects
SpatialReferenceSystemDao srsDao = geoPackage.getSpatialReferenceSystemDao();
SpatialReferenceSystem epsgSrs = srsDao.queryForId(4326l);
TestCase.assertNotNull(epsgSrs);
// Create the Tile Matrix Set and Tile Matrix tables
geoPackage.createTileMatrixSetTable();
geoPackage.createTileMatrixTable();
// Create new Contents
ContentsDao contentsDao = geoPackage.getContentsDao();
Contents contents = new Contents();
contents.setTableName("test_tiles");
contents.setDataType(ContentsDataType.TILES);
contents.setIdentifier("test_tiles");
// contents.setDescription("");
// contents.setLastChange(new Date());
contents.setMinX(-180.0);
contents.setMinY(-90.0);
contents.setMaxX(180.0);
contents.setMaxY(90.0);
contents.setSrs(epsgSrs);
// Create the user tile table
TileTable tileTable = TestUtils.buildTileTable(contents.getTableName());
geoPackage.createTileTable(tileTable);
// Create the contents
contentsDao.create(contents);
// Create new Tile Matrix Set
TileMatrixSetDao tileMatrixSetDao = geoPackage.getTileMatrixSetDao();
TileMatrixSet tileMatrixSet = new TileMatrixSet();
tileMatrixSet.setContents(contents);
tileMatrixSet.setSrs(contents.getSrs());
tileMatrixSet.setMinX(contents.getMinX());
tileMatrixSet.setMinY(contents.getMinY());
tileMatrixSet.setMaxX(contents.getMaxX());
tileMatrixSet.setMaxY(contents.getMaxY());
tileMatrixSetDao.create(tileMatrixSet);
// Create new Tile Matrix rows
TileMatrixDao tileMatrixDao = geoPackage.getTileMatrixDao();
int matrixWidthAndHeight = 2;
double pixelXSize = 69237.2;
double pixelYSize = 68412.1;
// Read the asset tile to bytes and convert to bitmap
byte[] assetTileData = TestUtils.getAssetFileBytes(testContext, TestConstants.TILE_FILE_NAME);
Bitmap bitmap = BitmapConverter.toBitmap(assetTileData);
// Get the width and height of the bitmap
final int tileWidth = bitmap.getWidth();
final int tileHeight = bitmap.getHeight();
// Compress the bitmap back to bytes and use those for the test
byte[] tileData = BitmapConverter.toBytes(bitmap, CompressFormat.valueOf(TestConstants.TILE_FILE_NAME_EXTENSION.toUpperCase()));
for (int zoom = 0; zoom < CREATE_TILE_MATRIX_COUNT; zoom++) {
TileMatrix tileMatrix = new TileMatrix();
tileMatrix.setContents(contents);
tileMatrix.setZoomLevel(zoom);
tileMatrix.setMatrixWidth(matrixWidthAndHeight);
tileMatrix.setMatrixHeight(matrixWidthAndHeight);
tileMatrix.setTileWidth(tileWidth);
tileMatrix.setTileHeight(tileHeight);
tileMatrix.setPixelXSize(pixelXSize);
tileMatrix.setPixelYSize(pixelYSize);
tileMatrixDao.create(tileMatrix);
matrixWidthAndHeight *= 2;
pixelXSize /= 2.0;
pixelYSize /= 2.0;
// Populate the tile table with rows
TestUtils.addRowsToTileTable(geoPackage, tileMatrix, tileData);
}
}
Aggregations