Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added wrapper for OGR_L_SetFeature #264

Merged
merged 2 commits into from Jun 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGES.md
Expand Up @@ -14,6 +14,10 @@

- <https://github.com/georust/gdal/pull/251>

- Implemented wrapper for `OGR_L_SetFeature`

- <https://github.com/georust/gdal/pull/264>

- Add `programs::raster::build_vrt`
- Add `GeoTransformEx` extension trait with `apply` and `invert`

Expand Down
7 changes: 7 additions & 0 deletions src/vector/layer.rs
Expand Up @@ -250,6 +250,13 @@ pub trait LayerAccess: Sized {
FeatureIterator::_with_layer(self)
}

/// Set a feature on this layer layer.
/// Refer[SetFeature](https://gdal.org/doxygen/classOGRLayer.html#a681139bfd585b74d7218e51a32144283)
fn set_feature(&self, feature: Feature) -> Result<()> {
unsafe { gdal_sys::OGR_L_SetFeature(self.c_layer(), feature.c_feature()) };
Ok(())
}

/// Set a spatial filter on this layer.
///
/// Refer [OGR_L_SetSpatialFilter](https://gdal.org/doxygen/classOGRLayer.html#a75c06b4993f8eb76b569f37365cd19ab)
Expand Down
27 changes: 25 additions & 2 deletions src/vector/vector_tests/mod.rs
Expand Up @@ -3,8 +3,7 @@ use super::{
OGRwkbGeometryType, OwnedLayer,
};
use crate::spatial_ref::SpatialRef;
use crate::{assert_almost_eq, Dataset, Driver};

use crate::{assert_almost_eq, Dataset, DatasetOptions, Driver, GdalOpenFlags};
mod convert_geo;
mod sql;

Expand Down Expand Up @@ -815,4 +814,28 @@ mod tests {
);
});
}

#[test]
fn test_set_feature() {
let ds_options = DatasetOptions {
open_flags: GdalOpenFlags::GDAL_OF_UPDATE,
..DatasetOptions::default()
};
let tmp_file = "/tmp/test.s3db";
std::fs::copy(fixture!("three_layer_ds.s3db"), tmp_file).unwrap();
let ds = Dataset::open_ex(tmp_file, ds_options).unwrap();
let mut layer = ds.layer(0).unwrap();
let fids: Vec<u64> = layer.features().map(|f| f.fid().unwrap()).collect();
let feature = layer.feature(fids[0]).unwrap();
// to original value of the id field in fid 0 is null; we will set it to 1.
feature.set_field_integer("id", 1).ok();
layer.set_feature(feature).ok();

// now we check that the field is 1.
let ds = Dataset::open(tmp_file).unwrap();
let layer = ds.layer(0).unwrap();
let feature = layer.feature(fids[0]).unwrap();
let value = feature.field("id").unwrap().unwrap().into_int().unwrap();
assert_eq!(value, 1);
}
}