Skip to content

Commit

Permalink
Applied suggested implementation of set_field_null in georust#427.
Browse files Browse the repository at this point in the history
Thanks to @nms-scribe.
  • Loading branch information
metasim committed Dec 20, 2023
1 parent bf43a3b commit 402cbcb
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

- Implemented `Feature::set_field_null`

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

- **Breaking**: Changed a number of APIs using `isize` when `usize` is semantically more appropriate: `Driver::create.*`, `Rasterband::overview`, `Dataset::{layer|into_layer|layer_count}`.

- <https://github.com/georust/gdal/pull/497>
Expand Down
48 changes: 43 additions & 5 deletions src/vector/feature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,21 @@ impl<'a> Feature<'a> {
}
}

fn set_field_null(&self, field_name: &str) -> Result<()> {
let c_str_field_name = CString::new(field_name)?;
let field_id =
unsafe { gdal_sys::OGR_F_GetFieldIndex(self.c_feature(), c_str_field_name.as_ptr()) };
if field_id == -1 {
return Err(GdalError::InvalidFieldName {
field_name: field_name.to_string(),
method_name: "OGR_F_GetFieldIndex",
});
}

unsafe { gdal_sys::OGR_F_SetFieldNull(self.c_feature(), field_id) };
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 @@ -911,9 +926,32 @@ pub fn field_type_to_name(ty: OGRFieldType::Type) -> String {
_string(rv)
}

#[test]
pub fn test_field_type_to_name() {
assert_eq!(field_type_to_name(OGRFieldType::OFTReal), "Real");
// We don't care what it returns when passed an invalid value, just that it doesn't crash.
field_type_to_name(4372521);
#[cfg(test)]
mod tests {
use super::*;
use crate::test_utils::fixture;
use crate::Dataset;

#[test]
fn test_field_type_to_name() {
assert_eq!(field_type_to_name(OGRFieldType::OFTReal), "Real");
// We don't care what it returns when passed an invalid value, just that it doesn't crash.
field_type_to_name(4372521);
}

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

for mut layer in ds.layers() {
for feature in layer.features() {
for (field_name, field_value) in feature.fields() {
if let Some(value) = field_value {
feature.set_field_null(&field_name).unwrap();
assert!(feature.field(&field_name).unwrap().is_none());
}
}
}
}
}
}

0 comments on commit 402cbcb

Please sign in to comment.