Skip to content

Commit

Permalink
Merge #389
Browse files Browse the repository at this point in the history
389: Add SpatialRef::to_projjson() r=ttencate a=ttencate

- [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: Thomas ten Cate <ttencate@gmail.com>
  • Loading branch information
bors[bot] and ttencate authored Apr 24, 2023
2 parents 670401a + dcb09ae commit faf48e3
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
/.vscode
/gdal-sys/target
/.idea
/fixtures/tinymarble.tif.aux.xml

# gtags
GPATH
Expand Down
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 `SpatialRef::to_projjson`

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

- Added `Geometry::length`

- <https://github.com/georust/gdal/pull/384>
Expand Down
17 changes: 17 additions & 0 deletions src/spatial_ref/srs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,23 @@ impl SpatialRef {
res
}

#[cfg(any(major_ge_4, all(major_ge_3, minor_ge_1)))]
pub fn to_projjson(&self) -> Result<String> {
let mut c_projjsonstr = ptr::null_mut();
let options = ptr::null();
let rv = unsafe { gdal_sys::OSRExportToPROJJSON(self.0, &mut c_projjsonstr, options) };
let res = if rv != OGRErr::OGRERR_NONE {
Err(GdalError::OgrError {
err: rv,
method_name: "OSRExportToPROJJSON",
})
} else {
Ok(_string(c_projjsonstr))
};
unsafe { gdal_sys::VSIFree(c_projjsonstr.cast::<std::ffi::c_void>()) };
res
}

pub fn auth_name(&self) -> Result<String> {
let c_ptr = unsafe { gdal_sys::OSRGetAuthorityName(self.0, ptr::null()) };
if c_ptr.is_null() {
Expand Down
16 changes: 16 additions & 0 deletions src/spatial_ref/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,22 @@ fn from_epsg_to_wkt_proj4() {
assert_eq!("+proj=longlat +datum=WGS84 +no_defs", proj4string.trim());
}

#[cfg(any(major_ge_4, all(major_ge_3, minor_ge_1)))]
#[test]
fn from_epsg_to_projjson() {
let spatial_ref = SpatialRef::from_epsg(4326).unwrap();
let projjson = spatial_ref.to_projjson().unwrap();
// Testing for exact string equality would be too strict, since the order of keys in JSON is
// unspecified. Ideally, we'd parse the JSON and then compare the values, but adding a JSON
// parser as a dependency just for this one test would be overkill. Thus, we do only a quick
// sanity check.
assert!(
projjson.contains("World Geodetic System 1984"),
"{:?} does not contain expected CRS name",
projjson
);
}

#[test]
fn from_esri_to_proj4() {
let spatial_ref = SpatialRef::from_esri("GEOGCS[\"GCS_WGS_1984\",DATUM[\"D_WGS_1984\",SPHEROID[\"WGS_1984\",6378137,298.257223563]],PRIMEM[\"Greenwich\",0],UNIT[\"Degree\",0.017453292519943295]]").unwrap();
Expand Down

0 comments on commit faf48e3

Please sign in to comment.