Skip to content

Commit

Permalink
Merge #295
Browse files Browse the repository at this point in the history
295: Enabled non-consuming fallible conversion from GDAL to geo-types Geometry r=lnicola a=metasim

Also added `Geometry::to_geo` method, for symmetry with `ToGdal::to_gdal`.

- [x] I agree to follow the project's [code of conduct](https://github.com/georust/gdal/blob/master/CODE_OF_CONDUCT.md).
- [x] I added an entry to `CHANGES.md` if knowledge of this change could be valuable to users.
---

Closes #276



Co-authored-by: Simeon H.K. Fitch <[email protected]>
  • Loading branch information
bors[bot] and metasim authored Sep 3, 2022
2 parents 6cb6433 + 57b4894 commit 875d644
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@

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

- Added `Geometry::to_geo` method for GDAL to geo-types Geometry conversions.

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

- Add `Rasterband::set_scale` and `Rasterband::set_offset` methods

- <https://github.com/georust/gdal/pull/294>
Expand Down
11 changes: 9 additions & 2 deletions src/vector/gdal_to_geo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ use gdal_sys::{self, OGRwkbGeometryType};
use crate::errors::GdalError;
use crate::vector::Geometry;

impl TryFrom<Geometry> for geo_types::Geometry<f64> {
impl TryFrom<&Geometry> for geo_types::Geometry<f64> {
type Error = GdalError;

fn try_from(geo: Geometry) -> Result<geo_types::Geometry<f64>, Self::Error> {
fn try_from(geo: &Geometry) -> Result<geo_types::Geometry<f64>, Self::Error> {
let geometry_type = geo.geometry_type();

let ring = |n: usize| {
Expand Down Expand Up @@ -110,3 +110,10 @@ impl TryFrom<Geometry> for geo_types::Geometry<f64> {
}
}
}

impl TryFrom<Geometry> for geo_types::Geometry<f64> {
type Error = GdalError;
fn try_from(value: Geometry) -> Result<Self, Self::Error> {
Self::try_from(&value)
}
}
5 changes: 5 additions & 0 deletions src/vector/geometry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,11 @@ impl Geometry {
gdal_sys::OGR_G_AssignSpatialReference(self.c_geometry(), spatial_ref.to_c_hsrs())
};
}

/// Create a copy of self as a `geo-types` geometry.
pub fn to_geo(&self) -> Result<geo_types::Geometry<f64>> {
self.try_into()
}
}

impl Drop for Geometry {
Expand Down

0 comments on commit 875d644

Please sign in to comment.