Skip to content

Commit

Permalink
Add SpatialRef::to_projjson()
Browse files Browse the repository at this point in the history
  • Loading branch information
ttencate committed Apr 24, 2023
1 parent b8ad047 commit c39baff
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
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
16 changes: 16 additions & 0 deletions src/spatial_ref/srs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,22 @@ impl SpatialRef {
res
}

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
11 changes: 11 additions & 0 deletions src/spatial_ref/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,17 @@ fn from_epsg_to_wkt_proj4() {
assert_eq!("+proj=longlat +datum=WGS84 +no_defs", proj4string.trim());
}

#[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 (G1674)\""));
}

#[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 c39baff

Please sign in to comment.