Skip to content

Commit

Permalink
Add Dataset::has_capability for dataset capabilities test
Browse files Browse the repository at this point in the history
  • Loading branch information
Atreyagaurav committed Nov 28, 2024
1 parent 6d93482 commit 0b65e6e
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 3 deletions.
50 changes: 48 additions & 2 deletions src/dataset.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
use std::{ffi::CString, ffi::NulError, path::Path, ptr};
use std::{
ffi::NulError,
ffi::{CStr, CString},
path::Path,
ptr,
};

use gdal_sys::{CPLErr, GDALDatasetH, GDALMajorObjectH};

Expand All @@ -11,6 +16,29 @@ use crate::{
gdal_major_object::MajorObject, spatial_ref::SpatialRef, Driver, GeoTransform, Metadata,
};

pub struct DatasetCapability(&'static CStr);

/// Dataset capabilities
impl DatasetCapability {
/// Dataset can create new layers.
pub const CREATE_LAYER: DatasetCapability = DatasetCapability(c"CreateLayer");
/// Dataset can delete existing layers.
pub const DELETE_LAYER: DatasetCapability = DatasetCapability(c"DeleteLayer");
/// Layers of this datasource support CreateGeomField() just after layer creation.
pub const CREATE_GEOM_FIELD_AFTER_CREATE_LAYER: DatasetCapability =
DatasetCapability(c"CreateGeomFieldAfterCreateLayer");
/// Dataset supports curve geometries.
pub const CURVE_GEOMETRIES: DatasetCapability = DatasetCapability(c"CurveGeometries");
/// Dataset supports (efficient) transactions.
pub const TRANSACTIONS: DatasetCapability = DatasetCapability(c"Transactions");
/// Dataset supports transactions through emulation.
pub const EMULATED_TRANSACTIONS: DatasetCapability = DatasetCapability(c"EmulatedTransactions");
/// Dataset has a dedicated GetNextFeature() implementation, potentially returning features from layers in a non sequential way.
pub const RANDOM_LAYER_READ: DatasetCapability = DatasetCapability(c"RandomLayerRead");
/// Dataset supports calling CreateFeature() on layers in a non sequential way.
pub const RANDOM_LAYER_WRITE: DatasetCapability = DatasetCapability(c"RandomLayerWrite");
}

/// Wrapper around a [`GDALDataset`][GDALDataset] object.
///
/// Represents both a [vector dataset][vector-data-model]
Expand Down Expand Up @@ -327,6 +355,10 @@ impl Dataset {
}
Ok(transformation)
}

pub fn has_capability(&self, capability: DatasetCapability) -> bool {
unsafe { gdal_sys::GDALDatasetTestCapability(self.c_dataset(), capability.0.as_ptr()) == 1 }
}
}

impl MajorObject for Dataset {
Expand All @@ -351,7 +383,8 @@ impl Drop for Dataset {
mod tests {
use gdal_sys::GDALAccess;

use crate::test_utils::fixture;
use crate::dataset::DatasetCapability;
use crate::test_utils::{fixture, open_gpkg_for_update};
use crate::GdalOpenFlags;

use super::*;
Expand Down Expand Up @@ -446,6 +479,19 @@ mod tests {
.unwrap_err();
}

#[test]
fn test_dataset_capabilities() {
let ds = Dataset::open(fixture("poly.gpkg")).unwrap();
assert!(!ds.has_capability(DatasetCapability::CREATE_LAYER));
assert!(!ds.has_capability(DatasetCapability::DELETE_LAYER));
assert!(ds.has_capability(DatasetCapability::TRANSACTIONS));

let (_tmp_path, ds) = open_gpkg_for_update(&fixture("poly.gpkg"));
assert!(ds.has_capability(DatasetCapability::CREATE_LAYER));
assert!(ds.has_capability(DatasetCapability::DELETE_LAYER));
assert!(ds.has_capability(DatasetCapability::TRANSACTIONS));
}

#[test]
fn test_raster_count_on_vector() {
let ds = Dataset::open(fixture("roads.geojson")).unwrap();
Expand Down
2 changes: 1 addition & 1 deletion src/vector/layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub enum LayerCaps {
OLCAlterFieldDefn,
/// Layer capability for transactions
OLCTransactions,
/// Layer capability for feature deletiond
/// Layer capability for feature deletion
OLCDeleteFeature,
/// Layer capability for setting next feature index
OLCFastSetNextByIndex,
Expand Down

0 comments on commit 0b65e6e

Please sign in to comment.