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

Add Feature::unset_field #503

Merged
merged 1 commit into from
Dec 23, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

## Unreleased

- Added ability to convert between `Buffer<T>` and `ndarray::Array2<T>`.
- Implemented `IntoIterator`, `Index` and `IndexMut` for `Buffer<T>`.
- Added `Feature::unset_field`
- <https://github.com/georust/gdal/pull/503>

- Added ability to convert between `Buffer<T>` and `ndarray::Array2<T>`.
- Implemented `IntoIterator`, `Index` and `IndexMut` for `Buffer<T>`.
- **Breaking**: `Buffer<T>::size` is now private and accessed via `Buffer<T>::shape().
- **Breaking**: `Buffer<T>::data` is now private and accessed via `Buffer<T>::data().
- **Breaking**: Removed `Rasterband::read_as_array`, changed signature of `Rasterband::read_block` to return a `Buffer<T>`.
- **Breaking**: `Buffer<T>::data` is now private and accessed via `Buffer<T>::data().
- **Breaking**: Removed `Rasterband::read_as_array`, changed signature of `Rasterband::read_block` to return a `Buffer<T>`.
- **Breaking**: `Rasterband::write` and `Rasterband::write_block` now require a `&mut Buffer<T>` to handle possible case of drivers temporarily mutating input buffer.

- <https://github.com/georust/gdal/pull/494>
Expand Down
27 changes: 27 additions & 0 deletions src/vector/feature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -648,12 +648,28 @@ impl<'a> Feature<'a> {
}
}

/// Clear a field, marking it as null.
///
/// See: [`OGRFeature::SetFieldNull`][SetFieldNull]
///
/// [SetFieldNull]: https://gdal.org/api/ogrfeature_cpp.html#_CPPv4N10OGRFeature12SetFieldNullEi
pub fn set_field_null(&self, field_name: &str) -> Result<()> {
let idx = self.field_idx_from_name(field_name)?;
unsafe { gdal_sys::OGR_F_SetFieldNull(self.c_feature(), idx) };
Ok(())
}

/// Clear a field, marking it as unset.
///
/// See: [`OGRFeature::UnsetField`][UnsetField]
///
/// [UnsetField]: https://gdal.org/api/ogrfeature_cpp.html#_CPPv4N10OGRFeature10UnsetFieldEi
pub fn unset_field(&self, field_name: &str) -> Result<()> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess that should be (same for set_field_null())

Suggested change
pub fn unset_field(&self, field_name: &str) -> Result<()> {
pub fn unset_field(&mut self, field_name: &str) -> Result<()> {

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should, but all of these currently take &self for some reason and @metasim ran into some issues when trying to change them to &mut self.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's #499. I think it should work, but didn't try it yet.

let idx = self.field_idx_from_name(field_name)?;
unsafe { gdal_sys::OGR_F_UnsetField(self.c_feature(), idx) };
Ok(())
}

pub fn set_geometry(&mut self, geom: Geometry) -> Result<()> {
let rv = unsafe { gdal_sys::OGR_F_SetGeometry(self.c_feature, geom.c_geometry()) };
if rv != OGRErr::OGRERR_NONE {
Expand Down Expand Up @@ -943,4 +959,15 @@ mod tests {
}
}
}

#[test]
fn test_field_unset() {
let ds = Dataset::open(fixture("roads.geojson")).unwrap();

let mut layer = ds.layers().next().expect("layer");
let feature = layer.features().next().expect("feature");
for (field_name, _) in feature.fields() {
feature.unset_field(&field_name).unwrap();
}
}
}