Skip to content

Commit

Permalink
Rename get_point_vec to get_points
Browse files Browse the repository at this point in the history
  • Loading branch information
lnicola committed Jan 11, 2025
1 parent 0a7fec0 commit 90d7cbf
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 19 deletions.
6 changes: 3 additions & 3 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>` instead of `Result<String>` ([#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

Expand All @@ -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

Expand Down
24 changes: 11 additions & 13 deletions src/vector/geometry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,23 +219,21 @@ 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()) };
out_points.extend((0..length).map(|i| self.get_point(i)));
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()) };
Expand Down Expand Up @@ -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());
Expand All @@ -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());
}

Expand All @@ -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)));
}
Expand Down
2 changes: 1 addition & 1 deletion src/vector/layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
[
Expand Down
2 changes: 1 addition & 1 deletion src/vector/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion src/vector/ops/conversions/gdal_to_geo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl TryFrom<&Geometry> for geo_types::Geometry<f64> {
}
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 })
Expand Down

0 comments on commit 90d7cbf

Please sign in to comment.