Skip to content

Commit

Permalink
Merge pull request #90 from NathanHowell/multipoint
Browse files Browse the repository at this point in the history
Add support for 3d points
  • Loading branch information
jdroenner authored Sep 30, 2020
2 parents 05136f4 + 1f8ae69 commit bf61b88
Showing 1 changed file with 73 additions and 0 deletions.
73 changes: 73 additions & 0 deletions src/vector/geometry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -140,13 +141,34 @@ 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 {
gdal_sys::OGR_G_SetPoint_2D(self.c_geometry(), i as c_int, x as c_double, y as c_double)
};
}

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.;
Expand Down Expand Up @@ -304,6 +326,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;
Expand Down Expand Up @@ -336,6 +375,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();
Expand Down

0 comments on commit bf61b88

Please sign in to comment.