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 support for 3d points #90

Merged
merged 2 commits into from
Sep 30, 2020
Merged
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
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 @@ -122,13 +123,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 @@ -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;
Expand Down Expand Up @@ -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();
Expand Down