diff --git a/src/dataset.rs b/src/dataset.rs index 857728d9..9833e767 100644 --- a/src/dataset.rs +++ b/src/dataset.rs @@ -221,7 +221,17 @@ impl Dataset { #[cfg(major_ge_3)] /// Get the spatial reference system for this dataset. pub fn spatial_ref(&self) -> Result { - unsafe { SpatialRef::from_c_obj(gdal_sys::GDALGetSpatialRef(self.c_dataset)) } + unsafe { + let spatial_ref = gdal_sys::GDALGetSpatialRef(self.c_dataset); + if spatial_ref.is_null() { + Err(GdalError::NullPointer { + method_name: "GDALGetSpatialRef", + msg: "Unable to get a spatial reference".to_string(), + }) + } else { + SpatialRef::from_c_obj(spatial_ref) + } + } } #[cfg(major_ge_3)] diff --git a/src/spatial_ref/srs.rs b/src/spatial_ref/srs.rs index e211319c..5909c728 100644 --- a/src/spatial_ref/srs.rs +++ b/src/spatial_ref/srs.rs @@ -51,6 +51,7 @@ impl SpatialRef { /// # Safety /// The handle passed to this function must be valid. pub unsafe fn from_c_obj(c_obj: gdal_sys::OGRSpatialReferenceH) -> Result { + assert!(!c_obj.is_null(), "Expected a pointer that is not null"); let mut_c_obj = gdal_sys::OSRClone(c_obj); if mut_c_obj.is_null() { Err(_last_null_pointer_err("OSRClone"))