Skip to content

Commit

Permalink
Merge #264
Browse files Browse the repository at this point in the history
264: Added wrapper for OGR_L_SetFeature r=rmanoka a=geohardtke

- [x] I agree to follow the project's [code of conduct](https://github.com/georust/gdal/blob/master/CODE_OF_CONDUCT.md).
- [x] I added an entry to `CHANGES.md` if knowledge of this change could be valuable to users.
---

Hi, I tried to find a way to update the value of a field in a vector and couldn't find a way to achieve that. Maybe I missed something? If so, sorry. Otherwise here is a pull request that implements a wrapper for OGR_L_SetFeature. 

It can be used as follows:

```rust
        let ds_options = DatasetOptions {
            open_flags: GdalOpenFlags::GDAL_OF_UPDATE,
            ..DatasetOptions::default()
        };
        let ds = Dataset::open_ex("three_layer_ds.s3db"), 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();
        feature.set_field_integer("id", 1).ok();
        layer.set_feature(feature).ok();
 ```

Co-authored-by: Leonardo Hardtke <leonardo.hardtke@des.qld.gov.au>
Co-authored-by: geohardtke <48337686+geohardtke@users.noreply.github.com>
  • Loading branch information
3 people committed Jun 30, 2022
2 parents 69e77cc + 8530e84 commit 674cfe9
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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);
}
}

0 comments on commit 674cfe9

Please sign in to comment.