mil.nga.geopackage.tiles.user.TileRow Java Examples

The following examples show how to use mil.nga.geopackage.tiles.user.TileRow. 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: XYZGeoPackageTileRetriever.java    From geopackage-android with MIT License 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public GeoPackageTile getTile(int x, int y, int zoom) {

    GeoPackageTile tile = null;

    TileRow tileRow = retrieveTileRow(x, y, zoom);
    if (tileRow != null) {
        TileMatrix tileMatrix = tileDao.getTileMatrix(zoom);
        int tileWidth = (int) tileMatrix.getTileWidth();
        int tileHeight = (int) tileMatrix.getTileHeight();
        tile = new GeoPackageTile(tileWidth, tileHeight, tileRow.getTileData());
    }

    return tile;
}
 
Example #2
Source File: TileUtils.java    From geopackage-java with MIT License 5 votes vote down vote up
/**
 * Test delete
 * 
 * @param geoPackage
 *            GeoPackage
 * @throws SQLException
 *             upon error
 */
public static void testDelete(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);
			TestCase.assertNotNull(dao);

			TileResultSet cursor = dao.queryForAll();
			int count = cursor.getCount();
			if (count > 0) {

				// Choose random tile
				int random = (int) (Math.random() * count);
				cursor.moveToPosition(random);

				TileRow tileRow = cursor.getRow();
				cursor.close();

				// Delete row
				TestCase.assertEquals(1, dao.delete(tileRow));

				// Verify deleted
				TileRow queryTileRow = dao.queryForIdRow(tileRow.getId());
				TestCase.assertNull(queryTileRow);
				cursor = dao.queryForAll();
				TestCase.assertEquals(count - 1, cursor.getCount());
				cursor.close();
			}
			cursor.close();
		}

	}
}
 
Example #3
Source File: CoverageDataPng.java    From geopackage-java with MIT License 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public double getValue(GriddedTile griddedTile, TileRow tileRow, int x,
		int y) {
	BufferedImage image = null;
	try {
		image = tileRow.getTileDataImage();
	} catch (IOException e) {
		throw new GeoPackageException(
				"Failed to get the Tile Row Data Image", e);
	}
	double value = getValue(griddedTile, image, x, y);
	return value;
}
 
Example #4
Source File: CoverageDataTiffImage.java    From geopackage-java with MIT License 5 votes vote down vote up
/**
 * Constructor, used for reading a TIFF
 *
 * @param tileRow
 *            tile row
 */
public CoverageDataTiffImage(TileRow tileRow) {
	imageBytes = tileRow.getTileData();
	TIFFImage tiffImage = TiffReader.readTiff(imageBytes);
	directory = tiffImage.getFileDirectory();
	CoverageDataTiff.validateImageType(directory);
	width = directory.getImageWidth().intValue();
	height = directory.getImageHeight().intValue();
}
 
Example #5
Source File: CoverageDataPng.java    From geopackage-android with MIT License 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public double getValue(GriddedTile griddedTile, TileRow tileRow,
                       int x, int y) {
    byte[] imageBytes = tileRow.getTileData();
    double value = getValue(griddedTile, imageBytes, x, y);
    return value;
}
 
Example #6
Source File: CoverageDataTiffImage.java    From geopackage-android with MIT License 5 votes vote down vote up
/**
 * Constructor, used for reading a TIFF
 *
 * @param tileRow tile row
 */
public CoverageDataTiffImage(TileRow tileRow) {
    imageBytes = tileRow.getTileData();
    TIFFImage tiffImage = TiffReader.readTiff(imageBytes);
    directory = tiffImage.getFileDirectory();
    CoverageDataTiff.validateImageType(directory);
    width = directory.getImageWidth().intValue();
    height = directory.getImageHeight().intValue();
}
 
Example #7
Source File: CoverageDataPngImage.java    From geopackage-android with MIT License 5 votes vote down vote up
/**
 * Constructor, used for reading a PNG
 *
 * @param tileRow tile row
 */
public CoverageDataPngImage(TileRow tileRow) {
    imageBytes = tileRow.getTileData();
    reader = new PngReaderInt(new ByteArrayInputStream(imageBytes));
    CoverageDataPng.validateImageType(reader);
    width = reader.imgInfo.cols;
    height = reader.imgInfo.rows;
}
 
Example #8
Source File: CoverageDataPngImage.java    From geopackage-java with MIT License 5 votes vote down vote up
/**
 * Constructor
 * 
 * @param tileRow
 *            tile row
 */
public CoverageDataPngImage(TileRow tileRow) {
	try {
		image = tileRow.getTileDataImage();
	} catch (IOException e) {
		throw new GeoPackageException(
				"Failed to get the Tile Row Data Image", e);
	}
	raster = image.getRaster();
}
 
Example #9
Source File: CoverageDataTiff.java    From geopackage-android with MIT License 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public double getValue(GriddedTile griddedTile, TileRow tileRow,
                       int x, int y) {
    byte[] imageBytes = tileRow.getTileData();
    double value = getValue(griddedTile, imageBytes, x, y);
    return value;
}
 
Example #10
Source File: CoverageDataTiff.java    From geopackage-java with MIT License 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public double getValue(GriddedTile griddedTile, TileRow tileRow, int x,
		int y) {
	byte[] imageBytes = tileRow.getTileData();
	double value = getValue(griddedTile, imageBytes, x, y);
	return value;
}
 
Example #11
Source File: CoverageDataPng.java    From geopackage-java with MIT License 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public CoverageDataPngImage createImage(TileRow tileRow) {
	return new CoverageDataPngImage(tileRow);
}
 
Example #12
Source File: TileUtils.java    From geopackage-android with MIT License 4 votes vote down vote up
/**
 * Test delete
 *
 * @param geoPackage GeoPackage
 * @throws SQLException upon error
 */
public static void testDelete(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);
            TestCase.assertNotNull(dao);

            TileCursor cursor = dao.queryForAll();
            int count = cursor.getCount();
            if (count > 0) {

                // Choose random tile
                int random = (int) (Math.random() * count);
                cursor.moveToPosition(random);

                TileRow tileRow = cursor.getRow();
                cursor.close();

                // Delete row
                try {
                    TestCase.assertEquals(1, dao.delete(tileRow));
                } catch (SQLiteException e) {
                    if (TestUtils.isFutureSQLiteException(e)) {
                        continue;
                    } else {
                        throw e;
                    }
                }

                // Verify deleted
                TileRow queryTileRow = dao.queryForIdRow(tileRow.getId());
                TestCase.assertNull(queryTileRow);
                cursor = dao.queryForAll();
                TestCase.assertEquals(count - 1, cursor.getCount());
                cursor.close();
            }
            cursor.close();
        }

    }
}
 
Example #13
Source File: CoverageDataTiff.java    From geopackage-java with MIT License 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public CoverageDataTiffImage createImage(TileRow tileRow) {
	return new CoverageDataTiffImage(tileRow);
}
 
Example #14
Source File: TileCreator.java    From geopackage-java with MIT License 4 votes vote down vote up
/**
 * Draw the tile from the tile results
 *
 * @param tileMatrix
 * @param tileResults
 * @param requestProjectedBoundingBox
 * @param tileWidth
 * @param tileHeight
 * @return tile bitmap
 */
private GeoPackageTile drawTile(TileMatrix tileMatrix,
		TileResultSet tileResults, BoundingBox requestProjectedBoundingBox,
		int tileWidth, int tileHeight) {

	// Draw the resulting bitmap with the matching tiles
	GeoPackageTile geoPackageTile = null;
	Graphics graphics = null;
	while (tileResults.moveToNext()) {

		// Get the next tile
		TileRow tileRow = tileResults.getRow();
		BufferedImage tileDataImage;
		try {
			tileDataImage = tileRow.getTileDataImage();
		} catch (IOException e) {
			throw new GeoPackageException(
					"Failed to read the tile row image data", e);
		}

		// Get the bounding box of the tile
		BoundingBox tileBoundingBox = TileBoundingBoxUtils.getBoundingBox(
				tileSetBoundingBox, tileMatrix, tileRow.getTileColumn(),
				tileRow.getTileRow());

		// Get the bounding box where the requested image and
		// tile overlap
		BoundingBox overlap = requestProjectedBoundingBox
				.overlap(tileBoundingBox);

		// If the tile overlaps with the requested box
		if (overlap != null) {

			// Get the rectangle of the tile image to draw
			ImageRectangle src = TileBoundingBoxJavaUtils.getRectangle(
					tileMatrix.getTileWidth(), tileMatrix.getTileHeight(),
					tileBoundingBox, overlap);

			// Get the rectangle of where to draw the tile in
			// the resulting image
			ImageRectangle dest = TileBoundingBoxJavaUtils.getRectangle(
					tileWidth, tileHeight, requestProjectedBoundingBox,
					overlap);

			if (src.isValid() && dest.isValid()) {

				if (imageFormat != null) {

					// Create the bitmap first time through
					if (geoPackageTile == null) {
						BufferedImage bufferedImage = ImageUtils
								.createBufferedImage(tileWidth, tileHeight,
										imageFormat);
						graphics = bufferedImage.getGraphics();
						geoPackageTile = new GeoPackageTile(tileWidth,
								tileHeight, bufferedImage);
					}

					// Draw the tile to the image
					graphics.drawImage(tileDataImage, dest.getLeft(),
							dest.getTop(), dest.getRight(),
							dest.getBottom(), src.getLeft(), src.getTop(),
							src.getRight(), src.getBottom(), null);
				} else {

					// Verify only one image was found and
					// it lines up perfectly
					if (geoPackageTile != null || !src.equals(dest)) {
						throw new GeoPackageException(
								"Raw image only supported when the images are aligned with the tile format requiring no combining and cropping");
					}

					geoPackageTile = new GeoPackageTile(tileWidth,
							tileHeight, tileRow.getTileData());
				}
			}
		}
	}

	// Check if the entire image is transparent
	if (geoPackageTile != null && geoPackageTile.getImage() != null
			&& ImageUtils.isFullyTransparent(geoPackageTile.getImage())) {
		geoPackageTile = null;
	}

	return geoPackageTile;
}
 
Example #15
Source File: TestUtils.java    From geopackage-java with MIT License 4 votes vote down vote up
/**
 * Add rows to the tile table
 *
 * @param geoPackage
 * @param tileMatrix
 * @param tileData
 */
public static void addRowsToTileTable(GeoPackage geoPackage,
		TileMatrix tileMatrix, byte[] tileData) {

	TileDao dao = geoPackage.getTileDao(tileMatrix.getTableName());

	for (int column = 0; column < tileMatrix.getMatrixWidth(); column++) {

		for (int row = 0; row < tileMatrix.getMatrixHeight(); row++) {

			TileRow newRow = dao.newRow();

			newRow.setZoomLevel(tileMatrix.getZoomLevel());
			newRow.setTileColumn(column);
			newRow.setTileRow(row);
			newRow.setTileData(tileData);

			dao.create(newRow);
		}

	}

}
 
Example #16
Source File: GeoPackageExample.java    From geopackage-java with MIT License 4 votes vote down vote up
private static void createCoverageDataPngExtension(GeoPackage geoPackage)
		throws SQLException {

	BoundingBox bbox = new BoundingBox(-11667347.997449303,
			4824705.2253603265, -11666125.00499674, 4825928.217812888);

	int contentsEpsg = ProjectionConstants.EPSG_WEB_MERCATOR;
	int tileMatrixSetEpsg = ProjectionConstants.EPSG_WEB_MERCATOR;

	SpatialReferenceSystemDao srsDao = geoPackage
			.getSpatialReferenceSystemDao();
	srsDao.getOrCreateFromEpsg(
			ProjectionConstants.EPSG_WORLD_GEODETIC_SYSTEM_GEOGRAPHICAL_3D);

	SpatialReferenceSystem contentsSrs = srsDao
			.getOrCreateFromEpsg(contentsEpsg);
	SpatialReferenceSystem tileMatrixSetSrs = srsDao
			.getOrCreateFromEpsg(tileMatrixSetEpsg);

	ProjectionTransform transform = tileMatrixSetSrs.getProjection()
			.getTransformation(contentsSrs.getProjection());
	BoundingBox contentsBoundingBox = bbox;
	if (!transform.isSameProjection()) {
		contentsBoundingBox = bbox.transform(transform);
	}

	CoverageDataPng coverageData = CoverageDataPng
			.createTileTableWithMetadata(geoPackage, "coverage_png",
					contentsBoundingBox, contentsSrs.getId(), bbox,
					tileMatrixSetSrs.getId());
	TileDao tileDao = coverageData.getTileDao();
	TileMatrixSet tileMatrixSet = coverageData.getTileMatrixSet();

	GriddedCoverageDao griddedCoverageDao = coverageData
			.getGriddedCoverageDao();

	GriddedCoverage griddedCoverage = new GriddedCoverage();
	griddedCoverage.setTileMatrixSet(tileMatrixSet);
	griddedCoverage.setDataType(GriddedCoverageDataType.INTEGER);
	griddedCoverage
			.setDataNull(new Double(Short.MAX_VALUE - Short.MIN_VALUE));
	griddedCoverage
			.setGridCellEncodingType(GriddedCoverageEncodingType.CENTER);
	griddedCoverageDao.create(griddedCoverage);

	GriddedTileDao griddedTileDao = coverageData.getGriddedTileDao();

	int width = 1;
	int height = 1;
	int tileWidth = 3;
	int tileHeight = 3;

	short[][] tilePixels = new short[tileHeight][tileWidth];

	tilePixels[0][0] = (short) 1661.95;
	tilePixels[0][1] = (short) 1665.40;
	tilePixels[0][2] = (short) 1668.19;
	tilePixels[1][0] = (short) 1657.18;
	tilePixels[1][1] = (short) 1663.39;
	tilePixels[1][2] = (short) 1669.65;
	tilePixels[2][0] = (short) 1654.78;
	tilePixels[2][1] = (short) 1660.31;
	tilePixels[2][2] = (short) 1666.44;

	byte[] imageBytes = coverageData.drawTileData(tilePixels);

	TileMatrixDao tileMatrixDao = geoPackage.getTileMatrixDao();

	TileMatrix tileMatrix = new TileMatrix();
	tileMatrix.setContents(tileMatrixSet.getContents());
	tileMatrix.setMatrixHeight(height);
	tileMatrix.setMatrixWidth(width);
	tileMatrix.setTileHeight(tileHeight);
	tileMatrix.setTileWidth(tileWidth);
	tileMatrix
			.setPixelXSize((bbox.getMaxLongitude() - bbox.getMinLongitude())
					/ width / tileWidth);
	tileMatrix.setPixelYSize((bbox.getMaxLatitude() - bbox.getMinLatitude())
			/ height / tileHeight);
	tileMatrix.setZoomLevel(15);
	tileMatrixDao.create(tileMatrix);

	TileRow tileRow = tileDao.newRow();
	tileRow.setTileColumn(0);
	tileRow.setTileRow(0);
	tileRow.setZoomLevel(tileMatrix.getZoomLevel());
	tileRow.setTileData(imageBytes);

	long tileId = tileDao.create(tileRow);

	GriddedTile griddedTile = new GriddedTile();
	griddedTile.setContents(tileMatrixSet.getContents());
	griddedTile.setTableId(tileId);

	griddedTileDao.create(griddedTile);

}
 
Example #17
Source File: GeoPackageExample.java    From geopackage-java with MIT License 4 votes vote down vote up
private static void createRelatedTablesTilesExtension(
		GeoPackage geoPackage) {

	String featureTable = "point2";
	String tileTable = "nga";

	RelatedTablesExtension relatedTables = new RelatedTablesExtension(
			geoPackage);

	List<UserCustomColumn> additionalMappingColumns = RelatedTablesUtils
			.createAdditionalUserColumns();

	UserMappingTable userMappingTable = UserMappingTable.create(
			featureTable + "_" + tileTable, additionalMappingColumns);
	ExtendedRelation relation = relatedTables.addTilesRelationship(
			featureTable, tileTable, userMappingTable);

	UserMappingDao userMappingDao = relatedTables.getMappingDao(relation);

	FeatureDao featureDao = geoPackage
			.getFeatureDao(relation.getBaseTableName());
	TileDao tileDao = geoPackage.getTileDao(relation.getRelatedTableName());

	FeatureResultSet featureResultSet = featureDao.queryForAll();
	while (featureResultSet.moveToNext()) {

		FeatureRow featureRow = featureResultSet.getRow();
		String featureName = featureRow.getValue(TEXT_COLUMN).toString();

		TileResultSet tileResultSet = tileDao
				.queryForTile(tileDao.getMinZoom());
		while (tileResultSet.moveToNext()) {

			TileRow tileRow = tileResultSet.getRow();

			UserMappingRow userMappingRow = userMappingDao.newRow();
			userMappingRow.setBaseId(featureRow.getId());
			userMappingRow.setRelatedId(tileRow.getId());
			RelatedTablesUtils.populateUserRow(userMappingDao.getTable(),
					userMappingRow, UserMappingTable.requiredColumns());
			DublinCoreMetadata.setValue(userMappingRow,
					DublinCoreType.TITLE, featureName);
			DublinCoreMetadata.setValue(userMappingRow,
					DublinCoreType.DESCRIPTION, featureName);
			DublinCoreMetadata.setValue(userMappingRow,
					DublinCoreType.SOURCE, featureName);
			userMappingDao.create(userMappingRow);
		}
		tileResultSet.close();

	}
	featureResultSet.close();

}
 
Example #18
Source File: TileUtils.java    From geopackage-java with MIT License 4 votes vote down vote up
/**
 * Validate a tile row
 * 
 * @param dao
 * @param columns
 * @param tileRow
 */
private static void validateTileRow(TileDao dao, String[] columns,
		TileRow tileRow) {
	TestCase.assertEquals(columns.length, tileRow.columnCount());

	for (int i = 0; i < tileRow.columnCount(); i++) {
		TileColumn column = tileRow.getTable().getColumns().get(i);
		TestCase.assertEquals(i, column.getIndex());
		TestCase.assertEquals(columns[i], tileRow.getColumnName(i));
		TestCase.assertEquals(i, tileRow.getColumnIndex(columns[i]));
		int rowType = tileRow.getRowColumnType(i);
		Object value = tileRow.getValue(i);

		switch (rowType) {

		case ResultUtils.FIELD_TYPE_INTEGER:
			TestUtils.validateIntegerValue(value, column.getDataType());
			break;

		case ResultUtils.FIELD_TYPE_FLOAT:
			TestUtils.validateFloatValue(value, column.getDataType());
			break;

		case ResultUtils.FIELD_TYPE_STRING:
			TestCase.assertTrue(value instanceof String);
			break;

		case ResultUtils.FIELD_TYPE_BLOB:
			TestCase.assertTrue(value instanceof byte[]);
			break;

		case ResultUtils.FIELD_TYPE_NULL:
			TestCase.assertNull(value);
			break;

		}
	}

	TestCase.assertTrue(tileRow.getId() >= 0);
	TestCase.assertTrue(tileRow.getZoomLevel() >= 0);
	TestCase.assertTrue(tileRow.getTileColumn() >= 0);
	TestCase.assertTrue(tileRow.getTileRow() >= 0);
	byte[] tileData = tileRow.getTileData();
	TestCase.assertNotNull(tileData);
	TestCase.assertTrue(tileData.length > 0);

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

}
 
Example #19
Source File: UrlTileGeneratorUtils.java    From geopackage-java with MIT License 4 votes vote down vote up
/**
 * Test generating tiles
 * 
 * @param tileGenerator
 * @throws SQLException
 * @throws IOException
 */
private static void testGenerateTiles(TileGenerator tileGenerator)
		throws SQLException, IOException {

	GeoPackage geoPackage = tileGenerator.getGeoPackage();
	String tableName = tileGenerator.getTableName();
	int minZoom = tileGenerator.getMinZoom();
	int maxZoom = tileGenerator.getMaxZoom();
	BoundingBox webMercatorBoundingBox = tileGenerator.getBoundingBox();

	TestGeoPackageProgress progress = new TestGeoPackageProgress();
	tileGenerator.setProgress(progress);

	int count = tileGenerator.generateTiles();

	long expected = expectedTiles(webMercatorBoundingBox, minZoom, maxZoom);
	TestCase.assertEquals(expected, count);
	TestCase.assertEquals(expected, progress.getProgress());

	TileDao tileDao = geoPackage.getTileDao(tableName);
	TestCase.assertEquals(expected, tileDao.count());
	TestCase.assertEquals(minZoom, tileDao.getMinZoom());
	TestCase.assertEquals(maxZoom, tileDao.getMaxZoom());

	BoundingBox tileMatrixSetBoundingBox = tileDao.getBoundingBox();

	for (int zoom = minZoom; zoom <= maxZoom; zoom++) {
		TileGrid expectedTileGrid = TileBoundingBoxUtils.getTileGrid(
				webMercatorBoundingBox, zoom);
		BoundingBox expectedBoundingBox = TileBoundingBoxUtils
				.getWebMercatorBoundingBox(expectedTileGrid, zoom);
		BoundingBox zoomBoundingBox = tileDao.getBoundingBox(zoom);
		TestCase.assertEquals(expectedBoundingBox.getMinLongitude(),
				zoomBoundingBox.getMinLongitude(), .000001);
		TestCase.assertEquals(expectedBoundingBox.getMaxLongitude(),
				zoomBoundingBox.getMaxLongitude(), .000001);
		TestCase.assertEquals(expectedBoundingBox.getMinLatitude(),
				zoomBoundingBox.getMinLatitude(), .000001);
		TestCase.assertEquals(expectedBoundingBox.getMaxLatitude(),
				zoomBoundingBox.getMaxLatitude(), .000001);
		long expectedZoomTiles = expectedTiles(webMercatorBoundingBox, zoom);
		TestCase.assertEquals(expectedZoomTiles, tileDao.count(zoom));

		TileMatrix tileMatrix = tileDao.getTileMatrix(zoom);

		TileGrid tileGrid = TileBoundingBoxUtils.getTileGrid(
				tileMatrixSetBoundingBox, tileMatrix.getMatrixWidth(),
				tileMatrix.getMatrixHeight(), zoomBoundingBox);

		TestCase.assertTrue(tileGrid.getMinX() >= 0);
		TestCase.assertTrue(tileGrid.getMaxX() < tileMatrix
				.getMatrixWidth());
		TestCase.assertTrue(tileGrid.getMinY() >= 0);
		TestCase.assertTrue(tileGrid.getMaxY() < tileMatrix
				.getMatrixHeight());

		TileResultSet resultSet = tileDao.queryForTile(zoom);
		TestCase.assertEquals(expectedZoomTiles, resultSet.getCount());
		int resultCount = 0;
		while (resultSet.moveToNext()) {
			TileRow tileRow = resultSet.getRow();
			resultCount++;
			byte[] tileData = tileRow.getTileData();
			TestCase.assertNotNull(tileData);
			BufferedImage image = tileRow.getTileDataImage();
			TestCase.assertNotNull(image);
			TestCase.assertEquals(tileMatrix.getTileWidth(),
					image.getWidth());
			TestCase.assertEquals(tileMatrix.getTileHeight(),
					image.getHeight());
		}
		TestCase.assertEquals(expectedZoomTiles, resultCount);
	}

}
 
Example #20
Source File: TestUtils.java    From geopackage-android-map with MIT License 4 votes vote down vote up
/**
 * Add rows to the tile table
 *
 * @param geoPackage
 * @param tileMatrix
 * @param tileData
 */
public static void addRowsToTileTable(GeoPackage geoPackage,
                                      TileMatrix tileMatrix, byte[] tileData) {

    TileDao dao = geoPackage.getTileDao(tileMatrix.getTableName());

    for (int column = 0; column < tileMatrix.getMatrixWidth(); column++) {

        for (int row = 0; row < tileMatrix.getMatrixHeight(); row++) {

            TileRow newRow = dao.newRow();

            newRow.setZoomLevel(tileMatrix.getZoomLevel());
            newRow.setTileColumn(column);
            newRow.setTileRow(row);
            newRow.setTileData(tileData);

            dao.create(newRow);
        }

    }

}
 
Example #21
Source File: TileUtils.java    From geopackage-android with MIT License 4 votes vote down vote up
/**
 * Validate a tile row
 *
 * @param dao
 * @param columns
 * @param tileRow
 * @param testBitmap
 */
private static void validateTileRow(TileDao dao, String[] columns,
                                    TileRow tileRow, boolean testBitmap) {
    TestCase.assertEquals(columns.length, tileRow.columnCount());

    for (int i = 0; i < tileRow.columnCount(); i++) {
        TileColumn column = tileRow.getTable().getColumns().get(i);
        TestCase.assertEquals(i, column.getIndex());
        TestCase.assertEquals(columns[i], tileRow.getColumnName(i));
        TestCase.assertEquals(i, tileRow.getColumnIndex(columns[i]));
        int rowType = tileRow.getRowColumnType(i);
        Object value = tileRow.getValue(i);

        switch (rowType) {

            case Cursor.FIELD_TYPE_INTEGER:
                TestUtils.validateIntegerValue(value, column.getDataType());
                break;

            case Cursor.FIELD_TYPE_FLOAT:
                TestUtils.validateFloatValue(value, column.getDataType());
                break;

            case Cursor.FIELD_TYPE_STRING:
                TestCase.assertTrue(value instanceof String);
                break;

            case Cursor.FIELD_TYPE_BLOB:
                TestCase.assertTrue(value instanceof byte[]);
                break;

            case Cursor.FIELD_TYPE_NULL:
                TestCase.assertNull(value);
                break;

        }
    }

    TestCase.assertTrue(tileRow.getId() >= 0);
    TestCase.assertTrue(tileRow.getZoomLevel() >= 0);
    TestCase.assertTrue(tileRow.getTileColumn() >= 0);
    TestCase.assertTrue(tileRow.getTileRow() >= 0);
    byte[] tileData = tileRow.getTileData();
    TestCase.assertNotNull(tileData);
    TestCase.assertTrue(tileData.length > 0);

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

    if (testBitmap) {
        Bitmap bitmap = tileRow.getTileDataBitmap();
        if (dao.getTileMatrixSet().getContents().getDataType() != ContentsDataType.GRIDDED_COVERAGE) {
            TestCase.assertNotNull(bitmap);
            TestCase.assertEquals(tileMatrix.getTileWidth(), bitmap.getWidth());
            TestCase.assertEquals(tileMatrix.getTileHeight(),
                    bitmap.getHeight());
        }
    }
}
 
Example #22
Source File: GeoPackageExample.java    From geopackage-android with MIT License 4 votes vote down vote up
private static void createRelatedTablesTilesExtension(
        GeoPackage geoPackage) {

    String featureTable = "point2";
    String tileTable = "nga";

    RelatedTablesExtension relatedTables = new RelatedTablesExtension(
            geoPackage);

    List<UserCustomColumn> additionalMappingColumns = RelatedTablesUtils
            .createAdditionalUserColumns();

    UserMappingTable userMappingTable = UserMappingTable.create(
            featureTable + "_" + tileTable, additionalMappingColumns);
    ExtendedRelation relation = relatedTables.addTilesRelationship(
            featureTable, tileTable, userMappingTable);

    UserMappingDao userMappingDao = relatedTables.getMappingDao(relation);

    FeatureDao featureDao = geoPackage
            .getFeatureDao(relation.getBaseTableName());
    TileDao tileDao = geoPackage.getTileDao(relation.getRelatedTableName());

    FeatureCursor featureCursor = featureDao.queryForAll();
    while (featureCursor.moveToNext()) {

        FeatureRow featureRow = featureCursor.getRow();
        String featureName = featureRow.getValue(TEXT_COLUMN).toString();

        TileCursor tileCursor = tileDao
                .queryForTile(tileDao.getMinZoom());
        while (tileCursor.moveToNext()) {

            TileRow tileRow = tileCursor.getRow();

            UserMappingRow userMappingRow = userMappingDao.newRow();
            userMappingRow.setBaseId(featureRow.getId());
            userMappingRow.setRelatedId(tileRow.getId());
            RelatedTablesUtils.populateUserRow(userMappingDao.getTable(),
                    userMappingRow, UserMappingTable.requiredColumns());
            DublinCoreMetadata.setValue(userMappingRow,
                    DublinCoreType.TITLE, featureName);
            DublinCoreMetadata.setValue(userMappingRow,
                    DublinCoreType.DESCRIPTION, featureName);
            DublinCoreMetadata.setValue(userMappingRow,
                    DublinCoreType.SOURCE, featureName);
            userMappingDao.create(userMappingRow);
        }
        tileCursor.close();

    }
    featureCursor.close();

}
 
Example #23
Source File: GeoPackageExample.java    From geopackage-android with MIT License 4 votes vote down vote up
private static void createCoverageDataPngExtension(GeoPackage geoPackage)
        throws SQLException {

    BoundingBox bbox = new BoundingBox(-11667347.997449303,
            4824705.2253603265, -11666125.00499674, 4825928.217812888);

    int contentsEpsg = ProjectionConstants.EPSG_WEB_MERCATOR;
    int tileMatrixSetEpsg = ProjectionConstants.EPSG_WEB_MERCATOR;

    SpatialReferenceSystemDao srsDao = geoPackage
            .getSpatialReferenceSystemDao();
    srsDao.getOrCreateFromEpsg(ProjectionConstants.EPSG_WORLD_GEODETIC_SYSTEM_GEOGRAPHICAL_3D);

    SpatialReferenceSystem contentsSrs = srsDao
            .getOrCreateFromEpsg(contentsEpsg);
    SpatialReferenceSystem tileMatrixSetSrs = srsDao
            .getOrCreateFromEpsg(tileMatrixSetEpsg);

    ProjectionTransform transform = tileMatrixSetSrs.getProjection()
            .getTransformation(contentsSrs.getProjection());
    BoundingBox contentsBoundingBox = bbox;
    if (!transform.isSameProjection()) {
        contentsBoundingBox = bbox.transform(transform);
    }

    CoverageDataPng coverageData = CoverageDataPng
            .createTileTableWithMetadata(geoPackage, "coverage_png",
                    contentsBoundingBox, contentsSrs.getId(), bbox,
                    tileMatrixSetSrs.getId());
    TileDao tileDao = coverageData.getTileDao();
    TileMatrixSet tileMatrixSet = coverageData.getTileMatrixSet();

    GriddedCoverageDao griddedCoverageDao = coverageData
            .getGriddedCoverageDao();

    GriddedCoverage griddedCoverage = new GriddedCoverage();
    griddedCoverage.setTileMatrixSet(tileMatrixSet);
    griddedCoverage.setDataType(GriddedCoverageDataType.INTEGER);
    griddedCoverage.setDataNull(new Double(Short.MAX_VALUE
            - Short.MIN_VALUE));
    griddedCoverage
            .setGridCellEncodingType(GriddedCoverageEncodingType.CENTER);
    griddedCoverageDao.create(griddedCoverage);

    GriddedTileDao griddedTileDao = coverageData.getGriddedTileDao();

    int width = 1;
    int height = 1;
    int tileWidth = 3;
    int tileHeight = 3;

    short[][] tilePixels = new short[tileHeight][tileWidth];

    tilePixels[0][0] = (short) 1661.95;
    tilePixels[0][1] = (short) 1665.40;
    tilePixels[0][2] = (short) 1668.19;
    tilePixels[1][0] = (short) 1657.18;
    tilePixels[1][1] = (short) 1663.39;
    tilePixels[1][2] = (short) 1669.65;
    tilePixels[2][0] = (short) 1654.78;
    tilePixels[2][1] = (short) 1660.31;
    tilePixels[2][2] = (short) 1666.44;

    byte[] imageBytes = coverageData.drawTileData(tilePixels);

    TileMatrixDao tileMatrixDao = geoPackage.getTileMatrixDao();

    TileMatrix tileMatrix = new TileMatrix();
    tileMatrix.setContents(tileMatrixSet.getContents());
    tileMatrix.setMatrixHeight(height);
    tileMatrix.setMatrixWidth(width);
    tileMatrix.setTileHeight(tileHeight);
    tileMatrix.setTileWidth(tileWidth);
    tileMatrix.setPixelXSize((bbox.getMaxLongitude() - bbox
            .getMinLongitude()) / width / tileWidth);
    tileMatrix
            .setPixelYSize((bbox.getMaxLatitude() - bbox.getMinLatitude())
                    / height / tileHeight);
    tileMatrix.setZoomLevel(15);
    tileMatrixDao.create(tileMatrix);

    TileRow tileRow = tileDao.newRow();
    tileRow.setTileColumn(0);
    tileRow.setTileRow(0);
    tileRow.setZoomLevel(tileMatrix.getZoomLevel());
    tileRow.setTileData(imageBytes);

    long tileId = tileDao.create(tileRow);

    GriddedTile griddedTile = new GriddedTile();
    griddedTile.setContents(tileMatrixSet.getContents());
    griddedTile.setTableId(tileId);

    griddedTileDao.create(griddedTile);

}
 
Example #24
Source File: TestUtils.java    From geopackage-android with MIT License 4 votes vote down vote up
/**
 * Add rows to the tile table
 *
 * @param geoPackage
 * @param tileMatrix
 * @param tileData
 */
public static void addRowsToTileTable(GeoPackage geoPackage,
                                      TileMatrix tileMatrix, byte[] tileData) {

    TileDao dao = geoPackage.getTileDao(tileMatrix.getTableName());

    for (int column = 0; column < tileMatrix.getMatrixWidth(); column++) {

        for (int row = 0; row < tileMatrix.getMatrixHeight(); row++) {

            TileRow newRow = dao.newRow();

            newRow.setZoomLevel(tileMatrix.getZoomLevel());
            newRow.setTileColumn(column);
            newRow.setTileRow(row);
            newRow.setTileData(tileData);

            dao.create(newRow);
        }

    }

}
 
Example #25
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 #26
Source File: CoverageDataTiff.java    From geopackage-android with MIT License 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public CoverageDataTiffImage createImage(TileRow tileRow) {
    return new CoverageDataTiffImage(tileRow);
}
 
Example #27
Source File: CoverageDataPng.java    From geopackage-android with MIT License 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public CoverageDataPngImage createImage(TileRow tileRow) {
    return new CoverageDataPngImage(tileRow);
}
 
Example #28
Source File: CoverageData.java    From geopackage-java with MIT License 2 votes vote down vote up
/**
 * Get the coverage data value of the pixel in the tile row image
 * 
 * @param griddedTile
 *            gridded tile
 * @param tileRow
 *            tile row
 * @param x
 *            x coordinate
 * @param y
 *            y coordinate
 * @return coverage data value
 */
public abstract double getValue(GriddedTile griddedTile, TileRow tileRow,
		int x, int y);
 
Example #29
Source File: CoverageData.java    From geopackage-java with MIT License 2 votes vote down vote up
/**
 * Get the coverage data value of the pixel in the tile row image
 * 
 * @param tileRow
 *            tile row
 * @param x
 *            x coordinate
 * @param y
 *            y coordinate
 * @return coverage data value
 */
public double getValue(TileRow tileRow, int x, int y) {
	GriddedTile griddedTile = getGriddedTile(tileRow.getId());
	double value = getValue(griddedTile, tileRow, x, y);
	return value;
}
 
Example #30
Source File: CoverageData.java    From geopackage-java with MIT License 2 votes vote down vote up
/**
 * Create a coverage data image
 *
 * @param tileRow
 *            tile row
 * @return image
 */
public abstract TImage createImage(TileRow tileRow);