Skip to content

Commit

Permalink
Fix code formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
dmarteau committed Jan 21, 2021
1 parent 397e93c commit b140f7c
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 35 deletions.
4 changes: 2 additions & 2 deletions src/spatial_ref/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
mod srs;

pub use srs::{ CoordTransform, SpatialRef, AxisOrientationType };
pub use gdal_sys::{ OGRAxisOrientation };
pub use gdal_sys::OGRAxisOrientation;
pub use srs::{AxisOrientationType, CoordTransform, SpatialRef};

#[cfg(test)]
mod tests;
43 changes: 26 additions & 17 deletions src/spatial_ref/srs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ pub struct AreaOfUse {

pub type AxisOrientationType = gdal_sys::OGRAxisOrientation::Type;


#[derive(Debug)]
pub struct SpatialRef(OGRSpatialReferenceH);

Expand Down Expand Up @@ -349,7 +348,7 @@ impl SpatialRef {
pub fn get_linear_units(&self) -> f64 {
unsafe { gdal_sys::OSRGetLinearUnits(self.0, ptr::null_mut()) }
}

#[inline]
pub fn is_geographic(&self) -> bool {
unsafe { gdal_sys::OSRIsGeographic(self.0) == 1 }
Expand All @@ -361,7 +360,7 @@ impl SpatialRef {
unsafe { gdal_sys::OSRIsDerivedGeographic(self.0) == 1 }
}

#[inline]
#[inline]
pub fn is_local(&self) -> bool {
unsafe { gdal_sys::OSRIsLocal(self.0) == 1 }
}
Expand All @@ -383,33 +382,41 @@ impl SpatialRef {

#[inline]
pub fn is_vertical(&self) -> bool {
unsafe { gdal_sys::OSRIsVertical(self.0) == 1 }
unsafe { gdal_sys::OSRIsVertical(self.0) == 1 }
}

pub fn get_axis_orientation(&self, target_key: &str, axis: i32 ) -> AxisOrientationType {
// We can almost safely assume that if we fail to build a CString then the input
// is not a valide key.
pub fn get_axis_orientation(&self, target_key: &str, axis: i32) -> AxisOrientationType {
// We can almost safely assume that if we fail to build a CString then the input
// is not a valide key.
let mut orientation = gdal_sys::OGRAxisOrientation::OAO_Other;
if let Ok(c_str) = CString::new(target_key) {
unsafe { gdal_sys::OSRGetAxis(self.0, c_str.as_ptr(), axis as c_int, &mut orientation) };
if let Ok(c_str) = CString::new(target_key) {
unsafe {
gdal_sys::OSRGetAxis(self.0, c_str.as_ptr(), axis as c_int, &mut orientation)
};
}
orientation
}

pub fn get_axis_name(&self, target_key: &str, axis: i32 ) -> Option<String> {
pub fn get_axis_name(&self, target_key: &str, axis: i32) -> Option<String> {
// See get_axis_orientation
if let Ok(c_str) = CString::new(target_key) {
let c_ptr = unsafe { gdal_sys::OSRGetAxis(self.0, c_str.as_ptr(), axis as c_int, ptr::null_mut()) };
let c_ptr = unsafe {
gdal_sys::OSRGetAxis(self.0, c_str.as_ptr(), axis as c_int, ptr::null_mut())
};
// null ptr indicate a failure (but no CPLError) see Gdal documentation.
if c_ptr.is_null() { None } else { Some(_string(c_ptr)) }
if c_ptr.is_null() {
None
} else {
Some(_string(c_ptr))
}
} else {
None
}
}

#[cfg(all(major_ge_3, minor_ge_1))]
pub fn get_axes_count(&self) -> i32 {
unsafe { gdal_sys::OSRGetAxesCount(self.0) }
unsafe { gdal_sys::OSRGetAxesCount(self.0) }
}

#[cfg(major_ge_3)]
Expand All @@ -427,16 +434,17 @@ impl SpatialRef {
#[cfg(major_ge_3)]
pub fn get_area_of_use(&self) -> Option<AreaOfUse> {
let mut c_area_name: *const libc::c_char = ptr::null_mut();
let (mut w_long, mut s_lat, mut e_long, mut n_lat): (f64, f64, f64, f64) = (0.0,0.0,0.0,0.0);
let (mut w_long, mut s_lat, mut e_long, mut n_lat): (f64, f64, f64, f64) =
(0.0, 0.0, 0.0, 0.0);
let ret_val = unsafe {
gdal_sys::OSRGetAreaOfUse(
self.0,
&mut w_long,
&mut s_lat,
&mut e_long,
&mut n_lat,
&mut c_area_name
) == 1
&mut c_area_name,
) == 1
};

if ret_val {
Expand All @@ -445,7 +453,8 @@ impl SpatialRef {
south_lat_degree: s_lat,
east_lon_degree: e_long,
north_lat_degree: n_lat,
name: _string(c_area_name) })
name: _string(c_area_name),
})
} else {
None
}
Expand Down
30 changes: 14 additions & 16 deletions src/spatial_ref/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,21 +228,20 @@ fn axis_mapping_strategy() {
fn get_area_of_use() {
let spatial_ref = SpatialRef::from_epsg(4326).unwrap();
let area_of_use = spatial_ref.get_area_of_use().unwrap();
assert_eq!(area_of_use.west_lon_degree, -180.0);
assert_eq!(area_of_use.south_lat_degree, -90.0);
assert_eq!(area_of_use.east_lon_degree, 180.0);
assert_eq!(area_of_use.north_lat_degree, 90.0);
assert_almost_eq(area_of_use.west_lon_degree, -180.0);
assert_almost_eq(area_of_use.south_lat_degree, -90.0);
assert_almost_eq(area_of_use.east_lon_degree, 180.0);
assert_almost_eq(area_of_use.north_lat_degree, 90.0);
}

#[cfg(major_ge_3)]
#[test]
fn get_name() {
let spatial_ref = SpatialRef::from_epsg(4326).unwrap();
let name = spatial_ref.get_name().unwrap();
assert_eq!(name,"WGS 84");
assert_eq!(name, "WGS 84");
}


#[test]
fn get_units_epsg4326() {
let spatial_ref = SpatialRef::from_epsg(4326).unwrap();
Expand Down Expand Up @@ -287,24 +286,23 @@ fn predicats_epsg2154() {

#[cfg(all(major_ge_3, minor_ge_1))]
assert!(!spatial_ref_2154.is_derived_geographic());
}
}

//XXX Gdal 2 implementation is partial
#[cfg(major_ge_3)]
#[cfg(major_ge_3)]
#[test]
fn crs_axis() {
let spatial_ref = SpatialRef::from_epsg(4326).unwrap();

#[cfg(all(major_ge_3, minor_ge_1))]
assert_eq!(spatial_ref.get_axes_count(),2);

let orientation = spatial_ref.get_axis_orientation("GEOGCS",0);
assert_eq!(orientation,gdal_sys::OGRAxisOrientation::OAO_North);
assert_eq!(spatial_ref.get_axes_count(), 2);

assert!(spatial_ref.get_axis_name("GEOGCS",0).is_some());
assert!(spatial_ref.get_axis_name("DO_NO_EXISTS",0).is_none());
let orientation = spatial_ref.get_axis_orientation("GEOGCS", 0);
assert_eq!(orientation, gdal_sys::OGRAxisOrientation::OAO_North);

let orientation = spatial_ref.get_axis_orientation("DO_NO_EXISTS",0);
assert!(spatial_ref.get_axis_name("GEOGCS", 0).is_some());
assert!(spatial_ref.get_axis_name("DO_NO_EXISTS", 0).is_none());

let orientation = spatial_ref.get_axis_orientation("DO_NO_EXISTS", 0);
assert_eq!(orientation, gdal_sys::OGRAxisOrientation::OAO_Other);
}

0 comments on commit b140f7c

Please sign in to comment.