From a4a5ad135b24c5bd2b8017ca47a29e1cf63b920b Mon Sep 17 00:00:00 2001 From: Leonardo Hardtke Date: Tue, 8 Mar 2022 20:46:02 +1000 Subject: [PATCH] Added wrapper for OGR_L_SetFeature --- CHANGES.md | 2 +- src/vector/layer.rs | 7 +++++++ src/vector/vector_tests/mod.rs | 27 +++++++++++++++++++++++++-- 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index e3f6851d..9532d572 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,7 +1,7 @@ # Changes ## Unreleased - +- Implemented wrapper for `OGR_L_SetFeature` - Add `programs::raster::build_vrt` - Add `GeoTransformEx` extension trait with `apply` and `invert` diff --git a/src/vector/layer.rs b/src/vector/layer.rs index 96cdc397..3cba4b44 100644 --- a/src/vector/layer.rs +++ b/src/vector/layer.rs @@ -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) diff --git a/src/vector/vector_tests/mod.rs b/src/vector/vector_tests/mod.rs index dce01dda..284410bc 100644 --- a/src/vector/vector_tests/mod.rs +++ b/src/vector/vector_tests/mod.rs @@ -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; @@ -777,4 +776,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 = 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); + } }