From e482fbd1ffba29ca4e7f1bd5becd689e3cca9a1a Mon Sep 17 00:00:00 2001 From: Thomas ten Cate Date: Mon, 24 Apr 2023 12:35:50 +0200 Subject: [PATCH 1/2] Add SpatialRef::to_projjson() --- CHANGES.md | 4 ++++ src/spatial_ref/srs.rs | 17 +++++++++++++++++ src/spatial_ref/tests.rs | 16 ++++++++++++++++ 3 files changed, 37 insertions(+) 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..cb6367a5 100644 --- a/src/spatial_ref/tests.rs +++ b/src/spatial_ref/tests.rs @@ -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(); From dcb09ae6ad7527766586cfb0d4f89572caa2790a Mon Sep 17 00:00:00 2001 From: Thomas ten Cate Date: Mon, 24 Apr 2023 20:28:02 +0200 Subject: [PATCH 2/2] Ignore .aux.xml file generated by test runs --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index a7ddff29..12fa417c 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,7 @@ /.vscode /gdal-sys/target /.idea +/fixtures/tinymarble.tif.aux.xml # gtags GPATH