Java Code Examples for mil.nga.geopackage.GeoPackage#getTileMatrixSetDao()

The following examples show how to use mil.nga.geopackage.GeoPackage#getTileMatrixSetDao() . 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: CoverageDataTestUtils.java    From geopackage-android with MIT License 6 votes vote down vote up
/**
 * Get the coverage data value at the coordinate
 *
 * @param geoPackage GeoPackage
 * @param algorithm  algorithm
 * @param latitude   latitude
 * @param longitude  longitude
 * @param epsg       epsg
 * @return coverage data value
 * @throws Exception
 */
public static Double getValue(GeoPackage geoPackage,
                              CoverageDataAlgorithm algorithm, double latitude,
                              double longitude, long epsg) throws Exception {

    Double value = null;

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

    for (String coverageTable : coverageDataTables) {

        TileMatrixSet tileMatrixSet = dao.queryForId(coverageTable);
        TileDao tileDao = geoPackage.getTileDao(tileMatrixSet);

        Projection requestProjection = ProjectionFactory
                .getProjection(epsg);

        // Test getting the coverage data value of a single coordinate
        CoverageData<?> coverageData = CoverageData.getCoverageData(geoPackage, tileDao, requestProjection);
        coverageData.setAlgorithm(algorithm);
        value = coverageData.getValue(latitude, longitude);
    }

    return value;
}
 
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: CoverageDataTestUtils.java    From geopackage-android with MIT License 5 votes vote down vote up
/**
 * Get the coverage data for the bounding box
 *
 * @param geoPackage  GeoPackage
 * @param algorithm   algorithm
 * @param boundingBox bounding box
 * @param width       results width
 * @param height      results height
 * @param epsg        epsg code
 * @return coverage data results
 * @throws Exception
 */
public static CoverageDataResults getValues(GeoPackage geoPackage,
                                            CoverageDataAlgorithm algorithm, BoundingBox boundingBox,
                                            int width, int height, long epsg) throws Exception {

    CoverageDataResults values = null;

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

    for (String coverageTable : coverageDataTables) {

        TileMatrixSet tileMatrixSet = dao.queryForId(coverageTable);
        TileDao tileDao = geoPackage.getTileDao(tileMatrixSet);

        Projection requestProjection = ProjectionFactory
                .getProjection(epsg);

        // Test getting the coverage data value of a single coordinate
        CoverageData<?> coverageData = CoverageData.getCoverageData(geoPackage, tileDao, requestProjection);
        coverageData.setAlgorithm(algorithm);
        coverageData.setWidth(width);
        coverageData.setHeight(height);
        values = coverageData.getValues(boundingBox);
    }

    return values;
}
 
Example 4
Source File: CoverageDataTestUtils.java    From geopackage-java with MIT License 5 votes vote down vote up
/**
 * Get the coverage data for the bounding box
 * 
 * @param geoPackage
 *            GeoPackage
 * @param algorithm
 *            algorithm
 * @param boundingBox
 *            bounding box
 * @param width
 *            results width
 * @param height
 *            results height
 * @param epsg
 *            epsg
 * @return coverage data results
 * @throws Exception
 */
public static CoverageDataResults getValues(GeoPackage geoPackage,
		CoverageDataAlgorithm algorithm, BoundingBox boundingBox,
		int width, int height, long epsg) throws Exception {

	CoverageDataResults values = null;

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

	for (String coverageTable : coverageDataTables) {

		TileMatrixSet tileMatrixSet = dao.queryForId(coverageTable);
		TileDao tileDao = geoPackage.getTileDao(tileMatrixSet);

		Projection requestProjection = ProjectionFactory
				.getProjection(epsg);

		// Test getting the coverage data value of a single coordinate
		CoverageData<?> coverageData = CoverageData.getCoverageData(
				geoPackage, tileDao, requestProjection);
		coverageData.setAlgorithm(algorithm);
		coverageData.setWidth(width);
		coverageData.setHeight(height);
		values = coverageData.getValues(boundingBox);
	}

	return values;
}
 
Example 5
Source File: CoverageDataTestUtils.java    From geopackage-java with MIT License 5 votes vote down vote up
/**
 * Get the coverage data value at the coordinate
 * 
 * @param geoPackage
 *            GeoPackage
 * @param algorithm
 *            algorithm
 * @param latitude
 *            latitude
 * @param longitude
 *            longitude
 * @param epsg
 *            epsg
 * @return coverage data value
 * @throws Exception
 */
public static Double getValue(GeoPackage geoPackage,
		CoverageDataAlgorithm algorithm, double latitude, double longitude,
		long epsg) throws Exception {

	Double value = null;

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

	for (String coverageTable : coverageDataTables) {

		TileMatrixSet tileMatrixSet = dao.queryForId(coverageTable);
		TileDao tileDao = geoPackage.getTileDao(tileMatrixSet);

		Projection requestProjection = ProjectionFactory
				.getProjection(epsg);

		// Test getting the coverage data value of a single coordinate
		CoverageData<?> coverageData = CoverageData.getCoverageData(
				geoPackage, tileDao, requestProjection);
		coverageData.setAlgorithm(algorithm);
		value = coverageData.getValue(latitude, longitude);
	}

	return value;
}
 
Example 6
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 7
Source File: TileUtils.java    From geopackage-java with MIT License 4 votes vote down vote up
/**
 * Test update
 * 
 * @param geoPackage
 *            GeoPackage
 * @throws SQLException
 *             upon error
 * @throws IOException
 *             upon error
 */
public static void testUpdate(GeoPackage geoPackage) throws SQLException,
		IOException {

	TileMatrixSetDao tileMatrixSetDao = geoPackage.getTileMatrixSetDao();

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

		for (TileMatrixSet tileMatrixSet : results) {

			TileDao dao = geoPackage.getTileDao(tileMatrixSet);
			testUpdate(dao);

		}
	}

}
 
Example 8
Source File: TileMatrixSetUtils.java    From geopackage-java with MIT License 4 votes vote down vote up
/**
 * Test delete
 * 
 * @param geoPackage
 * @throws SQLException
 */
public static void testDelete(GeoPackage geoPackage) throws SQLException {

	TileMatrixSetDao dao = geoPackage.getTileMatrixSetDao();
	if (dao.isTableExists()) {
		List<TileMatrixSet> results = dao.queryForAll();

		if (!results.isEmpty()) {

			// Choose random tile matrix set
			int random = (int) (Math.random() * results.size());
			TileMatrixSet tileMatrixSet = results.get(random);

			// Delete the tile matrix set
			geoPackage.foreignKeys(false);
			dao.delete(tileMatrixSet);

			// Verify deleted
			TileMatrixSet queryTileMatrixSet = dao.queryForId(tileMatrixSet
					.getId());
			TestCase.assertNull(queryTileMatrixSet);

			// Prepared deleted
			results = dao.queryForAll();
			if (!results.isEmpty()) {

				// Choose random tile matrix set
				random = (int) (Math.random() * results.size());
				tileMatrixSet = results.get(random);

				// Find which tile matrix set to delete
				QueryBuilder<TileMatrixSet, String> qb = dao.queryBuilder();
				qb.where().eq(TileMatrixSet.COLUMN_SRS_ID,
						tileMatrixSet.getSrsId());
				PreparedQuery<TileMatrixSet> query = qb.prepare();
				List<TileMatrixSet> queryResults = dao.query(query);
				int count = queryResults.size();

				// Delete
				DeleteBuilder<TileMatrixSet, String> db = dao
						.deleteBuilder();
				db.where().eq(TileMatrixSet.COLUMN_SRS_ID,
						tileMatrixSet.getSrsId());
				PreparedDelete<TileMatrixSet> deleteQuery = db.prepare();
				int deleted = dao.delete(deleteQuery);

				TestCase.assertEquals(count, deleted);

			}
		}
	}
}
 
Example 9
Source File: TileMatrixSetUtils.java    From geopackage-java with MIT License 4 votes vote down vote up
/**
 * Test create
 * 
 * @param geoPackage
 * @throws SQLException
 */
public static void testCreate(GeoPackage geoPackage) throws SQLException {

	SpatialReferenceSystemDao srsDao = geoPackage
			.getSpatialReferenceSystemDao();
	ContentsDao contentsDao = geoPackage.getContentsDao();
	TileMatrixSetDao dao = geoPackage.getTileMatrixSetDao();

	if (dao.isTableExists()) {

		// Get current count
		long count = dao.countOf();
		TestCase.assertEquals(count, dao.getTileTables().size());

		// Retrieve a random srs
		List<SpatialReferenceSystem> results = srsDao.queryForAll();
		SpatialReferenceSystem srs = null;
		if (!results.isEmpty()) {
			int random = (int) (Math.random() * results.size());
			srs = results.get(random);
		}

		// Create a new contents
		Contents contents = new Contents();
		contents.setTableName("test_contents");
		contents.setDataType(ContentsDataType.TILES);
		contents.setIdentifier("test_contents");
		contents.setDescription("");
		// contents.setLastChange(new Date());
		contents.setMinX(-180.0);
		contents.setMinY(-90.0);
		contents.setMaxX(180.0);
		contents.setMaxY(90.0);
		contents.setSrs(srs);

		// Create the user tile table
		geoPackage.createTileTable(TestUtils.buildTileTable(contents
				.getTableName()));

		contentsDao.create(contents);

		// Create new matrix tile set
		TileMatrixSet tileMatrixSet = new TileMatrixSet();
		tileMatrixSet.setContents(contents);
		tileMatrixSet.setSrs(contents.getSrs());
		tileMatrixSet.setMinX(contents.getMinX());
		tileMatrixSet.setMinY(contents.getMinY());
		tileMatrixSet.setMaxX(contents.getMaxX());
		tileMatrixSet.setMaxY(contents.getMaxY());
		dao.create(tileMatrixSet);

		// Verify count
		long newCount = dao.countOf();
		TestCase.assertEquals(count + 1, newCount);
		TestCase.assertEquals(newCount, dao.getTileTables().size());
		TestCase.assertTrue(dao.getTileTables().contains(
				contents.getTableName()));

		// Verify saved matrix tile set
		TileMatrixSet queryTileMatrixSet = dao.queryForId(tileMatrixSet
				.getId());
		TestCase.assertEquals(contents.getId(),
				queryTileMatrixSet.getTableName());
		TestCase.assertEquals(contents.getSrsId().longValue(),
				queryTileMatrixSet.getSrsId());
		TestCase.assertEquals(contents.getMinX(),
				queryTileMatrixSet.getMinX());
		TestCase.assertEquals(contents.getMinY(),
				queryTileMatrixSet.getMinY());
		TestCase.assertEquals(contents.getMaxX(),
				queryTileMatrixSet.getMaxX());
		TestCase.assertEquals(contents.getMaxY(),
				queryTileMatrixSet.getMaxY());
		TestCase.assertEquals(contents.getId(), queryTileMatrixSet
				.getContents().getId());
		TestCase.assertEquals(contents.getSrsId().longValue(),
				queryTileMatrixSet.getSrs().getId());
	}
}
 
Example 10
Source File: TestSetupTeardown.java    From geopackage-java with MIT License 4 votes vote down vote up
/**
 * Set up create for tiles test
 * 
 * @param geoPackage
 * @throws SQLException
 * @throws IOException
 */
private static void setUpCreateTiles(GeoPackage geoPackage)
		throws SQLException, IOException {

	// Get existing SRS objects
	SpatialReferenceSystemDao srsDao = geoPackage
			.getSpatialReferenceSystemDao();

	SpatialReferenceSystem epsgSrs = srsDao.queryForId(4326l);

	TestCase.assertNotNull(epsgSrs);

	// Create the Tile Matrix Set and Tile Matrix tables
	geoPackage.createTileMatrixSetTable();
	geoPackage.createTileMatrixTable();

	// Create new Contents
	ContentsDao contentsDao = geoPackage.getContentsDao();

	Contents contents = new Contents();
	contents.setTableName("test_tiles");
	contents.setDataType(ContentsDataType.TILES);
	contents.setIdentifier("test_tiles");
	// contents.setDescription("");
	// contents.setLastChange(new Date());
	contents.setMinX(-180.0);
	contents.setMinY(-90.0);
	contents.setMaxX(180.0);
	contents.setMaxY(90.0);
	contents.setSrs(epsgSrs);

	// Create the user tile table
	TileTable tileTable = TestUtils.buildTileTable(contents.getTableName());
	geoPackage.createTileTable(tileTable);

	// Create the contents
	contentsDao.create(contents);

	// Create new Tile Matrix Set
	TileMatrixSetDao tileMatrixSetDao = geoPackage.getTileMatrixSetDao();

	TileMatrixSet tileMatrixSet = new TileMatrixSet();
	tileMatrixSet.setContents(contents);
	tileMatrixSet.setSrs(contents.getSrs());
	tileMatrixSet.setMinX(contents.getMinX());
	tileMatrixSet.setMinY(contents.getMinY());
	tileMatrixSet.setMaxX(contents.getMaxX());
	tileMatrixSet.setMaxY(contents.getMaxY());
	tileMatrixSetDao.create(tileMatrixSet);

	// Create new Tile Matrix rows
	TileMatrixDao tileMatrixDao = geoPackage.getTileMatrixDao();

	final int tileWidth = 256;
	final int tileHeight = 256;

	int matrixWidthAndHeight = 2;
	double pixelXSize = (tileMatrixSet.getMaxX() - tileMatrixSet.getMinX())
			/ (matrixWidthAndHeight * tileWidth);
	double pixelYSize = (tileMatrixSet.getMaxY() - tileMatrixSet.getMinY())
			/ (matrixWidthAndHeight * tileHeight);

	byte[] tileData = TestUtils.getTileBytes();

	for (int zoom = 0; zoom < CREATE_TILE_MATRIX_COUNT; zoom++) {

		TileMatrix tileMatrix = new TileMatrix();
		tileMatrix.setContents(contents);
		tileMatrix.setZoomLevel(zoom);
		tileMatrix.setMatrixWidth(matrixWidthAndHeight);
		tileMatrix.setMatrixHeight(matrixWidthAndHeight);
		tileMatrix.setTileWidth(tileWidth);
		tileMatrix.setTileHeight(tileHeight);
		tileMatrix.setPixelXSize(pixelXSize);
		tileMatrix.setPixelYSize(pixelYSize);
		tileMatrixDao.create(tileMatrix);

		matrixWidthAndHeight *= 2;
		pixelXSize /= 2.0;
		pixelYSize /= 2.0;

		// Populate the tile table with rows
		TestUtils.addRowsToTileTable(geoPackage, tileMatrix, tileData);
	}

}
 
Example 11
Source File: GeoPackageTestUtils.java    From geopackage-android with MIT License 4 votes vote down vote up
/**
 * Test deleting tables by name
 *
 * @param geoPackage
 * @throws SQLException
 */
public static void testDeleteTables(GeoPackage geoPackage)
        throws SQLException {

    GeometryColumnsDao geometryColumnsDao = geoPackage
            .getGeometryColumnsDao();
    TileMatrixSetDao tileMatrixSetDao = geoPackage.getTileMatrixSetDao();
    ContentsDao contentsDao = geoPackage.getContentsDao();

    TestCase.assertTrue(geometryColumnsDao.isTableExists()
            || tileMatrixSetDao.isTableExists());

    geoPackage.foreignKeys(false);

    if (geometryColumnsDao.isTableExists()) {

        TestCase.assertEquals(geoPackage.getFeatureTables().size(),
                geometryColumnsDao.countOf());
        for (String featureTable : geoPackage.getFeatureTables()) {
            TestCase.assertTrue(geoPackage.isTable(featureTable));
            TestCase.assertNotNull(contentsDao.queryForId(featureTable));
            geoPackage.deleteTable(featureTable);
            TestCase.assertFalse(geoPackage.isTable(featureTable));
            TestCase.assertNull(contentsDao.queryForId(featureTable));
        }
        TestCase.assertEquals(0, geometryColumnsDao.countOf());

        geoPackage.dropTable(GeometryColumns.TABLE_NAME);

        TestCase.assertFalse(geometryColumnsDao.isTableExists());
    }

    if (tileMatrixSetDao.isTableExists()) {
        TileMatrixDao tileMatrixDao = geoPackage.getTileMatrixDao();

        TestCase.assertTrue(tileMatrixSetDao.isTableExists());
        TestCase.assertTrue(tileMatrixDao.isTableExists());

        TestCase.assertEquals(geoPackage.getTables(ContentsDataType.TILES).size() + geoPackage.getTables(ContentsDataType.GRIDDED_COVERAGE).size(),
                tileMatrixSetDao.countOf());
        for (String tileTable : geoPackage.getTileTables()) {
            TestCase.assertTrue(geoPackage.isTable(tileTable));
            TestCase.assertNotNull(contentsDao.queryForId(tileTable));
            geoPackage.deleteTable(tileTable);
            TestCase.assertFalse(geoPackage.isTable(tileTable));
            TestCase.assertNull(contentsDao.queryForId(tileTable));
        }
        TestCase.assertEquals(geoPackage.getTables(ContentsDataType.GRIDDED_COVERAGE).size(), tileMatrixSetDao.countOf());

        geoPackage.dropTable(TileMatrix.TABLE_NAME);
        geoPackage.dropTable(TileMatrixSet.TABLE_NAME);

        TestCase.assertFalse(tileMatrixSetDao.isTableExists());
        TestCase.assertFalse(tileMatrixDao.isTableExists());
    }

    for (String attributeTable : geoPackage.getAttributesTables()) {

        TestCase.assertTrue(geoPackage.isTable(attributeTable));
        TestCase.assertNotNull(contentsDao.queryForId(attributeTable));
        geoPackage.deleteTable(attributeTable);
        TestCase.assertFalse(geoPackage.isTable(attributeTable));
        TestCase.assertNull(contentsDao.queryForId(attributeTable));

    }

}
 
Example 12
Source File: GeoPackageTestUtils.java    From geopackage-java with MIT License 4 votes vote down vote up
/**
 * Test deleting tables by name
 * 
 * @param geoPackage
 * @throws SQLException
 */
public static void testDeleteTables(GeoPackage geoPackage)
		throws SQLException {

	GeometryColumnsDao geometryColumnsDao = geoPackage
			.getGeometryColumnsDao();
	TileMatrixSetDao tileMatrixSetDao = geoPackage.getTileMatrixSetDao();
	ContentsDao contentsDao = geoPackage.getContentsDao();

	TestCase.assertTrue(geometryColumnsDao.isTableExists()
			|| tileMatrixSetDao.isTableExists());

	geoPackage.foreignKeys(false);

	if (geometryColumnsDao.isTableExists()) {

		TestCase.assertEquals(geoPackage.getFeatureTables().size(),
				geometryColumnsDao.countOf());
		for (String featureTable : geoPackage.getFeatureTables()) {
			TestCase.assertTrue(geoPackage.isTable(featureTable));
			TestCase.assertNotNull(contentsDao.queryForId(featureTable));
			geoPackage.deleteTable(featureTable);
			TestCase.assertFalse(geoPackage.isTable(featureTable));
			TestCase.assertNull(contentsDao.queryForId(featureTable));
		}
		TestCase.assertEquals(0, geometryColumnsDao.countOf());

		geoPackage.dropTable(GeometryColumns.TABLE_NAME);

		TestCase.assertFalse(geometryColumnsDao.isTableExists());
	}

	if (tileMatrixSetDao.isTableExists()) {
		TileMatrixDao tileMatrixDao = geoPackage.getTileMatrixDao();

		TestCase.assertTrue(tileMatrixSetDao.isTableExists());
		TestCase.assertTrue(tileMatrixDao.isTableExists());

		TestCase.assertEquals(geoPackage.getTables(ContentsDataType.TILES)
				.size()
				+ geoPackage.getTables(ContentsDataType.GRIDDED_COVERAGE)
						.size(),
				tileMatrixSetDao.countOf());
		for (String tileTable : geoPackage.getTileTables()) {
			TestCase.assertTrue(geoPackage.isTable(tileTable));
			TestCase.assertNotNull(contentsDao.queryForId(tileTable));
			geoPackage.deleteTable(tileTable);
			TestCase.assertFalse(geoPackage.isTable(tileTable));
			TestCase.assertNull(contentsDao.queryForId(tileTable));
		}
		TestCase.assertEquals(geoPackage
				.getTables(ContentsDataType.GRIDDED_COVERAGE).size(),
				tileMatrixSetDao.countOf());

		geoPackage.dropTable(TileMatrix.TABLE_NAME);
		geoPackage.dropTable(TileMatrixSet.TABLE_NAME);

		TestCase.assertFalse(tileMatrixSetDao.isTableExists());
		TestCase.assertFalse(tileMatrixDao.isTableExists());
	}

	for (String attributeTable : geoPackage.getAttributesTables()) {

		TestCase.assertTrue(geoPackage.isTable(attributeTable));
		TestCase.assertNotNull(contentsDao.queryForId(attributeTable));
		geoPackage.deleteTable(attributeTable);
		TestCase.assertFalse(geoPackage.isTable(attributeTable));
		TestCase.assertNull(contentsDao.queryForId(attributeTable));

	}

}
 
Example 13
Source File: TileUtils.java    From geopackage-android with MIT License 4 votes vote down vote up
/**
 * Test update
 *
 * @param testContext test context
 * @param geoPackage  GeoPackage
 * @throws SQLException upon error
 * @throws IOException  upon error
 */
public static void testUpdate(Context testContext, GeoPackage geoPackage) throws SQLException,
        IOException {

    TileMatrixSetDao tileMatrixSetDao = geoPackage.getTileMatrixSetDao();

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

        for (TileMatrixSet tileMatrixSet : results) {

            TileDao dao = geoPackage.getTileDao(tileMatrixSet);
            testUpdate(testContext, dao);

        }
    }

}
 
Example 14
Source File: TileMatrixSetUtils.java    From geopackage-android with MIT License 4 votes vote down vote up
/**
 * Test delete
 * 
 * @param geoPackage
 * @throws SQLException
 */
public static void testDelete(GeoPackage geoPackage) throws SQLException {

	TileMatrixSetDao dao = geoPackage.getTileMatrixSetDao();
	if (dao.isTableExists()) {
		List<TileMatrixSet> results = dao.queryForAll();

		if (!results.isEmpty()) {

			// Choose random tile matrix set
			int random = (int) (Math.random() * results.size());
			TileMatrixSet tileMatrixSet = results.get(random);

			// Delete the tile matrix set
			geoPackage.foreignKeys(false);
			dao.delete(tileMatrixSet);

			// Verify deleted
			TileMatrixSet queryTileMatrixSet = dao.queryForId(tileMatrixSet
					.getId());
			TestCase.assertNull(queryTileMatrixSet);

			// Prepared deleted
			results = dao.queryForAll();
			if (!results.isEmpty()) {

				// Choose random tile matrix set
				random = (int) (Math.random() * results.size());
				tileMatrixSet = results.get(random);

				// Find which tile matrix set to delete
				QueryBuilder<TileMatrixSet, String> qb = dao.queryBuilder();
				qb.where().eq(TileMatrixSet.COLUMN_SRS_ID,
						tileMatrixSet.getSrsId());
				PreparedQuery<TileMatrixSet> query = qb.prepare();
				List<TileMatrixSet> queryResults = dao.query(query);
				int count = queryResults.size();

				// Delete
				DeleteBuilder<TileMatrixSet, String> db = dao
						.deleteBuilder();
				db.where().eq(TileMatrixSet.COLUMN_SRS_ID,
						tileMatrixSet.getSrsId());
				PreparedDelete<TileMatrixSet> deleteQuery = db.prepare();
				int deleted = dao.delete(deleteQuery);

				TestCase.assertEquals(count, deleted);

			}
		}
	}
}
 
Example 15
Source File: TileMatrixSetUtils.java    From geopackage-android with MIT License 4 votes vote down vote up
/**
 * Test create
 * 
 * @param geoPackage
 * @throws SQLException
 */
public static void testCreate(GeoPackage geoPackage) throws SQLException {

	SpatialReferenceSystemDao srsDao = geoPackage
			.getSpatialReferenceSystemDao();
	ContentsDao contentsDao = geoPackage.getContentsDao();
	TileMatrixSetDao dao = geoPackage.getTileMatrixSetDao();

	if (dao.isTableExists()) {

		// Get current count
		long count = dao.countOf();
		TestCase.assertEquals(count, dao.getTileTables().size());

		// Retrieve a random srs
		List<SpatialReferenceSystem> results = srsDao.queryForAll();
		SpatialReferenceSystem srs = null;
		if (!results.isEmpty()) {
			int random = (int) (Math.random() * results.size());
			srs = results.get(random);
		}

		// Create a new contents
		Contents contents = new Contents();
		contents.setTableName("test_contents");
		contents.setDataType(ContentsDataType.TILES);
		contents.setIdentifier("test_contents");
		contents.setDescription("");
		// contents.setLastChange(new Date());
		contents.setMinX(-180.0);
		contents.setMinY(-90.0);
		contents.setMaxX(180.0);
		contents.setMaxY(90.0);
		contents.setSrs(srs);

		// Create the user tile table
		geoPackage.createTileTable(TestUtils.buildTileTable(contents
				.getTableName()));

		contentsDao.create(contents);

		// Create new matrix tile set
		TileMatrixSet tileMatrixSet = new TileMatrixSet();
		tileMatrixSet.setContents(contents);
		tileMatrixSet.setSrs(contents.getSrs());
		tileMatrixSet.setMinX(contents.getMinX());
		tileMatrixSet.setMinY(contents.getMinY());
		tileMatrixSet.setMaxX(contents.getMaxX());
		tileMatrixSet.setMaxY(contents.getMaxY());
		dao.create(tileMatrixSet);

		// Verify count
		long newCount = dao.countOf();
		TestCase.assertEquals(count + 1, newCount);
		TestCase.assertEquals(newCount, dao.getTileTables().size());
		TestCase.assertTrue(dao.getTileTables().contains(
				contents.getTableName()));

		// Verify saved matrix tile set
		TileMatrixSet queryTileMatrixSet = dao.queryForId(tileMatrixSet
				.getId());
		TestCase.assertEquals(contents.getId(),
				queryTileMatrixSet.getTableName());
		TestCase.assertEquals(contents.getSrsId().longValue(),
				queryTileMatrixSet.getSrsId());
		TestCase.assertEquals(contents.getMinX(),
				queryTileMatrixSet.getMinX());
		TestCase.assertEquals(contents.getMinY(),
				queryTileMatrixSet.getMinY());
		TestCase.assertEquals(contents.getMaxX(),
				queryTileMatrixSet.getMaxX());
		TestCase.assertEquals(contents.getMaxY(),
				queryTileMatrixSet.getMaxY());
		TestCase.assertEquals(contents.getId(), queryTileMatrixSet
				.getContents().getId());
		TestCase.assertEquals(contents.getSrsId().longValue(),
				queryTileMatrixSet.getSrs().getId());
	}
}
 
Example 16
Source File: TestSetupTeardown.java    From geopackage-android with MIT License 4 votes vote down vote up
/**
 * Set up create for tiles test
 *
 * @param testContext
 * @param geoPackage
 * @throws SQLException
 * @throws IOException
 */
private static void setUpCreateTiles(Context testContext,
                                     GeoPackage geoPackage) throws SQLException, IOException {

    // Get existing SRS objects
    SpatialReferenceSystemDao srsDao = geoPackage
            .getSpatialReferenceSystemDao();

    SpatialReferenceSystem epsgSrs = srsDao.queryForId(4326l);

    TestCase.assertNotNull(epsgSrs);

    // Create the Tile Matrix Set and Tile Matrix tables
    geoPackage.createTileMatrixSetTable();
    geoPackage.createTileMatrixTable();

    // Create new Contents
    ContentsDao contentsDao = geoPackage.getContentsDao();

    Contents contents = new Contents();
    contents.setTableName("test_tiles");
    contents.setDataType(ContentsDataType.TILES);
    contents.setIdentifier("test_tiles");
    // contents.setDescription("");
    // contents.setLastChange(new Date());
    contents.setMinX(-180.0);
    contents.setMinY(-90.0);
    contents.setMaxX(180.0);
    contents.setMaxY(90.0);
    contents.setSrs(epsgSrs);

    // Create the user tile table
    TileTable tileTable = TestUtils.buildTileTable(contents.getTableName());
    geoPackage.createTileTable(tileTable);

    // Create the contents
    contentsDao.create(contents);

    // Create new Tile Matrix Set
    TileMatrixSetDao tileMatrixSetDao = geoPackage.getTileMatrixSetDao();

    TileMatrixSet tileMatrixSet = new TileMatrixSet();
    tileMatrixSet.setContents(contents);
    tileMatrixSet.setSrs(contents.getSrs());
    tileMatrixSet.setMinX(contents.getMinX());
    tileMatrixSet.setMinY(contents.getMinY());
    tileMatrixSet.setMaxX(contents.getMaxX());
    tileMatrixSet.setMaxY(contents.getMaxY());
    tileMatrixSetDao.create(tileMatrixSet);

    // Create new Tile Matrix rows
    TileMatrixDao tileMatrixDao = geoPackage.getTileMatrixDao();

    // Read the asset tile to bytes and convert to bitmap
    byte[] assetTileData = TestUtils.getAssetFileBytes(testContext,
            TestConstants.TILE_FILE_NAME);
    Bitmap bitmap = BitmapConverter.toBitmap(assetTileData);

    // Get the width and height of the bitmap
    final int tileWidth = bitmap.getWidth();
    final int tileHeight = bitmap.getHeight();

    int matrixWidthAndHeight = 2;
    double pixelXSize = (tileMatrixSet.getMaxX() - tileMatrixSet.getMinX()) / (matrixWidthAndHeight * tileWidth);
    double pixelYSize = (tileMatrixSet.getMaxY() - tileMatrixSet.getMinY()) / (matrixWidthAndHeight * tileHeight);

    // Compress the bitmap back to bytes and use those for the test
    byte[] tileData = BitmapConverter.toBytes(bitmap, CompressFormat
            .valueOf(TestConstants.TILE_FILE_NAME_EXTENSION.toUpperCase()));

    for (int zoom = 0; zoom < CREATE_TILE_MATRIX_COUNT; zoom++) {

        TileMatrix tileMatrix = new TileMatrix();
        tileMatrix.setContents(contents);
        tileMatrix.setZoomLevel(zoom);
        tileMatrix.setMatrixWidth(matrixWidthAndHeight);
        tileMatrix.setMatrixHeight(matrixWidthAndHeight);
        tileMatrix.setTileWidth(tileWidth);
        tileMatrix.setTileHeight(tileHeight);
        tileMatrix.setPixelXSize(pixelXSize);
        tileMatrix.setPixelYSize(pixelYSize);
        tileMatrixDao.create(tileMatrix);

        matrixWidthAndHeight *= 2;
        pixelXSize /= 2.0;
        pixelYSize /= 2.0;

        // Populate the tile table with rows
        TestUtils.addRowsToTileTable(geoPackage, tileMatrix, tileData);
    }

}
 
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: GeoPackageOverlayUtils.java    From geopackage-android-map with MIT License 3 votes vote down vote up
/**
 * Test overlay
 * 
 * @param geoPackage
 * @throws SQLException
 */
public static void testOverlay(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);

			GeoPackageOverlay overlay = new GeoPackageOverlay(dao);

			for (int zoom = 0; zoom <= 21; zoom++) {
				int tileLength = (int) Math.pow(2, zoom);

				int column = (int) (Math.random() * tileLength);
				int row = (int) (Math.random() * tileLength);

				for (int maxColumns = Math.min(tileLength, column + 2); column < maxColumns; column++) {
					for (int maxRows = Math.min(tileLength, row + 2); row < maxRows; row++) {
						Tile tile = overlay.getTile(column, row, zoom);
						if (tile != null) {
							TestCase.assertTrue(tile.height > 0);
							TestCase.assertTrue(tile.width > 0);
						}
					}
				}

			}

		}

	}

}
 
Example 19
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);

			}

		}

	}

}
 
Example 20
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);

            }

        }

    }

}