diff --git a/CHANGES.md b/CHANGES.md index 2884ca71..6047c178 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,6 +1,10 @@ # Changes ## Unreleased +- Added `SpatialRef::to_projjson` + + - + - Added `Geometry::length` - diff --git a/src/spatial_ref/srs.rs b/src/spatial_ref/srs.rs index 18ec9772..950f350a 100644 --- a/src/spatial_ref/srs.rs +++ b/src/spatial_ref/srs.rs @@ -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 { + 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::()) }; + res + } + pub fn auth_name(&self) -> Result { let c_ptr = unsafe { gdal_sys::OSRGetAuthorityName(self.0, ptr::null()) }; if c_ptr.is_null() { diff --git a/src/spatial_ref/tests.rs b/src/spatial_ref/tests.rs index e78118f6..163179f7 100644 --- a/src/spatial_ref/tests.rs +++ b/src/spatial_ref/tests.rs @@ -43,6 +43,18 @@ 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 (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();