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

The following examples show how to use mil.nga.geopackage.BoundingBox#getMaxLongitude() . 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 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 2
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 3
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 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: 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 9
Source File: TileBoundingBoxUtils.java    From geopackage-core-java with MIT License 5 votes vote down vote up
/**
 * Get the X pixel for where the longitude fits into the bounding box
 *
 * @param width
 *            width
 * @param boundingBox
 *            bounding box
 * @param longitude
 *            longitude
 * @return x pixel
 */
public static float getXPixel(long width, BoundingBox boundingBox,
		double longitude) {

	double boxWidth = boundingBox.getMaxLongitude()
			- boundingBox.getMinLongitude();
	double offset = longitude - boundingBox.getMinLongitude();
	double percentage = offset / boxWidth;
	float pixel = (float) (percentage * width);

	return pixel;
}
 
Example 10
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 11
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 12
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 13
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 14
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 15
Source File: CoverageDataTiffImportTest.java    From geopackage-android 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 16
Source File: CoverageDataPngImportTest.java    From geopackage-android 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 17
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 18
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;
}
 
Example 19
Source File: TileBoundingBoxUtils.java    From geopackage-core-java with MIT License 3 votes vote down vote up
/**
 * Get the longitude from the pixel location, bounding box, tile bounding
 * box (when different from bounding box), and image width
 * 
 * @param width
 *            width
 * @param boundingBox
 *            bounding box
 * @param tileBoundingBox
 *            tile bounding box
 * @param pixel
 *            pixel
 * @return longitude
 * @since 3.2.0
 */
public static double getLongitudeFromPixel(long width,
		BoundingBox boundingBox, BoundingBox tileBoundingBox, float pixel) {

	double boxWidth = tileBoundingBox.getMaxLongitude()
			- tileBoundingBox.getMinLongitude();
	double percentage = pixel / width;
	double offset = percentage * boxWidth;
	double longitude = offset + boundingBox.getMinLongitude();

	return longitude;
}
 
Example 20
Source File: TileBoundingBoxUtils.java    From geopackage-core-java with MIT License 3 votes vote down vote up
/**
 * Get the pixel x size for the bounding box with matrix width and tile
 * width
 * 
 * @param webMercatorBoundingBox
 *            web mercator bounding box
 * @param matrixWidth
 *            matrix width
 * @param tileWidth
 *            tile width
 * @return pixel x size
 */
public static double getPixelXSize(BoundingBox webMercatorBoundingBox,
		long matrixWidth, int tileWidth) {
	double pixelXSize = (webMercatorBoundingBox.getMaxLongitude()
			- webMercatorBoundingBox.getMinLongitude()) / matrixWidth
			/ tileWidth;
	return pixelXSize;
}