mil.nga.geopackage.core.srs.SpatialReferenceSystemDao Java Examples

The following examples show how to use mil.nga.geopackage.core.srs.SpatialReferenceSystemDao. 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: GeoPackageRepository.java    From geopackage-mapcache-android with MIT License 6 votes vote down vote up
/**
 * Create a tile table in the given GeoPackage
 * @return
 */
public boolean createTileTable(String gpName, BoundingBox boundingBox, long epsg, String tableName, TileScaling scaling){
    GeoPackage geoPackage = manager.open(gpName);
    try {
        // Create the srs if needed
        SpatialReferenceSystemDao srsDao = geoPackage.getSpatialReferenceSystemDao();
        SpatialReferenceSystem srs = srsDao.getOrCreateFromEpsg(epsg);
        // Create the tile table
        mil.nga.sf.proj.Projection projection = ProjectionFactory.getProjection(epsg);
        BoundingBox bbox = LoadTilesTask.transform(boundingBox, projection);
        geoPackage.createTileTableWithMetadata(
                tableName, bbox, srs.getSrsId(),
                bbox, srs.getSrsId());

        TileTableScaling tileTableScaling = new TileTableScaling(geoPackage, tableName);
        tileTableScaling.createOrUpdate(scaling);
    } catch (Exception e) {
        Log.i("Exception", e.toString());
        return false;
    } finally {
        geoPackage.close();
    }
    return true;
}
 
Example #2
Source File: GeoPackageTableCreator.java    From geopackage-core-java with MIT License 6 votes vote down vote up
/**
 * Create the minimum required GeoPackage tables
 */
public void createRequired() {

	// Create the Spatial Reference System table (spec Requirement 10)
	createSpatialReferenceSystem();

	// Create the Contents table (spec Requirement 13)
	createContents();

	// Create the required Spatial Reference Systems (spec Requirement
	// 11)
	try {
		SpatialReferenceSystemDao dao = DaoManager.createDao(
				db.getConnectionSource(), SpatialReferenceSystem.class);
		dao.createWgs84();
		dao.createUndefinedCartesian();
		dao.createUndefinedGeographic();
	} catch (SQLException e) {
		throw new GeoPackageException(
				"Error creating default required Spatial Reference Systems",
				e);
	}
}
 
Example #3
Source File: FeatureInfoBuilder.java    From geopackage-android-map with MIT License 5 votes vote down vote up
/**
 * Project the geometry into the provided projection
 *
 * @param geometryData geometry data
 * @param projection   projection
 */
public void projectGeometry(GeoPackageGeometryData geometryData, Projection projection) {

    if (geometryData.getGeometry() != null) {

        try {
            SpatialReferenceSystemDao srsDao = DaoManager.createDao(featureDao.getDb().getConnectionSource(), SpatialReferenceSystem.class);
            int srsId = geometryData.getSrsId();
            SpatialReferenceSystem srs = srsDao.queryForId((long) srsId);

            if (!projection.equals(srs.getOrganization(), srs.getOrganizationCoordsysId())) {

                Projection geomProjection = srs.getProjection();
                ProjectionTransform transform = geomProjection.getTransformation(projection);

                Geometry projectedGeometry = transform.transform(geometryData.getGeometry());
                geometryData.setGeometry(projectedGeometry);
                SpatialReferenceSystem projectionSrs = srsDao.getOrCreateCode(projection.getAuthority(), Long.parseLong(projection.getCode()));
                geometryData.setSrsId((int) projectionSrs.getSrsId());
            }
        } catch (SQLException e) {
            throw new GeoPackageException("Failed to project geometry to projection with Authority: "
                    + projection.getAuthority() + ", Code: " + projection.getCode(), e);
        }
    }

}
 
Example #4
Source File: GeoPackageCoreImpl.java    From geopackage-core-java with MIT License 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public SpatialReferenceSystemDao getSpatialReferenceSystemDao() {
	SpatialReferenceSystemDao dao = createDao(SpatialReferenceSystem.class);
	dao.setCrsWktExtension(new CrsWktExtension(this));
	return dao;
}
 
Example #5
Source File: FeatureCoreGenerator.java    From geopackage-core-java with MIT License 5 votes vote down vote up
/**
 * Create the Spatial Reference System
 * 
 * @throws SQLException
 *             upon error
 */
public void createSrs() throws SQLException {

	SpatialReferenceSystemDao srsDao = geoPackage
			.getSpatialReferenceSystemDao();
	Projection srsProjection = getSrsProjection();
	srs = srsDao.getOrCreateCode(srsProjection.getAuthority(),
			Long.parseLong(srsProjection.getCode()));

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

    CrsWktExtension wktExtension = new CrsWktExtension(geoPackage);
    wktExtension.getOrCreate();

    SpatialReferenceSystemDao srsDao = geoPackage
            .getSpatialReferenceSystemDao();

    SpatialReferenceSystem srs = srsDao.queryForOrganizationCoordsysId(
            ProjectionConstants.AUTHORITY_EPSG,
            ProjectionConstants.EPSG_WORLD_GEODETIC_SYSTEM);

    SpatialReferenceSystem testSrs = new SpatialReferenceSystem();
    testSrs.setSrsName("test");
    testSrs.setSrsId(12345);
    testSrs.setOrganization("test_org");
    testSrs.setOrganizationCoordsysId(testSrs.getSrsId());
    testSrs.setDefinition(srs.getDefinition());
    testSrs.setDescription(srs.getDescription());
    testSrs.setDefinition_12_063(srs.getDefinition_12_063());
    srsDao.create(testSrs);

    SpatialReferenceSystem testSrs2 = new SpatialReferenceSystem();
    testSrs2.setSrsName("test2");
    testSrs2.setSrsId(54321);
    testSrs2.setOrganization("test_org");
    testSrs2.setOrganizationCoordsysId(testSrs2.getSrsId());
    testSrs2.setDefinition(srs.getDefinition());
    testSrs2.setDescription(srs.getDescription());
    srsDao.create(testSrs2);

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

	CrsWktExtension wktExtension = new CrsWktExtension(geoPackage);
	wktExtension.getOrCreate();

	SpatialReferenceSystemDao srsDao = geoPackage
			.getSpatialReferenceSystemDao();

	SpatialReferenceSystem srs = srsDao.queryForOrganizationCoordsysId(
			ProjectionConstants.AUTHORITY_EPSG,
			ProjectionConstants.EPSG_WORLD_GEODETIC_SYSTEM);

	SpatialReferenceSystem testSrs = new SpatialReferenceSystem();
	testSrs.setSrsName("test");
	testSrs.setSrsId(12345);
	testSrs.setOrganization("test_org");
	testSrs.setOrganizationCoordsysId(testSrs.getSrsId());
	testSrs.setDefinition(srs.getDefinition());
	testSrs.setDescription(srs.getDescription());
	testSrs.setDefinition_12_063(srs.getDefinition_12_063());
	srsDao.create(testSrs);

	SpatialReferenceSystem testSrs2 = new SpatialReferenceSystem();
	testSrs2.setSrsName("test2");
	testSrs2.setSrsId(54321);
	testSrs2.setOrganization("test_org");
	testSrs2.setOrganizationCoordsysId(testSrs2.getSrsId());
	testSrs2.setDefinition(srs.getDefinition());
	testSrs2.setDescription(srs.getDescription());
	srsDao.create(testSrs2);

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

	SpatialReferenceSystemDao srsDao = geoPackage
			.getSpatialReferenceSystemDao();

	SpatialReferenceSystem srs = srsDao.getOrCreateCode(
			ProjectionConstants.AUTHORITY_EPSG,
			(long) ProjectionConstants.EPSG_WORLD_GEODETIC_SYSTEM);

	GeometryExtensions extensions = new GeometryExtensions(geoPackage);

	String tableName = "non_linear_geometries";

	List<Geometry> geometries = new ArrayList<>();
	List<String> geometryNames = new ArrayList<>();

	CircularString circularString = new CircularString();
	circularString.addPoint(new Point(-122.358, 47.653));
	circularString.addPoint(new Point(-122.348, 47.649));
	circularString.addPoint(new Point(-122.348, 47.658));
	circularString.addPoint(new Point(-122.358, 47.658));
	circularString.addPoint(new Point(-122.358, 47.653));

	for (int i = GeometryCodes
			.getCode(GeometryType.CIRCULARSTRING); i <= GeometryCodes
					.getCode(GeometryType.SURFACE); i++) {

		GeometryType geometryType = GeometryCodes.getGeometryType(i);
		extensions.getOrCreate(tableName, GEOMETRY_COLUMN, geometryType);

		Geometry geometry = null;
		String name = geometryType.getName().toLowerCase();

		switch (geometryType) {
		case CIRCULARSTRING:
			geometry = circularString;
			break;
		case COMPOUNDCURVE:
			CompoundCurve compoundCurve = new CompoundCurve();
			compoundCurve.addLineString(circularString);
			geometry = compoundCurve;
			break;
		case CURVEPOLYGON:
			CurvePolygon<CircularString> curvePolygon = new CurvePolygon<>();
			curvePolygon.addRing(circularString);
			geometry = curvePolygon;
			break;
		case MULTICURVE:
			MultiLineString multiCurve = new MultiLineString();
			multiCurve.addLineString(circularString);
			geometry = multiCurve;
			break;
		case MULTISURFACE:
			MultiPolygon multiSurface = new MultiPolygon();
			Polygon polygon = new Polygon();
			polygon.addRing(circularString);
			multiSurface.addPolygon(polygon);
			geometry = multiSurface;
			break;
		case CURVE:
			CompoundCurve curve = new CompoundCurve();
			curve.addLineString(circularString);
			geometry = curve;
			break;
		case SURFACE:
			CurvePolygon<CircularString> surface = new CurvePolygon<>();
			surface.addRing(circularString);
			geometry = surface;
			break;
		default:
			throw new GeoPackageException(
					"Unexpected Geometry Type: " + geometryType);
		}

		geometries.add(geometry);
		geometryNames.add(name);

	}

	createFeatures(geoPackage, srs, tableName, GeometryType.GEOMETRY,
			geometries, geometryNames);

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

	SpatialReferenceSystemDao srsDao = geoPackage
			.getSpatialReferenceSystemDao();

	SpatialReferenceSystem srs = srsDao.getOrCreateCode(
			ProjectionConstants.AUTHORITY_EPSG,
			(long) ProjectionConstants.EPSG_WORLD_GEODETIC_SYSTEM);

	geoPackage.createGeometryColumnsTable();

	createFeatures1(geoPackage, srs);
	createFeatures2(geoPackage, srs);

}
 
Example #10
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 #11
Source File: TransactionTest.java    From geopackage-java with MIT License 4 votes vote down vote up
/**
 * Test an ORMLite transaction
 * 
 * @param geoPackage
 *            GeoPackage
 * @param successful
 *            true for a successful transaction
 * @throws SQLException
 *             upon error
 */
private void testORMLite(final GeoPackage geoPackage,
		final boolean successful) throws SQLException {

	final String tableName = "test_table";

	final Contents contents = new Contents();
	contents.setTableName(tableName);
	contents.setDataType(ContentsDataType.ATTRIBUTES);

	if (!geoPackage.isTable(tableName)) {
		geoPackage.execSQL("CREATE TABLE " + tableName
				+ " (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL)");
	}

	final SpatialReferenceSystemDao srsDao = geoPackage
			.getSpatialReferenceSystemDao();
	final ContentsDao contentsDao = geoPackage.getContentsDao();

	long srsCount = srsDao.countOf();
	long contentsCount = contentsDao.countOf();

	Callable<Void> callable = new Callable<Void>() {
		public Void call() throws Exception {

			SpatialReferenceSystem srs = srsDao.createWgs84Geographical3D();

			contents.setSrs(srs);
			contentsDao.create(contents);

			if (!successful) {
				throw new SQLException();
			}

			return null;
		}
	};

	try {
		geoPackage.callInTransaction(callable);
	} catch (SQLException e) {
		if (successful) {
			TestCase.fail(e.getMessage());
		}
	}

	TestCase.assertEquals(successful ? srsCount + 1 : srsCount,
			srsDao.countOf());
	TestCase.assertEquals(successful ? contentsCount + 1 : contentsCount,
			contentsDao.countOf());

	TestCase.assertEquals(successful,
			geoPackage.isAttributeTable(tableName));

}
 
Example #12
Source File: GeometryColumnsUtils.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();
	GeometryColumnsDao dao = geoPackage.getGeometryColumnsDao();

	if (dao.isTableExists()) {

		// Get current count
		long count = dao.countOf();
		TestCase.assertEquals(count, dao.getFeatureTables().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.FEATURES);
		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 feature table
		geoPackage.createFeatureTable(TestUtils.buildFeatureTable(
				contents.getTableName(), "geom", GeometryType.GEOMETRY));

		contentsDao.create(contents);

		String columnName = "TEST_COLUMN_NAME";
		GeometryType geometryType = GeometryType.POINT;
		byte z = 2;
		byte m = 2;

		// Create new geometry columns
		GeometryColumns geometryColumns = new GeometryColumns();
		geometryColumns.setContents(contents);
		geometryColumns.setColumnName(columnName);
		geometryColumns.setGeometryType(geometryType);
		geometryColumns.setSrs(contents.getSrs());
		geometryColumns.setZ(z);
		geometryColumns.setM(m);
		dao.create(geometryColumns);

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

		// Verify saved geometry columns
		GeometryColumns queryGeometryColumns = dao
				.queryForId(geometryColumns.getId());
		TestCase.assertEquals(contents.getId(),
				queryGeometryColumns.getTableName());
		TestCase.assertEquals(columnName,
				queryGeometryColumns.getColumnName());
		TestCase.assertEquals(geometryType,
				queryGeometryColumns.getGeometryType());
		TestCase.assertEquals(contents.getSrsId().longValue(),
				queryGeometryColumns.getSrsId());
		TestCase.assertEquals(z, queryGeometryColumns.getZ());
		TestCase.assertEquals(m, queryGeometryColumns.getM());
		TestCase.assertEquals(contents.getId(), queryGeometryColumns
				.getContents().getId());
		TestCase.assertEquals(contents.getSrsId().longValue(),
				queryGeometryColumns.getSrs().getId());
	}
}
 
Example #13
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 #14
Source File: CoverageDataTiffImportTest.java    From geopackage-java with MIT License 4 votes vote down vote up
/**
 * Test 10 random locations and optionally print
 * 
 * @throws Exception
 */
@Test
public void testRandomLocations() throws Exception {

	BoundingBox projectedBoundingBox = null;

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

	for (String coverageTable : coverageDataTables) {

		TileMatrixSet tileMatrixSet = dao.queryForId(coverageTable);

		BoundingBox boundingBox = tileMatrixSet.getBoundingBox();
		if (PRINT) {
			System.out.println("Min Latitude: "
					+ boundingBox.getMinLatitude());
			System.out.println("Max Latitude: "
					+ boundingBox.getMaxLatitude());
			System.out.println("Min Longitude: "
					+ boundingBox.getMinLongitude());
			System.out.println("Max Longitude: "
					+ boundingBox.getMaxLongitude());
			System.out.println();
		}
		SpatialReferenceSystemDao srsDao = geoPackage
				.getSpatialReferenceSystemDao();
		long srsId = tileMatrixSet.getSrsId();
		SpatialReferenceSystem srs = srsDao.queryForId(srsId);
		Projection projection = srs.getProjection();
		Projection requestProjection = ProjectionFactory
				.getProjection(ProjectionConstants.EPSG_WORLD_GEODETIC_SYSTEM);
		ProjectionTransform coverageToRequest = projection
				.getTransformation(requestProjection);
		projectedBoundingBox = boundingBox.transform(coverageToRequest);

	}
	if (PRINT) {
		System.out.println("Min Latitude: "
				+ projectedBoundingBox.getMinLatitude());
		System.out.println("Max Latitude: "
				+ projectedBoundingBox.getMaxLatitude());
		System.out.println("Min Longitude: "
				+ projectedBoundingBox.getMinLongitude());
		System.out.println("Max Longitude: "
				+ projectedBoundingBox.getMaxLongitude());
		System.out.println();
	}

	double latDistance = projectedBoundingBox.getMaxLatitude()
			- projectedBoundingBox.getMinLatitude();
	double lonDistance = projectedBoundingBox.getMaxLongitude()
			- projectedBoundingBox.getMinLongitude();

	for (int i = 0; i < 10; i++) {

		// Get a random coordinate
		double latitude = latDistance * .9 * Math.random()
				+ projectedBoundingBox.getMinLatitude()
				+ (.05 * latDistance);
		double longitude = lonDistance * .9 * Math.random()
				+ projectedBoundingBox.getMinLongitude()
				+ (.05 * lonDistance);
		testLocation(latitude, longitude);
		if (PRINT) {
			System.out.println();
		}
	}
}
 
Example #15
Source File: CoverageDataPngImportTest.java    From geopackage-java with MIT License 4 votes vote down vote up
/**
 * Test 10 random locations and optionally print
 * 
 * @throws Exception
 */
@Test
public void testRandomLocations() throws Exception {

	BoundingBox projectedBoundingBox = null;

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

	for (String coverageTable : coverageDataTables) {

		TileMatrixSet tileMatrixSet = dao.queryForId(coverageTable);

		BoundingBox boundingBox = tileMatrixSet.getBoundingBox();
		if (PRINT) {
			System.out.println("Min Latitude: "
					+ boundingBox.getMinLatitude());
			System.out.println("Max Latitude: "
					+ boundingBox.getMaxLatitude());
			System.out.println("Min Longitude: "
					+ boundingBox.getMinLongitude());
			System.out.println("Max Longitude: "
					+ boundingBox.getMaxLongitude());
			System.out.println();
		}
		SpatialReferenceSystemDao srsDao = geoPackage
				.getSpatialReferenceSystemDao();
		long srsId = tileMatrixSet.getSrsId();
		SpatialReferenceSystem srs = srsDao.queryForId(srsId);
		Projection projection = srs.getProjection();
		Projection requestProjection = ProjectionFactory
				.getProjection(ProjectionConstants.EPSG_WORLD_GEODETIC_SYSTEM);
		ProjectionTransform coverageToRequest = projection
				.getTransformation(requestProjection);
		projectedBoundingBox = boundingBox.transform(coverageToRequest);

	}
	if (PRINT) {
		System.out.println("Min Latitude: "
				+ projectedBoundingBox.getMinLatitude());
		System.out.println("Max Latitude: "
				+ projectedBoundingBox.getMaxLatitude());
		System.out.println("Min Longitude: "
				+ projectedBoundingBox.getMinLongitude());
		System.out.println("Max Longitude: "
				+ projectedBoundingBox.getMaxLongitude());
		System.out.println();
	}

	double latDistance = projectedBoundingBox.getMaxLatitude()
			- projectedBoundingBox.getMinLatitude();
	double lonDistance = projectedBoundingBox.getMaxLongitude()
			- projectedBoundingBox.getMinLongitude();

	for (int i = 0; i < 10; i++) {

		// Get a random coordinate
		double latitude = latDistance * .9 * Math.random()
				+ projectedBoundingBox.getMinLatitude()
				+ (.05 * latDistance);
		double longitude = lonDistance * .9 * Math.random()
				+ projectedBoundingBox.getMinLongitude()
				+ (.05 * lonDistance);
		testLocation(latitude, longitude);
		if (PRINT) {
			System.out.println();
		}
	}
}
 
Example #16
Source File: GeoPackageGeometryDataUtils.java    From geopackage-java with MIT License 4 votes vote down vote up
/**
 * Test transforming geometries between projections
 * 
 * @param geoPackage
 * @throws SQLException
 * @throws IOException
 */
public static void testGeometryProjectionTransform(GeoPackage geoPackage)
		throws SQLException, IOException {

	GeometryColumnsDao geometryColumnsDao = geoPackage
			.getGeometryColumnsDao();

	if (geometryColumnsDao.isTableExists()) {
		List<GeometryColumns> results = geometryColumnsDao.queryForAll();

		for (GeometryColumns geometryColumns : results) {

			FeatureDao dao = geoPackage.getFeatureDao(geometryColumns);
			TestCase.assertNotNull(dao);

			FeatureResultSet cursor = dao.queryForAll();

			while (cursor.moveToNext()) {

				GeoPackageGeometryData geometryData = cursor.getGeometry();
				if (geometryData != null) {

					Geometry geometry = geometryData.getGeometry();

					if (geometry != null) {

						SpatialReferenceSystemDao srsDao = geoPackage
								.getSpatialReferenceSystemDao();
						long srsId = geometryData.getSrsId();
						SpatialReferenceSystem srs = srsDao
								.queryForId(srsId);

						long epsg = srs.getOrganizationCoordsysId();
						Projection projection = srs.getProjection();
						long toEpsg = -1;
						if (epsg == ProjectionConstants.EPSG_WORLD_GEODETIC_SYSTEM) {
							toEpsg = ProjectionConstants.EPSG_WEB_MERCATOR;
						} else {
							toEpsg = ProjectionConstants.EPSG_WORLD_GEODETIC_SYSTEM;
						}
						ProjectionTransform transformTo = projection
								.getTransformation(toEpsg);
						ProjectionTransform transformFrom = srs
								.getTransformation(transformTo
										.getToProjection());

						byte[] bytes = geometryData.getWkbBytes();

						Geometry projectedGeometry = transformTo
								.transform(geometry);
						GeoPackageGeometryData projectedGeometryData = new GeoPackageGeometryData(
								-1);
						projectedGeometryData
								.setGeometry(projectedGeometry);
						projectedGeometryData.toBytes();
						byte[] projectedBytes = projectedGeometryData
								.getWkbBytes();

						if (epsg > 0) {
							TestCase.assertFalse(equalByteArrays(bytes,
									projectedBytes));
						}

						Geometry restoredGeometry = transformFrom
								.transform(projectedGeometry);

						compareGeometries(geometry, restoredGeometry, .001);
					}
				}

			}
			cursor.close();
		}
	}
}
 
Example #17
Source File: SpatialReferenceSystemUtils.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 dao = geoPackage
			.getSpatialReferenceSystemDao();

	// Get current count
	long count = dao.countOf();

	String srsName = "TEST_SRS_NAME";
	long srsId = 123456l;
	String organization = "TEST_ORG";
	int organizationCoordSysId = 123456;
	String definition = "TEST_DEFINITION";
	String description = "TEST_DESCRIPTION";

	// Create new srs
	SpatialReferenceSystem srs = new SpatialReferenceSystem();
	srs.setSrsName(srsName);
	srs.setSrsId(srsId);
	srs.setOrganization(organization);
	srs.setOrganizationCoordsysId(organizationCoordSysId);
	srs.setDefinition(definition);
	srs.setDescription(description);
	dao.create(srs);

	// Verify count
	long newCount = dao.countOf();
	TestCase.assertEquals(count + 1, newCount);

	// Verify saved srs
	SpatialReferenceSystem querySrs = dao.queryForId(srsId);
	TestCase.assertEquals(srsName, querySrs.getSrsName());
	TestCase.assertEquals(srsId, querySrs.getSrsId());
	TestCase.assertEquals(organization, querySrs.getOrganization());
	TestCase.assertEquals(organizationCoordSysId,
			querySrs.getOrganizationCoordsysId());
	TestCase.assertEquals(definition, querySrs.getDefinition());
	TestCase.assertEquals(description, querySrs.getDescription());

	// Test copied srs
	SpatialReferenceSystem copySrs = new SpatialReferenceSystem(querySrs);
	TestCase.assertEquals(querySrs.getSrsName(), copySrs.getSrsName());
	TestCase.assertEquals(querySrs.getId(), copySrs.getId());
	TestCase.assertEquals(querySrs.getOrganization(),
			copySrs.getOrganization());
	TestCase.assertEquals(querySrs.getOrganizationCoordsysId(),
			copySrs.getOrganizationCoordsysId());
	TestCase.assertEquals(querySrs.getDefinition(), copySrs.getDefinition());
	TestCase.assertEquals(querySrs.getDescription(),
			copySrs.getDescription());
	TestCase.assertEquals(querySrs.getDefinition_12_063(),
			copySrs.getDefinition_12_063());

	// Change pk
	long copySrsId = 654321l;
	copySrs.setSrsId(copySrsId);

	dao.create(copySrs);

	// Verify count
	long newCount2 = dao.countOf();
	TestCase.assertEquals(count + 2, newCount2);

	// Verify saved contents
	SpatialReferenceSystem queryCopiedSrs = dao.queryForId(copySrsId);
	TestCase.assertEquals(querySrs.getSrsName(),
			queryCopiedSrs.getSrsName());
	TestCase.assertEquals(copySrsId, queryCopiedSrs.getSrsId());
	TestCase.assertEquals(querySrs.getOrganization(),
			queryCopiedSrs.getOrganization());
	TestCase.assertEquals(querySrs.getOrganizationCoordsysId(),
			queryCopiedSrs.getOrganizationCoordsysId());
	TestCase.assertEquals(querySrs.getDefinition(),
			queryCopiedSrs.getDefinition());
	TestCase.assertEquals(querySrs.getDescription(),
			queryCopiedSrs.getDescription());

}
 
Example #18
Source File: TileMatrixUtils.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();
	TileMatrixDao dao = geoPackage.getTileMatrixDao();

	if (dao.isTableExists()) {

		// Get current count
		long count = dao.countOf();

		// 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
		int zoom = 3;
		int matrixWidth = 4;
		int matrixHeight = 5;
		int tileWidth = 128;
		int tileHeight = 256;
		double pixelXSize = 889.5;
		double pixelYSize = 900.1;

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

		// Verify count
		long newCount = dao.countOf();
		TestCase.assertEquals(count + 1, newCount);

		// Verify saved matrix tile
		TileMatrix queryTileMatrix = dao.queryForId(tileMatrix.getId());
		TestCase.assertEquals(contents.getId(),
				queryTileMatrix.getTableName());
		TestCase.assertEquals(zoom, queryTileMatrix.getZoomLevel());
		TestCase.assertEquals(matrixWidth, queryTileMatrix.getMatrixWidth());
		TestCase.assertEquals(matrixHeight,
				queryTileMatrix.getMatrixHeight());
		TestCase.assertEquals(tileWidth, queryTileMatrix.getTileWidth());
		TestCase.assertEquals(tileHeight, queryTileMatrix.getTileHeight());
		TestCase.assertEquals(pixelXSize, queryTileMatrix.getPixelXSize());
		TestCase.assertEquals(pixelYSize, queryTileMatrix.getPixelYSize());
		TestCase.assertEquals(contents.getId(), queryTileMatrix
				.getContents().getId());
	}
}
 
Example #19
Source File: GeoPackageRepository.java    From geopackage-mapcache-android with MIT License 4 votes vote down vote up
/**
 * Create an alert dialog with a GeoPackage's details for viewing
 *
 * @param geoPackageName
 * @param activity
 * @return
 */
public AlertDialog getGeoPackageDetailDialog(String geoPackageName, Activity activity) {
    StringBuilder databaseInfo = new StringBuilder();
    GeoPackage geoPackage = manager.open(geoPackageName, false);
    try {
        SpatialReferenceSystemDao srsDao = geoPackage
                .getSpatialReferenceSystemDao();

        List<SpatialReferenceSystem> srsList = srsDao.queryForAll();
        databaseInfo.append("Size: ")
                .append(manager.readableSize(geoPackageName));
        databaseInfo.append("\n\nLocation: ").append(
                manager.isExternal(geoPackageName) ? "External" : "Local");
        databaseInfo.append("\nPath: ").append(manager.getPath(geoPackageName));
        databaseInfo.append("\n\nFeature Tables: ").append(
                geoPackage.getFeatureTables().size());
        databaseInfo.append("\nTile Tables: ").append(
                geoPackage.getTileTables().size());
        databaseInfo.append("\n\nSpatial Reference Systems: ").append(
                srsList.size());
        for (SpatialReferenceSystem srs : srsList) {
            databaseInfo.append("\n");
            addSrs(databaseInfo, srs);
        }

    } catch (Exception e) {
        databaseInfo.append(e.getMessage());
    } finally {
        geoPackage.close();
    }
    AlertDialog viewDialog = new AlertDialog.Builder(activity, R.style.AppCompatAlertDialogStyle)
            .setTitle(geoPackageName)
            .setPositiveButton(R.string.button_ok_label,

                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    }).setMessage(databaseInfo.toString()).create();

    return viewDialog;

}
 
Example #20
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 #21
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 #22
Source File: TileMatrixUtils.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();
	TileMatrixDao dao = geoPackage.getTileMatrixDao();

	if (dao.isTableExists()) {

		// Get current count
		long count = dao.countOf();

		// 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
		int zoom = 3;
		int matrixWidth = 4;
		int matrixHeight = 5;
		int tileWidth = 128;
		int tileHeight = 256;
		double pixelXSize = 889.5;
		double pixelYSize = 900.1;

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

		// Verify count
		long newCount = dao.countOf();
		TestCase.assertEquals(count + 1, newCount);

		// Verify saved matrix tile
		TileMatrix queryTileMatrix = dao.queryForId(tileMatrix.getId());
		TestCase.assertEquals(contents.getId(),
				queryTileMatrix.getTableName());
		TestCase.assertEquals(zoom, queryTileMatrix.getZoomLevel());
		TestCase.assertEquals(matrixWidth, queryTileMatrix.getMatrixWidth());
		TestCase.assertEquals(matrixHeight,
				queryTileMatrix.getMatrixHeight());
		TestCase.assertEquals(tileWidth, queryTileMatrix.getTileWidth());
		TestCase.assertEquals(tileHeight, queryTileMatrix.getTileHeight());
		TestCase.assertEquals(pixelXSize, queryTileMatrix.getPixelXSize());
		TestCase.assertEquals(pixelYSize, queryTileMatrix.getPixelYSize());
		TestCase.assertEquals(contents.getId(), queryTileMatrix
				.getContents().getId());
	}
}
 
Example #23
Source File: SpatialReferenceSystemUtils.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 dao = geoPackage
			.getSpatialReferenceSystemDao();

	// Get current count
	long count = dao.countOf();

	String srsName = "TEST_SRS_NAME";
	long srsId = 123456l;
	String organization = "TEST_ORG";
	int organizationCoordSysId = 123456;
	String definition = "TEST_DEFINITION";
	String description = "TEST_DESCRIPTION";

	// Create new srs
	SpatialReferenceSystem srs = new SpatialReferenceSystem();
	srs.setSrsName(srsName);
	srs.setSrsId(srsId);
	srs.setOrganization(organization);
	srs.setOrganizationCoordsysId(organizationCoordSysId);
	srs.setDefinition(definition);
	srs.setDescription(description);
	dao.create(srs);

	// Verify count
	long newCount = dao.countOf();
	TestCase.assertEquals(count + 1, newCount);

	// Verify saved srs
	SpatialReferenceSystem querySrs = dao.queryForId(srsId);
	TestCase.assertEquals(srsName, querySrs.getSrsName());
	TestCase.assertEquals(srsId, querySrs.getSrsId());
	TestCase.assertEquals(organization, querySrs.getOrganization());
	TestCase.assertEquals(organizationCoordSysId,
			querySrs.getOrganizationCoordsysId());
	TestCase.assertEquals(definition, querySrs.getDefinition());
	TestCase.assertEquals(description, querySrs.getDescription());

	// Test copied srs
	SpatialReferenceSystem copySrs = new SpatialReferenceSystem(querySrs);
	TestCase.assertEquals(querySrs.getSrsName(), copySrs.getSrsName());
	TestCase.assertEquals(querySrs.getId(), copySrs.getId());
	TestCase.assertEquals(querySrs.getOrganization(),
			copySrs.getOrganization());
	TestCase.assertEquals(querySrs.getOrganizationCoordsysId(),
			copySrs.getOrganizationCoordsysId());
	TestCase.assertEquals(querySrs.getDefinition(), copySrs.getDefinition());
	TestCase.assertEquals(querySrs.getDescription(),
			copySrs.getDescription());
	TestCase.assertEquals(querySrs.getDefinition_12_063(),
			copySrs.getDefinition_12_063());

	// Change pk
	long copySrsId = 654321l;
	copySrs.setSrsId(copySrsId);

	dao.create(copySrs);

	// Verify count
	long newCount2 = dao.countOf();
	TestCase.assertEquals(count + 2, newCount2);

	// Verify saved contents
	SpatialReferenceSystem queryCopiedSrs = dao.queryForId(copySrsId);
	TestCase.assertEquals(querySrs.getSrsName(),
			queryCopiedSrs.getSrsName());
	TestCase.assertEquals(copySrsId, queryCopiedSrs.getSrsId());
	TestCase.assertEquals(querySrs.getOrganization(),
			queryCopiedSrs.getOrganization());
	TestCase.assertEquals(querySrs.getOrganizationCoordsysId(),
			queryCopiedSrs.getOrganizationCoordsysId());
	TestCase.assertEquals(querySrs.getDefinition(),
			queryCopiedSrs.getDefinition());
	TestCase.assertEquals(querySrs.getDescription(),
			queryCopiedSrs.getDescription());

}
 
Example #24
Source File: GeoPackageGeometryDataUtils.java    From geopackage-android with MIT License 4 votes vote down vote up
/**
 * Test transforming geometries between projections
 *
 * @param geoPackage
 * @throws SQLException
 * @throws IOException
 */
public static void testGeometryProjectionTransform(GeoPackage geoPackage)
        throws SQLException, IOException {

    GeometryColumnsDao geometryColumnsDao = geoPackage
            .getGeometryColumnsDao();

    if (geometryColumnsDao.isTableExists()) {
        List<GeometryColumns> results = geometryColumnsDao.queryForAll();

        for (GeometryColumns geometryColumns : results) {

            FeatureDao dao = geoPackage.getFeatureDao(geometryColumns);
            TestCase.assertNotNull(dao);

            FeatureCursor cursor = dao.queryForAll();

            while (cursor.moveToNext()) {

                GeoPackageGeometryData geometryData = cursor.getGeometry();
                if (geometryData != null) {

                    Geometry geometry = geometryData.getGeometry();

                    if (geometry != null) {

                        SpatialReferenceSystemDao srsDao = geoPackage
                                .getSpatialReferenceSystemDao();
                        long srsId = geometryData.getSrsId();
                        SpatialReferenceSystem srs = srsDao
                                .queryForId(srsId);

                        long epsg = srs.getOrganizationCoordsysId();
                        Projection projection = srs.getProjection();
                        long toEpsg = -1;
                        if (epsg == ProjectionConstants.EPSG_WORLD_GEODETIC_SYSTEM) {
                            toEpsg = ProjectionConstants.EPSG_WEB_MERCATOR;
                        } else {
                            toEpsg = ProjectionConstants.EPSG_WORLD_GEODETIC_SYSTEM;
                        }
                        ProjectionTransform transformTo = projection
                                .getTransformation(toEpsg);
                        ProjectionTransform transformFrom = srs.getTransformation(transformTo
                                .getToProjection());

                        byte[] bytes = geometryData.getWkbBytes();

                        Geometry projectedGeometry = transformTo
                                .transform(geometry);
                        GeoPackageGeometryData projectedGeometryData = new GeoPackageGeometryData(
                                -1);
                        projectedGeometryData
                                .setGeometry(projectedGeometry);
                        projectedGeometryData.toBytes();
                        byte[] projectedBytes = projectedGeometryData
                                .getWkbBytes();

                        if (epsg > 0) {
                            TestCase.assertFalse(equalByteArrays(bytes,
                                    projectedBytes));
                        }

                        Geometry restoredGeometry = transformFrom
                                .transform(projectedGeometry);

                        compareGeometries(geometry, restoredGeometry, .001);
                    }
                }

            }
            cursor.close();
        }
    }
}
 
Example #25
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 #26
Source File: GeoPackageExample.java    From geopackage-android with MIT License 4 votes vote down vote up
private static void createNonLinearGeometryTypesExtension(
        GeoPackage geoPackage) throws SQLException {

    SpatialReferenceSystemDao srsDao = geoPackage
            .getSpatialReferenceSystemDao();

    SpatialReferenceSystem srs = srsDao.getOrCreateCode(
            ProjectionConstants.AUTHORITY_EPSG,
            (long) ProjectionConstants.EPSG_WORLD_GEODETIC_SYSTEM);

    GeometryExtensions extensions = new GeometryExtensions(geoPackage);

    String tableName = "non_linear_geometries";

    List<Geometry> geometries = new ArrayList<>();
    List<String> geometryNames = new ArrayList<>();

    CircularString circularString = new CircularString();
    circularString.addPoint(new Point(-122.358, 47.653));
    circularString.addPoint(new Point(-122.348, 47.649));
    circularString.addPoint(new Point(-122.348, 47.658));
    circularString.addPoint(new Point(-122.358, 47.658));
    circularString.addPoint(new Point(-122.358, 47.653));

    for (int i = GeometryCodes.getCode(GeometryType.CIRCULARSTRING); i <= GeometryCodes.getCode(GeometryType.SURFACE); i++) {

        GeometryType geometryType = GeometryCodes.getGeometryType(i);
        extensions.getOrCreate(tableName, GEOMETRY_COLUMN, geometryType);

        Geometry geometry = null;
        String name = geometryType.getName().toLowerCase();

        switch (geometryType) {
            case CIRCULARSTRING:
                geometry = circularString;
                break;
            case COMPOUNDCURVE:
                CompoundCurve compoundCurve = new CompoundCurve();
                compoundCurve.addLineString(circularString);
                geometry = compoundCurve;
                break;
            case CURVEPOLYGON:
                CurvePolygon<CircularString> curvePolygon = new CurvePolygon<>();
                curvePolygon.addRing(circularString);
                geometry = curvePolygon;
                break;
            case MULTICURVE:
                MultiLineString multiCurve = new MultiLineString();
                multiCurve.addLineString(circularString);
                geometry = multiCurve;
                break;
            case MULTISURFACE:
                MultiPolygon multiSurface = new MultiPolygon();
                Polygon polygon = new Polygon();
                polygon.addRing(circularString);
                multiSurface.addPolygon(polygon);
                geometry = multiSurface;
                break;
            case CURVE:
                CompoundCurve curve = new CompoundCurve();
                curve.addLineString(circularString);
                geometry = curve;
                break;
            case SURFACE:
                CurvePolygon<CircularString> surface = new CurvePolygon<>();
                surface.addRing(circularString);
                geometry = surface;
                break;
            default:
                throw new GeoPackageException("Unexpected Geometry Type: "
                        + geometryType);
        }

        geometries.add(geometry);
        geometryNames.add(name);

    }

    createFeatures(geoPackage, srs, tableName, GeometryType.GEOMETRY,
            geometries, geometryNames);

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

    SpatialReferenceSystemDao srsDao = geoPackage
            .getSpatialReferenceSystemDao();

    SpatialReferenceSystem srs = srsDao.getOrCreateCode(
            ProjectionConstants.AUTHORITY_EPSG,
            (long) ProjectionConstants.EPSG_WORLD_GEODETIC_SYSTEM);

    geoPackage.createGeometryColumnsTable();

    createFeatures1(geoPackage, srs);
    createFeatures2(geoPackage, srs);

}
 
Example #28
Source File: TransactionTest.java    From geopackage-android with MIT License 4 votes vote down vote up
/**
 * Test an ORMLite transaction
 *
 * @param geoPackage GeoPackage
 * @param successful true for a successful transaction
 * @throws SQLException upon error
 */
private void testORMLite(final GeoPackage geoPackage,
                         final boolean successful) throws SQLException {

    final String tableName = "test_table";

    final Contents contents = new Contents();
    contents.setTableName(tableName);
    contents.setDataType(ContentsDataType.ATTRIBUTES);

    if (!geoPackage.isTable(tableName)) {
        geoPackage.execSQL("CREATE TABLE " + tableName
                + " (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL)");
    }

    final SpatialReferenceSystemDao srsDao = geoPackage
            .getSpatialReferenceSystemDao();
    final ContentsDao contentsDao = geoPackage.getContentsDao();

    long srsCount = srsDao.countOf();
    long contentsCount = contentsDao.countOf();

    Callable<Void> callable = new Callable<Void>() {
        public Void call() throws Exception {

            SpatialReferenceSystem srs = srsDao.createWgs84Geographical3D();

            contents.setSrs(srs);
            contentsDao.create(contents);

            if (!successful) {
                throw new SQLException();
            }

            return null;
        }
    };

    try {
        geoPackage.callInTransaction(callable);
    } catch (SQLException e) {
        if (successful) {
            TestCase.fail(e.getMessage());
        }
    }

    TestCase.assertEquals(successful ? srsCount + 1 : srsCount,
            srsDao.countOf());
    TestCase.assertEquals(successful ? contentsCount + 1 : contentsCount,
            contentsDao.countOf());

    TestCase.assertEquals(successful,
            geoPackage.isAttributeTable(tableName));

}
 
Example #29
Source File: GeometryColumnsUtils.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();
    GeometryColumnsDao dao = geoPackage.getGeometryColumnsDao();

    if (dao.isTableExists()) {

        // Get current count
        long count = dao.countOf();
        TestCase.assertEquals(count, dao.getFeatureTables().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.FEATURES);
        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 feature table
        geoPackage.createFeatureTable(TestUtils.buildFeatureTable(
                contents.getTableName(), "geom", GeometryType.GEOMETRY));

        contentsDao.create(contents);

        String columnName = "TEST_COLUMN_NAME";
        GeometryType geometryType = GeometryType.POINT;
        byte z = 2;
        byte m = 2;

        // Create new geometry columns
        GeometryColumns geometryColumns = new GeometryColumns();
        geometryColumns.setContents(contents);
        geometryColumns.setColumnName(columnName);
        geometryColumns.setGeometryType(geometryType);
        geometryColumns.setSrs(contents.getSrs());
        geometryColumns.setZ(z);
        geometryColumns.setM(m);
        dao.create(geometryColumns);

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

        // Verify saved geometry columns
        GeometryColumns queryGeometryColumns = dao
                .queryForId(geometryColumns.getId());
        TestCase.assertEquals(contents.getId(),
                queryGeometryColumns.getTableName());
        TestCase.assertEquals(columnName,
                queryGeometryColumns.getColumnName());
        TestCase.assertEquals(geometryType,
                queryGeometryColumns.getGeometryType());
        TestCase.assertEquals(contents.getSrsId().longValue(),
                queryGeometryColumns.getSrsId());
        TestCase.assertEquals(z, queryGeometryColumns.getZ());
        TestCase.assertEquals(m, queryGeometryColumns.getM());
        TestCase.assertEquals(contents.getId(), queryGeometryColumns
                .getContents().getId());
        TestCase.assertEquals(contents.getSrsId().longValue(),
                queryGeometryColumns.getSrs().getId());
    }
}
 
Example #30
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);
    }

}