Skip to content

Commit

Permalink
Merge #411
Browse files Browse the repository at this point in the history
411: impl DerefMut trait for GeoemetryRef  r=metasim a=scially

- [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.
---

This impl impl DerefMut trait for GeoemetryRef. This geometry of Geometery::get_geometry(i32) function can modify point coordinate. 

Co-authored-by: scially <[email protected]>
  • Loading branch information
bors[bot] and scially authored Jun 19, 2023
2 parents 004dabe + e1e0222 commit 106c2ae
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
/gdal-sys/target
/.idea
/fixtures/tinymarble.tif.aux.xml
/3rd

# gtags
GPATH
Expand Down
24 changes: 23 additions & 1 deletion src/vector/geometry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::cell::RefCell;
use std::fmt::{self, Debug, Formatter};
use std::marker::PhantomData;
use std::mem::MaybeUninit;
use std::ops::Deref;
use std::ops::{Deref, DerefMut};

use libc::{c_double, c_int};

Expand Down Expand Up @@ -369,6 +369,12 @@ impl Deref for GeometryRef<'_> {
}
}

impl DerefMut for GeometryRef<'_> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.geom
}
}

impl Debug for GeometryRef<'_> {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
Debug::fmt(&self.geom, f)
Expand Down Expand Up @@ -527,4 +533,20 @@ mod tests {
// We don't care what it returns when passed an invalid value, just that it doesn't crash.
geometry_type_to_name(4372521);
}

#[test]
pub fn test_geometry_modify() {
let polygon = Geometry::from_wkt("POLYGON ((30 10, 40 40, 20 40, 10 20, 30 10))").unwrap();
for i in 0..polygon.geometry_count() {
let mut ring = polygon.get_geometry(i);
for j in 0..ring.point_count() {
let (x, y, _) = ring.get_point(j as i32);
ring.set_point_2d(j, (x * 10.0, y * 10.0));
}
}
assert_eq!(
"POLYGON ((300 100,400 400,200 400,100 200,300 100))",
polygon.wkt().unwrap()
);
}
}

0 comments on commit 106c2ae

Please sign in to comment.