Java Code Examples for mil.nga.geopackage.BoundingBox#getMaxLatitude()

The following examples show how to use mil.nga.geopackage.BoundingBox#getMaxLatitude() . 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: TileBoundingBoxUtils.java    From geopackage-core-java with MIT License 6 votes vote down vote up
/**
 * Bound the upper and lower bounds of the degrees bounding box with web
 * mercator limits
 * 
 * @param boundingBox
 *            degrees bounding box
 * @return bounding box
 * @since 1.3.1
 */
public static BoundingBox boundDegreesBoundingBoxWithWebMercatorLimits(
		BoundingBox boundingBox) {
	BoundingBox bounded = new BoundingBox(boundingBox);
	if (bounded
			.getMinLatitude() < ProjectionConstants.WEB_MERCATOR_MIN_LAT_RANGE) {
		bounded.setMinLatitude(
				ProjectionConstants.WEB_MERCATOR_MIN_LAT_RANGE);
	}
	if (bounded
			.getMaxLatitude() < ProjectionConstants.WEB_MERCATOR_MIN_LAT_RANGE) {
		bounded.setMaxLatitude(
				ProjectionConstants.WEB_MERCATOR_MIN_LAT_RANGE);
	}
	if (bounded
			.getMaxLatitude() > ProjectionConstants.WEB_MERCATOR_MAX_LAT_RANGE) {
		bounded.setMaxLatitude(
				ProjectionConstants.WEB_MERCATOR_MAX_LAT_RANGE);
	}
	if (bounded
			.getMinLatitude() > ProjectionConstants.WEB_MERCATOR_MAX_LAT_RANGE) {
		bounded.setMinLatitude(
				ProjectionConstants.WEB_MERCATOR_MAX_LAT_RANGE);
	}
	return bounded;
}
 
Example 2
Source File: TileBoundingBoxUtils.java    From geopackage-core-java with MIT License 6 votes vote down vote up
/**
 * Get the tile row of the latitude in constant units
 *
 * @param totalBox
 *            total bounding box
 * @param matrixHeight
 *            matrix height
 * @param latitude
 *            in constant units
 * @return tile row if in the range, -1 if before,
 *         {@link TileMatrix#getMatrixHeight()} if after
 */
public static long getTileRow(BoundingBox totalBox, long matrixHeight,
		double latitude) {

	double minY = totalBox.getMinLatitude();
	double maxY = totalBox.getMaxLatitude();

	long tileId;
	if (latitude <= minY) {
		tileId = matrixHeight;
	} else if (latitude > maxY) {
		tileId = -1;
	} else {
		double matrixHeightMeters = totalBox.getMaxLatitude()
				- totalBox.getMinLatitude();
		double tileHeight = matrixHeightMeters / matrixHeight;
		tileId = (long) ((maxY - latitude) / tileHeight);
	}

	return tileId;
}
 
Example 3
Source File: CoverageData.java    From geopackage-android with MIT License 5 votes vote down vote up
/**
 * Get the tile matrix for the zoom level as defined by the area of the
 * request
 *
 * @param request coverage data request
 * @return tile matrix or null
 */
private TileMatrix getTileMatrix(CoverageDataRequest request) {

    TileMatrix tileMatrix = null;

    // Check if the request overlaps coverage data bounding box
    if (request.overlap(coverageBoundingBox) != null) {

        // Get the tile distance
        BoundingBox projectedBoundingBox = request
                .getProjectedBoundingBox();
        double distanceWidth = projectedBoundingBox.getMaxLongitude()
                - projectedBoundingBox.getMinLongitude();
        double distanceHeight = projectedBoundingBox.getMaxLatitude()
                - projectedBoundingBox.getMinLatitude();

        // Get the zoom level to request based upon the tile size
        Long zoomLevel = tileDao.getClosestZoomLevel(distanceWidth,
                distanceHeight);

        // If there is a matching zoom level
        if (zoomLevel != null) {
            tileMatrix = tileDao.getTileMatrix(zoomLevel);
        }
    }

    return tileMatrix;
}
 
Example 4
Source File: CoverageData.java    From geopackage-java with MIT License 5 votes vote down vote up
/**
 * Get the tile matrix for the zoom level as defined by the area of the
 * request
 *
 * @param request
 *            coverage data request
 * @return tile matrix or null
 */
private TileMatrix getTileMatrix(CoverageDataRequest request) {

	TileMatrix tileMatrix = null;

	// Check if the request overlaps coverage data bounding box
	if (request.overlap(coverageBoundingBox) != null) {

		// Get the tile distance
		BoundingBox projectedBoundingBox = request
				.getProjectedBoundingBox();
		double distanceWidth = projectedBoundingBox.getMaxLongitude()
				- projectedBoundingBox.getMinLongitude();
		double distanceHeight = projectedBoundingBox.getMaxLatitude()
				- projectedBoundingBox.getMinLatitude();

		// Get the zoom level to request based upon the tile size
		Long zoomLevel = tileDao.getClosestZoomLevel(distanceWidth,
				distanceHeight);

		// If there is a matching zoom level
		if (zoomLevel != null) {
			tileMatrix = tileDao.getTileMatrix(zoomLevel);
		}
	}

	return tileMatrix;
}
 
Example 5
Source File: TileBoundingBoxUtils.java    From geopackage-core-java with MIT License 5 votes vote down vote up
/**
 * Get the tile grid that includes the entire tile bounding box
 *
 * @param boundingBox
 *            wgs84 bounding box
 * @param zoom
 *            zoom level
 *
 * @return tile grid
 * @since 1.2.0
 */
public static TileGrid getTileGridWGS84(BoundingBox boundingBox, int zoom) {

	int tilesPerLat = tilesPerWGS84LatSide(zoom);
	int tilesPerLon = tilesPerWGS84LonSide(zoom);

	double tileSizeLat = tileSizeLatPerWGS84Side(tilesPerLat);
	double tileSizeLon = tileSizeLonPerWGS84Side(tilesPerLon);

	int minX = (int) ((boundingBox.getMinLongitude()
			+ ProjectionConstants.WGS84_HALF_WORLD_LON_WIDTH)
			/ tileSizeLon);
	double tempMaxX = (boundingBox.getMaxLongitude()
			+ ProjectionConstants.WGS84_HALF_WORLD_LON_WIDTH) / tileSizeLon;
	int maxX = (int) tempMaxX;
	if (tempMaxX % 1 == 0) {
		maxX--;
	}
	maxX = Math.min(maxX, tilesPerLon - 1);

	int minY = (int) (((boundingBox.getMaxLatitude()
			- ProjectionConstants.WGS84_HALF_WORLD_LAT_HEIGHT) * -1)
			/ tileSizeLat);
	double tempMaxY = ((boundingBox.getMinLatitude()
			- ProjectionConstants.WGS84_HALF_WORLD_LAT_HEIGHT) * -1)
			/ tileSizeLat;
	int maxY = (int) tempMaxY;
	if (tempMaxY % 1 == 0) {
		maxY--;
	}
	maxY = Math.min(maxY, tilesPerLat - 1);

	TileGrid grid = new TileGrid(minX, minY, maxX, maxY);

	return grid;
}
 
Example 6
Source File: TileBoundingBoxUtils.java    From geopackage-core-java with MIT License 5 votes vote down vote up
/**
 * Get the zoom level of where the web mercator bounding box fits into the
 * complete world
 *
 * @param webMercatorBoundingBox
 *            web mercator bounding box
 * @return zoom level
 */
public static int getZoomLevel(BoundingBox webMercatorBoundingBox) {

	double worldLength = ProjectionConstants.WEB_MERCATOR_HALF_WORLD_WIDTH
			* 2;

	double longitudeDistance = webMercatorBoundingBox.getMaxLongitude()
			- webMercatorBoundingBox.getMinLongitude();
	double latitudeDistance = webMercatorBoundingBox.getMaxLatitude()
			- webMercatorBoundingBox.getMinLatitude();

	if (longitudeDistance <= 0) {
		longitudeDistance = Double.MIN_VALUE;
	}
	if (latitudeDistance <= 0) {
		latitudeDistance = Double.MIN_VALUE;
	}

	int widthTiles = (int) (worldLength / longitudeDistance);
	int heightTiles = (int) (worldLength / latitudeDistance);

	int tilesPerSide = Math.min(widthTiles, heightTiles);
	tilesPerSide = Math.max(tilesPerSide, 1);

	int zoom = zoomFromTilesPerSide(tilesPerSide);

	return zoom;
}
 
Example 7
Source File: TileBoundingBoxUtils.java    From geopackage-core-java with MIT License 5 votes vote down vote up
/**
 * Get the bounding box of the tile grid in the tile width and height bounds
 * using the total bounding box with constant units
 * 
 * @param totalBox
 *            total bounding box
 * @param tileMatrixWidth
 *            matrix width
 * @param tileMatrixHeight
 *            matrix height
 * @param tileGrid
 *            tile grid
 * @return bounding box
 * @since 1.2.0
 */
public static BoundingBox getBoundingBox(BoundingBox totalBox,
		long tileMatrixWidth, long tileMatrixHeight, TileGrid tileGrid) {

	// Get the tile width
	double matrixMinX = totalBox.getMinLongitude();
	double matrixMaxX = totalBox.getMaxLongitude();
	double matrixWidth = matrixMaxX - matrixMinX;
	double tileWidth = matrixWidth / tileMatrixWidth;

	// Find the longitude range
	double minLon = matrixMinX + (tileWidth * tileGrid.getMinX());
	double maxLon = matrixMinX + (tileWidth * (tileGrid.getMaxX() + 1));

	// Get the tile height
	double matrixMinY = totalBox.getMinLatitude();
	double matrixMaxY = totalBox.getMaxLatitude();
	double matrixHeight = matrixMaxY - matrixMinY;
	double tileHeight = matrixHeight / tileMatrixHeight;

	// Find the latitude range
	double maxLat = matrixMaxY - (tileHeight * tileGrid.getMinY());
	double minLat = matrixMaxY - (tileHeight * (tileGrid.getMaxY() + 1));

	BoundingBox boundingBox = new BoundingBox(minLon, minLat, maxLon,
			maxLat);

	return boundingBox;
}
 
Example 8
Source File: TileBoundingBoxUtils.java    From geopackage-core-java with MIT License 5 votes vote down vote up
/**
 * Get the tile grid that includes the entire tile bounding box
 *
 * @param webMercatorBoundingBox
 *            web mercator bounding box
 * @param zoom
 *            zoom level
 * @return tile grid
 */
public static TileGrid getTileGrid(BoundingBox webMercatorBoundingBox,
		int zoom) {

	int tilesPerSide = tilesPerSide(zoom);
	double tileSize = tileSize(tilesPerSide);

	int minX = (int) ((webMercatorBoundingBox.getMinLongitude()
			+ ProjectionConstants.WEB_MERCATOR_HALF_WORLD_WIDTH)
			/ tileSize);
	double tempMaxX = (webMercatorBoundingBox.getMaxLongitude()
			+ ProjectionConstants.WEB_MERCATOR_HALF_WORLD_WIDTH) / tileSize;
	int maxX = (int) (tempMaxX
			- ProjectionConstants.WEB_MERCATOR_PRECISION);
	maxX = Math.min(maxX, tilesPerSide - 1);

	int minY = (int) (((webMercatorBoundingBox.getMaxLatitude()
			- ProjectionConstants.WEB_MERCATOR_HALF_WORLD_WIDTH) * -1)
			/ tileSize);
	double tempMaxY = ((webMercatorBoundingBox.getMinLatitude()
			- ProjectionConstants.WEB_MERCATOR_HALF_WORLD_WIDTH) * -1)
			/ tileSize;
	int maxY = (int) (tempMaxY
			- ProjectionConstants.WEB_MERCATOR_PRECISION);
	maxY = Math.min(maxY, tilesPerSide - 1);

	TileGrid grid = new TileGrid(minX, minY, maxX, maxY);

	return grid;
}
 
Example 9
Source File: TileDao.java    From geopackage-java with 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 10
Source File: TileBoundingBoxUtils.java    From geopackage-core-java with MIT License 5 votes vote down vote up
/**
 * Get the Y pixel for where the latitude fits into the bounding box
 *
 * @param height
 *            height
 * @param boundingBox
 *            bounding box
 * @param latitude
 *            latitude
 * @return y pixel
 */
public static float getYPixel(long height, BoundingBox boundingBox,
		double latitude) {

	double boxHeight = boundingBox.getMaxLatitude()
			- boundingBox.getMinLatitude();
	double offset = boundingBox.getMaxLatitude() - latitude;
	double percentage = offset / boxHeight;
	float pixel = (float) (percentage * height);

	return pixel;
}
 
Example 11
Source File: CoverageDataCore.java    From geopackage-core-java with MIT License 5 votes vote down vote up
/**
 * Pad the bounding box with extra space for the overlapping pixels
 * 
 * @param tileMatrix
 *            tile matrix
 * @param boundingBox
 *            bounding box
 * @param overlap
 *            overlapping pixels
 * @return padded bounding box
 */
protected BoundingBox padBoundingBox(TileMatrix tileMatrix,
		BoundingBox boundingBox, int overlap) {
	double lonPixelPadding = tileMatrix.getPixelXSize() * overlap;
	double latPixelPadding = tileMatrix.getPixelYSize() * overlap;
	BoundingBox paddedBoundingBox = new BoundingBox(
			boundingBox.getMinLongitude() - lonPixelPadding,
			boundingBox.getMinLatitude() - latPixelPadding,
			boundingBox.getMaxLongitude() + lonPixelPadding,
			boundingBox.getMaxLatitude() + latPixelPadding);
	return paddedBoundingBox;
}
 
Example 12
Source File: TileWriter.java    From geopackage-java with MIT License 5 votes vote down vote up
/**
 * Get the length of the bounding box
 * 
 * @param boundingBox
 * @return length
 */
private static double getLength(BoundingBox boundingBox) {

	double width = boundingBox.getMaxLongitude()
			- boundingBox.getMinLongitude();
	double height = boundingBox.getMaxLatitude()
			- boundingBox.getMinLatitude();
	double length = Math.min(width, height);

	return length;
}
 
Example 13
Source File: TileDao.java    From geopackage-android with 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 14
Source File: GoogleMapShape.java    From geopackage-android-map with MIT License 5 votes vote down vote up
/**
 * Expand the bounding box by the LatLng
 *
 * @param boundingBox bounding box
 * @param latLng      lat lng
 */
private void expandBoundingBox(BoundingBox boundingBox, LatLng latLng) {

    double latitude = latLng.latitude;
    double longitude = latLng.longitude;

    if (boundingBox.getMinLongitude() <= 3 * ProjectionConstants.WGS84_HALF_WORLD_LON_WIDTH && boundingBox.getMaxLongitude() >= 3 * -ProjectionConstants.WGS84_HALF_WORLD_LON_WIDTH) {
        if (longitude < boundingBox.getMinLongitude()) {
            if (boundingBox.getMinLongitude()
                    - longitude > (longitude + (2 * ProjectionConstants.WGS84_HALF_WORLD_LON_WIDTH)) - boundingBox.getMaxLongitude()) {
                longitude += (2 * ProjectionConstants.WGS84_HALF_WORLD_LON_WIDTH);
            }
        } else if (longitude > boundingBox.getMaxLongitude()) {
            if (longitude - boundingBox.getMaxLongitude() > boundingBox.getMinLongitude()
                    - (longitude - (2 * ProjectionConstants.WGS84_HALF_WORLD_LON_WIDTH))) {
                longitude -= (2 * ProjectionConstants.WGS84_HALF_WORLD_LON_WIDTH);
            }
        }
    }

    if (latitude < boundingBox.getMinLatitude()) {
        boundingBox.setMinLatitude(latitude);
    }
    if (latitude > boundingBox.getMaxLatitude()) {
        boundingBox.setMaxLatitude(latitude);
    }
    if (longitude < boundingBox.getMinLongitude()) {
        boundingBox.setMinLongitude(longitude);
    }
    if (longitude > boundingBox.getMaxLongitude()) {
        boundingBox.setMaxLongitude(longitude);
    }

}
 
Example 15
Source File: CoverageDataCore.java    From geopackage-core-java with MIT License 4 votes vote down vote up
/**
 * Reproject the coverage data to the requested projection
 *
 * @param values
 *            coverage data values
 * @param requestedCoverageWidth
 *            requested coverage data width
 * @param requestedCoverageHeight
 *            requested coverage data height
 * @param requestBoundingBox
 *            request bounding box in the request projection
 * @param transformRequestToCoverage
 *            transformation from request to coverage data
 * @param coverageBoundingBox
 *            coverage data bounding box
 * @return projected coverage data
 */
protected Double[][] reprojectCoverageData(Double[][] values,
		int requestedCoverageWidth, int requestedCoverageHeight,
		BoundingBox requestBoundingBox,
		ProjectionTransform transformRequestToCoverage,
		BoundingBox coverageBoundingBox) {

	final double requestedWidthUnitsPerPixel = (requestBoundingBox
			.getMaxLongitude() - requestBoundingBox.getMinLongitude())
			/ requestedCoverageWidth;
	final double requestedHeightUnitsPerPixel = (requestBoundingBox
			.getMaxLatitude() - requestBoundingBox.getMinLatitude())
			/ requestedCoverageHeight;

	final double tilesDistanceWidth = coverageBoundingBox.getMaxLongitude()
			- coverageBoundingBox.getMinLongitude();
	final double tilesDistanceHeight = coverageBoundingBox.getMaxLatitude()
			- coverageBoundingBox.getMinLatitude();

	final int width = values[0].length;
	final int height = values.length;

	Double[][] projectedValues = new Double[requestedCoverageHeight][requestedCoverageWidth];

	// Retrieve each coverage data value in the unprojected coverage data
	for (int y = 0; y < requestedCoverageHeight; y++) {
		for (int x = 0; x < requestedCoverageWidth; x++) {

			double longitude = requestBoundingBox.getMinLongitude()
					+ (x * requestedWidthUnitsPerPixel);
			double latitude = requestBoundingBox.getMaxLatitude()
					- (y * requestedHeightUnitsPerPixel);
			ProjCoordinate fromCoord = new ProjCoordinate(longitude,
					latitude);
			ProjCoordinate toCoord = transformRequestToCoverage
					.transform(fromCoord);
			double projectedLongitude = toCoord.x;
			double projectedLatitude = toCoord.y;

			int xPixel = (int) Math
					.round(((projectedLongitude - coverageBoundingBox
							.getMinLongitude()) / tilesDistanceWidth)
							* width);
			int yPixel = (int) Math
					.round(((coverageBoundingBox.getMaxLatitude() - projectedLatitude) / tilesDistanceHeight)
							* height);

			xPixel = Math.max(0, xPixel);
			xPixel = Math.min(width - 1, xPixel);

			yPixel = Math.max(0, yPixel);
			yPixel = Math.min(height - 1, yPixel);

			Double coverageData = values[yPixel][xPixel];
			projectedValues[y][x] = coverageData;
		}
	}

	return projectedValues;
}
 
Example 16
Source File: CoverageDataTiffImportTest.java    From geopackage-java with MIT License 4 votes vote down vote up
/**
 * Test 10 random locations and optionally print
 * 
 * @throws Exception
 */
@Test
public void testRandomLocations() throws Exception {

	BoundingBox projectedBoundingBox = null;

	List<String> coverageDataTables = CoverageDataTiff
			.getTables(geoPackage);
	TileMatrixSetDao dao = geoPackage.getTileMatrixSetDao();

	for (String coverageTable : coverageDataTables) {

		TileMatrixSet tileMatrixSet = dao.queryForId(coverageTable);

		BoundingBox boundingBox = tileMatrixSet.getBoundingBox();
		if (PRINT) {
			System.out.println("Min Latitude: "
					+ boundingBox.getMinLatitude());
			System.out.println("Max Latitude: "
					+ boundingBox.getMaxLatitude());
			System.out.println("Min Longitude: "
					+ boundingBox.getMinLongitude());
			System.out.println("Max Longitude: "
					+ boundingBox.getMaxLongitude());
			System.out.println();
		}
		SpatialReferenceSystemDao srsDao = geoPackage
				.getSpatialReferenceSystemDao();
		long srsId = tileMatrixSet.getSrsId();
		SpatialReferenceSystem srs = srsDao.queryForId(srsId);
		Projection projection = srs.getProjection();
		Projection requestProjection = ProjectionFactory
				.getProjection(ProjectionConstants.EPSG_WORLD_GEODETIC_SYSTEM);
		ProjectionTransform coverageToRequest = projection
				.getTransformation(requestProjection);
		projectedBoundingBox = boundingBox.transform(coverageToRequest);

	}
	if (PRINT) {
		System.out.println("Min Latitude: "
				+ projectedBoundingBox.getMinLatitude());
		System.out.println("Max Latitude: "
				+ projectedBoundingBox.getMaxLatitude());
		System.out.println("Min Longitude: "
				+ projectedBoundingBox.getMinLongitude());
		System.out.println("Max Longitude: "
				+ projectedBoundingBox.getMaxLongitude());
		System.out.println();
	}

	double latDistance = projectedBoundingBox.getMaxLatitude()
			- projectedBoundingBox.getMinLatitude();
	double lonDistance = projectedBoundingBox.getMaxLongitude()
			- projectedBoundingBox.getMinLongitude();

	for (int i = 0; i < 10; i++) {

		// Get a random coordinate
		double latitude = latDistance * .9 * Math.random()
				+ projectedBoundingBox.getMinLatitude()
				+ (.05 * latDistance);
		double longitude = lonDistance * .9 * Math.random()
				+ projectedBoundingBox.getMinLongitude()
				+ (.05 * lonDistance);
		testLocation(latitude, longitude);
		if (PRINT) {
			System.out.println();
		}
	}
}
 
Example 17
Source File: CoverageDataTestUtils.java    From geopackage-android with 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();
        }
    }

}
 
Example 18
Source File: CoverageDataPngImportTest.java    From geopackage-java with MIT License 4 votes vote down vote up
/**
 * Test 10 random locations and optionally print
 * 
 * @throws Exception
 */
@Test
public void testRandomLocations() throws Exception {

	BoundingBox projectedBoundingBox = null;

	List<String> coverageDataTables = CoverageDataPng.getTables(geoPackage);
	TileMatrixSetDao dao = geoPackage.getTileMatrixSetDao();

	for (String coverageTable : coverageDataTables) {

		TileMatrixSet tileMatrixSet = dao.queryForId(coverageTable);

		BoundingBox boundingBox = tileMatrixSet.getBoundingBox();
		if (PRINT) {
			System.out.println("Min Latitude: "
					+ boundingBox.getMinLatitude());
			System.out.println("Max Latitude: "
					+ boundingBox.getMaxLatitude());
			System.out.println("Min Longitude: "
					+ boundingBox.getMinLongitude());
			System.out.println("Max Longitude: "
					+ boundingBox.getMaxLongitude());
			System.out.println();
		}
		SpatialReferenceSystemDao srsDao = geoPackage
				.getSpatialReferenceSystemDao();
		long srsId = tileMatrixSet.getSrsId();
		SpatialReferenceSystem srs = srsDao.queryForId(srsId);
		Projection projection = srs.getProjection();
		Projection requestProjection = ProjectionFactory
				.getProjection(ProjectionConstants.EPSG_WORLD_GEODETIC_SYSTEM);
		ProjectionTransform coverageToRequest = projection
				.getTransformation(requestProjection);
		projectedBoundingBox = boundingBox.transform(coverageToRequest);

	}
	if (PRINT) {
		System.out.println("Min Latitude: "
				+ projectedBoundingBox.getMinLatitude());
		System.out.println("Max Latitude: "
				+ projectedBoundingBox.getMaxLatitude());
		System.out.println("Min Longitude: "
				+ projectedBoundingBox.getMinLongitude());
		System.out.println("Max Longitude: "
				+ projectedBoundingBox.getMaxLongitude());
		System.out.println();
	}

	double latDistance = projectedBoundingBox.getMaxLatitude()
			- projectedBoundingBox.getMinLatitude();
	double lonDistance = projectedBoundingBox.getMaxLongitude()
			- projectedBoundingBox.getMinLongitude();

	for (int i = 0; i < 10; i++) {

		// Get a random coordinate
		double latitude = latDistance * .9 * Math.random()
				+ projectedBoundingBox.getMinLatitude()
				+ (.05 * latDistance);
		double longitude = lonDistance * .9 * Math.random()
				+ projectedBoundingBox.getMinLongitude()
				+ (.05 * lonDistance);
		testLocation(latitude, longitude);
		if (PRINT) {
			System.out.println();
		}
	}
}
 
Example 19
Source File: TileCreator.java    From geopackage-android with MIT License 4 votes vote down vote up
/**
 * Reproject the tile to the requested projection
 *
 * @param tile                    tile in the tile matrix projection
 * @param requestedTileWidth      requested tile width
 * @param requestedTileHeight     requested tile height
 * @param requestBoundingBox      request bounding box in the request projection
 * @param transformRequestToTiles transformation from request to tiles
 * @param tilesBoundingBox        request bounding box in the tile matrix projection
 * @return projected tile
 */
private Bitmap reprojectTile(Bitmap tile, int requestedTileWidth, int requestedTileHeight, BoundingBox requestBoundingBox, ProjectionTransform transformRequestToTiles, BoundingBox tilesBoundingBox) {

    final double requestedWidthUnitsPerPixel = (requestBoundingBox.getMaxLongitude() - requestBoundingBox.getMinLongitude()) / requestedTileWidth;
    final double requestedHeightUnitsPerPixel = (requestBoundingBox.getMaxLatitude() - requestBoundingBox.getMinLatitude()) / requestedTileHeight;

    final double tilesDistanceWidth = tilesBoundingBox.getMaxLongitude() - tilesBoundingBox.getMinLongitude();
    final double tilesDistanceHeight = tilesBoundingBox.getMaxLatitude() - tilesBoundingBox.getMinLatitude();

    final int width = tile.getWidth();
    final int height = tile.getHeight();

    // Tile pixels of the tile matrix tiles
    int[] pixels = new int[width * height];
    tile.getPixels(pixels, 0, width, 0, 0, width, height);

    // Projected tile pixels to draw the reprojected tile
    int[] projectedPixels = new int[requestedTileWidth * requestedTileHeight];

    // Retrieve each pixel in the new tile from the unprojected tile
    for (int y = 0; y < requestedTileHeight; y++) {
        for (int x = 0; x < requestedTileWidth; x++) {

            double longitude = requestBoundingBox.getMinLongitude() + (x * requestedWidthUnitsPerPixel);
            double latitude = requestBoundingBox.getMaxLatitude() - (y * requestedHeightUnitsPerPixel);
            ProjCoordinate fromCoord = new ProjCoordinate(longitude, latitude);
            ProjCoordinate toCoord = transformRequestToTiles.transform(fromCoord);
            double projectedLongitude = toCoord.x;
            double projectedLatitude = toCoord.y;

            int xPixel = (int) Math.round(((projectedLongitude - tilesBoundingBox.getMinLongitude()) / tilesDistanceWidth) * width);
            int yPixel = (int) Math.round(((tilesBoundingBox.getMaxLatitude() - projectedLatitude) / tilesDistanceHeight) * height);

            xPixel = Math.max(0, xPixel);
            xPixel = Math.min(width - 1, xPixel);

            yPixel = Math.max(0, yPixel);
            yPixel = Math.min(height - 1, yPixel);

            int color = pixels[(yPixel * width) + xPixel];
            projectedPixels[(y * requestedTileWidth) + x] = color;
        }
    }

    // Draw the new tile bitmap
    Bitmap projectedTileBitmap = Bitmap.createBitmap(requestedTileWidth,
            requestedTileHeight, tile.getConfig());
    projectedTileBitmap.setPixels(projectedPixels, 0, requestedTileWidth, 0, 0, requestedTileWidth, requestedTileHeight);

    return projectedTileBitmap;
}
 
Example 20
Source File: TileCreator.java    From geopackage-java with MIT License 4 votes vote down vote up
/**
 * Reproject the tile to the requested projection
 *
 * @param tile
 *            tile in the tile matrix projection
 * @param requestedTileWidth
 *            requested tile width
 * @param requestedTileHeight
 *            requested tile height
 * @param requestBoundingBox
 *            request bounding box in the request projection
 * @param transformRequestToTiles
 *            transformation from request to tiles
 * @param tilesBoundingBox
 *            request bounding box in the tile matrix projection
 * @return projected tile
 */
private BufferedImage reprojectTile(BufferedImage tile,
		int requestedTileWidth, int requestedTileHeight,
		BoundingBox requestBoundingBox,
		ProjectionTransform transformRequestToTiles,
		BoundingBox tilesBoundingBox) {

	final double requestedWidthUnitsPerPixel = (requestBoundingBox
			.getMaxLongitude() - requestBoundingBox.getMinLongitude())
			/ requestedTileWidth;
	final double requestedHeightUnitsPerPixel = (requestBoundingBox
			.getMaxLatitude() - requestBoundingBox.getMinLatitude())
			/ requestedTileHeight;

	final double tilesDistanceWidth = tilesBoundingBox.getMaxLongitude()
			- tilesBoundingBox.getMinLongitude();
	final double tilesDistanceHeight = tilesBoundingBox.getMaxLatitude()
			- tilesBoundingBox.getMinLatitude();

	final int width = tile.getWidth();
	final int height = tile.getHeight();

	// Tile pixels of the tile matrix tiles
	int[] pixels = new int[width * height];
	tile.getRGB(0, 0, width, height, pixels, 0, width);

	// Projected tile pixels to draw the reprojected tile
	int[] projectedPixels = new int[requestedTileWidth
			* requestedTileHeight];

	// Retrieve each pixel in the new tile from the unprojected tile
	for (int y = 0; y < requestedTileHeight; y++) {
		for (int x = 0; x < requestedTileWidth; x++) {

			double longitude = requestBoundingBox.getMinLongitude()
					+ (x * requestedWidthUnitsPerPixel);
			double latitude = requestBoundingBox.getMaxLatitude()
					- (y * requestedHeightUnitsPerPixel);
			ProjCoordinate fromCoord = new ProjCoordinate(longitude,
					latitude);
			ProjCoordinate toCoord = transformRequestToTiles
					.transform(fromCoord);
			double projectedLongitude = toCoord.x;
			double projectedLatitude = toCoord.y;

			int xPixel = (int) Math
					.round(((projectedLongitude - tilesBoundingBox
							.getMinLongitude()) / tilesDistanceWidth)
							* width);
			int yPixel = (int) Math
					.round(((tilesBoundingBox.getMaxLatitude() - projectedLatitude) / tilesDistanceHeight)
							* height);

			xPixel = Math.max(0, xPixel);
			xPixel = Math.min(width - 1, xPixel);

			yPixel = Math.max(0, yPixel);
			yPixel = Math.min(height - 1, yPixel);

			int color = pixels[(yPixel * width) + xPixel];
			projectedPixels[(y * requestedTileWidth) + x] = color;
		}
	}

	// Draw the new image
	BufferedImage projectedTileImage = new BufferedImage(
			requestedTileWidth, requestedTileHeight, tile.getType());
	projectedTileImage.setRGB(0, 0, requestedTileWidth,
			requestedTileHeight, projectedPixels, 0, requestedTileWidth);

	return projectedTileImage;
}