Skip to content

Commit

Permalink
Merge #378
Browse files Browse the repository at this point in the history
378: Add `Geometry::from_gml` r=lnicola a=lnicola

- [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.
---



Co-authored-by: Laurențiu Nicola <[email protected]>
  • Loading branch information
bors[bot] and lnicola authored Mar 8, 2023
2 parents 7efcc41 + f69a32a commit 1dbc602
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

- Added `Geometry::from_gml`

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

- Added `CoordTransform::new_with_options` and `CoordTransformOptions`

- <https://github.com/georust/gdal/pull/372>
Expand Down
26 changes: 25 additions & 1 deletion src/vector/ops/conversions/formats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,18 @@ impl Geometry {
let c_geojson = CString::new(json)?;
let c_geom = unsafe { gdal_sys::OGR_G_CreateGeometryFromJson(c_geojson.as_ptr()) };
if c_geom.is_null() {
return Err(_last_null_pointer_err("OGR_G_ExportToJson"));
return Err(_last_null_pointer_err("OGR_G_CreateGeometryFromJson"));
}
Ok(unsafe { Geometry::with_c_geometry(c_geom, true) })
}

/// Create a geometry by parsing a
/// [GML](https://en.wikipedia.org/wiki/Geography_Markup_Language) string.
pub fn from_gml(json: &str) -> Result<Geometry> {
let c_gml = CString::new(json)?;
let c_geom = unsafe { gdal_sys::OGR_G_CreateFromGML(c_gml.as_ptr()) };
if c_geom.is_null() {
return Err(_last_null_pointer_err("OGR_G_CreateFromGML"));
}
Ok(unsafe { Geometry::with_c_geometry(c_geom, true) })
}
Expand Down Expand Up @@ -142,4 +153,17 @@ mod tests {
let res = Geometry::from_geojson(json);
assert!(res.is_err());
}

#[test]
pub fn test_gml() {
let json = r#"<gml:Point xmlns:gml="http://www.opengis.net/gml"><gml:coordinates>10,20</gml:coordinates></gml:Point>"#;
let geom = Geometry::from_gml(json).unwrap();
let (x, y, _) = geom.get_point(0);
assert_eq!(x, 10.0);
assert_eq!(y, 20.0);

let json = r#"<gml:Point xmlns:gml="http://www.opengis.net/gml"><gml:coordinates>10</gml:coordinates></gml:Point>"#;
let res = Geometry::from_gml(json);
assert!(res.is_err());
}
}

0 comments on commit 1dbc602

Please sign in to comment.