diff --git a/src/vector/geometry.rs b/src/vector/geometry.rs index 759d7843..91d14054 100644 --- a/src/vector/geometry.rs +++ b/src/vector/geometry.rs @@ -3,6 +3,7 @@ use crate::utils::{_last_null_pointer_err, _string}; use gdal_sys::{self, OGRErr, OGRGeometryH, OGRwkbGeometryType}; use libc::{c_double, c_int, c_void}; use std::cell::RefCell; +use std::fmt::{self, Debug}; use std::ffi::CString; use std::ptr::null_mut; @@ -122,6 +123,13 @@ impl Geometry { self.c_geometry() } + pub fn set_point(&mut self, i: usize, p: (f64, f64, f64)) { + let (x, y, z) = p; + unsafe { + gdal_sys::OGR_G_SetPoint(self.c_geometry(), i as c_int, x as c_double, y as c_double, z as c_double); + }; + } + pub fn set_point_2d(&mut self, i: usize, p: (f64, f64)) { let (x, y) = p; unsafe { @@ -129,6 +137,20 @@ impl Geometry { }; } + pub fn add_point(&mut self, p: (f64, f64, f64)) { + let (x, y, z) = p; + unsafe { + gdal_sys::OGR_G_AddPoint(self.c_geometry(), x as c_double, y as c_double, z as c_double) + }; + } + + pub fn add_point_2d(&mut self, p: (f64, f64)) { + let (x, y) = p; + unsafe { + gdal_sys::OGR_G_AddPoint_2D(self.c_geometry(), x as c_double, y as c_double) + }; + } + pub fn get_point(&self, i: i32) -> (f64, f64, f64) { let mut x: c_double = 0.; let mut y: c_double = 0.; @@ -276,6 +298,23 @@ impl Clone for Geometry { } } +impl Debug for Geometry { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self.wkt() { + Ok(wkt) => f.write_str(wkt.as_str()), + Err(_) => Err(fmt::Error), + } + } +} + +impl PartialEq for Geometry { + fn eq(&self, other: &Self) -> bool { + unsafe { gdal_sys::OGR_G_Equal(self.c_geometry(), other.c_geometry()) != 0 } + } +} + +impl Eq for Geometry {} + #[cfg(test)] mod tests { use super::Geometry; @@ -307,6 +346,40 @@ mod tests { assert!(!geom.is_empty()); } + #[test] + pub fn test_create_multipoint_2d() { + let mut geom = Geometry::empty(::gdal_sys::OGRwkbGeometryType::wkbMultiPoint).unwrap(); + let mut point = Geometry::empty(::gdal_sys::OGRwkbGeometryType::wkbPoint).unwrap(); + point.add_point_2d((1.0, 2.0)); + geom.add_geometry(point).unwrap(); + let mut point = Geometry::empty(::gdal_sys::OGRwkbGeometryType::wkbPoint).unwrap(); + point.add_point_2d((2.0, 3.0)); + assert!(!point.is_empty()); + point.set_point_2d(0, (2.0, 4.0)); + geom.add_geometry(point).unwrap(); + assert!(!geom.is_empty()); + + let expected = Geometry::from_wkt("MULTIPOINT((1.0 2.0), (2.0 4.0))").unwrap(); + assert_eq!(geom, expected); + } + + #[test] + pub fn test_create_multipoint_3d() { + let mut geom = Geometry::empty(::gdal_sys::OGRwkbGeometryType::wkbMultiPoint).unwrap(); + let mut point = Geometry::empty(::gdal_sys::OGRwkbGeometryType::wkbPoint).unwrap(); + point.add_point((1.0, 2.0, 3.0)); + geom.add_geometry(point).unwrap(); + let mut point = Geometry::empty(::gdal_sys::OGRwkbGeometryType::wkbPoint).unwrap(); + point.add_point((3.0, 2.0, 1.0)); + assert!(!point.is_empty()); + point.set_point(0, (4.0, 2.0, 1.0)); + geom.add_geometry(point).unwrap(); + assert!(!geom.is_empty()); + + let expected = Geometry::from_wkt("MULTIPOINT((1.0 2.0 3.0), (4.0 2.0 1.0))").unwrap(); + assert_eq!(geom, expected); + } + #[test] pub fn test_spatial_reference() { let geom = Geometry::empty(::gdal_sys::OGRwkbGeometryType::wkbMultiPolygon).unwrap();