Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for creating a SpatialRef from a esri "wkt" definition #37

Merged
merged 1 commit into from
Aug 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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