Java Code Examples for mil.nga.geopackage.tiles.matrix.TileMatrix#getPixelXSize()

The following examples show how to use mil.nga.geopackage.tiles.matrix.TileMatrix#getPixelXSize() . 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: TileDaoUtils.java    From geopackage-core-java with MIT License 6 votes vote down vote up
/**
 * Adjust the tile matrix lengths if needed. Check if the tile matrix width
 * and height need to expand to account for pixel * number of pixels fitting
 * into the tile matrix lengths
 * 
 * @param tileMatrixSet
 *            tile matrix set
 * @param tileMatrices
 *            tile matrices
 */
public static void adjustTileMatrixLengths(TileMatrixSet tileMatrixSet,
		List<TileMatrix> tileMatrices) {
	double tileMatrixWidth = tileMatrixSet.getMaxX()
			- tileMatrixSet.getMinX();
	double tileMatrixHeight = tileMatrixSet.getMaxY()
			- tileMatrixSet.getMinY();
	for (TileMatrix tileMatrix : tileMatrices) {
		int tempMatrixWidth = (int) (tileMatrixWidth / (tileMatrix
				.getPixelXSize() * tileMatrix.getTileWidth()));
		int tempMatrixHeight = (int) (tileMatrixHeight / (tileMatrix
				.getPixelYSize() * tileMatrix.getTileHeight()));
		if (tempMatrixWidth > tileMatrix.getMatrixWidth()) {
			tileMatrix.setMatrixWidth(tempMatrixWidth);
		}
		if (tempMatrixHeight > tileMatrix.getMatrixHeight()) {
			tileMatrix.setMatrixHeight(tempMatrixHeight);
		}
	}
}
 
Example 2
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 3
Source File: TileDao.java    From geopackage-android with MIT License 4 votes vote down vote up
/**
 * Constructor
 *
 * @param database      database name
 * @param db            GeoPackage connection
 * @param tileMatrixSet tile matrix set
 * @param tileMatrices  tile matrices
 * @param table         tile table
 */
public TileDao(String database, GeoPackageConnection db, TileMatrixSet tileMatrixSet,
               List<TileMatrix> tileMatrices, TileTable table) {
    super(database, db, new TileConnection(db), table);

    this.tileDb = (TileConnection) getUserDb();
    this.tileMatrixSet = tileMatrixSet;
    this.tileMatrices = tileMatrices;
    this.widths = new double[tileMatrices.size()];
    this.heights = new double[tileMatrices.size()];

    projection = tileMatrixSet.getProjection();

    // Set the min and max zoom levels
    if (!tileMatrices.isEmpty()) {
        minZoom = tileMatrices.get(0).getZoomLevel();
        maxZoom = tileMatrices.get(tileMatrices.size() - 1).getZoomLevel();
    } else {
        minZoom = 0;
        maxZoom = 0;
    }

    // Populate the zoom level to tile matrix and the sorted tile widths and
    // heights
    for (int i = 0; i < tileMatrices.size(); i++) {
        TileMatrix tileMatrix = tileMatrices.get(i);
        zoomLevelToTileMatrix.put(tileMatrix.getZoomLevel(), tileMatrix);
        widths[tileMatrices.size() - i - 1] = tileMatrix.getPixelXSize()
                * tileMatrix.getTileWidth();
        heights[tileMatrices.size() - i - 1] = tileMatrix.getPixelYSize()
                * tileMatrix.getTileHeight();
    }

    if (tileMatrixSet.getContents() == null) {
        throw new GeoPackageException(TileMatrixSet.class.getSimpleName()
                + " " + tileMatrixSet.getId() + " has null "
                + Contents.class.getSimpleName());
    }
    if (tileMatrixSet.getSrs() == null) {
        throw new GeoPackageException(TileMatrixSet.class.getSimpleName()
                + " " + tileMatrixSet.getId() + " has null "
                + SpatialReferenceSystem.class.getSimpleName());
    }

}
 
Example 4
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 5
Source File: TileDao.java    From geopackage-java with MIT License 4 votes vote down vote up
/**
 * Constructor
 * 
 * @param database
 *            database
 * @param db
 *            GeoPackage connection
 * @param tileMatrixSet
 *            tile matrix set
 * @param tileMatrices
 *            tile matrices
 * @param table
 *            tile table
 */
public TileDao(String database, GeoPackageConnection db,
		TileMatrixSet tileMatrixSet, List<TileMatrix> tileMatrices,
		TileTable table) {
	super(database, db, new TileConnection(db), table);

	this.tileDb = (TileConnection) getUserDb();
	this.tileMatrixSet = tileMatrixSet;
	this.tileMatrices = tileMatrices;
	this.widths = new double[tileMatrices.size()];
	this.heights = new double[tileMatrices.size()];

	projection = tileMatrixSet.getProjection();

	// Set the min and max zoom levels
	if (!tileMatrices.isEmpty()) {
		minZoom = tileMatrices.get(0).getZoomLevel();
		maxZoom = tileMatrices.get(tileMatrices.size() - 1).getZoomLevel();
	} else {
		minZoom = 0;
		maxZoom = 0;
	}

	// Populate the zoom level to tile matrix and the sorted tile widths and
	// heights
	for (int i = 0; i < tileMatrices.size(); i++) {
		TileMatrix tileMatrix = tileMatrices.get(i);
		zoomLevelToTileMatrix.put(tileMatrix.getZoomLevel(), tileMatrix);
		widths[tileMatrices.size() - i - 1] = tileMatrix.getPixelXSize()
				* tileMatrix.getTileWidth();
		heights[tileMatrices.size() - i - 1] = tileMatrix.getPixelYSize()
				* tileMatrix.getTileHeight();
	}

	if (tileMatrixSet.getContents() == null) {
		throw new GeoPackageException(TileMatrixSet.class.getSimpleName()
				+ " " + tileMatrixSet.getId() + " has null "
				+ Contents.class.getSimpleName());
	}
	if (tileMatrixSet.getSrs() == null) {
		throw new GeoPackageException(TileMatrixSet.class.getSimpleName()
				+ " " + tileMatrixSet.getId() + " has null "
				+ SpatialReferenceSystem.class.getSimpleName());
	}

}
 
Example 6
Source File: TileUtils.java    From geopackage-android with MIT License 2 votes vote down vote up
/**
 * Test getZoomLevel
 *
 * @param geoPackage GeoPackage
 * @throws SQLException upon error
 */
public static void testGetZoomLevel(GeoPackage geoPackage)
        throws SQLException {

    TileMatrixSetDao tileMatrixSetDao = geoPackage.getTileMatrixSetDao();

    if (tileMatrixSetDao.isTableExists()) {
        List<TileMatrixSet> results = tileMatrixSetDao.queryForAll();

        for (TileMatrixSet tileMatrixSet : results) {

            TileDao dao = geoPackage.getTileDao(tileMatrixSet);

            List<TileMatrix> tileMatrices = dao.getTileMatrices();

            for (TileMatrix tileMatrix : tileMatrices) {

                double width = tileMatrix.getPixelXSize()
                        * tileMatrix.getTileWidth();
                double height = tileMatrix.getPixelYSize()
                        * tileMatrix.getTileHeight();

                long zoomLevel = dao.getZoomLevel(width);
                TestCase.assertEquals(tileMatrix.getZoomLevel(), zoomLevel);

                zoomLevel = dao.getZoomLevel(width, height);
                TestCase.assertEquals(tileMatrix.getZoomLevel(), zoomLevel);

                zoomLevel = dao.getZoomLevel(width + 1);
                TestCase.assertEquals(tileMatrix.getZoomLevel(), zoomLevel);

                zoomLevel = dao.getZoomLevel(width + 1, height + 1);
                TestCase.assertEquals(tileMatrix.getZoomLevel(), zoomLevel);

                zoomLevel = dao.getZoomLevel(width - 1);
                TestCase.assertEquals(tileMatrix.getZoomLevel(), zoomLevel);

                zoomLevel = dao.getZoomLevel(width - 1, height - 1);
                TestCase.assertEquals(tileMatrix.getZoomLevel(), zoomLevel);

            }

        }

    }

}
 
Example 7
Source File: TileUtils.java    From geopackage-java with MIT License 2 votes vote down vote up
/**
 * Test getZoomLevel
 * 
 * @param geoPackage
 *            GeoPackage
 * @throws SQLException
 *             upon error
 */
public static void testGetZoomLevel(GeoPackage geoPackage)
		throws SQLException {

	TileMatrixSetDao tileMatrixSetDao = geoPackage.getTileMatrixSetDao();

	if (tileMatrixSetDao.isTableExists()) {
		List<TileMatrixSet> results = tileMatrixSetDao.queryForAll();

		for (TileMatrixSet tileMatrixSet : results) {

			TileDao dao = geoPackage.getTileDao(tileMatrixSet);

			List<TileMatrix> tileMatrices = dao.getTileMatrices();

			for (TileMatrix tileMatrix : tileMatrices) {

				double width = tileMatrix.getPixelXSize()
						* tileMatrix.getTileWidth();
				double height = tileMatrix.getPixelYSize()
						* tileMatrix.getTileHeight();

				long zoomLevel = dao.getZoomLevel(width);
				TestCase.assertEquals(tileMatrix.getZoomLevel(), zoomLevel);

				zoomLevel = dao.getZoomLevel(width, height);
				TestCase.assertEquals(tileMatrix.getZoomLevel(), zoomLevel);

				zoomLevel = dao.getZoomLevel(width + 1);
				TestCase.assertEquals(tileMatrix.getZoomLevel(), zoomLevel);

				zoomLevel = dao.getZoomLevel(width + 1, height + 1);
				TestCase.assertEquals(tileMatrix.getZoomLevel(), zoomLevel);

				zoomLevel = dao.getZoomLevel(width - 1);
				TestCase.assertEquals(tileMatrix.getZoomLevel(), zoomLevel);

				zoomLevel = dao.getZoomLevel(width - 1, height - 1);
				TestCase.assertEquals(tileMatrix.getZoomLevel(), zoomLevel);

			}

		}

	}

}