Skip to content

Commit

Permalink
Add Geometry::length
Browse files Browse the repository at this point in the history
  • Loading branch information
lnicola committed Mar 29, 2023
1 parent 7b874de commit 635efbb
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 15 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Changes

## Unreleased
- Added `Geometry::length`

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

- Added `Geometry::union`

- <https://github.com/georust/gdal/pull/379>
Expand Down
54 changes: 39 additions & 15 deletions src/vector/geometry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,19 @@ impl Geometry {
Ok(())
}

/// Compute geometry area in units of the spatial reference system in use.
///
/// Supported for `Curve` (including `LineString` and `CircularString`) and `MultiCurve`.
/// Returns zero for all other geometry types.
///
/// See: [`OGR_G_Length`](https://gdal.org/api/vector_c_api.html#_CPPv412OGR_G_Length12OGRGeometryH)
pub fn length(&self) -> f64 {
unsafe { gdal_sys::OGR_G_Length(self.c_geometry()) }
}

/// Compute geometry area in square units of the spatial reference system in use.
///
/// Supported for `LinearRing`, `Polygon` or `MultiPolygon`.
/// Supported for `LinearRing`, `Polygon` and `MultiPolygon`.
/// Returns zero for all other geometry types.
///
/// See: [`OGR_G_Area`](https://gdal.org/api/vector_c_api.html#_CPPv410OGR_G_Area12OGRGeometryH)
Expand Down Expand Up @@ -370,19 +380,36 @@ mod tests {
use super::*;
use crate::spatial_ref::SpatialRef;
use crate::test_utils::SuppressGDALErrorLog;
use gdal_sys::OGRwkbGeometryType::{wkbLineString, wkbLinearRing, wkbPolygon};
use gdal_sys::OGRwkbGeometryType::{
wkbLineString, wkbLinearRing, wkbMultiPoint, wkbMultiPolygon, wkbPoint, wkbPolygon,
};

#[test]
fn test_create_bbox() {
let bbox = Geometry::bbox(-27., 33., 52., 85.).unwrap();
assert_eq!(bbox.json().unwrap(), "{ \"type\": \"Polygon\", \"coordinates\": [ [ [ -27.0, 85.0 ], [ 52.0, 85.0 ], [ 52.0, 33.0 ], [ -27.0, 33.0 ], [ -27.0, 85.0 ] ] ] }");
}

#[test]
#[allow(clippy::float_cmp)]
pub fn test_length() {
let _nolog = SuppressGDALErrorLog::new();
let geom = Geometry::empty(wkbPoint).unwrap();
assert_eq!(geom.area(), 0.0);

let geom = Geometry::from_wkt("POINT(0 0)").unwrap();
assert_eq!(geom.area(), 0.0);

let wkt = "LINESTRING (0 10, 10 10, 10 15)";
let geom = Geometry::from_wkt(wkt).unwrap();
assert_eq!(geom.length() as i32, 15);
}

#[test]
#[allow(clippy::float_cmp)]
pub fn test_area() {
let _nolog = SuppressGDALErrorLog::new();
let geom = Geometry::empty(::gdal_sys::OGRwkbGeometryType::wkbMultiPolygon).unwrap();
let geom = Geometry::empty(wkbMultiPolygon).unwrap();
assert_eq!(geom.area(), 0.0);

let geom = Geometry::from_wkt("POINT(0 0)").unwrap();
Expand All @@ -395,7 +422,7 @@ mod tests {

#[test]
pub fn test_is_empty() {
let geom = Geometry::empty(::gdal_sys::OGRwkbGeometryType::wkbMultiPolygon).unwrap();
let geom = Geometry::empty(wkbMultiPolygon).unwrap();
assert!(geom.is_empty());

let geom = Geometry::from_wkt("POINT(0 0)").unwrap();
Expand All @@ -408,11 +435,11 @@ mod tests {

#[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();
let mut geom = Geometry::empty(wkbMultiPoint).unwrap();
let mut point = Geometry::empty(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();
let mut point = Geometry::empty(wkbPoint).unwrap();
point.add_point_2d((2.0, 3.0));
assert!(!point.is_empty());
point.set_point_2d(0, (2.0, 4.0));
Expand All @@ -425,11 +452,11 @@ mod tests {

#[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();
let mut geom = Geometry::empty(wkbMultiPoint).unwrap();
let mut point = Geometry::empty(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();
let mut point = Geometry::empty(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));
Expand All @@ -442,7 +469,7 @@ mod tests {

#[test]
pub fn test_spatial_ref() {
let geom = Geometry::empty(::gdal_sys::OGRwkbGeometryType::wkbMultiPolygon).unwrap();
let geom = Geometry::empty(wkbMultiPolygon).unwrap();
assert!(geom.spatial_ref().is_none());

let geom = Geometry::from_wkt("POINT(0 0)").unwrap();
Expand Down Expand Up @@ -496,10 +523,7 @@ mod tests {

#[test]
pub fn test_geometry_type_to_name() {
assert_eq!(
geometry_type_to_name(::gdal_sys::OGRwkbGeometryType::wkbLineString),
"Line String"
);
assert_eq!(geometry_type_to_name(wkbLineString), "Line String");
// We don't care what it returns when passed an invalid value, just that it doesn't crash.
geometry_type_to_name(4372521);
}
Expand Down

0 comments on commit 635efbb

Please sign in to comment.