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

Fix windows tests #341

Merged
merged 8 commits into from
Nov 21, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
8 changes: 5 additions & 3 deletions src/raster/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::vsi::unlink_mem_file;
use crate::DriverManager;
use gdal_sys::GDALDataType;
use std::path::Path;
use tempfile::tempdir;

#[cfg(feature = "ndarray")]
use ndarray::arr2;
Expand Down Expand Up @@ -308,17 +309,18 @@ fn test_create_with_band_type_with_options() {
},
];

let tmp_filename = "/tmp/test.tif";
let tmp_dir = tempdir().unwrap();
mwm126 marked this conversation as resolved.
Show resolved Hide resolved
let tmp_filename = tmp_dir.path().join("test.tif");
{
let dataset = driver
.create_with_band_type_with_options::<u8, _>(tmp_filename, 256, 256, 1, &options)
.create_with_band_type_with_options::<u8, _>(&tmp_filename, 256, 256, 1, &options)
.unwrap();
let rasterband = dataset.rasterband(1).unwrap();
let block_size = rasterband.block_size();
assert_eq!(block_size, (128, 64));
}

let dataset = Dataset::open(Path::new(tmp_filename)).unwrap();
let dataset = Dataset::open(tmp_filename).unwrap();
let key = "INTERLEAVE";
let domain = "IMAGE_STRUCTURE";
let meta = dataset.metadata_item(key, domain);
Expand Down
34 changes: 24 additions & 10 deletions src/vector/feature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::convert::TryInto;
use std::ffi::{CString, NulError};
use std::ptr;

use chrono::{Date, DateTime, Datelike, FixedOffset, TimeZone, Timelike};
use chrono::{DateTime, Datelike, FixedOffset, NaiveDate, TimeZone, Timelike};

use crate::errors::*;
use std::slice;
Expand Down Expand Up @@ -152,7 +152,7 @@ impl<'a> Feature<'a> {
self._field_as_datetime(field_id)?,
))),
OGRFieldType::OFTDate => Ok(Some(FieldValue::DateValue(
self._field_as_datetime(field_id)?.date(),
self._field_as_datetime(field_id)?.date_naive(),
))),
_ => Err(GdalError::UnhandledFieldType {
field_type,
Expand Down Expand Up @@ -428,10 +428,18 @@ impl<'a> Feature<'a> {
} else {
(tzflag as i32 - 100) * 15 * 60
};
let rv = FixedOffset::east(tzoffset_secs)
.ymd(year as i32, month as u32, day as u32)
.and_hms(hour as u32, minute as u32, second as u32);
Ok(rv)
let rv = FixedOffset::east_opt(tzoffset_secs)
.ok_or_else(|| GdalError::BadArgument("Invalid offset".to_string()))?;
rv.with_ymd_and_hms(
year as i32,
month as u32,
day as u32,
hour as u32,
minute as u32,
second as u32,
)
.single()
.ok_or_else(|| GdalError::BadArgument("Invalid datetime".to_string()))
}

/// Get the field's geometry.
Expand Down Expand Up @@ -614,7 +622,13 @@ impl<'a> Feature<'a> {
FieldValue::RealValue(value) => self.set_field_double(field_name, *value),
FieldValue::RealListValue(value) => self.set_field_double_list(field_name, value),
FieldValue::DateValue(value) => {
self.set_field_datetime(field_name, value.and_hms(0, 0, 0))
let naive_datetime = value
.and_hms_opt(0, 0, 0)
.ok_or_else(|| GdalError::BadArgument("Bad datetime".to_string()))?;
self.set_field_datetime(
field_name,
DateTime::from_local(naive_datetime, FixedOffset::east_opt(0).unwrap()),
)
}
FieldValue::DateTimeValue(value) => self.set_field_datetime(field_name, *value),
}
Expand Down Expand Up @@ -709,7 +723,7 @@ pub enum FieldValue {
StringListValue(Vec<String>),
RealValue(f64),
RealListValue(Vec<f64>),
DateValue(Date<FixedOffset>),
DateValue(NaiveDate),
DateTimeValue(DateTime<FixedOffset>),
}

Expand Down Expand Up @@ -749,10 +763,10 @@ impl FieldValue {
}

/// Interpret the value as `Date`. Returns `None` if the value is something else.
pub fn into_date(self) -> Option<Date<FixedOffset>> {
pub fn into_date(self) -> Option<NaiveDate> {
match self {
FieldValue::DateValue(rv) => Some(rv),
FieldValue::DateTimeValue(rv) => Some(rv.date()),
FieldValue::DateTimeValue(rv) => Some(rv.date_naive()),
mwm126 marked this conversation as resolved.
Show resolved Hide resolved
_ => None,
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/vector/gdal_to_geo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl TryFrom<&Geometry> for geo_types::Geometry<f64> {
OGRwkbGeometryType::wkbPoint => {
let (x, y, _) = geo.get_point(0);
Ok(geo_types::Geometry::Point(geo_types::Point(
geo_types::Coordinate { x, y },
geo_types::Coord { x, y },
)))
}
OGRwkbGeometryType::wkbMultiPoint => {
Expand All @@ -47,7 +47,7 @@ impl TryFrom<&Geometry> for geo_types::Geometry<f64> {
let coords = geo
.get_point_vec()
.iter()
.map(|&(x, y, _)| geo_types::Coordinate { x, y })
.map(|&(x, y, _)| geo_types::Coord { x, y })
.collect();
Ok(geo_types::Geometry::LineString(geo_types::LineString(
coords,
Expand Down
44 changes: 22 additions & 22 deletions src/vector/vector_tests/convert_geo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::vector::{Geometry, ToGdal};
#[test]
fn test_import_export_point() {
let wkt = "POINT (1 2)";
let coord = geo_types::Coordinate { x: 1., y: 2. };
let coord = geo_types::Coord { x: 1., y: 2. };
let geo = geo_types::Geometry::Point(geo_types::Point(coord));

assert_eq!(
Expand All @@ -19,9 +19,9 @@ fn test_import_export_point() {
fn test_import_export_multipoint() {
let wkt = "MULTIPOINT (0 0,0 1,1 2)";
let coord = vec![
geo_types::Point(geo_types::Coordinate { x: 0., y: 0. }),
geo_types::Point(geo_types::Coordinate { x: 0., y: 1. }),
geo_types::Point(geo_types::Coordinate { x: 1., y: 2. }),
geo_types::Point(geo_types::Coord { x: 0., y: 0. }),
geo_types::Point(geo_types::Coord { x: 0., y: 1. }),
geo_types::Point(geo_types::Coord { x: 1., y: 2. }),
];
let geo = geo_types::Geometry::MultiPoint(geo_types::MultiPoint(coord));

Expand All @@ -36,9 +36,9 @@ fn test_import_export_multipoint() {
fn test_import_export_linestring() {
let wkt = "LINESTRING (0 0,0 1,1 2)";
let coord = vec![
geo_types::Coordinate { x: 0., y: 0. },
geo_types::Coordinate { x: 0., y: 1. },
geo_types::Coordinate { x: 1., y: 2. },
geo_types::Coord { x: 0., y: 0. },
geo_types::Coord { x: 0., y: 1. },
geo_types::Coord { x: 1., y: 2. },
];
let geo = geo_types::Geometry::LineString(geo_types::LineString(coord));

Expand All @@ -54,14 +54,14 @@ fn test_import_export_multilinestring() {
let wkt = "MULTILINESTRING ((0 0,0 1,1 2),(3 3,3 4,4 5))";
let strings = vec![
geo_types::LineString(vec![
geo_types::Coordinate { x: 0., y: 0. },
geo_types::Coordinate { x: 0., y: 1. },
geo_types::Coordinate { x: 1., y: 2. },
geo_types::Coord { x: 0., y: 0. },
geo_types::Coord { x: 0., y: 1. },
geo_types::Coord { x: 1., y: 2. },
]),
geo_types::LineString(vec![
geo_types::Coordinate { x: 3., y: 3. },
geo_types::Coordinate { x: 3., y: 4. },
geo_types::Coordinate { x: 4., y: 5. },
geo_types::Coord { x: 3., y: 3. },
geo_types::Coord { x: 3., y: 4. },
geo_types::Coord { x: 4., y: 5. },
]),
];
let geo = geo_types::Geometry::MultiLineString(geo_types::MultiLineString(strings));
Expand All @@ -75,23 +75,23 @@ fn test_import_export_multilinestring() {

fn square(x0: isize, y0: isize, x1: isize, y1: isize) -> geo_types::LineString<f64> {
geo_types::LineString(vec![
geo_types::Coordinate {
geo_types::Coord {
x: x0 as f64,
y: y0 as f64,
},
geo_types::Coordinate {
geo_types::Coord {
x: x0 as f64,
y: y1 as f64,
},
geo_types::Coordinate {
geo_types::Coord {
x: x1 as f64,
y: y1 as f64,
},
geo_types::Coordinate {
geo_types::Coord {
x: x1 as f64,
y: y0 as f64,
},
geo_types::Coordinate {
geo_types::Coord {
x: x0 as f64,
y: y0 as f64,
},
Expand Down Expand Up @@ -146,12 +146,12 @@ fn test_import_export_multipolygon() {
#[test]
fn test_import_export_geometrycollection() {
let wkt = "GEOMETRYCOLLECTION (POINT (1 2),LINESTRING (0 0,0 1,1 2))";
let coord = geo_types::Coordinate { x: 1., y: 2. };
let coord = geo_types::Coord { x: 1., y: 2. };
let point = geo_types::Geometry::Point(geo_types::Point(coord));
let coords = vec![
geo_types::Coordinate { x: 0., y: 0. },
geo_types::Coordinate { x: 0., y: 1. },
geo_types::Coordinate { x: 1., y: 2. },
geo_types::Coord { x: 0., y: 0. },
geo_types::Coord { x: 0., y: 1. },
geo_types::Coord { x: 1., y: 2. },
];
let linestring = geo_types::Geometry::LineString(geo_types::LineString(coords));
let collection = geo_types::GeometryCollection(vec![point, linestring]);
Expand Down
22 changes: 16 additions & 6 deletions src/vector/vector_tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use super::{
};
use crate::spatial_ref::SpatialRef;
use crate::{assert_almost_eq, Dataset, DatasetOptions, GdalOpenFlags};
use std::path::Path;
use tempfile::tempdir;
mod convert_geo;
mod sql;

Expand Down Expand Up @@ -393,11 +395,15 @@ mod tests {
with_features("points_with_datetime.json", |mut features| {
let feature = features.next().unwrap();

let dt = FixedOffset::east(-5 * hour_secs)
.ymd(2011, 7, 14)
.and_hms(19, 43, 37);
let dt = FixedOffset::east_opt(-5 * hour_secs)
.unwrap()
.with_ymd_and_hms(2011, 7, 14, 19, 43, 37)
.unwrap();

let d = FixedOffset::east(0).ymd(2018, 1, 4).and_hms(0, 0, 0);
let d = FixedOffset::east_opt(0)
.unwrap()
.with_ymd_and_hms(2018, 1, 4, 0, 0, 0)
.unwrap();

assert_eq!(feature.field_as_datetime_by_name("dt").unwrap(), Some(dt));

Expand Down Expand Up @@ -785,7 +791,6 @@ mod tests {
}

let ds = Dataset::open(fixture!("output.geojson")).unwrap();
fs::remove_file(fixture!("output.geojson")).unwrap();
let mut layer = ds.layer(0).unwrap();
let ft = layer.features().next().unwrap();
assert_eq!(ft.geometry().wkt().unwrap(), "POINT (1 2)");
Expand All @@ -795,6 +800,7 @@ mod tests {
);
assert_eq!(ft.field("Value").unwrap().unwrap().into_real(), Some(45.78));
assert_eq!(ft.field("Int_value").unwrap().unwrap().into_int(), Some(1));
fs::remove_file(fixture!("output.geojson")).unwrap();
}

#[test]
Expand Down Expand Up @@ -851,7 +857,11 @@ mod tests {
open_flags: GdalOpenFlags::GDAL_OF_UPDATE,
..DatasetOptions::default()
};
let tmp_file = "/tmp/test.s3db";

let tmpdir = tempdir().unwrap();
mwm126 marked this conversation as resolved.
Show resolved Hide resolved
let tmp_pathbuf = tmpdir.path().join("test.s3db");
let tmp_file = Path::new(&tmp_pathbuf);

std::fs::copy(fixture!("three_layer_ds.s3db"), tmp_file).unwrap();
let ds = Dataset::open_ex(tmp_file, ds_options).unwrap();
let mut layer = ds.layer(0).unwrap();
Expand Down