diff --git a/CHANGES.md b/CHANGES.md index 616e5976..e5ea38ad 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -16,7 +16,7 @@ - Drop `LayerAccess::create_feature_fields` ([#581](https://github.com/georust/gdal/pull/581)) - Drop `Feature::geometry_by_name` ([#594](https://github.com/georust/gdal/pull/594)) - Update `SpatialRef::auth_name`, `SpatialRef::name`, `SpatialRef::angular_units_name`, `SpatialRef::linear_units_name` to return `Option` instead of `Result` ([#589](https://github.com/georust/gdal/pull/589)) - - Update `Geometry::get_point_vec` to modify a `&mut Vec` as opposed to new allocations, returning an `i32` of points added ([#600](https://github.com/georust/gdal/pull/600)) + - Rename `Geometry::get_point_vec` to `get_points` and take a `&mut Vec`, to reduce allocations ([#600](https://github.com/georust/gdal/pull/600)) ### Added @@ -29,13 +29,13 @@ - Add `Defn::field_index` and `Feature::field_index` ([#581](https://github.com/georust/gdal/pull/581)) - Add `Defn::geometry_field_index` and `Feature::geometry_field_index` ([#594](https://github.com/georust/gdal/pull/594)) - Add `Dataset::has_capability` for dataset capability check ([#581](https://github.com/georust/gdal/pull/585)) - - Add methods `add_point_zm`, `add_point_m`, `set_point_zm`, `set_point_m`, `get_point_zm`, `get_point_vec_zm`, `iso_wkt` and `iso_wkb` to `Geometry` ([#600](https://github.com/georust/gdal/pull/600)) + - Add methods `add_point_zm`, `add_point_m`, `set_point_zm`, `set_point_m`, `get_point_zm`, `get_points_zm`, `iso_wkt` and `iso_wkb` to `Geometry` ([#600](https://github.com/georust/gdal/pull/600)) - Add functions `geometry_type_flatten`, `geometry_type_set_z`, `geometry_type_set_m`, `geometry_type_set_modifier`, `geometry_type_has_z` and `geometry_type_has_m` to `vector::geometry` ([#600](https://github.com/georust/gdal/pull/600)) ### Fixed - Fix conversion from `ndarray` when the data is offsetted from the start of the buffer ([#569](https://github.com/georust/gdal/pull/569)) - - use ISO WKT for the `Debug` implementation of `Geometry`, in order to properly display measure values, ([#600](https://github.com/georust/gdal/pull/600)) + - use ISO WKT for the `Debug` implementation of `Geometry`, in order to properly display measure values ([#600](https://github.com/georust/gdal/pull/600)) ### Removed diff --git a/src/vector/geometry.rs b/src/vector/geometry.rs index 4261ece4..1007404f 100644 --- a/src/vector/geometry.rs +++ b/src/vector/geometry.rs @@ -219,11 +219,10 @@ impl Geometry { (x, y, z, m) } - /// Appends all points of a line string to `out_points`. + /// Appends all points in the geometry to `out_points`, as XYZ. /// - /// Only wkbPoint[X], wkbLineString[X] or wkbCircularString[X] may alter `out_points`. Other geometry types will silently do nothing, see - /// [`OGR_G_GetPointCount`](https://gdal.org/en/stable/api/vector_c_api.html#_CPPv419OGR_G_GetPointCount12OGRGeometryH) - pub fn get_point_vec(&self, out_points: &mut Vec<(f64, f64, f64)>) -> usize { + /// For some geometry types, like polygons, that don't consist of points, `out_points` will not be modified. + pub fn get_points(&self, out_points: &mut Vec<(f64, f64, f64)>) -> usize { // Consider replacing logic with // [OGR_G_GetPoints](https://gdal.org/en/stable/api/vector_c_api.html#_CPPv415OGR_G_GetPoints12OGRGeometryHPviPviPvi) let length = unsafe { gdal_sys::OGR_G_GetPointCount(self.c_geometry()) }; @@ -231,11 +230,10 @@ impl Geometry { length as usize } - /// Appends all points of a line string to `out_points`. + /// Appends all points in the geometry to `out_points`, as XYZM. /// - /// Only wkbPoint[X], wkbLineString[X] or wkbCircularString[X] may alter `out_points`. Other geometry types will silently do nothing, see - /// [`OGR_G_GetPointCount`](https://gdal.org/en/stable/api/vector_c_api.html#_CPPv419OGR_G_GetPointCount12OGRGeometryH) - pub fn get_point_vec_zm(&self, out_points: &mut Vec<(f64, f64, f64, f64)>) -> usize { + /// For some geometry types, like polygons, that don't consist of points, `out_points` will not be modified. + pub fn get_points_zm(&self, out_points: &mut Vec<(f64, f64, f64, f64)>) -> usize { // Consider replacing logic with // [OGR_G_GetPoints](https://gdal.org/en/stable/api/vector_c_api.html#_CPPv415OGR_G_GetPoints12OGRGeometryHPviPviPvi) let length = unsafe { gdal_sys::OGR_G_GetPointCount(self.c_geometry()) }; @@ -656,19 +654,19 @@ mod tests { ring.add_point_2d((1179091.1646903288, 712782.8838459781)); assert!(!ring.is_empty()); let mut ring_vec: Vec<(f64, f64, f64)> = Vec::new(); - ring.get_point_vec(&mut ring_vec); + ring.get_points(&mut ring_vec); assert_eq!(ring_vec.len(), 6); let mut poly = Geometry::empty(wkbPolygon).unwrap(); poly.add_geometry(ring.to_owned()).unwrap(); let mut poly_vec: Vec<(f64, f64, f64)> = Vec::new(); - poly.get_point_vec(&mut poly_vec); + poly.get_points(&mut poly_vec); // Points are in ring, not containing geometry. // NB: In Python SWIG bindings, `GetPoints` is fallible. assert!(poly_vec.is_empty()); assert_eq!(poly.geometry_count(), 1); let ring_out = poly.get_geometry(0); let mut ring_out_vec: Vec<(f64, f64, f64)> = Vec::new(); - ring_out.get_point_vec(&mut ring_out_vec); + ring_out.get_points(&mut ring_out_vec); // 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()); @@ -685,7 +683,7 @@ mod tests { assert!(geom.json().unwrap().contains("Polygon")); let inner = geom.get_geometry(0); let mut points: Vec<(f64, f64, f64)> = Vec::new(); - inner.get_point_vec(&mut points); + inner.get_points(&mut points); assert!(!points.is_empty()); } @@ -696,7 +694,7 @@ mod tests { line.add_point_zm((1.0, 0.0, 0.25, 0.5)); line.add_point_zm((1.0, 1.0, 0.5, 1.0)); let mut line_points: Vec<(f64, f64, f64, f64)> = Vec::new(); - line.get_point_vec_zm(&mut line_points); + line.get_points_zm(&mut line_points); assert_eq!(line_points.len(), 3); assert_eq!(line_points.get(2), Some(&(1.0, 1.0, 0.5, 1.0))); } diff --git a/src/vector/layer.rs b/src/vector/layer.rs index 30323bfd..6eaeb28f 100644 --- a/src/vector/layer.rs +++ b/src/vector/layer.rs @@ -1271,7 +1271,7 @@ mod tests { let geom = feature.geometry().unwrap(); assert_eq!(geom.geometry_type(), OGRwkbGeometryType::wkbLineString); let mut coords: Vec<(f64, f64, f64)> = Vec::new(); - geom.get_point_vec(&mut coords); + geom.get_points(&mut coords); assert_eq!( coords, [ diff --git a/src/vector/mod.rs b/src/vector/mod.rs index 30ab740e..7adeb0c8 100644 --- a/src/vector/mod.rs +++ b/src/vector/mod.rs @@ -30,7 +30,7 @@ //! // Summarize the geometry //! let geometry = feature.geometry().unwrap(); //! let geom_type = geometry_type_to_name(geometry.geometry_type()); -//! let geom_len = geometry.get_point_vec(&mut Vec::new()); +//! let geom_len = geometry.get_points(&mut Vec::new()); //! println!(" Feature fid={fid:?}, geometry_type='{geom_type}', geometry_len={geom_len}"); //! // Get all the available fields and print their values //! for field in feature.fields() { diff --git a/src/vector/ops/conversions/gdal_to_geo.rs b/src/vector/ops/conversions/gdal_to_geo.rs index cabe15c7..7a225b47 100644 --- a/src/vector/ops/conversions/gdal_to_geo.rs +++ b/src/vector/ops/conversions/gdal_to_geo.rs @@ -45,7 +45,7 @@ impl TryFrom<&Geometry> for geo_types::Geometry { } OGRwkbGeometryType::wkbLineString => { let mut gdal_coords: Vec<(f64, f64, f64)> = Vec::new(); - geo.get_point_vec(&mut gdal_coords); + geo.get_points(&mut gdal_coords); let coords = gdal_coords .into_iter() .map(|(x, y, _)| geo_types::Coord { x, y })