Java Code Examples for mil.nga.geopackage.tiles.TileBoundingBoxUtils

The following examples show how to use mil.nga.geopackage.tiles.TileBoundingBoxUtils. These examples are extracted from open source projects. 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 Project: geopackage-android-map   Source File: BoundedOverlay.java    License: 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 Project: geopackage-android   Source File: CoverageData.java    License: MIT License 6 votes vote down vote up
/**
 * Get the tile row results of coverage data tiles needed to create the
 * requested bounding box coverage data, sorted by row and then column
 *
 * @param projectedRequestBoundingBox bounding box projected to the coverage data
 * @param tileMatrix                  tile matrix
 * @return tile results or null
 */
private TileCursor retrieveSortedTileResults(
        BoundingBox projectedRequestBoundingBox, TileMatrix tileMatrix) {

    TileCursor tileResults = null;

    if (tileMatrix != null) {

        // Get the tile grid
        TileGrid tileGrid = TileBoundingBoxUtils.getTileGrid(
                coverageBoundingBox, tileMatrix.getMatrixWidth(),
                tileMatrix.getMatrixHeight(), projectedRequestBoundingBox);

        // Query for matching tiles in the tile grid
        tileResults = tileDao.queryByTileGrid(tileGrid,
                tileMatrix.getZoomLevel(), TileTable.COLUMN_TILE_ROW + ","
                        + TileTable.COLUMN_TILE_COLUMN);

    }

    return tileResults;
}
 
Example 3
Source Project: geopackage-android   Source File: DefaultFeatureTiles.java    License: MIT License 6 votes vote down vote up
/**
 * Add the linestring to the path
 *
 * @param simplifyTolerance simplify tolerance in meters
 * @param boundingBox       bounding box
 * @param transform         projection transform
 * @param path              path
 * @param lineString        line string
 */
private void addLineString(double simplifyTolerance, BoundingBox boundingBox, ProjectionTransform transform, Path path, LineString lineString) {

    List<Point> points = lineString.getPoints();

    if (points.size() >= 2) {

        // Try to simplify the number of points in the LineString
        points = simplifyPoints(simplifyTolerance, points);

        for (int i = 0; i < points.size(); i++) {
            Point point = points.get(i);
            Point webMercatorPoint = transform.transform(point);
            float x = TileBoundingBoxUtils.getXPixel(tileWidth, boundingBox,
                    webMercatorPoint.getX());
            float y = TileBoundingBoxUtils.getYPixel(tileHeight, boundingBox,
                    webMercatorPoint.getY());
            if (i == 0) {
                path.moveTo(x, y);
            } else {
                path.lineTo(x, y);
            }
        }
    }
}
 
Example 4
Source Project: geopackage-android   Source File: DefaultFeatureTiles.java    License: MIT License 6 votes vote down vote up
/**
 * Add a ring
 *
 * @param simplifyTolerance simplify tolerance in meters
 * @param boundingBox       bounding box
 * @param transform         projection transform
 * @param path              path
 * @param points            points
 */
private void addRing(double simplifyTolerance, BoundingBox boundingBox, ProjectionTransform transform, Path path, List<Point> points) {

    // Try to simplify the number of points in the LineString
    points = simplifyPoints(simplifyTolerance, points);

    for (int i = 0; i < points.size(); i++) {
        Point point = points.get(i);
        Point webMercatorPoint = transform.transform(point);
        float x = TileBoundingBoxUtils.getXPixel(tileWidth, boundingBox,
                webMercatorPoint.getX());
        float y = TileBoundingBoxUtils.getYPixel(tileHeight, boundingBox,
                webMercatorPoint.getY());
        if (i == 0) {
            path.moveTo(x, y);
        } else {
            path.lineTo(x, y);
        }
    }
    path.close();
}
 
Example 5
Source Project: geopackage-android   Source File: FeatureTileGenerator.java    License: 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 6
Source Project: geopackage-android   Source File: TileCreator.java    License: MIT License 6 votes vote down vote up
/**
 * Get the tile row results of tiles needed to draw the requested bounding box tile
 *
 * @param projectedRequestBoundingBox bounding box projected to the tiles
 * @param tileMatrix
 * @return tile cursor results or null
 */
private TileCursor retrieveTileResults(BoundingBox projectedRequestBoundingBox, TileMatrix tileMatrix) {

    TileCursor tileResults = null;

    if (tileMatrix != null) {

        // Get the tile grid
        TileGrid tileGrid = TileBoundingBoxUtils.getTileGrid(
                tileSetBoundingBox, tileMatrix.getMatrixWidth(),
                tileMatrix.getMatrixHeight(), projectedRequestBoundingBox);

        // Query for matching tiles in the tile grid
        tileResults = tileDao.queryByTileGrid(tileGrid,
                tileMatrix.getZoomLevel());

    }

    return tileResults;
}
 
Example 7
Source Project: geopackage-android   Source File: FeatureTilesTest.java    License: MIT License 6 votes vote down vote up
private void createTiles(FeatureTiles featureTiles, int zoom) {
    int tilesPerSide = TileBoundingBoxUtils.tilesPerSide(zoom);
    for (int i = 0; i < tilesPerSide; i++) {
        for (int j = 0; j < tilesPerSide; j++) {
            Bitmap bitmap = featureTiles.drawTile(i, j, zoom);
            if (bitmap != null) {
                long count = featureTiles.queryIndexedFeaturesCount(i, j,
                        zoom);
                assertTrue(count > 0);
                assertEquals(featureTiles.getTileWidth(),
                        bitmap.getWidth());
                assertEquals(featureTiles.getTileHeight(),
                        bitmap.getHeight());
            }
        }
    }
}
 
Example 8
Source Project: geopackage-core-java   Source File: UserCoreDao.java    License: MIT License 6 votes vote down vote up
/**
 * Get the approximate zoom level of where the bounding box of the user data
 * fits into the world
 * 
 * @return zoom level
 * @since 1.1.0
 */
public int getZoomLevel() {
	Projection projection = getProjection();
	if (projection == null) {
		throw new GeoPackageException(
				"No projection was set which is required to determine the zoom level");
	}
	int zoomLevel = 0;
	BoundingBox boundingBox = getBoundingBox();
	if (boundingBox != null) {
		if (projection.isUnit(Units.DEGREES)) {
			boundingBox = TileBoundingBoxUtils
					.boundDegreesBoundingBoxWithWebMercatorLimits(
							boundingBox);
		}
		ProjectionTransform webMercatorTransform = projection
				.getTransformation(ProjectionConstants.EPSG_WEB_MERCATOR);
		BoundingBox webMercatorBoundingBox = boundingBox
				.transform(webMercatorTransform);
		zoomLevel = TileBoundingBoxUtils
				.getZoomLevel(webMercatorBoundingBox);
	}
	return zoomLevel;
}
 
Example 9
Source Project: geopackage-java   Source File: CoverageData.java    License: MIT License 6 votes vote down vote up
/**
 * Get the tile row results of coverage data tiles needed to create the
 * requested bounding box coverage data, sorted by row and then column
 *
 * @param projectedRequestBoundingBox
 *            bounding box projected to the coverage data
 * @param tileMatrix
 *            tile matrix
 * @return tile results or null
 */
private TileResultSet retrieveSortedTileResults(
		BoundingBox projectedRequestBoundingBox, TileMatrix tileMatrix) {

	TileResultSet tileResults = null;

	if (tileMatrix != null) {

		// Get the tile grid
		TileGrid tileGrid = TileBoundingBoxUtils.getTileGrid(
				coverageBoundingBox, tileMatrix.getMatrixWidth(),
				tileMatrix.getMatrixHeight(), projectedRequestBoundingBox);

		// Query for matching tiles in the tile grid
		tileResults = tileDao.queryByTileGrid(tileGrid,
				tileMatrix.getZoomLevel(), TileTable.COLUMN_TILE_ROW + ","
						+ TileTable.COLUMN_TILE_COLUMN);

	}

	return tileResults;
}
 
Example 10
Source Project: geopackage-java   Source File: FeatureTileGenerator.java    License: 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 11
Source Project: geopackage-java   Source File: FeatureTilesTest.java    License: MIT License 6 votes vote down vote up
private void createTiles(FeatureTiles featureTiles, int zoom) {
	int tilesPerSide = TileBoundingBoxUtils.tilesPerSide(zoom);
	for (int i = 0; i < tilesPerSide; i++) {
		for (int j = 0; j < tilesPerSide; j++) {
			BufferedImage image = featureTiles.drawTile(i, j, zoom);
			if (image != null) {
				long count = featureTiles.queryIndexedFeaturesCount(i, j,
						zoom);
				TestCase.assertTrue(count > 0);
				TestCase.assertEquals(featureTiles.getTileWidth(),
						image.getWidth());
				TestCase.assertEquals(featureTiles.getTileHeight(),
						image.getHeight());
			}
		}
	}
}
 
Example 12
Source Project: geopackage-android-map   Source File: FeatureOverlayQuery.java    License: MIT License 5 votes vote down vote up
/**
 * Determine if the feature overlay is on for the provided zoom level at the location
 *
 * @param zoom   zoom level
 * @param latLng lat lon location
 * @return true if on
 * @since 1.2.6
 */
public boolean isOnAtCurrentZoom(double zoom, LatLng latLng) {

    Point point = new Point(latLng.longitude, latLng.latitude);
    TileGrid tileGrid = TileBoundingBoxUtils.getTileGridFromWGS84(point, (int) zoom);

    boolean on = boundedOverlay.hasTile((int) tileGrid.getMinX(), (int) tileGrid.getMinY(), (int) zoom);
    return on;
}
 
Example 13
Source Project: geopackage-android   Source File: FeatureTiles.java    License: 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 14
Source Project: geopackage-android   Source File: FeatureTiles.java    License: 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 15
Source Project: geopackage-android   Source File: FeatureTiles.java    License: MIT License 5 votes vote down vote up
/**
 * Create an expanded bounding box to handle features outside the tile that
 * overlap
 *
 * @param webMercatorBoundingBox     web mercator bounding box
 * @param tileWebMercatorBoundingBox tile web mercator bounding box
 * @return bounding box
 * @since 3.2.0
 */
public BoundingBox expandBoundingBox(BoundingBox webMercatorBoundingBox, BoundingBox tileWebMercatorBoundingBox) {

    // Create an expanded bounding box to handle features outside the tile
    // that overlap
    double minLongitude = TileBoundingBoxUtils.getLongitudeFromPixel(
            tileWidth, webMercatorBoundingBox, tileWebMercatorBoundingBox, 0 - widthOverlap);
    double maxLongitude = TileBoundingBoxUtils.getLongitudeFromPixel(
            tileWidth, webMercatorBoundingBox, tileWebMercatorBoundingBox, tileWidth + widthOverlap);
    double maxLatitude = TileBoundingBoxUtils.getLatitudeFromPixel(
            tileHeight, webMercatorBoundingBox, tileWebMercatorBoundingBox, 0 - heightOverlap);
    double minLatitude = TileBoundingBoxUtils.getLatitudeFromPixel(
            tileHeight, webMercatorBoundingBox, tileWebMercatorBoundingBox, tileHeight + heightOverlap);

    // Choose the most expanded longitudes and latitudes
    minLongitude = Math.min(minLongitude, webMercatorBoundingBox.getMinLongitude());
    maxLongitude = Math.max(maxLongitude, webMercatorBoundingBox.getMaxLongitude());
    minLatitude = Math.min(minLatitude, webMercatorBoundingBox.getMinLatitude());
    maxLatitude = Math.max(maxLatitude, webMercatorBoundingBox.getMaxLatitude());

    BoundingBox expandedBoundingBox = new BoundingBox(minLongitude,
            minLatitude, maxLongitude, maxLatitude);

    // Bound with the web mercator limits
    expandedBoundingBox = TileBoundingBoxUtils
            .boundWebMercatorBoundingBox(expandedBoundingBox);

    return expandedBoundingBox;
}
 
Example 16
Source Project: geopackage-android   Source File: FeaturePreview.java    License: MIT License 5 votes vote down vote up
/**
 * Draw a preview image
 *
 * @return preview image
 */
public Bitmap draw() {

    Bitmap image = null;

    FeatureDao featureDao = featureTiles.getFeatureDao();
    String table = featureDao.getTableName();

    Projection webMercator = ProjectionFactory
            .getProjection(ProjectionConstants.EPSG_WEB_MERCATOR);

    BoundingBox boundingBox = geoPackage.getFeatureBoundingBox(webMercator,
            table, false);
    if (boundingBox == null) {
        boundingBox = geoPackage.getContentsBoundingBox(webMercator, table);
    }
    if (boundingBox == null && manual) {
        boundingBox = geoPackage.getFeatureBoundingBox(webMercator, table,
                manual);
    }
    if (boundingBox != null) {
        boundingBox = TileBoundingBoxUtils
                .boundWebMercatorBoundingBox(boundingBox);
        BoundingBox expandedBoundingBox = boundingBox
                .squareExpand(bufferPercentage);
        expandedBoundingBox = TileBoundingBoxUtils
                .boundWebMercatorBoundingBox(expandedBoundingBox);
        int zoom = TileBoundingBoxUtils.getZoomLevel(expandedBoundingBox);

        FeatureCursor results = featureDao.query(
                columns.toArray(new String[]{}), where, whereArgs, null,
                null, null, limit != null ? limit.toString() : null);
        image = featureTiles.drawTile(zoom, expandedBoundingBox, results);
    }

    return image;
}
 
Example 17
Source Project: geopackage-android   Source File: TileDao.java    License: MIT License 5 votes vote down vote up
/**
 * Get the bounding box of tiles
 *
 * @param zoomLevel zoom level
 * @return bounding box of zoom level, or null if no tiles
 * @since 1.1.1
 */
public BoundingBox getBoundingBox(long zoomLevel) {
    BoundingBox boundingBox = null;
    TileMatrix tileMatrix = getTileMatrix(zoomLevel);
    if (tileMatrix != null) {
        TileGrid tileGrid = queryForTileGrid(zoomLevel);
        if (tileGrid != null) {
            BoundingBox matrixSetBoundingBox = getBoundingBox();
            boundingBox = TileBoundingBoxUtils.getBoundingBox(
                    matrixSetBoundingBox, tileMatrix, tileGrid);
        }

    }
    return boundingBox;
}
 
Example 18
Source Project: geopackage-android   Source File: TileDao.java    License: MIT License 5 votes vote down vote up
/**
 * Determine if the tiles are in the XYZ tile coordinate format
 *
 * @return true if XYZ tile format
 * @since 3.5.0
 */
public boolean isXYZTiles() {

    // Convert the bounding box to wgs84
    BoundingBox boundingBox = tileMatrixSet.getBoundingBox();
    BoundingBox wgs84BoundingBox = boundingBox.transform(
            projection.getTransformation(
                    ProjectionConstants.EPSG_WORLD_GEODETIC_SYSTEM));

    boolean xyzTiles = false;

    // Verify the bounds are the entire world
    if (wgs84BoundingBox.getMinLatitude() <= ProjectionConstants.WEB_MERCATOR_MIN_LAT_RANGE
            && wgs84BoundingBox.getMaxLatitude() >= ProjectionConstants.WEB_MERCATOR_MAX_LAT_RANGE
            && wgs84BoundingBox.getMinLongitude() <= -ProjectionConstants.WGS84_HALF_WORLD_LON_WIDTH
            && wgs84BoundingBox.getMaxLongitude() >= ProjectionConstants.WGS84_HALF_WORLD_LON_WIDTH) {

        xyzTiles = true;

        // Verify each tile matrix is the correct width and height
        for (TileMatrix tileMatrix : tileMatrices) {
            long zoomLevel = tileMatrix.getZoomLevel();
            long tilesPerSide = TileBoundingBoxUtils
                    .tilesPerSide((int) zoomLevel);
            if (tileMatrix.getMatrixWidth() != tilesPerSide
                    || tileMatrix.getMatrixHeight() != tilesPerSide) {
                xyzTiles = false;
                break;
            }
        }
    }

    return xyzTiles;
}
 
Example 19
Source Project: geopackage-android   Source File: GeoPackageTileRetriever.java    License: 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 20
Source Project: geopackage-android   Source File: GeoPackageExample.java    License: MIT License 5 votes vote down vote up
private static void createFeatureTileLinkExtension(Context context, GeoPackage geoPackage)
        throws SQLException, IOException {

    List<String> featureTables = geoPackage.getFeatureTables();
    for (String featureTable : featureTables) {

        FeatureDao featureDao = geoPackage.getFeatureDao(featureTable);
        FeatureTiles featureTiles = new DefaultFeatureTiles(context, geoPackage, featureDao,
                context.getResources().getDisplayMetrics().density);

        BoundingBox boundingBox = featureDao.getBoundingBox();
        Projection projection = featureDao.getProjection();

        Projection requestProjection = ProjectionFactory
                .getProjection(ProjectionConstants.EPSG_WEB_MERCATOR);
        ProjectionTransform transform = projection
                .getTransformation(requestProjection);
        BoundingBox requestBoundingBox = boundingBox.transform(transform);

        int zoomLevel = TileBoundingBoxUtils
                .getZoomLevel(requestBoundingBox);
        zoomLevel = Math.max(zoomLevel, 8);
        zoomLevel = Math.min(zoomLevel, 19);

        int minZoom = zoomLevel - 8;
        int maxZoom = zoomLevel + 2;

        TileGenerator tileGenerator = new FeatureTileGenerator(context, geoPackage,
                featureTable + "_tiles", featureTiles, minZoom, maxZoom,
                requestBoundingBox, requestProjection);

        tileGenerator.generateTiles();
        featureTiles.close();
    }
}
 
Example 21
Source Project: geopackage-java   Source File: DefaultFeatureTiles.java    License: MIT License 5 votes vote down vote up
/**
 * Get the path of the line string
 *
 * @param simplifyTolerance
 *            simplify tolerance in meters
 * @param boundingBox
 * @param transform
 * @param lineString
 */
private Path2D getPath(double simplifyTolerance, BoundingBox boundingBox,
		ProjectionTransform transform, LineString lineString) {

	Path2D path = null;

	// Try to simplify the number of points in the LineString
	List<Point> lineStringPoints = simplifyPoints(simplifyTolerance,
			lineString.getPoints());

	for (Point point : lineStringPoints) {

		Point projectedPoint = transform.transform(point);

		float x = TileBoundingBoxUtils.getXPixel(tileWidth, boundingBox,
				projectedPoint.getX());
		float y = TileBoundingBoxUtils.getYPixel(tileHeight, boundingBox,
				projectedPoint.getY());

		if (path == null) {
			path = new Path2D.Double();
			path.moveTo(x, y);
		} else {
			path.lineTo(x, y);
		}

	}

	return path;
}
 
Example 22
Source Project: geopackage-java   Source File: FeatureTiles.java    License: 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 23
Source Project: geopackage-java   Source File: FeatureTiles.java    License: MIT License 5 votes vote down vote up
/**
 * Create an expanded bounding box to handle features outside the tile that
 * overlap
 *
 * @param webMercatorBoundingBox
 *            web mercator bounding box
 * @param tileWebMercatorBoundingBox
 *            tile web mercator bounding box
 * @return bounding box
 * @since 3.2.0
 */
public BoundingBox expandBoundingBox(BoundingBox webMercatorBoundingBox,
		BoundingBox tileWebMercatorBoundingBox) {

	// Create an expanded bounding box to handle features outside the tile
	// that overlap
	double minLongitude = TileBoundingBoxUtils.getLongitudeFromPixel(
			tileWidth, webMercatorBoundingBox, tileWebMercatorBoundingBox,
			0 - widthOverlap);
	double maxLongitude = TileBoundingBoxUtils.getLongitudeFromPixel(
			tileWidth, webMercatorBoundingBox, tileWebMercatorBoundingBox,
			tileWidth + widthOverlap);
	double maxLatitude = TileBoundingBoxUtils.getLatitudeFromPixel(
			tileHeight, webMercatorBoundingBox, tileWebMercatorBoundingBox,
			0 - heightOverlap);
	double minLatitude = TileBoundingBoxUtils.getLatitudeFromPixel(
			tileHeight, webMercatorBoundingBox, tileWebMercatorBoundingBox,
			tileHeight + heightOverlap);

	// Choose the most expanded longitudes and latitudes
	minLongitude = Math.min(minLongitude,
			webMercatorBoundingBox.getMinLongitude());
	maxLongitude = Math.max(maxLongitude,
			webMercatorBoundingBox.getMaxLongitude());
	minLatitude = Math.min(minLatitude,
			webMercatorBoundingBox.getMinLatitude());
	maxLatitude = Math.max(maxLatitude,
			webMercatorBoundingBox.getMaxLatitude());

	BoundingBox expandedBoundingBox = new BoundingBox(minLongitude,
			minLatitude, maxLongitude, maxLatitude);

	// Bound with the web mercator limits
	expandedBoundingBox = TileBoundingBoxUtils
			.boundWebMercatorBoundingBox(expandedBoundingBox);

	return expandedBoundingBox;
}
 
Example 24
Source Project: geopackage-java   Source File: FeatureTiles.java    License: 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 25
Source Project: geopackage-java   Source File: FeaturePreview.java    License: MIT License 5 votes vote down vote up
/**
 * Draw a preview image
 * 
 * @return preview image
 */
public BufferedImage draw() {

	BufferedImage image = null;

	FeatureDao featureDao = featureTiles.getFeatureDao();
	String table = featureDao.getTableName();

	Projection webMercator = ProjectionFactory
			.getProjection(ProjectionConstants.EPSG_WEB_MERCATOR);

	BoundingBox boundingBox = geoPackage.getFeatureBoundingBox(webMercator,
			table, false);
	if (boundingBox == null) {
		boundingBox = geoPackage.getContentsBoundingBox(webMercator, table);
	}
	if (boundingBox == null && manual) {
		boundingBox = geoPackage.getFeatureBoundingBox(webMercator, table,
				manual);
	}
	if (boundingBox != null) {
		boundingBox = TileBoundingBoxUtils
				.boundWebMercatorBoundingBox(boundingBox);
		BoundingBox expandedBoundingBox = boundingBox
				.squareExpand(bufferPercentage);
		expandedBoundingBox = TileBoundingBoxUtils
				.boundWebMercatorBoundingBox(expandedBoundingBox);
		int zoom = TileBoundingBoxUtils.getZoomLevel(expandedBoundingBox);

		FeatureResultSet results = featureDao.query(
				columns.toArray(new String[] {}), where, whereArgs, null,
				null, null, limit != null ? limit.toString() : null);
		image = featureTiles.drawTile(zoom, expandedBoundingBox, results);
	}

	return image;
}
 
Example 26
Source Project: geopackage-java   Source File: TileDao.java    License: MIT License 5 votes vote down vote up
/**
 * Get the bounding box of tiles
 * 
 * @param zoomLevel
 *            zoom level
 * @return bounding box of zoom level, or null if no tiles
 * @since 1.1.1
 */
public BoundingBox getBoundingBox(long zoomLevel) {
	BoundingBox boundingBox = null;
	TileMatrix tileMatrix = getTileMatrix(zoomLevel);
	if (tileMatrix != null) {
		TileGrid tileGrid = queryForTileGrid(zoomLevel);
		if (tileGrid != null) {
			BoundingBox matrixSetBoundingBox = getBoundingBox();
			boundingBox = TileBoundingBoxUtils.getBoundingBox(
					matrixSetBoundingBox, tileMatrix, tileGrid);
		}

	}
	return boundingBox;
}
 
Example 27
Source Project: geopackage-java   Source File: TileDao.java    License: MIT License 5 votes vote down vote up
/**
 * Determine if the tiles are in the XYZ tile coordinate format
 * 
 * @return true if XYZ tile format
 * @since 3.5.0
 */
public boolean isXYZTiles() {

	// Convert the bounding box to wgs84
	BoundingBox boundingBox = tileMatrixSet.getBoundingBox();
	BoundingBox wgs84BoundingBox = boundingBox
			.transform(projection.getTransformation(
					ProjectionConstants.EPSG_WORLD_GEODETIC_SYSTEM));

	boolean xyzTiles = false;

	// Verify the bounds are the entire world
	if (wgs84BoundingBox
			.getMinLatitude() <= ProjectionConstants.WEB_MERCATOR_MIN_LAT_RANGE
			&& wgs84BoundingBox
					.getMaxLatitude() >= ProjectionConstants.WEB_MERCATOR_MAX_LAT_RANGE
			&& wgs84BoundingBox
					.getMinLongitude() <= -ProjectionConstants.WGS84_HALF_WORLD_LON_WIDTH
			&& wgs84BoundingBox
					.getMaxLongitude() >= ProjectionConstants.WGS84_HALF_WORLD_LON_WIDTH) {

		xyzTiles = true;

		// Verify each tile matrix is the correct width and height
		for (TileMatrix tileMatrix : tileMatrices) {
			long zoomLevel = tileMatrix.getZoomLevel();
			long tilesPerSide = TileBoundingBoxUtils
					.tilesPerSide((int) zoomLevel);
			if (tileMatrix.getMatrixWidth() != tilesPerSide
					|| tileMatrix.getMatrixHeight() != tilesPerSide) {
				xyzTiles = false;
				break;
			}
		}
	}

	return xyzTiles;
}
 
Example 28
Source Project: geopackage-java   Source File: GeoPackageExample.java    License: MIT License 5 votes vote down vote up
private static void createFeatureTileLinkExtension(GeoPackage geoPackage)
		throws SQLException, IOException {

	List<String> featureTables = geoPackage.getFeatureTables();
	for (String featureTable : featureTables) {

		FeatureDao featureDao = geoPackage.getFeatureDao(featureTable);
		FeatureTiles featureTiles = new DefaultFeatureTiles(geoPackage,
				featureDao);

		BoundingBox boundingBox = featureDao.getBoundingBox();
		Projection projection = featureDao.getProjection();

		Projection requestProjection = ProjectionFactory
				.getProjection(ProjectionConstants.EPSG_WEB_MERCATOR);
		ProjectionTransform transform = projection
				.getTransformation(requestProjection);
		BoundingBox requestBoundingBox = boundingBox.transform(transform);

		int zoomLevel = TileBoundingBoxUtils
				.getZoomLevel(requestBoundingBox);
		zoomLevel = Math.max(zoomLevel, 8);
		zoomLevel = Math.min(zoomLevel, 19);

		int minZoom = zoomLevel - 8;
		int maxZoom = zoomLevel + 2;

		TileGenerator tileGenerator = new FeatureTileGenerator(geoPackage,
				featureTable + "_tiles", featureTiles, minZoom, maxZoom,
				requestBoundingBox, requestProjection);

		tileGenerator.generateTiles();
	}
}
 
Example 29
Source Project: geopackage-java   Source File: UrlTileGeneratorUtils.java    License: MIT License 5 votes vote down vote up
private static BoundingBox getBoundingBox(BoundingBox boundingBox) {
	boundingBox = TileBoundingBoxUtils
			.boundWgs84BoundingBoxWithWebMercatorLimits(boundingBox);
	boundingBox = boundingBox.transform(ProjectionFactory.getProjection(
			ProjectionConstants.EPSG_WORLD_GEODETIC_SYSTEM)
			.getTransformation(ProjectionConstants.EPSG_WEB_MERCATOR));
	return boundingBox;
}
 
Example 30
Source Project: geopackage-android   Source File: CoverageDataTestUtils.java    License: MIT License 4 votes vote down vote up
/**
 * Test the pixel encoding location
 *
 * @param geoPackage GeoPackage
 * @param allowNulls allow nulls
 * @throws Exception
 */
public static void testPixelEncoding(GeoPackage geoPackage,
                                     boolean allowNulls) throws Exception {

    List<String> coverageDataTables = CoverageData.getTables(geoPackage);
    TestCase.assertFalse(coverageDataTables.isEmpty());

    TileMatrixSetDao tileMatrixSetDao = geoPackage.getTileMatrixSetDao();
    TestCase.assertTrue(tileMatrixSetDao.isTableExists());
    TileMatrixDao tileMatrixDao = geoPackage.getTileMatrixDao();
    TestCase.assertTrue(tileMatrixDao.isTableExists());

    for (String coverageTable : coverageDataTables) {

        TileMatrixSet tileMatrixSet = tileMatrixSetDao
                .queryForId(coverageTable);

        TileDao tileDao = geoPackage.getTileDao(tileMatrixSet);
        CoverageData<?> coverageData = CoverageData.getCoverageData(
                geoPackage, tileDao);
        GriddedCoverage griddedCoverage = coverageData.getGriddedCoverage();
        GriddedCoverageEncodingType encoding = griddedCoverage
                .getGridCellEncodingType();

        TileCursor tileCursor = tileDao.queryForTile(tileDao
                .getMaxZoom());
        TestCase.assertNotNull(tileCursor);
        try {
            TestCase.assertTrue(tileCursor.getCount() > 0);
            while (tileCursor.moveToNext()) {
                TileRow tileRow = tileCursor.getRow();

                TileMatrix tileMatrix = tileDao.getTileMatrix(tileRow
                        .getZoomLevel());
                TestCase.assertNotNull(tileMatrix);

                GriddedTile griddedTile = coverageData.getGriddedTile(tileRow
                        .getId());
                TestCase.assertNotNull(griddedTile);

                byte[] tileData = tileRow.getTileData();
                TestCase.assertNotNull(tileData);

                BoundingBox boundingBox = TileBoundingBoxUtils.getBoundingBox(
                        tileMatrixSet.getBoundingBox(), tileMatrix,
                        tileRow.getTileColumn(), tileRow.getTileRow());

                int tileHeight = (int) tileMatrix.getTileHeight();
                int tileWidth = (int) tileMatrix.getTileWidth();

                int heightChunk = Math.max(tileHeight / 10, 1);
                int widthChunk = Math.max(tileWidth / 10, 1);

                for (int y = 0; y < tileHeight; y = Math.min(y + heightChunk,
                        y == tileHeight - 1 ? tileHeight : tileHeight - 1)) {
                    for (int x = 0; x < tileWidth; x = Math.min(x + widthChunk,
                            x == tileWidth - 1 ? tileWidth : tileWidth - 1)) {

                        Double pixelValue = coverageData.getValue(griddedTile,
                                tileData, x, y);
                        double pixelLongitude = boundingBox.getMinLongitude()
                                + (x * tileMatrix.getPixelXSize());
                        double pixelLatitude = boundingBox.getMaxLatitude()
                                - (y * tileMatrix.getPixelYSize());
                        switch (encoding) {
                            case CENTER:
                            case AREA:
                                pixelLongitude += (tileMatrix.getPixelXSize() / 2.0);
                                pixelLatitude -= (tileMatrix.getPixelYSize() / 2.0);
                                break;
                            case CORNER:
                                pixelLatitude -= tileMatrix.getPixelYSize();
                                break;
                        }
                        Double value = coverageData.getValue(pixelLatitude,
                                pixelLongitude);

                        if (!allowNulls || pixelValue != null) {
                            TestCase.assertEquals("x: " + x + ", y: " + y
                                            + ", encoding: " + encoding, pixelValue,
                                    value);
                        }
                    }
                }

                break;
            }
        } finally {
            tileCursor.close();
        }
    }

}