Skip to content

Commit

Permalink
fix tests to work with gdal 3.0 + fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
jdroenner committed Jun 17, 2020
1 parent 9fefb0e commit efd060f
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 17 deletions.
2 changes: 1 addition & 1 deletion examples/read_write_ogr_datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
use chrono::Duration;
use gdal::errors::Error;
use gdal::vector::*;
use std::path::Path;
use std::ops::Add;
use std::path::Path;

#[cfg(feature = "datetime")]
fn run() -> Result<(), Error> {
Expand Down
5 changes: 1 addition & 4 deletions src/spatial_ref/srs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,11 +313,8 @@ impl SpatialRef {

#[cfg(feature = "gdal_3_0")]
pub fn get_axis_mapping_strategy(&self) -> gdal_sys::OSRAxisMappingStrategy::Type {
unsafe {
gdal_sys::OSRGetAxisMappingStrategy(self.0)
}
unsafe { gdal_sys::OSRGetAxisMappingStrategy(self.0) }
}


// TODO: should this take self instead of &self?
pub fn to_c_hsrs(&self) -> OGRSpatialReferenceH {
Expand Down
69 changes: 57 additions & 12 deletions src/spatial_ref/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ use crate::vector::Geometry;
fn from_wkt_to_proj4() {
let spatial_ref = 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();
assert_eq!(
"+proj=longlat +ellps=WGS84 +towgs84=0,0,0,0,0,0,0 +no_defs ",
spatial_ref.to_proj4().unwrap()
"+proj=longlat +ellps=WGS84 +towgs84=0,0,0,0,0,0,0 +no_defs",
spatial_ref.to_proj4().unwrap().trim()
);
let spatial_ref = SpatialRef::from_definition("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();
assert_eq!(
"+proj=longlat +ellps=WGS84 +towgs84=0,0,0,0,0,0,0 +no_defs ",
spatial_ref.to_proj4().unwrap()
"+proj=longlat +ellps=WGS84 +towgs84=0,0,0,0,0,0,0 +no_defs",
spatial_ref.to_proj4().unwrap().trim()
);
}

Expand All @@ -23,23 +23,31 @@ fn from_proj4_to_wkt() {
"+proj=laea +lat_0=52 +lon_0=10 +x_0=4321000 +y_0=3210000 +ellps=GRS80 +units=m +no_defs",
)
.unwrap();
// TODO: handle proj changes on lib level
#[cfg(not(feature = "gdal_3_0"))]
assert_eq!(spatial_ref.to_wkt().unwrap(), "PROJCS[\"unnamed\",GEOGCS[\"GRS 1980(IUGG, 1980)\",DATUM[\"unknown\",SPHEROID[\"GRS80\",6378137,298.257222101]],PRIMEM[\"Greenwich\",0],UNIT[\"degree\",0.0174532925199433]],PROJECTION[\"Lambert_Azimuthal_Equal_Area\"],PARAMETER[\"latitude_of_center\",52],PARAMETER[\"longitude_of_center\",10],PARAMETER[\"false_easting\",4321000],PARAMETER[\"false_northing\",3210000],UNIT[\"Meter\",1]]");
#[cfg(feature = "gdal_3_0")]
assert_eq!(spatial_ref.to_wkt().unwrap(), "PROJCS[\"unknown\",GEOGCS[\"unknown\",DATUM[\"Unknown based on GRS80 ellipsoid\",SPHEROID[\"GRS 1980\",6378137,298.257222101,AUTHORITY[\"EPSG\",\"7019\"]]],PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],UNIT[\"degree\",0.0174532925199433,AUTHORITY[\"EPSG\",\"9122\"]]],PROJECTION[\"Lambert_Azimuthal_Equal_Area\"],PARAMETER[\"latitude_of_center\",52],PARAMETER[\"longitude_of_center\",10],PARAMETER[\"false_easting\",4321000],PARAMETER[\"false_northing\",3210000],UNIT[\"metre\",1,AUTHORITY[\"EPSG\",\"9001\"]],AXIS[\"Easting\",EAST],AXIS[\"Northing\",NORTH]]");
}

#[test]
fn from_epsg_to_wkt_proj4() {
let spatial_ref = SpatialRef::from_epsg(4326).unwrap();
let wkt = spatial_ref.to_wkt().unwrap();
// TODO: handle proj changes on lib level
#[cfg(not(feature = "gdal_3_0"))]
assert_eq!("GEOGCS[\"WGS 84\",DATUM[\"WGS_1984\",SPHEROID[\"WGS 84\",6378137,298.257223563,AUTHORITY[\"EPSG\",\"7030\"]],AUTHORITY[\"EPSG\",\"6326\"]],PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],UNIT[\"degree\",0.0174532925199433,AUTHORITY[\"EPSG\",\"9122\"]],AUTHORITY[\"EPSG\",\"4326\"]]", wkt);
#[cfg(feature = "gdal_3_0")]
assert_eq!("GEOGCS[\"WGS 84\",DATUM[\"WGS_1984\",SPHEROID[\"WGS 84\",6378137,298.257223563,AUTHORITY[\"EPSG\",\"7030\"]],AUTHORITY[\"EPSG\",\"6326\"]],PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],UNIT[\"degree\",0.0174532925199433,AUTHORITY[\"EPSG\",\"9122\"]],AXIS[\"Latitude\",NORTH],AXIS[\"Longitude\",EAST],AUTHORITY[\"EPSG\",\"4326\"]]", wkt);
let proj4string = spatial_ref.to_proj4().unwrap();
assert_eq!("+proj=longlat +datum=WGS84 +no_defs ", proj4string);
assert_eq!("+proj=longlat +datum=WGS84 +no_defs", proj4string.trim());
}

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

#[test]
Expand All @@ -60,6 +68,15 @@ fn comparison() {
fn transform_coordinates() {
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(3035).unwrap();

// TODO: handle axis order in tests
#[cfg(feature = "gdal_3_0")]
spatial_ref1
.set_axis_mapping_strategy(gdal_sys::OSRAxisMappingStrategy::OAMS_TRADITIONAL_GIS_ORDER);
#[cfg(feature = "gdal_3_0")]
spatial_ref2
.set_axis_mapping_strategy(gdal_sys::OSRAxisMappingStrategy::OAMS_TRADITIONAL_GIS_ORDER);

let transform = CoordTransform::new(&spatial_ref1, &spatial_ref2).unwrap();
let mut xs = [23.43, 23.50];
let mut ys = [37.58, 37.70];
Expand All @@ -84,6 +101,15 @@ fn transform_ogr_geometry() {
)
.unwrap();
let spatial_ref2 = 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();

// TODO: handle axis order in tests
#[cfg(feature = "gdal_3_0")]
spatial_ref1
.set_axis_mapping_strategy(gdal_sys::OSRAxisMappingStrategy::OAMS_TRADITIONAL_GIS_ORDER);
#[cfg(feature = "gdal_3_0")]
spatial_ref2
.set_axis_mapping_strategy(gdal_sys::OSRAxisMappingStrategy::OAMS_TRADITIONAL_GIS_ORDER);

let htransform = CoordTransform::new(&spatial_ref2, &spatial_ref1).unwrap();
geom.transform_inplace(&htransform).unwrap();
assert_eq!(expected_value, geom.wkt().unwrap());
Expand Down Expand Up @@ -117,6 +143,12 @@ fn failing_transformation() {
let wgs84 = SpatialRef::from_epsg(4326).unwrap();
let dhd_2 = SpatialRef::from_epsg(31462).unwrap();

// TODO: handle axis order in tests
#[cfg(feature = "gdal_3_0")]
wgs84.set_axis_mapping_strategy(gdal_sys::OSRAxisMappingStrategy::OAMS_TRADITIONAL_GIS_ORDER);
#[cfg(feature = "gdal_3_0")]
dhd_2.set_axis_mapping_strategy(gdal_sys::OSRAxisMappingStrategy::OAMS_TRADITIONAL_GIS_ORDER);

let mut x = [1979105.06, 0.0];
let mut y = [5694052.67, 0.0];
let mut z = [0.0, 0.0];
Expand All @@ -128,6 +160,13 @@ fn failing_transformation() {
let wgs84 = SpatialRef::from_epsg(4326).unwrap();
let webmercator = SpatialRef::from_epsg(3857).unwrap();

// TODO: handle axis order in tests
#[cfg(feature = "gdal_3_0")]
wgs84.set_axis_mapping_strategy(gdal_sys::OSRAxisMappingStrategy::OAMS_TRADITIONAL_GIS_ORDER);
#[cfg(feature = "gdal_3_0")]
webmercator
.set_axis_mapping_strategy(gdal_sys::OSRAxisMappingStrategy::OAMS_TRADITIONAL_GIS_ORDER);

let mut x = [1000000.0];
let mut y = [1000000.0];
let mut z = [0.0, 0.0];
Expand All @@ -136,8 +175,8 @@ fn failing_transformation() {
let r = trafo.transform_coords(&mut x, &mut y, &mut z);

assert_eq!(r.is_err(), true);
if let &ErrorKind::InvalidCoordinateRange { ref msg, .. } = r.unwrap_err().kind_ref() {
assert_eq!(msg, &Some("latitude or longitude exceeded limits".into()));
if let &ErrorKind::InvalidCoordinateRange { .. } = r.unwrap_err().kind_ref() {
// assert_eq!(msg, &Some("latitude or longitude exceeded limits".into()));
} else {
panic!("Wrong error type");
}
Expand Down Expand Up @@ -168,14 +207,20 @@ fn auto_identify() {
assert_eq!(spatial_ref.auth_code().unwrap(), 32632);
}


#[cfg(feature = "gdal_3_0")]
#[test]
fn axis_mapping_strategy() {
use gdal_sys;

let spatial_ref = SpatialRef::from_epsg(4326).unwrap();
assert_eq!(spatial_ref.get_axis_mapping_strategy(), gdal_sys::OSRAxisMappingStrategy::OAMS_AUTHORITY_COMPLIANT);
spatial_ref.set_axis_mapping_strategy(gdal_sys::OSRAxisMappingStrategy::OAMS_TRADITIONAL_GIS_ORDER);
assert_eq!(spatial_ref.get_axis_mapping_strategy(), gdal_sys::OSRAxisMappingStrategy::OAMS_TRADITIONAL_GIS_ORDER);
assert_eq!(
spatial_ref.get_axis_mapping_strategy(),
gdal_sys::OSRAxisMappingStrategy::OAMS_AUTHORITY_COMPLIANT
);
spatial_ref
.set_axis_mapping_strategy(gdal_sys::OSRAxisMappingStrategy::OAMS_TRADITIONAL_GIS_ORDER);
assert_eq!(
spatial_ref.get_axis_mapping_strategy(),
gdal_sys::OSRAxisMappingStrategy::OAMS_TRADITIONAL_GIS_ORDER
);
}
3 changes: 3 additions & 0 deletions src/vector/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,9 @@ fn test_geom_fields() {

let geom_field = layer.defn().geom_fields().next().unwrap();
let spatial_ref2 = SpatialRef::from_epsg(4326).unwrap();
#[cfg(feature = "gdal_3_0")]
spatial_ref2.set_axis_mapping_strategy(0);

assert!(geom_field.spatial_ref().unwrap() == spatial_ref2);
}

Expand Down

0 comments on commit efd060f

Please sign in to comment.