mil.nga.geopackage.features.columns.GeometryColumnsDao Java Examples

The following examples show how to use mil.nga.geopackage.features.columns.GeometryColumnsDao. 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: GeoPackageCoreImpl.java    From geopackage-core-java with MIT License 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public boolean createGeometryColumnsTable() {
	verifyWritable();

	boolean created = false;
	GeometryColumnsDao dao = getGeometryColumnsDao();
	try {
		if (!dao.isTableExists()) {
			created = tableCreator.createGeometryColumns() > 0;
		}
	} catch (SQLException e) {
		throw new GeoPackageException("Failed to check if "
				+ GeometryColumns.class.getSimpleName()
				+ " table exists and create it", e);
	}
	return created;
}
 
Example #2
Source File: GeoPackageImpl.java    From geopackage-android with MIT License 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public FeatureDao getFeatureDao(String tableName) {
    GeometryColumnsDao dao = getGeometryColumnsDao();
    List<GeometryColumns> geometryColumnsList;
    try {
        geometryColumnsList = dao.queryForEq(
                GeometryColumns.COLUMN_TABLE_NAME, tableName);
    } catch (SQLException e) {
        throw new GeoPackageException("Failed to retrieve "
                + FeatureDao.class.getSimpleName() + " for table name: "
                + tableName + ". Exception retrieving "
                + GeometryColumns.class.getSimpleName() + ".", e);
    }
    if (geometryColumnsList.isEmpty()) {
        throw new GeoPackageException(
                "No Feature Table exists for table name: " + tableName);
    } else if (geometryColumnsList.size() > 1) {
        // This shouldn't happen with the table name unique constraint on
        // geometry columns
        throw new GeoPackageException("Unexpected state. More than one "
                + GeometryColumns.class.getSimpleName()
                + " matched for table name: " + tableName + ", count: "
                + geometryColumnsList.size());
    }
    return getFeatureDao(geometryColumnsList.get(0));
}
 
Example #3
Source File: FeatureUtils.java    From geopackage-java with MIT License 5 votes vote down vote up
/**
 * Test delete
 * 
 * @param geoPackage
 *            GeoPackage
 * @throws SQLException
 *             upon error
 */
public static void testDelete(GeoPackage geoPackage) throws SQLException {

	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();
			int count = cursor.getCount();
			if (count > 0) {

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

				FeatureRow featureRow = cursor.getRow();
				cursor.close();

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

				// Verify deleted
				FeatureRow queryFeatureRow = dao.queryForIdRow(featureRow
						.getId());
				TestCase.assertNull(queryFeatureRow);
				cursor = dao.queryForAll();
				TestCase.assertEquals(count - 1, cursor.getCount());
				cursor.close();
			}
			cursor.close();
		}
	}
}
 
Example #4
Source File: GeoPackageExtensions.java    From geopackage-core-java with MIT License 5 votes vote down vote up
/**
 * Copy the RTree Spatial extension for the table
 * 
 * @param geoPackage
 *            GeoPackage
 * @param table
 *            table name
 * @param newTable
 *            new table name
 * @since 3.3.0
 */
public static void copyRTreeSpatialIndex(GeoPackageCore geoPackage,
		String table, String newTable) {

	try {

		RTreeIndexCoreExtension rTreeIndexExtension = getRTreeIndexExtension(
				geoPackage);
		if (rTreeIndexExtension.has(table)) {
			GeometryColumnsDao geometryColumnsDao = geoPackage
					.getGeometryColumnsDao();

			GeometryColumns geometryColumns = geometryColumnsDao
					.queryForTableName(newTable);
			if (geometryColumns != null) {
				TableInfo tableInfo = TableInfo
						.info(geoPackage.getDatabase(), newTable);
				if (tableInfo != null) {
					String pk = tableInfo.getPrimaryKey().getName();
					rTreeIndexExtension.create(newTable,
							geometryColumns.getColumnName(), pk);
				}
			}
		}

	} catch (Exception e) {
		logger.log(Level.WARNING, "Failed to create RTree for table: "
				+ newTable + ", copied from table: " + table, e);
	}
}
 
Example #5
Source File: GeoPackageImpl.java    From geopackage-java with MIT License 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public FeatureDao getFeatureDao(String tableName) {
	GeometryColumnsDao dao = getGeometryColumnsDao();
	List<GeometryColumns> geometryColumnsList;
	try {
		geometryColumnsList = dao
				.queryForEq(GeometryColumns.COLUMN_TABLE_NAME, tableName);
	} catch (SQLException e) {
		throw new GeoPackageException("Failed to retrieve "
				+ FeatureDao.class.getSimpleName() + " for table name: "
				+ tableName + ". Exception retrieving "
				+ GeometryColumns.class.getSimpleName() + ".", e);
	}
	if (geometryColumnsList.isEmpty()) {
		throw new GeoPackageException(
				"No Feature Table exists for table name: " + tableName);
	} else if (geometryColumnsList.size() > 1) {
		// This shouldn't happen with the table name unique constraint on
		// geometry columns
		throw new GeoPackageException("Unexpected state. More than one "
				+ GeometryColumns.class.getSimpleName()
				+ " matched for table name: " + tableName + ", count: "
				+ geometryColumnsList.size());
	}
	return getFeatureDao(geometryColumnsList.get(0));
}
 
Example #6
Source File: SpatialReferenceSystemDao.java    From geopackage-core-java with MIT License 5 votes vote down vote up
/**
 * Get or create a Geometry Columns DAO
 * 
 * @return geometry columns dao
 * @throws SQLException
 *             upon creation failure
 */
private GeometryColumnsDao getGeometryColumnsDao() throws SQLException {
	if (geometryColumnsDao == null) {
		geometryColumnsDao = DaoManager.createDao(connectionSource,
				GeometryColumns.class);
	}
	return geometryColumnsDao;
}
 
Example #7
Source File: SpatialReferenceSystemDao.java    From geopackage-core-java with 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 #8
Source File: ContentsDao.java    From geopackage-core-java with MIT License 5 votes vote down vote up
/**
 * Get or create a Geometry Columns DAO
 * 
 * @return geometry columns dao
 * @throws SQLException
 *             upon dao creation failure
 */
private GeometryColumnsDao getGeometryColumnsDao() throws SQLException {
	if (geometryColumnsDao == null) {
		geometryColumnsDao = DaoManager.createDao(connectionSource,
				GeometryColumns.class);
	}
	return geometryColumnsDao;
}
 
Example #9
Source File: GeoPackageTestUtils.java    From geopackage-java with MIT License 4 votes vote down vote up
/**
 * Test deleting tables by name
 * 
 * @param geoPackage
 * @throws SQLException
 */
public static void testDeleteTables(GeoPackage geoPackage)
		throws SQLException {

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

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

	geoPackage.foreignKeys(false);

	if (geometryColumnsDao.isTableExists()) {

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

		geoPackage.dropTable(GeometryColumns.TABLE_NAME);

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

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

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

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

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

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

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

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

	}

}
 
Example #10
Source File: GoogleMapShapeConverterUtils.java    From geopackage-android-map with MIT License 4 votes vote down vote up
/**
 * Test shapes
 * 
 * @param geoPackage
 * @throws SQLException
 */
public static void testShapes(GeoPackage geoPackage) throws SQLException {

	GeometryColumnsDao geometryColumnsDao = geoPackage
			.getGeometryColumnsDao();

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

		for (GeometryColumns geometryColumns : results) {

			FeatureDao dao = geoPackage.getFeatureDao(geometryColumns);

			GoogleMapShapeConverter converter = new GoogleMapShapeConverter(
					dao.getProjection());
			converter.setExteriorOrientation(null);
			converter.setHoleOrientation(null);

			// Query for all
			FeatureCursor cursor = dao.queryForAll();
			while (cursor.moveToNext()) {
				FeatureRow featureRow = cursor.getRow();

				GeoPackageGeometryData geometryData = featureRow
						.getGeometry();

				if (geometryData != null) {
					Geometry geometry = geometryData.getGeometry();
					GeometryType geometryType = geometry.getGeometryType();

					switch (geometryType) {
					case POINT:
						convertPoint(converter, (Point) geometry);
						break;
					case LINESTRING:
						convertLineString(converter, (LineString) geometry);
						break;
					case POLYGON:
						convertPolygon(converter, (Polygon) geometry);
						break;
					case MULTIPOINT:
						convertMultiPoint(converter, (MultiPoint) geometry);
						break;
					case MULTILINESTRING:
						convertMultiLineString(converter,
								(MultiLineString) geometry);
						break;
					case MULTIPOLYGON:
						convertMultiPolygon(converter,
								(MultiPolygon) geometry);
						break;
					case CIRCULARSTRING:
						convertLineString(converter,
								(CircularString) geometry);
						break;
					case COMPOUNDCURVE:
						convertCompoundCurve(converter,
								(CompoundCurve) geometry);
						break;
					case POLYHEDRALSURFACE:
						convertMultiPolygon(converter,
								(PolyhedralSurface) geometry);
						break;
					case TIN:
						convertMultiPolygon(converter, (TIN) geometry);
						break;
					case TRIANGLE:
						convertPolygon(converter, (Triangle) geometry);
						break;
					default:
					}
				}

			}
			cursor.close();

		}
	}

}
 
Example #11
Source File: GeoPackageTestUtils.java    From geopackage-java with MIT License 4 votes vote down vote up
/**
 * Validate feature table with metadata
 * 
 * @param geoPackage
 * @throws SQLException
 */
private static void validateFeatureTableWithMetadata(GeoPackage geoPackage,
		GeometryColumns geometryColumns, String idColumn,
		List<FeatureColumn> additionalColumns) throws SQLException {

	GeometryColumnsDao dao = geoPackage.getGeometryColumnsDao();

	GeometryColumns queryGeometryColumns = dao
			.queryForId(geometryColumns.getId());
	TestCase.assertNotNull(queryGeometryColumns);

	TestCase.assertEquals(geometryColumns.getTableName(),
			queryGeometryColumns.getTableName());
	TestCase.assertEquals(geometryColumns.getColumnName(),
			queryGeometryColumns.getColumnName());
	TestCase.assertEquals(GeometryType.POINT,
			queryGeometryColumns.getGeometryType());
	TestCase.assertEquals(geometryColumns.getZ(),
			queryGeometryColumns.getZ());
	TestCase.assertEquals(geometryColumns.getM(),
			queryGeometryColumns.getM());

	FeatureDao featureDao = geoPackage
			.getFeatureDao(geometryColumns.getTableName());
	FeatureRow featureRow = featureDao.newRow();

	TestCase.assertEquals(
			2 + (additionalColumns != null ? additionalColumns.size() : 0),
			featureRow.columnCount());
	if (idColumn == null) {
		idColumn = "id";
	}
	TestCase.assertEquals(idColumn, featureRow.getColumnName(0));
	TestCase.assertEquals(geometryColumns.getColumnName(),
			featureRow.getColumnName(1));

	if (additionalColumns != null) {
		TestCase.assertEquals("test_text", featureRow.getColumnName(2));
		TestCase.assertEquals("test_real", featureRow.getColumnName(3));
		TestCase.assertEquals("test_boolean", featureRow.getColumnName(4));
		TestCase.assertEquals("test_blob", featureRow.getColumnName(5));
		TestCase.assertEquals("test_integer", featureRow.getColumnName(6));
		TestCase.assertEquals("test_text_limited",
				featureRow.getColumnName(7));
		TestCase.assertEquals("test_blob_limited",
				featureRow.getColumnName(8));
	}
}
 
Example #12
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 #13
Source File: GeometryColumnsUtils.java    From geopackage-java with MIT License 4 votes vote down vote up
/**
 * Test delete
 * 
 * @param geoPackage
 * @throws SQLException
 */
public static void testDelete(GeoPackage geoPackage) throws SQLException {

	GeometryColumnsDao dao = geoPackage.getGeometryColumnsDao();

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

		if (!results.isEmpty()) {

			// Choose random geometry columns
			int random = (int) (Math.random() * results.size());
			GeometryColumns geometryColumns = results.get(random);

			// Delete the geometry columns
			dao.delete(geometryColumns);

			// Verify deleted
			GeometryColumns queryGeometryColumns = dao
					.queryForId(geometryColumns.getId());
			TestCase.assertNull(queryGeometryColumns);

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

				// Choose random geometry columns
				random = (int) (Math.random() * results.size());
				geometryColumns = results.get(random);

				// Find which geometry columns to delete
				QueryBuilder<GeometryColumns, TableColumnKey> qb = dao
						.queryBuilder();
				qb.where().eq(GeometryColumns.COLUMN_GEOMETRY_TYPE_NAME,
						geometryColumns.getGeometryType().getName());
				PreparedQuery<GeometryColumns> query = qb.prepare();
				List<GeometryColumns> queryResults = dao.query(query);
				int count = queryResults.size();

				// Delete
				DeleteBuilder<GeometryColumns, TableColumnKey> db = dao
						.deleteBuilder();
				db.where().eq(GeometryColumns.COLUMN_GEOMETRY_TYPE_NAME,
						geometryColumns.getGeometryType().getName());
				PreparedDelete<GeometryColumns> deleteQuery = db.prepare();
				int deleted = dao.delete(deleteQuery);

				TestCase.assertEquals(count, deleted);

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

	GeometryColumnsDao geometryColumnsDao = geoPackage
			.getGeometryColumnsDao();

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

		for (GeometryColumns geometryColumns : results) {

			FeatureDao dao = geoPackage.getFeatureDao(geometryColumns);
			testUpdate(dao);

		}
	}

}
 
Example #16
Source File: ContentsDao.java    From geopackage-core-java with MIT License 4 votes vote down vote up
/**
 * Verify the tables are in the expected state for the Contents create
 * 
 * @param contents
 *            contents
 * @throws SQLException
 *             upon verify creation error
 */
private void verifyCreate(Contents contents) throws SQLException {
	ContentsDataType dataType = contents.getDataType();
	if (dataType != null) {
		switch (dataType) {
		case FEATURES:

			// Features require Geometry Columns table (Spec Requirement 21)
			GeometryColumnsDao geometryColumnsDao = getGeometryColumnsDao();
			if (!geometryColumnsDao.isTableExists()) {
				throw new GeoPackageException(
						"A data type of "
								+ dataType.getName()
								+ " requires the "
								+ GeometryColumns.class.getSimpleName()
								+ " table to first be created using the GeoPackage.");
			}

			break;

		case TILES:
			verifyTiles(dataType);
			break;

		case GRIDDED_COVERAGE:
			verifyTiles(dataType);
			break;

		case ATTRIBUTES:
			break;

		default:
			throw new GeoPackageException("Unsupported data type: "
					+ dataType);
		}
	}

	// Verify the feature or tile table exists
	if (!db.tableExists(contents.getTableName())) {
		throw new GeoPackageException(
				"No table exists for Content Table Name: "
						+ contents.getTableName()
						+ ". Table must first be created.");
	}
}
 
Example #17
Source File: ContentsDao.java    From geopackage-core-java with MIT License 4 votes vote down vote up
/**
 * Delete the Contents, cascading
 * 
 * @param contents
 *            contents
 * @return deleted count
 * @throws SQLException
 *             upon deletion error
 */
public int deleteCascade(Contents contents) throws SQLException {
	int count = 0;

	if (contents != null) {

		ContentsDataType dataType = contents.getDataType();

		if (dataType != null) {

			switch (dataType) {
			case FEATURES:

				// Delete Geometry Columns
				GeometryColumnsDao geometryColumnsDao = getGeometryColumnsDao();
				if (geometryColumnsDao.isTableExists()) {
					GeometryColumns geometryColumns = contents
							.getGeometryColumns();
					if (geometryColumns != null) {
						geometryColumnsDao.delete(geometryColumns);
					}
				}

				break;

			case TILES:
			case GRIDDED_COVERAGE:

				// Delete Tile Matrix collection
				TileMatrixDao tileMatrixDao = getTileMatrixDao();
				if (tileMatrixDao.isTableExists()) {
					ForeignCollection<TileMatrix> tileMatrixCollection = contents
							.getTileMatrix();
					if (!tileMatrixCollection.isEmpty()) {
						tileMatrixDao.delete(tileMatrixCollection);
					}
				}

				// Delete Tile Matrix Set
				TileMatrixSetDao tileMatrixSetDao = getTileMatrixSetDao();
				if (tileMatrixSetDao.isTableExists()) {
					TileMatrixSet tileMatrixSet = contents
							.getTileMatrixSet();
					if (tileMatrixSet != null) {
						tileMatrixSetDao.delete(tileMatrixSet);
					}
				}

				break;

			case ATTRIBUTES:

				dropTable(contents.getTableName());

				break;

			}

		} else {
			dropTable(contents.getTableName());
		}

		count = delete(contents);
	}

	return count;
}
 
Example #18
Source File: GeoPackageCoreImpl.java    From geopackage-core-java with MIT License 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public GeometryColumnsDao getGeometryColumnsDao() {
	return createDao(GeometryColumns.class);
}
 
Example #19
Source File: GeoPackageTestUtils.java    From geopackage-android with MIT License 4 votes vote down vote up
/**
 * Test deleting tables by name
 *
 * @param geoPackage
 * @throws SQLException
 */
public static void testDeleteTables(GeoPackage geoPackage)
        throws SQLException {

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

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

    geoPackage.foreignKeys(false);

    if (geometryColumnsDao.isTableExists()) {

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

        geoPackage.dropTable(GeometryColumns.TABLE_NAME);

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

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

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

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

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

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

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

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

    }

}
 
Example #20
Source File: GeoPackageTestUtils.java    From geopackage-android with MIT License 4 votes vote down vote up
/**
 * Validate feature table with metadata
 *
 * @param geoPackage
 * @throws SQLException
 */
private static void validateFeatureTableWithMetadata(GeoPackage geoPackage,
                                                     GeometryColumns geometryColumns, String idColumn,
                                                     List<FeatureColumn> additionalColumns) throws SQLException {

    GeometryColumnsDao dao = geoPackage.getGeometryColumnsDao();

    GeometryColumns queryGeometryColumns = dao.queryForId(geometryColumns
            .getId());
    TestCase.assertNotNull(queryGeometryColumns);

    TestCase.assertEquals(geometryColumns.getTableName(),
            queryGeometryColumns.getTableName());
    TestCase.assertEquals(geometryColumns.getColumnName(),
            queryGeometryColumns.getColumnName());
    TestCase.assertEquals(GeometryType.POINT,
            queryGeometryColumns.getGeometryType());
    TestCase.assertEquals(geometryColumns.getZ(),
            queryGeometryColumns.getZ());
    TestCase.assertEquals(geometryColumns.getM(),
            queryGeometryColumns.getM());

    FeatureDao featureDao = geoPackage.getFeatureDao(geometryColumns
            .getTableName());
    FeatureRow featureRow = featureDao.newRow();

    TestCase.assertEquals(
            2 + (additionalColumns != null ? additionalColumns.size() : 0),
            featureRow.columnCount());
    if (idColumn == null) {
        idColumn = "id";
    }
    TestCase.assertEquals(idColumn, featureRow.getColumnName(0));
    TestCase.assertEquals(geometryColumns.getColumnName(),
            featureRow.getColumnName(1));

    if (additionalColumns != null) {
        TestCase.assertEquals("test_text", featureRow.getColumnName(2));
        TestCase.assertEquals("test_real", featureRow.getColumnName(3));
        TestCase.assertEquals("test_boolean", featureRow.getColumnName(4));
        TestCase.assertEquals("test_blob", featureRow.getColumnName(5));
        TestCase.assertEquals("test_integer", featureRow.getColumnName(6));
        TestCase.assertEquals("test_text_limited",
                featureRow.getColumnName(7));
        TestCase.assertEquals("test_blob_limited",
                featureRow.getColumnName(8));
    }
}
 
Example #21
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 #22
Source File: GeometryColumnsUtils.java    From geopackage-android with MIT License 4 votes vote down vote up
/**
 * Test delete
 *
 * @param geoPackage
 * @throws SQLException
 */
public static void testDelete(GeoPackage geoPackage) throws SQLException {

    GeometryColumnsDao dao = geoPackage.getGeometryColumnsDao();

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

        if (!results.isEmpty()) {

            // Choose random geometry columns
            int random = (int) (Math.random() * results.size());
            GeometryColumns geometryColumns = results.get(random);

            // Delete the geometry columns
            dao.delete(geometryColumns);

            // Verify deleted
            GeometryColumns queryGeometryColumns = dao
                    .queryForId(geometryColumns.getId());
            TestCase.assertNull(queryGeometryColumns);

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

                // Choose random geometry columns
                random = (int) (Math.random() * results.size());
                geometryColumns = results.get(random);

                // Find which geometry columns to delete
                QueryBuilder<GeometryColumns, TableColumnKey> qb = dao
                        .queryBuilder();
                qb.where().eq(GeometryColumns.COLUMN_GEOMETRY_TYPE_NAME,
                        geometryColumns.getGeometryType().getName());
                PreparedQuery<GeometryColumns> query = qb.prepare();
                List<GeometryColumns> queryResults = dao.query(query);
                int count = queryResults.size();

                // Delete
                DeleteBuilder<GeometryColumns, TableColumnKey> db = dao
                        .deleteBuilder();
                db.where().eq(GeometryColumns.COLUMN_GEOMETRY_TYPE_NAME,
                        geometryColumns.getGeometryType().getName());
                PreparedDelete<GeometryColumns> deleteQuery = db.prepare();
                int deleted = dao.delete(deleteQuery);

                TestCase.assertEquals(count, deleted);

            }
        }
    }
}
 
Example #23
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 #24
Source File: FeatureUtils.java    From geopackage-android with MIT License 4 votes vote down vote up
/**
 * Test delete
 *
 * @param geoPackage GeoPackage
 * @throws SQLException upon error
 */
public static void testDelete(GeoPackage geoPackage) throws SQLException {

    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();
            int count = cursor.getCount();
            if (count > 0) {

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

                FeatureRow featureRow = cursor.getRow();
                cursor.close();

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

                // Verify deleted
                FeatureRow queryFeatureRow = dao.queryForIdRow(featureRow
                        .getId());
                TestCase.assertNull(queryFeatureRow);
                cursor = dao.queryForAll();
                TestCase.assertEquals(count - 1, cursor.getCount());
                cursor.close();
            }
            cursor.close();
        }
    }
}
 
Example #25
Source File: FeatureUtils.java    From geopackage-android with MIT License 4 votes vote down vote up
/**
 * Test update
 *
 * @param geoPackage GeoPackage
 * @throws SQLException upon error
 */
public static void testUpdate(GeoPackage geoPackage) throws SQLException {

    GeometryColumnsDao geometryColumnsDao = geoPackage
            .getGeometryColumnsDao();

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

        for (GeometryColumns geometryColumns : results) {

            FeatureDao dao = geoPackage.getFeatureDao(geometryColumns);
            testUpdate(dao);

        }
    }

}
 
Example #26
Source File: GeoPackageCore.java    From geopackage-core-java with MIT License 2 votes vote down vote up
/**
 * Get a Geometry Columns DAO
 * 
 * @return Geometry Columns DAO
 */
public GeometryColumnsDao getGeometryColumnsDao();