Java Code Examples for mil.nga.geopackage.GeoPackageException

The following examples show how to use mil.nga.geopackage.GeoPackageException. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: geopackage-core-java   Source File: ContentsIdExtension.java    License: MIT License 6 votes vote down vote up
/**
 * Remove all trace of the extension
 */
public void removeExtension() {

	try {
		if (contentsIdDao.isTableExists()) {
			geoPackage.dropTable(contentsIdDao.getTableName());
		}
		if (extensionsDao.isTableExists()) {
			extensionsDao.deleteByExtension(EXTENSION_NAME);
		}
		geoPackage.getContentsDao().deleteById(ContentsId.TABLE_NAME);
	} catch (SQLException e) {
		throw new GeoPackageException(
				"Failed to delete Contents Id extension and table. GeoPackage: "
						+ geoPackage.getName(),
				e);
	}
}
 
Example 2
Source Project: geopackage-core-java   Source File: UserCoreRow.java    License: MIT License 6 votes vote down vote up
/**
 * Validate the value and its actual value types against the column data
 * type class
 * 
 * @param column
 *            column
 * @param value
 *            value
 * @param valueTypes
 *            value types
 */
protected void validateValue(TColumn column, Object value,
		Class<?>... valueTypes) {

	GeoPackageDataType dataType = column.getDataType();
	Class<?> dataTypeClass = dataType.getClassType();

	boolean valid = false;
	for (Class<?> valueType : valueTypes) {
		if (valueType.equals(dataTypeClass)) {
			valid = true;
			break;
		}
	}

	if (!valid) {
		throw new GeoPackageException("Illegal value. Column: "
				+ column.getName() + ", Value: " + value
				+ ", Expected Type: " + dataTypeClass.getSimpleName()
				+ ", Actual Type: " + valueTypes[0].getSimpleName());
	}

}
 
Example 3
Source Project: geopackage-core-java   Source File: UserColumns.java    License: MIT License 6 votes vote down vote up
/**
 * Get the column index of the column name
 * 
 * @param columnName
 *            column name
 * @param required
 *            column existence is required
 * @return column index
 */
public Integer getColumnIndex(String columnName, boolean required) {
	Integer index = nameToIndex.get(columnName.toLowerCase());
	if (required && index == null) {
		StringBuilder error = new StringBuilder(
				"Column does not exist in ");
		if (custom) {
			error.append("custom specified table columns");
		} else {
			error.append("table");
		}
		error.append(". table: " + tableName + ", column: " + columnName);
		if (custom) {
			error.append(", columns: " + columnNames);
		}
		throw new GeoPackageException(error.toString());
	}
	return index;
}
 
Example 4
Source Project: geopackage-core-java   Source File: RTreeIndexCoreExtension.java    License: MIT License 6 votes vote down vote up
/**
 * Delete all RTree Index extensions. Drops the triggers, RTree tables, and
 * deletes the extensions.
 * 
 * @since 3.2.0
 */
public void deleteAll() {
	try {
		if (extensionsDao.isTableExists()) {
			List<Extensions> extensions = extensionsDao
					.queryByExtension(EXTENSION_NAME);
			for (Extensions extension : extensions) {
				delete(extension.getTableName(), extension.getColumnName());
			}
		}
	} catch (SQLException e) {
		throw new GeoPackageException(
				"Failed to delete all RTree Index extensions. GeoPackage: "
						+ geoPackage.getName(),
				e);
	}

}
 
Example 5
Source Project: geopackage-java   Source File: GeoPackageImpl.java    License: MIT License 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public AttributesDao getAttributesDao(String tableName) {

	ContentsDao dao = getContentsDao();
	Contents contents = null;
	try {
		contents = dao.queryForId(tableName);
	} catch (SQLException e) {
		throw new GeoPackageException(
				"Failed to retrieve " + Contents.class.getSimpleName()
						+ " for table name: " + tableName,
				e);
	}
	if (contents == null) {
		throw new GeoPackageException(
				"No Contents Table exists for table name: " + tableName);
	}
	return getAttributesDao(contents);
}
 
Example 6
Source Project: geopackage-core-java   Source File: GeoPackageTableCreator.java    License: 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 7
Source Project: geopackage-core-java   Source File: GeoPackageCoreImpl.java    License: MIT License 6 votes vote down vote up
/**
 * Copy the user table
 * 
 * @param tableName
 *            table name
 * @param newTableName
 *            new table name
 * @param transferContent
 *            transfer user table content flag
 * @param validateContents
 *            true to validate a contents was copied
 * @return copied contents
 * @since 3.3.0
 */
protected Contents copyUserTable(String tableName, String newTableName,
		boolean transferContent, boolean validateContents) {

	AlterTable.copyTable(database, tableName, newTableName,
			transferContent);

	Contents contents = copyContents(tableName, newTableName);

	if (contents == null && validateContents) {
		throw new GeoPackageException(
				"No table contents found for table: " + tableName);
	}

	return contents;
}
 
Example 8
Source Project: geopackage-core-java   Source File: GeoPackageCoreImpl.java    License: MIT License 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public boolean createGeometryIndexTable() {
	verifyWritable();

	boolean created = false;
	GeometryIndexDao dao = getGeometryIndexDao();
	try {
		if (!dao.isTableExists()) {
			created = tableCreator.createGeometryIndex() > 0;
		}
	} catch (SQLException e) {
		throw new GeoPackageException(
				"Failed to check if " + GeometryIndex.class.getSimpleName()
						+ " table exists and create it",
				e);
	}
	return created;
}
 
Example 9
Source Project: geopackage-core-java   Source File: NGAExtensions.java    License: MIT License 6 votes vote down vote up
/**
 * Delete the Feature Tile Link extensions for the table
 * 
 * @param geoPackage
 *            GeoPackage
 * @param table
 *            table name
 * @since 1.1.5
 */
public static void deleteFeatureTileLink(GeoPackageCore geoPackage,
		String table) {

	FeatureTileLinkDao featureTileLinkDao = geoPackage
			.getFeatureTileLinkDao();
	try {
		if (featureTileLinkDao.isTableExists()) {
			featureTileLinkDao.deleteByTableName(table);
		}
	} catch (SQLException e) {
		throw new GeoPackageException(
				"Failed to delete Feature Tile Link. GeoPackage: "
						+ geoPackage.getName() + ", Table: " + table,
				e);
	}
}
 
Example 10
Source Project: geopackage-core-java   Source File: RelatedTablesCoreExtension.java    License: MIT License 6 votes vote down vote up
/**
 * Validate that the relation name is valid between the base and related
 * table
 * 
 * @param baseTableName
 *            base table name
 * @param relatedTableName
 *            related table name
 * @param relationName
 *            relation name
 */
private void validateRelationship(String baseTableName,
		String relatedTableName, String relationName) {

	// Verify the base and related tables exist
	if (!geoPackage.isTable(baseTableName)) {
		throw new GeoPackageException(
				"Base Relationship table does not exist: " + baseTableName
						+ ", Relation: " + relationName);
	}
	if (!geoPackage.isTable(relatedTableName)) {
		throw new GeoPackageException(
				"Related Relationship table does not exist: "
						+ relatedTableName + ", Relation: " + relationName);
	}

	// Verify spec defined relation types
	RelationType relationType = RelationType.fromName(relationName);
	if (relationType != null) {
		validateRelationship(baseTableName, relatedTableName, relationType);
	}

}
 
Example 11
Source Project: geopackage-java   Source File: SQLUtils.java    License: MIT License 6 votes vote down vote up
/**
 * Query the SQL for a single integer result
 * 
 * @param connection
 *            connection
 * @param sql
 *            sql
 * @param args
 *            query arguments
 * @param allowEmptyResults
 *            true to accept empty results as a 0 return
 * @return Integer result, null if no result
 */
private static int querySingleInteger(Connection connection, String sql,
		String[] args, boolean allowEmptyResults) {

	int result = 0;

	Object value = querySingleResult(connection, sql, args, 0,
			GeoPackageDataType.MEDIUMINT);
	if (value != null) {
		result = ((Number) value).intValue();
	} else if (!allowEmptyResults) {
		throw new GeoPackageException(
				"Failed to query for single result. SQL: " + sql);
	}

	return result;
}
 
Example 12
Source Project: geopackage-java   Source File: FeatureIndexManager.java    License: MIT License 6 votes vote down vote up
/**
 * Is the feature table indexed in the provided type location
 *
 * @param type
 *            index location type
 * @return true if indexed
 */
public boolean isIndexed(FeatureIndexType type) {
	boolean indexed = false;
	if (type == null) {
		indexed = isIndexed();
	} else {
		switch (type) {
		case GEOPACKAGE:
			indexed = featureTableIndex.isIndexed();
			break;
		case RTREE:
			indexed = rTreeIndexTableDao.has();
			break;
		default:
			throw new GeoPackageException(
					"Unsupported FeatureIndexType: " + type);
		}
	}
	return indexed;
}
 
Example 13
Source Project: geopackage-core-java   Source File: CoverageDataCore.java    License: MIT License 6 votes vote down vote up
/**
 * Get the Y encoded location from the base provided y
 * 
 * @param y
 *            y location
 * @param encodingType
 *            pixel encoding type
 * @return encoded y location
 */
private float getYEncodedLocation(float y,
		GriddedCoverageEncodingType encodingType) {

	float yLocation = y;

	switch (encodingType) {
	case CENTER:
	case AREA:
		yLocation += 0.5f;
		break;
	case CORNER:
		yLocation += 1.0f;
		break;
	default:
		throw new GeoPackageException("Unsupported Encoding Type: "
				+ encodingType);
	}

	return yLocation;
}
 
Example 14
Source Project: geopackage-android   Source File: FeatureDao.java    License: MIT License 6 votes vote down vote up
/**
 * Constructor
 *
 * @param database        database name
 * @param db              connection
 * @param geometryColumns geometry columns
 * @param table           feature table
 */
public FeatureDao(String database, GeoPackageConnection db, GeometryColumns geometryColumns,
                  FeatureTable table) {
    super(database, db, new FeatureConnection(db), table);

    this.featureDb = (FeatureConnection) getUserDb();
    this.geometryColumns = geometryColumns;
    if (geometryColumns.getContents() == null) {
        throw new GeoPackageException(GeometryColumns.class.getSimpleName()
                + " " + geometryColumns.getId() + " has null "
                + Contents.class.getSimpleName());
    }
    if (geometryColumns.getSrs() == null) {
        throw new GeoPackageException(GeometryColumns.class.getSimpleName()
                + " " + geometryColumns.getId() + " has null "
                + SpatialReferenceSystem.class.getSimpleName());
    }

    projection = geometryColumns.getProjection();
}
 
Example 15
Source Project: geopackage-core-java   Source File: GeoPackageCoreImpl.java    License: MIT License 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public boolean createContentsIdTable() {
	verifyWritable();

	boolean created = false;
	ContentsIdDao dao = getContentsIdDao();
	try {
		if (!dao.isTableExists()) {
			created = tableCreator.createContentsId() > 0;
		}
	} catch (SQLException e) {
		throw new GeoPackageException(
				"Failed to check if " + ContentsId.class.getSimpleName()
						+ " table exists and create it",
				e);
	}
	return created;
}
 
Example 16
Source Project: geopackage-android   Source File: FeatureIndexManager.java    License: MIT License 6 votes vote down vote up
/**
 * Delete the feature index for the geometry id
 *
 * @param type   feature index type
 * @param geomId geometry id
 * @return true if deleted
 */
public boolean deleteIndex(FeatureIndexType type, long geomId) {
    if (type == null) {
        throw new GeoPackageException("FeatureIndexType is required to delete index");
    }
    boolean deleted = false;
    switch (type) {
        case GEOPACKAGE:
            deleted = featureTableIndex.deleteIndex(geomId) > 0;
            break;
        case METADATA:
            deleted = featureIndexer.deleteIndex(geomId);
            break;
        case RTREE:
            // Updated by triggers, ignore for RTree
            deleted = true;
            break;
        default:
            throw new GeoPackageException("Unsupported FeatureIndexType: " + type);
    }
    return deleted;
}
 
Example 17
Source Project: geopackage-core-java   Source File: TileMatrix.java    License: MIT License 6 votes vote down vote up
public void setContents(Contents contents) {
	this.contents = contents;
	if (contents != null) {
		// Verify the Contents have a tiles data type (Spec Requirement 42)
		ContentsDataType dataType = contents.getDataType();
		if (dataType == null
				|| (dataType != ContentsDataType.TILES && dataType != ContentsDataType.GRIDDED_COVERAGE)) {
			throw new GeoPackageException("The "
					+ Contents.class.getSimpleName() + " of a "
					+ TileMatrix.class.getSimpleName()
					+ " must have a data type of "
					+ ContentsDataType.TILES.getName() + " or "
					+ ContentsDataType.GRIDDED_COVERAGE.getName());
		}
		tableName = contents.getId();
	} else {
		tableName = null;
	}
}
 
Example 18
Source Project: geopackage-java   Source File: StyleRow.java    License: MIT License 6 votes vote down vote up
/**
 * Validate and adjust the color value
 * 
 * @param color
 *            color
 */
private String validateColor(String color) {
	String validated = color;
	if (color != null) {
		if (!color.startsWith("#")) {
			validated = "#" + color;
		}
		if (!colorPattern.matcher(validated).matches()) {
			throw new GeoPackageException(
					"Color must be in hex format #RRGGBB or #RGB, invalid value: "
							+ color);
		}
		validated = validated.toUpperCase();
	}
	return validated;
}
 
Example 19
Source Project: geopackage-core-java   Source File: FeatureCoreStyleExtension.java    License: MIT License 6 votes vote down vote up
/**
 * Check if the style extension relationship between a feature table and
 * style extension table exists
 * 
 * @param mappingTableName
 *            mapping table name
 * @param featureTable
 *            feature table name
 * @param baseTable
 *            base table name
 * @param relatedTable
 *            related table name
 * @return true if relationship exists
 */
private boolean hasStyleRelationship(String mappingTableName,
		String baseTable, String relatedTable) {

	boolean has = false;

	try {
		has = relatedTables.hasRelations(baseTable, relatedTable,
				mappingTableName);
	} catch (SQLException e) {
		throw new GeoPackageException(
				"Failed to check if Feature Style Relationship exists. Base Table: "
						+ baseTable + ", Related Table: " + relatedTable
						+ ", Mapping Table: " + mappingTableName, e);
	}

	return has;
}
 
Example 20
@Override
protected GeoPackage getGeoPackage() throws Exception {

    GeoPackageManager manager = GeoPackageFactory.getManager(activity);

    // Delete
    manager.delete(TestConstants.IMPORT_COVERAGE_DATA_TIFF_DB_NAME);

    // Copy the test db file from assets to the internal storage
    TestUtils.copyAssetFileToInternalStorage(activity, testContext,
            TestConstants.IMPORT_COVERAGE_DATA_TIFF_DB_FILE_NAME);

    // Import
    String importLocation = TestUtils.getAssetFileInternalStorageLocation(
            activity, TestConstants.IMPORT_COVERAGE_DATA_TIFF_DB_FILE_NAME);
    manager.importGeoPackage(new File(importLocation));

    // Open
    GeoPackage geoPackage = manager.open(TestConstants.IMPORT_COVERAGE_DATA_TIFF_DB_NAME);
    if (geoPackage == null) {
        throw new GeoPackageException("Failed to open database");
    }

    return geoPackage;
}
 
Example 21
Source Project: geopackage-core-java   Source File: GeometryColumnsSfSql.java    License: MIT License 6 votes vote down vote up
public void setContents(Contents contents) {
	this.contents = contents;
	if (contents != null) {
		// Verify the Contents have a features data type (Spec Requirement
		// 23)
		ContentsDataType dataType = contents.getDataType();
		if (dataType == null || dataType != ContentsDataType.FEATURES) {
			throw new GeoPackageException("The "
					+ Contents.class.getSimpleName() + " of a "
					+ GeometryColumnsSfSql.class.getSimpleName()
					+ " must have a data type of "
					+ ContentsDataType.FEATURES.getName());
		}
		fTableName = contents.getId();
	}
}
 
Example 22
Source Project: geopackage-core-java   Source File: FeatureCoreStyleExtension.java    License: MIT License 6 votes vote down vote up
/**
 * Completely remove and delete the extension and all styles and icons
 */
public void removeExtension() {

	deleteRelationships();

	geoPackage.deleteTable(StyleTable.TABLE_NAME);

	geoPackage.deleteTable(IconTable.TABLE_NAME);

	try {
		if (extensionsDao.isTableExists()) {
			extensionsDao.deleteByExtension(EXTENSION_NAME);
		}
	} catch (SQLException e) {
		throw new GeoPackageException(
				"Failed to delete Feature Style extension. GeoPackage: "
						+ geoPackage.getName(), e);
	}

}
 
Example 23
Source Project: geopackage-java   Source File: LoadGeoPackageTestCase.java    License: MIT License 6 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * @throws IOException
 * @throws SQLException
 */
@Override
protected GeoPackage getGeoPackage() throws Exception {
	File testFolder = folder.newFolder();

	File testFile = TestUtils.getTestFile(file);

	File newFile = new File(testFolder, testFile.getName());
	try {
		GeoPackageIOUtils.copyFile(testFile, newFile);
	} catch (IOException e) {
		throw new GeoPackageException(
				"Failed to copy GeoPackage to test directory. File: "
						+ testFile.getAbsolutePath() + ", Test Directory: "
						+ testFolder.getAbsolutePath(), e);
	}

	// Open
	GeoPackage geoPackage = GeoPackageManager.open(newFile);
	if (geoPackage == null) {
		throw new GeoPackageException("Failed to open database");
	}

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

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

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

    return getTileDao(tileMatrixSet);
}
 
Example 25
Source Project: geopackage-core-java   Source File: TileTableScaling.java    License: MIT License 6 votes vote down vote up
/**
 * Create or update the tile scaling
 * 
 * @param tileScaling
 *            tile scaling
 * @return true upon success
 */
public boolean createOrUpdate(TileScaling tileScaling) {

	boolean success = false;
	tileScaling.setTableName(tableName);

	getOrCreateExtension();
	try {
		if (!tileScalingDao.isTableExists()) {
			geoPackage.createTileScalingTable();
		}

		CreateOrUpdateStatus status = tileScalingDao
				.createOrUpdate(tileScaling);
		success = status.isCreated() || status.isUpdated();

	} catch (SQLException e) {
		throw new GeoPackageException(
				"Failed to create or update tile scaling for GeoPackage: "
						+ geoPackage.getName() + ", Tile Table: "
						+ tableName, e);
	}

	return success;
}
 
Example 26
Source Project: geopackage-core-java   Source File: RTreeIndexCoreExtension.java    License: MIT License 6 votes vote down vote up
/**
 * Delete all RTree Index extensions for the table. Drops the triggers,
 * RTree tables, and deletes the extensions.
 * 
 * @param tableName
 *            table name
 * 
 * @since 3.2.0
 */
public void delete(String tableName) {
	try {
		if (extensionsDao.isTableExists()) {
			List<Extensions> extensions = extensionsDao
					.queryByExtension(EXTENSION_NAME, tableName);
			for (Extensions extension : extensions) {
				delete(extension.getTableName(), extension.getColumnName());
			}
		}
	} catch (SQLException e) {
		throw new GeoPackageException(
				"Failed to delete RTree Index extensions for table. GeoPackage: "
						+ geoPackage.getName() + ", Table: " + tableName,
				e);
	}

}
 
Example 27
Source Project: geopackage-java   Source File: UrlTileGenerator.java    License: MIT License 6 votes vote down vote up
/**
 * Set the tile format
 * 
 * @param tileFormat
 *            tile format
 */
public void setTileFormat(TileFormatType tileFormat) {
	if (tileFormat == null) {
		this.tileFormat = TileFormatType.XYZ;
	} else {
		switch (tileFormat) {
		case XYZ:
		case TMS:
			this.tileFormat = tileFormat;
			break;
		default:
			throw new GeoPackageException(
					"Unsupported Tile Format Type for URL Tile Generation: "
							+ tileFormat);
		}
	}
}
 
Example 28
Source Project: geopackage-core-java   Source File: FeatureTableCoreIndex.java    License: MIT License 6 votes vote down vote up
/**
 * Clear the Geometry Indices for the table name
 * 
 * @return number of rows deleted
 */
private int clearGeometryIndices() {
	int deleted = 0;
	DeleteBuilder<GeometryIndex, GeometryIndexKey> db = geometryIndexDao
			.deleteBuilder();
	try {
		db.where().eq(GeometryIndex.COLUMN_TABLE_NAME, tableName);
		PreparedDelete<GeometryIndex> deleteQuery = db.prepare();
		deleted = geometryIndexDao.delete(deleteQuery);
	} catch (SQLException e) {
		throw new GeoPackageException(
				"Failed to clear Geometry Index rows for GeoPackage: "
						+ geoPackage.getName() + ", Table Name: "
						+ tableName + ", Column Name: " + columnName,
				e);
	}

	return deleted;
}
 
Example 29
Source Project: geopackage-android   Source File: GeoPackageMetadataDataSource.java    License: MIT License 6 votes vote down vote up
/**
 * Create a new GeoPackage metadata
 *
 * @param metadata GeoPackage metadata
 */
public void create(GeoPackageMetadata metadata) {
    ContentValues values = new ContentValues();
    values.put(GeoPackageMetadata.COLUMN_NAME, metadata.getName());
    values.put(GeoPackageMetadata.COLUMN_EXTERNAL_PATH, metadata.getExternalPath());
    long insertId = db.insert(
            GeoPackageMetadata.TABLE_NAME, null,
            values);
    if (insertId == -1) {
        throw new GeoPackageException(
                "Failed to insert GeoPackage metadata. Name: "
                        + metadata.getName() + ", External Path: "
                        + metadata.getExternalPath());
    }
    metadata.setId(insertId);
}
 
Example 30
Source Project: geopackage-core-java   Source File: FeatureTableCoreIndex.java    License: MIT License 6 votes vote down vote up
/**
 * Delete the feature table index
 * 
 * @return true if index deleted
 */
public boolean deleteIndex() {

	boolean deleted = false;

	ExtensionsDao extensionsDao = geoPackage.getExtensionsDao();
	TableIndexDao tableIndexDao = geoPackage.getTableIndexDao();
	try {
		// Delete geometry indices and table index
		if (tableIndexDao.isTableExists()) {
			deleted = tableIndexDao.deleteByIdCascade(tableName) > 0;
		}
		// Delete the extensions entry
		if (extensionsDao.isTableExists()) {
			deleted = extensionsDao.deleteByExtension(EXTENSION_NAME,
					tableName) > 0 || deleted;
		}
	} catch (SQLException e) {
		throw new GeoPackageException(
				"Failed to delete Table Index. GeoPackage: "
						+ geoPackage.getName() + ", Table: " + tableName,
				e);
	}

	return deleted;
}