Skip to content

Commit

Permalink
Merge pull request #37 from weiznich/esri_srs
Browse files Browse the repository at this point in the history
Add support for creating a SpatialRef from a esri "wkt" definition
  • Loading branch information
jdroenner authored Aug 22, 2017
2 parents d9929c3 + 181faf0 commit b7a8245
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
1 change: 1 addition & 0 deletions gdal-sys/src/osr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ extern {
pub fn OSRSetFromUserInput(hSRS: *mut c_void, pszDefinition: *const c_char) -> OGRErr;
pub fn OSRImportFromEPSG(hSRS: *const c_void, nCode: c_int) -> OGRErr;
pub fn OSRImportFromProj4(hSRS: *mut c_void, proj4_string: *const c_char) -> OGRErr;
pub fn OSRImportFromESRI(hSRS: *mut c_void, esri_wkt: *const *const c_char) -> OGRErr;
pub fn OSRExportToWkt(hSRS: *const c_void, ppszReturn: &mut *const c_char) -> OGRErr;
pub fn OSRExportToPrettyWkt(hSRS: *const c_void, ppszReturn: &mut *const c_char, bSimplify: c_int) -> OGRErr;
pub fn OSRExportToProj4(hSRS: *const c_void, ppszReturn: &mut *const c_char) -> OGRErr;
Expand Down
13 changes: 13 additions & 0 deletions src/spatial_ref/srs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,19 @@ impl SpatialRef {
}
}

pub fn from_esri(esri_wkt: &str) -> Result<SpatialRef> {
let c_str = CString::new(esri_wkt)?;
let ptrs = vec![c_str.as_ptr(), ptr::null_mut()];
let null_ptr = ptr::null_mut();
let c_obj = unsafe { osr::OSRNewSpatialReference(null_ptr) };
let rv = unsafe { osr::OSRImportFromESRI(c_obj, ptrs.as_ptr()) };
if rv != ogr_enums::OGRErr::OGRERR_NONE {
Err(ErrorKind::OgrError(rv, "OSRImportFromESRI").into())
} else {
Ok(SpatialRef(c_obj))
}
}

pub fn from_c_obj(c_obj: *const c_void) -> Result<SpatialRef> {
let mut_c_obj = unsafe { osr::OSRClone(c_obj) };
if mut_c_obj.is_null() {
Expand Down
10 changes: 10 additions & 0 deletions src/spatial_ref/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,25 @@ fn from_epsg_to_wkt_proj4(){
assert_eq!("+proj=longlat +datum=WGS84 +no_defs ", proj4string);
}

#[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();
let proj4string = spatial_ref.to_proj4().unwrap();
assert_eq!("+proj=longlat +datum=WGS84 +no_defs ", proj4string);
}

#[test]
fn comparison(){
let spatial_ref1 = SpatialRef::from_wkt("GEOGCS[\"WGS 84\",DATUM[\"WGS_1984\",SPHEROID[\"WGS 84\",6378137,298.257223563,AUTHORITY[\"EPSG\",7030]],TOWGS84[0,0,0,0,0,0,0],AUTHORITY[\"EPSG\",6326]],PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",8901]],UNIT[\"DMSH\",0.0174532925199433,AUTHORITY[\"EPSG\",9108]],AXIS[\"Lat\",NORTH],AXIS[\"Long\",EAST],AUTHORITY[\"EPSG\",4326]]").unwrap();
let spatial_ref2 = SpatialRef::from_epsg(4326).unwrap();
let spatial_ref3 = SpatialRef::from_epsg(3025).unwrap();
let spatial_ref4 = SpatialRef::from_proj4("+proj=longlat +datum=WGS84 +no_defs ").unwrap();
let spatial_ref5 = 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();

assert_eq!(true, spatial_ref1 == spatial_ref2);
assert_eq!(false, spatial_ref2 == spatial_ref3);
assert_eq!(true, spatial_ref4 == spatial_ref2);
assert_eq!(true, spatial_ref5 == spatial_ref4);
}

#[test]
Expand Down

0 comments on commit b7a8245

Please sign in to comment.