Java Code Examples for mil.nga.geopackage.tiles.matrixset.TileMatrixSet

The following examples show how to use mil.nga.geopackage.tiles.matrixset.TileMatrixSet. These examples are extracted from open source projects. 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 Project: geopackage-android   Source File: GeoPackageImpl.java    License: MIT License 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public TileDao getTileDao(Contents contents) {

    if (contents == null) {
        throw new GeoPackageException("Non null "
                + Contents.class.getSimpleName()
                + " is required to create " + TileDao.class.getSimpleName());
    }

    TileMatrixSet tileMatrixSet = contents.getTileMatrixSet();
    if (tileMatrixSet == null) {
        throw new GeoPackageException("No "
                + TileMatrixSet.class.getSimpleName() + " exists for "
                + Contents.class.getSimpleName() + " " + contents.getId());
    }

    return getTileDao(tileMatrixSet);
}
 
Example 2
Source Project: geopackage-android   Source File: CoverageDataTestUtils.java    License: 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 3
Source Project: geopackage-core-java   Source File: CoverageDataCore.java    License: MIT License 6 votes vote down vote up
/**
 * Constructor
 * 
 * @param geoPackage
 *            GeoPackage
 * @param tileMatrixSet
 *            tile matrix set
 * @param width
 *            specified results width
 * @param height
 *            specified results height
 * @param requestProjection
 *            request projection
 */
protected CoverageDataCore(GeoPackageCore geoPackage,
		TileMatrixSet tileMatrixSet, Integer width, Integer height,
		Projection requestProjection) {
	super(geoPackage);

	this.tileMatrixSet = tileMatrixSet;
	griddedCoverageDao = geoPackage.getGriddedCoverageDao();
	griddedTileDao = geoPackage.getGriddedTileDao();
	queryGriddedCoverage();

	this.width = width;
	this.height = height;
	this.requestProjection = requestProjection;
	coverageProjection = tileMatrixSet.getProjection();
	coverageBoundingBox = tileMatrixSet.getBoundingBox();

	// Check if the projections have the same units
	if (requestProjection != null) {
		sameProjection = (requestProjection.getUnit().name
				.equals(coverageProjection.getUnit().name));
	} else {
		sameProjection = true;
	}
}
 
Example 4
Source Project: geopackage-core-java   Source File: GeoPackageCoreImpl.java    License: MIT License 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public boolean createTileMatrixSetTable() {
	verifyWritable();

	boolean created = false;
	TileMatrixSetDao dao = getTileMatrixSetDao();
	try {
		if (!dao.isTableExists()) {
			created = tableCreator.createTileMatrixSet() > 0;
		}
	} catch (SQLException e) {
		throw new GeoPackageException(
				"Failed to check if " + TileMatrixSet.class.getSimpleName()
						+ " table exists and create it",
				e);
	}
	return created;
}
 
Example 5
Source Project: geopackage-core-java   Source File: GeoPackageDaoManager.java    License: MIT License 6 votes vote down vote up
/**
 * Unregister all GeoPackage DAO with the connection source
 * 
 * @param connectionSource
 *            connection source
 */
public static void unregisterDaos(ConnectionSource connectionSource) {
	// TODO when ormlite-core version > 5.1 is released, replace with:
	// "DaoManager.unregisterDaos(connectionSource);"
	// See https://github.com/j256/ormlite-core/pull/149
	unregisterDao(connectionSource, Contents.class,
			SpatialReferenceSystem.class,
			SpatialReferenceSystemSfSql.class,
			SpatialReferenceSystemSqlMm.class, Extensions.class,
			GriddedCoverage.class, GriddedTile.class, GeometryIndex.class,
			TableIndex.class, FeatureTileLink.class,
			ExtendedRelation.class, TileScaling.class,
			GeometryColumns.class, GeometryColumnsSfSql.class,
			GeometryColumnsSqlMm.class, Metadata.class,
			MetadataReference.class, DataColumns.class,
			DataColumnConstraints.class, TileMatrix.class,
			TileMatrixSet.class, ContentsId.class);
}
 
Example 6
Source Project: geopackage-core-java   Source File: ContentsDao.java    License: MIT License 6 votes vote down vote up
/**
 * Verify the required tile tables exist
 * 
 * @param dataType
 *            data type
 * @throws SQLException
 *             upon tiles verification error
 */
private void verifyTiles(ContentsDataType dataType) throws SQLException {
	// Tiles require Tile Matrix Set table (Spec Requirement 37)
	TileMatrixSetDao tileMatrixSetDao = getTileMatrixSetDao();
	if (!tileMatrixSetDao.isTableExists()) {
		throw new GeoPackageException("A data type of "
				+ dataType.getName() + " requires the "
				+ TileMatrixSet.class.getSimpleName()
				+ " table to first be created using the GeoPackage.");
	}

	// Tiles require Tile Matrix table (Spec Requirement 41)
	TileMatrixDao tileMatrixDao = getTileMatrixDao();
	if (!tileMatrixDao.isTableExists()) {
		throw new GeoPackageException("A data type of "
				+ dataType.getName() + " requires the "
				+ TileMatrix.class.getSimpleName()
				+ " table to first be created using the GeoPackage.");
	}
}
 
Example 7
Source Project: geopackage-core-java   Source File: Contents.java    License: MIT License 6 votes vote down vote up
/**
 * Get the Tile Matrix Set, should only return one or no value
 * 
 * @return tile matrix set
 */
public TileMatrixSet getTileMatrixSet() {
	TileMatrixSet result = null;
	if (tileMatrixSet.size() > 1) {
		// This shouldn't happen with the table name primary key on tile
		// matrix set
		throw new GeoPackageException(
				"Unexpected state. More than one TileMatrixSet has a foreign key to the Contents. Count: "
						+ tileMatrixSet.size());
	} else if (tileMatrixSet.size() == 1) {
		CloseableIterator<TileMatrixSet> iterator = tileMatrixSet
				.closeableIterator();
		try {
			result = iterator.next();
		} finally {
			try {
				iterator.close();
			} catch (IOException e) {
				throw new GeoPackageException(
						"Failed to close the Tile Matrix Set iterator", e);
			}
		}
	}
	return result;
}
 
Example 8
Source Project: geopackage-core-java   Source File: TileDaoUtils.java    License: 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 9
Source Project: geopackage-java   Source File: GeoPackageTextOutput.java    License: MIT License 6 votes vote down vote up
/**
 * Text output from a TileMatrixSet
 * 
 * @param tileMatrixSet
 *            tile matrix set
 * @return text
 */
public String textOutput(TileMatrixSet tileMatrixSet) {
	StringBuilder output = new StringBuilder();
	output.append("\t" + TileMatrixSet.COLUMN_TABLE_NAME + ": "
			+ tileMatrixSet.getTableName());
	output.append("\n" + textOutput(tileMatrixSet.getSrs()));
	output.append("\n\t" + TileMatrixSet.COLUMN_MIN_X + ": "
			+ tileMatrixSet.getMinX());
	output.append("\n\t" + TileMatrixSet.COLUMN_MIN_Y + ": "
			+ tileMatrixSet.getMinY());
	output.append("\n\t" + TileMatrixSet.COLUMN_MAX_X + ": "
			+ tileMatrixSet.getMaxX());
	output.append("\n\t" + TileMatrixSet.COLUMN_MAX_Y + ": "
			+ tileMatrixSet.getMaxY());
	return output.toString();
}
 
Example 10
Source Project: geopackage-android   Source File: CoverageData.java    License: MIT License 5 votes vote down vote up
/**
 * Get a Tiled Gridded Coverage Data
 *
 * @param geoPackage        GeoPackage
 * @param tileDao           tile dao
 * @param width             coverage data response width
 * @param height            coverage data response height
 * @param requestProjection request projection
 * @return coverage data
 */
public static CoverageData<?> getCoverageData(GeoPackage geoPackage,
                                              TileDao tileDao, Integer width, Integer height,
                                              Projection requestProjection) {

    TileMatrixSet tileMatrixSet = tileDao.getTileMatrixSet();
    GriddedCoverageDao griddedCoverageDao = geoPackage
            .getGriddedCoverageDao();

    GriddedCoverage griddedCoverage = null;
    try {
        if (griddedCoverageDao.isTableExists()) {
            griddedCoverage = griddedCoverageDao.query(tileMatrixSet);
        }
    } catch (SQLException e) {
        throw new GeoPackageException(
                "Failed to get Gridded Coverage for table name: "
                        + tileMatrixSet.getTableName(), e);
    }

    CoverageData<?> coverageData = null;

    GriddedCoverageDataType dataType = griddedCoverage.getDataType();
    switch (dataType) {
        case INTEGER:
            coverageData = new CoverageDataPng(geoPackage, tileDao, width,
                    height, requestProjection);
            break;
        case FLOAT:
            coverageData = new CoverageDataTiff(geoPackage, tileDao, width,
                    height, requestProjection);
            break;
        default:
            throw new GeoPackageException(
                    "Unsupported Gridded Coverage Data Type: " + dataType);
    }

    return coverageData;
}
 
Example 11
Source Project: geopackage-android   Source File: CoverageData.java    License: MIT License 5 votes vote down vote up
/**
 * Create the coverage data tile table with metadata and extension
 *
 * @param geoPackage               GeoPackage
 * @param tableName                table name
 * @param contentsBoundingBox      contents bounding box
 * @param contentsSrsId            contents srs id
 * @param tileMatrixSetBoundingBox tile matrix set bounding box
 * @param tileMatrixSetSrsId       tile matrix set srs id
 * @param dataType                 gridded coverage data type
 * @return coverage data
 */
public static CoverageData<?> createTileTableWithMetadata(
        GeoPackage geoPackage, String tableName,
        BoundingBox contentsBoundingBox, long contentsSrsId,
        BoundingBox tileMatrixSetBoundingBox, long tileMatrixSetSrsId,
        GriddedCoverageDataType dataType) {

    TileMatrixSet tileMatrixSet = CoverageDataCore
            .createTileTableWithMetadata(geoPackage, tableName,
                    contentsBoundingBox, contentsSrsId,
                    tileMatrixSetBoundingBox, tileMatrixSetSrsId);
    TileDao tileDao = geoPackage.getTileDao(tileMatrixSet);

    CoverageData<?> coverageData = null;
    switch (dataType) {
        case INTEGER:
            coverageData = new CoverageDataPng(geoPackage, tileDao);
            break;
        case FLOAT:
            coverageData = new CoverageDataTiff(geoPackage, tileDao);
            break;
        default:
            throw new GeoPackageException(
                    "Unsupported Gridded Coverage Data Type: " + dataType);
    }

    coverageData.getOrCreate();

    return coverageData;
}
 
Example 12
Source Project: geopackage-android   Source File: GeoPackageImpl.java    License: MIT License 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public TileDao getTileDao(String tableName) {

    TileMatrixSetDao dao = getTileMatrixSetDao();
    List<TileMatrixSet> tileMatrixSetList;
    try {
        tileMatrixSetList = dao.queryForEq(TileMatrixSet.COLUMN_TABLE_NAME,
                tableName);
    } catch (SQLException e) {
        throw new GeoPackageException("Failed to retrieve "
                + TileDao.class.getSimpleName() + " for table name: "
                + tableName + ". Exception retrieving "
                + TileMatrixSet.class.getSimpleName() + ".", e);
    }
    if (tileMatrixSetList.isEmpty()) {
        throw new GeoPackageException(
                "No Tile Table exists for table name: " + tableName);
    } else if (tileMatrixSetList.size() > 1) {
        // This shouldn't happen with the table name primary key on tile
        // matrix set table
        throw new GeoPackageException("Unexpected state. More than one "
                + TileMatrixSet.class.getSimpleName()
                + " matched for table name: " + tableName + ", count: "
                + tileMatrixSetList.size());
    }
    return getTileDao(tileMatrixSetList.get(0));
}
 
Example 13
Source Project: geopackage-android   Source File: CoverageDataTestUtils.java    License: 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 14
Source Project: geopackage-core-java   Source File: GriddedCoverage.java    License: MIT License 5 votes vote down vote up
/**
 * Set the tile matrix set
 * 
 * @param tileMatrixSet
 *            tile matrix set
 */
public void setTileMatrixSet(TileMatrixSet tileMatrixSet) {
	this.tileMatrixSet = tileMatrixSet;
	if (tileMatrixSet != null) {
		tileMatrixSetName = tileMatrixSet.getTableName();
	} else {
		tileMatrixSetName = null;
	}
}
 
Example 15
Source Project: geopackage-core-java   Source File: GeoPackageCoreImpl.java    License: MIT License 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public TileMatrixSet createTileTableWithMetadata(String tableName,
		BoundingBox contentsBoundingBox, long contentsSrsId,
		BoundingBox tileMatrixSetBoundingBox, long tileMatrixSetSrsId) {
	return createTileTableWithMetadata(ContentsDataType.TILES, tableName,
			contentsBoundingBox, contentsSrsId, tileMatrixSetBoundingBox,
			tileMatrixSetSrsId);
}
 
Example 16
Source Project: geopackage-core-java   Source File: ContentsDao.java    License: MIT License 5 votes vote down vote up
/**
 * Get or create a Tile Matrix Set DAO
 * 
 * @return tile matrix set dao
 * @throws SQLException
 *             upon dao creation failure
 */
private TileMatrixSetDao getTileMatrixSetDao() throws SQLException {
	if (tileMatrixSetDao == null) {
		tileMatrixSetDao = DaoManager.createDao(connectionSource,
				TileMatrixSet.class);
	}
	return tileMatrixSetDao;
}
 
Example 17
Source Project: geopackage-core-java   Source File: SpatialReferenceSystemDao.java    License: MIT License 5 votes vote down vote up
/**
 * Delete the Spatial Reference System, cascading
 * 
 * @param srs
 *            spatial reference system
 * @return deleted count
 * @throws SQLException
 *             upon deletion failure
 */
public int deleteCascade(SpatialReferenceSystem srs) throws SQLException {
	int count = 0;

	if (srs != null) {

		// Delete Contents
		ForeignCollection<Contents> contentsCollection = srs.getContents();
		if (!contentsCollection.isEmpty()) {
			ContentsDao dao = getContentsDao();
			dao.deleteCascade(contentsCollection);
		}

		// Delete Geometry Columns
		GeometryColumnsDao geometryColumnsDao = getGeometryColumnsDao();
		if (geometryColumnsDao.isTableExists()) {
			ForeignCollection<GeometryColumns> geometryColumnsCollection = srs
					.getGeometryColumns();
			if (!geometryColumnsCollection.isEmpty()) {
				geometryColumnsDao.delete(geometryColumnsCollection);
			}
		}

		// Delete Tile Matrix Set
		TileMatrixSetDao tileMatrixSetDao = getTileMatrixSetDao();
		if (tileMatrixSetDao.isTableExists()) {
			ForeignCollection<TileMatrixSet> tileMatrixSetCollection = srs
					.getTileMatrixSet();
			if (!tileMatrixSetCollection.isEmpty()) {
				tileMatrixSetDao.delete(tileMatrixSetCollection);
			}
		}

		// Delete
		count = delete(srs);
	}
	return count;
}
 
Example 18
Source Project: geopackage-core-java   Source File: SpatialReferenceSystemDao.java    License: MIT License 5 votes vote down vote up
/**
 * Get or create a Tile Matrix Set DAO
 * 
 * @return tile matrix set dao
 * @throws SQLException
 *             upon creation failure
 */
private TileMatrixSetDao getTileMatrixSetDao() throws SQLException {
	if (tileMatrixSetDao == null) {
		tileMatrixSetDao = DaoManager.createDao(connectionSource,
				TileMatrixSet.class);
	}
	return tileMatrixSetDao;
}
 
Example 19
Source Project: geopackage-java   Source File: CoverageData.java    License: MIT License 5 votes vote down vote up
/**
 * Get a Tiled Gridded Coverage Data
 * 
 * @param geoPackage
 *            GeoPackage
 * @param tileDao
 *            tile dao
 * @param width
 *            coverage data response width
 * @param height
 *            coverage data response height
 * @param requestProjection
 *            request projection
 * @return coverage data
 */
public static CoverageData<?> getCoverageData(GeoPackage geoPackage,
		TileDao tileDao, Integer width, Integer height,
		Projection requestProjection) {

	TileMatrixSet tileMatrixSet = tileDao.getTileMatrixSet();
	GriddedCoverageDao griddedCoverageDao = geoPackage
			.getGriddedCoverageDao();

	GriddedCoverage griddedCoverage = null;
	try {
		if (griddedCoverageDao.isTableExists()) {
			griddedCoverage = griddedCoverageDao.query(tileMatrixSet);
		}
	} catch (SQLException e) {
		throw new GeoPackageException(
				"Failed to get Gridded Coverage for table name: "
						+ tileMatrixSet.getTableName(), e);
	}

	CoverageData<?> coverageData = null;

	GriddedCoverageDataType dataType = griddedCoverage.getDataType();
	switch (dataType) {
	case INTEGER:
		coverageData = new CoverageDataPng(geoPackage, tileDao, width,
				height, requestProjection);
		break;
	case FLOAT:
		coverageData = new CoverageDataTiff(geoPackage, tileDao, width,
				height, requestProjection);
		break;
	default:
		throw new GeoPackageException(
				"Unsupported Gridded Coverage Data Type: " + dataType);
	}

	return coverageData;
}
 
Example 20
Source Project: geopackage-java   Source File: CoverageData.java    License: MIT License 5 votes vote down vote up
/**
 * Create the coverage data tile table with metadata and extension
 * 
 * @param geoPackage
 *            GeoPackage
 * @param tableName
 *            table name
 * @param contentsBoundingBox
 *            contents bounding box
 * @param contentsSrsId
 *            contents srs id
 * @param tileMatrixSetBoundingBox
 *            tile matrix set bounding box
 * @param tileMatrixSetSrsId
 *            tile matrix set srs id
 * @param dataType
 *            gridded coverage data type
 * @return coverage data
 */
public static CoverageData<?> createTileTableWithMetadata(
		GeoPackage geoPackage, String tableName,
		BoundingBox contentsBoundingBox, long contentsSrsId,
		BoundingBox tileMatrixSetBoundingBox, long tileMatrixSetSrsId,
		GriddedCoverageDataType dataType) {

	TileMatrixSet tileMatrixSet = CoverageDataCore
			.createTileTableWithMetadata(geoPackage, tableName,
					contentsBoundingBox, contentsSrsId,
					tileMatrixSetBoundingBox, tileMatrixSetSrsId);
	TileDao tileDao = geoPackage.getTileDao(tileMatrixSet);

	CoverageData<?> coverageData = null;
	switch (dataType) {
	case INTEGER:
		coverageData = new CoverageDataPng(geoPackage, tileDao);
		break;
	case FLOAT:
		coverageData = new CoverageDataTiff(geoPackage, tileDao);
		break;
	default:
		throw new GeoPackageException(
				"Unsupported Gridded Coverage Data Type: " + dataType);
	}

	coverageData.getOrCreate();

	return coverageData;
}
 
Example 21
Source Project: geopackage-java   Source File: GeoPackageImpl.java    License: MIT License 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public TileDao getTileDao(Contents contents) {

	if (contents == null) {
		throw new GeoPackageException("Non null "
				+ Contents.class.getSimpleName() + " is required to create "
				+ TileDao.class.getSimpleName());
	}

	TileMatrixSet tileMatrixSet = null;
	try {
		tileMatrixSet = getTileMatrixSetDao()
				.queryForId(contents.getTableName());
	} catch (SQLException e) {
		throw new GeoPackageException("No "
				+ TileMatrixSet.class.getSimpleName()
				+ " could be retrieved for "
				+ Contents.class.getSimpleName() + " " + contents.getId());
	}

	if (tileMatrixSet == null) {
		throw new GeoPackageException("No "
				+ TileMatrixSet.class.getSimpleName() + " exists for "
				+ Contents.class.getSimpleName() + " " + contents.getId());
	}

	return getTileDao(tileMatrixSet);
}
 
Example 22
Source Project: geopackage-java   Source File: GeoPackageImpl.java    License: MIT License 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public TileDao getTileDao(String tableName) {

	TileMatrixSetDao dao = getTileMatrixSetDao();
	List<TileMatrixSet> tileMatrixSetList;
	try {
		tileMatrixSetList = dao.queryForEq(TileMatrixSet.COLUMN_TABLE_NAME,
				tableName);
	} catch (SQLException e) {
		throw new GeoPackageException("Failed to retrieve "
				+ TileDao.class.getSimpleName() + " for table name: "
				+ tableName + ". Exception retrieving "
				+ TileMatrixSet.class.getSimpleName() + ".", e);
	}
	if (tileMatrixSetList.isEmpty()) {
		throw new GeoPackageException(
				"No Tile Table exists for table name: " + tableName
						+ ", Tile Tables: " + getTileTables());
	} else if (tileMatrixSetList.size() > 1) {
		// This shouldn't happen with the table name primary key on tile
		// matrix set table
		throw new GeoPackageException("Unexpected state. More than one "
				+ TileMatrixSet.class.getSimpleName()
				+ " matched for table name: " + tableName + ", count: "
				+ tileMatrixSetList.size());
	}
	return getTileDao(tileMatrixSetList.get(0));
}
 
Example 23
Source Project: geopackage-java   Source File: GeoPackageTextOutput.java    License: MIT License 5 votes vote down vote up
/**
 * Build text from a tile table
 * 
 * @param table
 *            tile table
 * @return text
 */
public String tileTable(String table) {

	StringBuilder output = new StringBuilder();
	TileDao tileDao = geoPackage.getTileDao(table);
	output.append("Table Name: " + tileDao.getTableName());
	long minZoom = tileDao.getMinZoom();
	long maxZoom = tileDao.getMaxZoom();
	output.append("\nMin Zoom: " + minZoom);
	output.append("\nMax Zoom: " + maxZoom);
	output.append("\nTiles: " + tileDao.count());

	TileMatrixSet tileMatrixSet = tileDao.getTileMatrixSet();

	output.append("\n\nContents\n\n")
			.append(textOutput(tileMatrixSet.getContents()));

	output.append("\n\nTile Matrix Set\n\n")
			.append(textOutput(tileMatrixSet));

	output.append("\n\n Tile Matrices");

	for (long zoom = minZoom; zoom <= maxZoom; zoom++) {
		TileMatrix tileMatrix = tileDao.getTileMatrix(zoom);
		if (tileMatrix != null) {
			output.append("\n\n").append(textOutput(tileMatrix));
			output.append("\n\tTiles: " + tileDao.count(zoom));
			BoundingBox boundingBox = tileDao.getBoundingBox(zoom);
			output.append("\n\tTile Bounds: \n")
					.append(textOutput(boundingBox));
		}
	}

	return output.toString();
}
 
Example 24
Source Project: geopackage-java   Source File: TileProperties.java    License: MIT License 5 votes vote down vote up
/**
 * Write the properties file using the tile dao
 * 
 * @param tileDao
 *            tile dao
 */
public void writeFile(TileDao tileDao) {
	try {
		PrintWriter pw = new PrintWriter(propertiesFile);

		TileMatrixSet tileMatrixSet = tileDao.getTileMatrixSet();
		pw.println(GEOPACKAGE_PROPERTIES_EPSG + "="
				+ tileMatrixSet.getSrs().getOrganizationCoordsysId());
		pw.println(GEOPACKAGE_PROPERTIES_MIN_X + "="
				+ tileMatrixSet.getMinX());
		pw.println(GEOPACKAGE_PROPERTIES_MAX_X + "="
				+ tileMatrixSet.getMaxX());
		pw.println(GEOPACKAGE_PROPERTIES_MIN_Y + "="
				+ tileMatrixSet.getMinY());
		pw.println(GEOPACKAGE_PROPERTIES_MAX_Y + "="
				+ tileMatrixSet.getMaxY());

		for (TileMatrix tileMatrix : tileDao.getTileMatrices()) {
			long zoom = tileMatrix.getZoomLevel();
			pw.println(getMatrixWidthProperty(zoom) + "="
					+ tileMatrix.getMatrixWidth());
			pw.println(getMatrixHeightProperty(zoom) + "="
					+ tileMatrix.getMatrixHeight());
		}

		pw.close();

	} catch (FileNotFoundException e) {
		throw new GeoPackageException(
				"GeoPackage file format properties file could not be created: "
						+ propertiesFile, e);
	}
}
 
Example 25
Source Project: geopackage-java   Source File: CoverageDataTestUtils.java    License: 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 26
Source Project: geopackage-java   Source File: CoverageDataTestUtils.java    License: 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 27
Source Project: geopackage-java   Source File: TileUtils.java    License: 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 28
Source Project: geopackage-android-map   Source File: TestSetupTeardown.java    License: 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();

	int matrixWidthAndHeight = 2;
	double pixelXSize = 69237.2;
	double pixelYSize = 68412.1;

	// 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();

	// 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 29
Source Project: geopackage-android   Source File: GeoPackageImpl.java    License: MIT License 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public TileDao getTileDao(TileMatrixSet tileMatrixSet) {

    if (tileMatrixSet == null) {
        throw new GeoPackageException("Non null "
                + TileMatrixSet.class.getSimpleName()
                + " is required to create " + TileDao.class.getSimpleName());
    }

    // Get the Tile Matrix collection, order by zoom level ascending & pixel
    // size descending per requirement 51
    List<TileMatrix> tileMatrices;
    try {
        TileMatrixDao tileMatrixDao = getTileMatrixDao();
        QueryBuilder<TileMatrix, TileMatrixKey> qb = tileMatrixDao
                .queryBuilder();
        qb.where().eq(TileMatrix.COLUMN_TABLE_NAME,
                tileMatrixSet.getTableName());
        qb.orderBy(TileMatrix.COLUMN_ZOOM_LEVEL, true);
        qb.orderBy(TileMatrix.COLUMN_PIXEL_X_SIZE, false);
        qb.orderBy(TileMatrix.COLUMN_PIXEL_Y_SIZE, false);
        PreparedQuery<TileMatrix> query = qb.prepare();
        tileMatrices = tileMatrixDao.query(query);
    } catch (SQLException e) {
        throw new GeoPackageException("Failed to retrieve "
                + TileDao.class.getSimpleName() + " for table name: "
                + tileMatrixSet.getTableName() + ". Exception retrieving "
                + TileMatrix.class.getSimpleName() + " collection.", e);
    }

    // Read the existing table and create the dao
    TileTableReader tableReader = new TileTableReader(
            tileMatrixSet.getTableName());
    final TileTable tileTable = tableReader.readTable(database);
    tileTable.setContents(tileMatrixSet.getContents());
    TileDao dao = new TileDao(getName(), database, tileMatrixSet, tileMatrices,
            tileTable);

    // Register the table name (with and without quotes) to wrap cursors with the tile cursor
    registerCursorWrapper(tileMatrixSet.getTableName(),
            new GeoPackageCursorWrapper() {

                @Override
                public Cursor wrapCursor(Cursor cursor) {
                    return new TileCursor(tileTable, cursor);
                }
            });

    return dao;
}
 
Example 30
Source Project: geopackage-android   Source File: TileDao.java    License: 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());
    }

}