Java Code Examples for mil.nga.geopackage.tiles.TileBoundingBoxUtils#getWebMercatorBoundingBox()

The following examples show how to use mil.nga.geopackage.tiles.TileBoundingBoxUtils#getWebMercatorBoundingBox() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BoundedOverlay.java    From geopackage-android-map with MIT License 6 votes vote down vote up
/**
 * Check if the tile request is within the desired tile bounds
 *
 * @param x    x coordinate
 * @param y    y coordinate
 * @param zoom zoom value
 * @return true if within bounds
 */
public boolean isWithinBoundingBox(int x, int y, int zoom) {
    boolean withinBounds = true;

    // If a bounding box is set, check if it overlaps with the request
    if (webMercatorBoundingBox != null) {

        // Get the bounding box of the requested tile
        BoundingBox tileWebMercatorBoundingBox = TileBoundingBoxUtils
                .getWebMercatorBoundingBox(x, y, zoom);

        // Adjust the bounding box if needed
        BoundingBox adjustedWebMercatorBoundingBox = getWebMercatorBoundingBox(tileWebMercatorBoundingBox);

        // Check if the request overlaps
        withinBounds = adjustedWebMercatorBoundingBox.intersects(
                tileWebMercatorBoundingBox, true);
    }

    return withinBounds;
}
 
Example 2
Source File: FeatureTileGenerator.java    From geopackage-android with MIT License 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public BoundingBox getBoundingBox(int zoom) {

    ProjectionTransform projectionToWebMercator = projection
            .getTransformation(ProjectionConstants.EPSG_WEB_MERCATOR);
    BoundingBox webMercatorBoundingBox = boundingBox
            .transform(projectionToWebMercator);

    TileGrid tileGrid = TileBoundingBoxUtils.getTileGrid(webMercatorBoundingBox, zoom);
    BoundingBox tileBoundingBox = TileBoundingBoxUtils.getWebMercatorBoundingBox(
            tileGrid.getMinX(), tileGrid.getMinY(), zoom);

    BoundingBox expandedBoundingBox = featureTiles.expandBoundingBox(webMercatorBoundingBox, tileBoundingBox);

    BoundingBox zoomBoundingBox = expandedBoundingBox.transform(projectionToWebMercator.getInverseTransformation());

    return zoomBoundingBox;
}
 
Example 3
Source File: FeatureTileGenerator.java    From geopackage-java with MIT License 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public BoundingBox getBoundingBox(int zoom) {

	ProjectionTransform projectionToWebMercator = projection
			.getTransformation(ProjectionConstants.EPSG_WEB_MERCATOR);
	BoundingBox webMercatorBoundingBox = boundingBox
			.transform(projectionToWebMercator);

	TileGrid tileGrid = TileBoundingBoxUtils.getTileGrid(
			webMercatorBoundingBox, zoom);
	BoundingBox tileBoundingBox = TileBoundingBoxUtils
			.getWebMercatorBoundingBox(tileGrid.getMinX(),
					tileGrid.getMinY(), zoom);

	BoundingBox expandedBoundingBox = featureTiles.expandBoundingBox(
			webMercatorBoundingBox, tileBoundingBox);

	BoundingBox zoomBoundingBox = expandedBoundingBox
			.transform(projectionToWebMercator.getInverseTransformation());

	return zoomBoundingBox;
}
 
Example 4
Source File: FeatureTiles.java    From geopackage-android with MIT License 5 votes vote down vote up
/**
 * Query for feature result count in the x, y, and zoom
 *
 * @param x    x coordinate
 * @param y    y coordinate
 * @param zoom zoom level
 * @return feature count
 * @since 1.1.0
 */
public long queryIndexedFeaturesCount(int x, int y, int zoom) {

    // Get the web mercator bounding box
    BoundingBox webMercatorBoundingBox = TileBoundingBoxUtils
            .getWebMercatorBoundingBox(x, y, zoom);

    // Query for the count of geometries matching the bounds in the index
    long count = queryIndexedFeaturesCount(webMercatorBoundingBox);

    return count;
}
 
Example 5
Source File: FeatureTiles.java    From geopackage-android with MIT License 5 votes vote down vote up
/**
 * Query for feature results in the x, y, and zoom level by querying features in the tile location
 *
 * @param x    x coordinate
 * @param y    y coordinate
 * @param zoom zoom level
 * @return feature index results
 * @since 3.2.0
 */
public FeatureIndexResults queryIndexedFeatures(int x, int y, int zoom) {

    // Get the web mercator bounding box
    BoundingBox webMercatorBoundingBox = TileBoundingBoxUtils
            .getWebMercatorBoundingBox(x, y, zoom);

    // Query for the geometries matching the bounds in the index
    return queryIndexedFeatures(webMercatorBoundingBox);
}
 
Example 6
Source File: GeoPackageTileRetriever.java    From geopackage-android with MIT License 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public GeoPackageTile getTile(int x, int y, int zoom) {

    // Get the bounding box of the requested tile
    BoundingBox webMercatorBoundingBox = TileBoundingBoxUtils
            .getWebMercatorBoundingBox(x, y, zoom);

    GeoPackageTile tile = tileCreator.getTile(webMercatorBoundingBox);

    return tile;
}
 
Example 7
Source File: FeatureTiles.java    From geopackage-java with MIT License 5 votes vote down vote up
/**
 * Query for feature result count in the x, y, and zoom
 *
 * @param x
 *            x coordinate
 * @param y
 *            y coordinate
 * @param zoom
 *            zoom level
 * @return feature count
 */
public long queryIndexedFeaturesCount(int x, int y, int zoom) {

	// Get the web mercator bounding box
	BoundingBox webMercatorBoundingBox = TileBoundingBoxUtils
			.getWebMercatorBoundingBox(x, y, zoom);

	// Query for the count of geometries matching the bounds in the index
	long count = queryIndexedFeaturesCount(webMercatorBoundingBox);

	return count;
}
 
Example 8
Source File: FeatureTiles.java    From geopackage-java with MIT License 5 votes vote down vote up
/**
 * Draw a tile image from the x, y, and zoom level by querying all features.
 * This could be very slow if there are a lot of features
 *
 * @param x
 *            x coordinate
 * @param y
 *            y coordinate
 * @param zoom
 *            zoom level
 * @return drawn image, or null
 */
public BufferedImage drawTileQueryAll(int x, int y, int zoom) {

	BoundingBox boundingBox = TileBoundingBoxUtils
			.getWebMercatorBoundingBox(x, y, zoom);

	BufferedImage image = null;

	// Query for all features
	FeatureResultSet resultSet = featureDao.queryForAll();

	try {

		int totalCount = resultSet.getCount();

		// Draw if at least one geometry exists
		if (totalCount > 0) {

			if (maxFeaturesPerTile == null
					|| totalCount <= maxFeaturesPerTile) {

				// Draw the tile image
				image = drawTile(zoom, boundingBox, resultSet);

			} else if (maxFeaturesTileDraw != null) {

				// Draw the unindexed max features tile
				image = maxFeaturesTileDraw.drawUnindexedTile(tileWidth,
						tileHeight, totalCount, resultSet);
			}

		}
	} finally {
		resultSet.close();
	}

	return image;
}
 
Example 9
Source File: FeatureTiles.java    From geopackage-java with MIT License 4 votes vote down vote up
/**
 * Draw a tile image from the x, y, and zoom level by querying features in
 * the tile location
 *
 * @param x
 *            x coordinate
 * @param y
 *            y coordinate
 * @param zoom
 *            zoom level
 * @return drawn image, or null
 */
public BufferedImage drawTileQueryIndex(int x, int y, int zoom) {

	// Get the web mercator bounding box
	BoundingBox webMercatorBoundingBox = TileBoundingBoxUtils
			.getWebMercatorBoundingBox(x, y, zoom);

	BufferedImage image = null;

	// Query for the geometry count matching the bounds in the index
	long tileCount = queryIndexedFeaturesCount(webMercatorBoundingBox);

	// Draw if at least one geometry exists
	if (tileCount > 0) {

		// Query for geometries matching the bounds in the index
		CloseableIterator<GeometryIndex> results = queryIndexedFeatures(
				webMercatorBoundingBox);

		try {

			if (maxFeaturesPerTile == null
					|| tileCount <= maxFeaturesPerTile.longValue()) {

				// Draw the tile image
				image = drawTile(zoom, webMercatorBoundingBox, results);

			} else if (maxFeaturesTileDraw != null) {

				// Draw the max features tile
				image = maxFeaturesTileDraw.drawTile(tileWidth, tileHeight,
						tileCount, results);
			}
		} finally {
			try {
				results.close();
			} catch (IOException e) {
				LOGGER.log(Level.WARNING,
						"Failed to close result set for query on x: " + x
								+ ", y: " + y + ", zoom: " + zoom,
						e);
			}
		}
	}

	return image;
}
 
Example 10
Source File: UrlTileGeneratorUtils.java    From geopackage-java with MIT License 4 votes vote down vote up
/**
 * 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);
	}

}
 
Example 11
Source File: FeatureTiles.java    From geopackage-android with MIT License 3 votes vote down vote up
/**
 * Draw a tile bitmap from the x, y, and zoom level by querying features in the tile location
 *
 * @param x    x coordinate
 * @param y    y coordinate
 * @param zoom zoom level
 * @return drawn bitmap, or null
 */
public Bitmap drawTileQueryIndex(int x, int y, int zoom) {

    // Get the web mercator bounding box
    BoundingBox webMercatorBoundingBox = TileBoundingBoxUtils
            .getWebMercatorBoundingBox(x, y, zoom);

    Bitmap bitmap = null;

    // Query for geometries matching the bounds in the index
    FeatureIndexResults results = queryIndexedFeatures(webMercatorBoundingBox);

    try {

        long tileCount = results.count();

        // Draw if at least one geometry exists
        if (tileCount > 0) {

            if (maxFeaturesPerTile == null || tileCount <= maxFeaturesPerTile.longValue()) {

                // Draw the tile bitmap
                bitmap = drawTile(zoom, webMercatorBoundingBox, results);

            } else if (maxFeaturesTileDraw != null) {

                // Draw the max features tile
                bitmap = maxFeaturesTileDraw.drawTile(tileWidth, tileHeight, tileCount, results);
            }

        }
    } finally {
        results.close();
    }

    return bitmap;
}
 
Example 12
Source File: FeatureTiles.java    From geopackage-android with MIT License 3 votes vote down vote up
/**
 * Draw a tile bitmap from the x, y, and zoom level by querying all features. This could
 * be very slow if there are a lot of features
 *
 * @param x    x coordinate
 * @param y    y coordinate
 * @param zoom zoom level
 * @return drawn bitmap, or null
 */
public Bitmap drawTileQueryAll(int x, int y, int zoom) {

    BoundingBox boundingBox = TileBoundingBoxUtils
            .getWebMercatorBoundingBox(x, y, zoom);

    Bitmap bitmap = null;

    // Query for all features
    FeatureCursor cursor = featureDao.queryForAll();

    try {

        int totalCount = cursor.getCount();

        // Draw if at least one geometry exists
        if (totalCount > 0) {

            if (maxFeaturesPerTile == null || totalCount <= maxFeaturesPerTile) {

                // Draw the tile bitmap
                bitmap = drawTile(zoom, boundingBox, cursor);

            } else if (maxFeaturesTileDraw != null) {

                // Draw the unindexed max features tile
                bitmap = maxFeaturesTileDraw.drawUnindexedTile(tileWidth, tileHeight, totalCount, cursor);
            }

        }
    } finally {
        cursor.close();
    }

    return bitmap;
}
 
Example 13
Source File: FeatureTiles.java    From geopackage-java with MIT License 3 votes vote down vote up
/**
 * Query for feature results in the x, y, and zoom
 *
 * @param x
 *            x coordinate
 * @param y
 *            y coordinate
 * @param zoom
 *            zoom level
 * @return feature count
 * @since 3.2.0
 */
public CloseableIterator<GeometryIndex> queryIndexedFeatures(int x, int y,
		int zoom) {

	// Get the web mercator bounding box
	BoundingBox webMercatorBoundingBox = TileBoundingBoxUtils
			.getWebMercatorBoundingBox(x, y, zoom);

	// Query for the geometries matching the bounds in the index
	return queryIndexedFeatures(webMercatorBoundingBox);
}