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

Ability to get owned reference to inner geometry. #274

Merged
merged 6 commits into from Jun 14, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGES.md
Expand Up @@ -31,6 +31,10 @@

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

- Add `GeometryRef<'a>` to reference owned nested geometry in a lifetime-safe way.

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

## 0.12

- Bump Rust edition to 2021
Expand Down
31 changes: 30 additions & 1 deletion src/vector/geometry.rs
@@ -1,6 +1,8 @@
use std::cell::RefCell;
use std::ffi::CString;
use std::fmt::{self, Debug};
use std::fmt::{self, Debug, Formatter};
use std::marker::PhantomData;
use std::ops::Deref;
use std::ptr::null_mut;

use libc::{c_char, c_double, c_int, c_void};
Expand Down Expand Up @@ -310,6 +312,13 @@ impl Geometry {
Geometry::with_c_geometry(c_geom, false)
}

/// Get a reference to the geometry at given `index`
pub fn get_geometry(&self, index: usize) -> Result<GeometryRef> {
let geom = unsafe { self.get_unowned_geometry(index) };
let gref = GeometryRef { geom, _lifetime: PhantomData::default() };
Ok(gref)
metasim marked this conversation as resolved.
Show resolved Hide resolved
}

pub fn add_geometry(&mut self, mut sub: Geometry) -> Result<()> {
assert!(sub.owned);
sub.owned = false;
Expand Down Expand Up @@ -444,6 +453,26 @@ pub fn geometry_type_to_name(ty: OGRwkbGeometryType::Type) -> String {
_string(rv)
}

/// Reference to owned geometry
pub struct GeometryRef<'a> {
geom: Geometry,
_lifetime: PhantomData<&'a ()>
}

impl Deref for GeometryRef<'_> {
type Target = Geometry;

fn deref(&self) -> &Self::Target {
&self.geom
}
}

impl Debug for GeometryRef<'_> {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
Debug::fmt(&self.geom, f)
}
}

#[cfg(test)]
mod tests {
use crate::spatial_ref::SpatialRef;
Expand Down
38 changes: 38 additions & 0 deletions src/vector/vector_tests/mod.rs
Expand Up @@ -120,6 +120,7 @@ where

#[cfg(test)]
mod tests {
use gdal_sys::OGRwkbGeometryType::{wkbLinearRing, wkbLineString, wkbPolygon};
use super::*;
use crate::errors::{GdalError, Result};

Expand Down Expand Up @@ -508,6 +509,43 @@ mod tests {
});
}

#[test]
fn test_ring_points() {
let mut ring = Geometry::empty(wkbLinearRing).unwrap();
ring.add_point_2d((1179091.1646903288, 712782.8838459781));
ring.add_point_2d((1161053.0218226474, 667456.2684348812));
ring.add_point_2d((1214704.933941905, 641092.8288590391));
ring.add_point_2d((1228580.428455506, 682719.3123998424));
ring.add_point_2d((1218405.0658121984, 721108.1805541387));
ring.add_point_2d((1179091.1646903288, 712782.8838459781));
assert!(!ring.is_empty());
assert_eq!(ring.get_point_vec().len(), 6);
let mut poly = Geometry::empty(wkbPolygon).unwrap();
poly.add_geometry(ring.to_owned()).unwrap();
// Points are in ring, not containing geometry.
// NB: In Python SWIG bindings, `GetPoints` is fallible.
assert!(poly.get_point_vec().is_empty());
assert_eq!(poly.geometry_count(), 1);
let ring_out = poly.get_geometry(0).unwrap();
// NB: `wkb()` shows it to be a `LINEARRING`, but returned type is LineString
assert_eq!(ring_out.geometry_type(), wkbLineString);
assert!(!&ring_out.is_empty());
assert_eq!(ring.get_point_vec(), ring_out.get_point_vec());
}

#[test]
fn test_get_inner_points() {
let geom = Geometry::bbox(0., 0., 1., 1.).unwrap();
assert!(!geom.is_empty());
assert_eq!(geom.geometry_count(), 1);
assert!(geom.area() > 0.);
assert_eq!(geom.geometry_type(), OGRwkbGeometryType::wkbPolygon);
assert!(geom.json().unwrap().contains("Polygon"));
let inner = geom.get_geometry(0).unwrap();
let points = inner.get_point_vec();
assert!(!points.is_empty());
}

#[test]
fn test_wkt() {
with_feature("roads.geojson", 236194095, |feature| {
Expand Down