Skip to content

Commit

Permalink
Merge #302
Browse files Browse the repository at this point in the history
302: use tempfiles to not access file multiple times r=jdroenner a=ChristianBeilschmidt

- [x] I agree to follow the project's [code of conduct](https://github.com/georust/gdal/blob/master/CODE_OF_CONDUCT.md).
- [x] I added an entry to `CHANGES.md` if knowledge of this change could be valuable to users.
---



Co-authored-by: Christian Beilschmidt <christian.beilschmidt@geoengine.de>
  • Loading branch information
bors[bot] and ChristianBeilschmidt authored Sep 2, 2022
2 parents 260494b + 499cac4 commit 748cbe1
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 50 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@

- <https://github.com/georust/gdal/pull/292>

- Added a workaround in multi-dim tests to not access files multiple times

- <https://github.com/georust/gdal/pull/302>

## 0.12

- Bump Rust edition to 2021
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ mod metadata;
pub mod programs;
pub mod raster;
pub mod spatial_ref;
#[cfg(test)]
pub mod test_utils;
mod utils;
pub mod vector;
pub mod version;
Expand Down
11 changes: 8 additions & 3 deletions src/programs/raster/mdimtranslate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,19 +227,22 @@ fn _multi_dim_translate(

#[cfg(test)]
mod tests {

use super::*;

use crate::{DatasetOptions, Driver, GdalOpenFlags};
use crate::{test_utils::TempFixture, DatasetOptions, Driver, GdalOpenFlags};

#[test]
fn test_build_tiff_from_path() {
let fixture = TempFixture::fixture("cf_nasa_4326.nc");

let dataset_options = DatasetOptions {
open_flags: GdalOpenFlags::GDAL_OF_MULTIDIM_RASTER,
allowed_drivers: None,
open_options: None,
sibling_files: None,
};
let dataset = Dataset::open_ex("fixtures/cf_nasa_4326.nc", dataset_options).unwrap();
let dataset = Dataset::open_ex(&fixture, dataset_options).unwrap();

let mem_file_path = "/vsimem/2d3e9124-a7a0-413e-97b5-e79d46e50ff8";

Expand All @@ -263,13 +266,15 @@ mod tests {

#[test]
fn test_build_tiff_from_dataset() {
let fixture = TempFixture::fixture("cf_nasa_4326.nc");

let dataset_options = DatasetOptions {
open_flags: GdalOpenFlags::GDAL_OF_MULTIDIM_RASTER,
allowed_drivers: None,
open_options: None,
sibling_files: None,
};
let dataset = Dataset::open_ex("fixtures/cf_nasa_4326.nc", dataset_options).unwrap();
let dataset = Dataset::open_ex(&fixture, dataset_options).unwrap();

let driver = Driver::get_by_name("MEM").unwrap();
let output_dataset = driver.create("", 5, 7, 1).unwrap();
Expand Down
108 changes: 66 additions & 42 deletions src/raster/mdarray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -790,33 +790,38 @@ impl Attribute {

#[cfg(test)]
mod tests {

use super::*;

use crate::{Dataset, DatasetOptions, GdalOpenFlags};
use crate::{test_utils::TempFixture, Dataset, DatasetOptions, GdalOpenFlags};

#[test]
fn test_root_group_name() {
let fixture = TempFixture::fixture("byte_no_cf.nc");

let options = DatasetOptions {
open_flags: GdalOpenFlags::GDAL_OF_MULTIDIM_RASTER,
allowed_drivers: None,
open_options: None,
sibling_files: None,
};
let dataset = Dataset::open_ex("fixtures/byte_no_cf.nc", options).unwrap();
let dataset = Dataset::open_ex(&fixture, options).unwrap();
let root_group = dataset.root_group().unwrap();
let root_group_name = root_group.name();
assert_eq!(root_group_name, "/");
}

#[test]
fn test_array_names() {
let fixture = TempFixture::fixture("byte_no_cf.nc");

let dataset_options = DatasetOptions {
open_flags: GdalOpenFlags::GDAL_OF_MULTIDIM_RASTER,
allowed_drivers: None,
open_options: None,
sibling_files: None,
};
let dataset = Dataset::open_ex("fixtures/byte_no_cf.nc", dataset_options).unwrap();
let dataset = Dataset::open_ex(&fixture, dataset_options).unwrap();
let root_group = dataset.root_group().unwrap();
let options = CslStringList::new(); //Driver specific options determining how groups should be retrieved. Pass nullptr for default behavior.
let array_names = root_group.array_names(options);
Expand All @@ -825,13 +830,15 @@ mod tests {

#[test]
fn test_n_dimension() {
let fixture = TempFixture::fixture("byte_no_cf.nc");

let dataset_options = DatasetOptions {
open_flags: GdalOpenFlags::GDAL_OF_MULTIDIM_RASTER,
allowed_drivers: None,
open_options: None,
sibling_files: None,
};
let dataset = Dataset::open_ex("fixtures/byte_no_cf.nc", dataset_options).unwrap();
let dataset = Dataset::open_ex(&fixture, dataset_options).unwrap();
let root_group = dataset.root_group().unwrap();
let array_name = "Band1".to_string();
let options = CslStringList::new(); //Driver specific options determining how the array should be opened. Pass nullptr for default behavior.
Expand All @@ -842,13 +849,15 @@ mod tests {

#[test]
fn test_n_elements() {
let fixture = TempFixture::fixture("byte_no_cf.nc");

let dataset_options = DatasetOptions {
open_flags: GdalOpenFlags::GDAL_OF_MULTIDIM_RASTER,
allowed_drivers: None,
open_options: None,
sibling_files: None,
};
let dataset = Dataset::open_ex("fixtures/byte_no_cf.nc", dataset_options).unwrap();
let dataset = Dataset::open_ex(&fixture, dataset_options).unwrap();
let root_group = dataset.root_group().unwrap();
let array_name = "Band1".to_string();
let options = CslStringList::new(); //Driver specific options determining how the array should be opened. Pass nullptr for default behavior.
Expand All @@ -859,13 +868,15 @@ mod tests {

#[test]
fn test_dimension_name() {
let fixture = TempFixture::fixture("byte_no_cf.nc");

let dataset_options = DatasetOptions {
open_flags: GdalOpenFlags::GDAL_OF_MULTIDIM_RASTER,
allowed_drivers: None,
open_options: None,
sibling_files: None,
};
let dataset = Dataset::open_ex("fixtures/byte_no_cf.nc", dataset_options).unwrap();
let dataset = Dataset::open_ex(&fixture, dataset_options).unwrap();
let root_group = dataset.root_group().unwrap();

// group dimensions
Expand All @@ -891,13 +902,15 @@ mod tests {

#[test]
fn test_dimension_size() {
let fixture = TempFixture::fixture("byte_no_cf.nc");

let dataset_options = DatasetOptions {
open_flags: GdalOpenFlags::GDAL_OF_MULTIDIM_RASTER,
allowed_drivers: None,
open_options: None,
sibling_files: None,
};
let dataset = Dataset::open_ex("fixtures/byte_no_cf.nc", dataset_options).unwrap();
let dataset = Dataset::open_ex(&fixture, dataset_options).unwrap();
let root_group = dataset.root_group().unwrap();
let array_name = "Band1".to_string();
let options = CslStringList::new(); //Driver specific options determining how the array should be opened. Pass nullptr for default behavior.
Expand All @@ -912,13 +925,15 @@ mod tests {

#[test]
fn test_read_data() {
let fixture = TempFixture::fixture("byte_no_cf.nc");

let dataset_options = DatasetOptions {
open_flags: GdalOpenFlags::GDAL_OF_MULTIDIM_RASTER,
allowed_drivers: None,
open_options: None,
sibling_files: None,
};
let dataset = Dataset::open_ex("fixtures/byte_no_cf.nc", dataset_options).unwrap();
let dataset = Dataset::open_ex(&fixture, dataset_options).unwrap();

let root_group = dataset.root_group().unwrap();
let md_array = root_group
Expand All @@ -935,13 +950,15 @@ mod tests {

#[test]
fn test_read_string_array() {
let fixture = TempFixture::fixture("alldatatypes.nc");

let dataset_options = DatasetOptions {
open_flags: GdalOpenFlags::GDAL_OF_MULTIDIM_RASTER,
allowed_drivers: None,
open_options: None,
sibling_files: None,
};
let dataset = Dataset::open_ex("fixtures/alldatatypes.nc", dataset_options).unwrap();
let dataset = Dataset::open_ex(&fixture, dataset_options).unwrap();

let root_group = dataset.root_group().unwrap();

Expand All @@ -963,13 +980,15 @@ mod tests {

#[test]
fn test_datatype() {
let fixture = TempFixture::fixture("byte_no_cf.nc");

let dataset_options = DatasetOptions {
open_flags: GdalOpenFlags::GDAL_OF_MULTIDIM_RASTER,
allowed_drivers: None,
open_options: None,
sibling_files: None,
};
let dataset = Dataset::open_ex("fixtures/byte_no_cf.nc", dataset_options).unwrap();
let dataset = Dataset::open_ex(&fixture, dataset_options).unwrap();

let root_group = dataset.root_group().unwrap();

Expand All @@ -986,13 +1005,15 @@ mod tests {

#[test]
fn test_spatial_ref() {
let fixture = TempFixture::fixture("byte_no_cf.nc");

let dataset_options = DatasetOptions {
open_flags: GdalOpenFlags::GDAL_OF_MULTIDIM_RASTER,
allowed_drivers: None,
open_options: None,
sibling_files: None,
};
let dataset = Dataset::open_ex("fixtures/byte_no_cf.nc", dataset_options).unwrap();
let dataset = Dataset::open_ex(&fixture, dataset_options).unwrap();

let root_group = dataset.root_group().unwrap();
let md_array = root_group
Expand All @@ -1008,13 +1029,15 @@ mod tests {

#[test]
fn test_no_data_value() {
let fixture = TempFixture::fixture("byte_no_cf.nc");

let dataset_options = DatasetOptions {
open_flags: GdalOpenFlags::GDAL_OF_MULTIDIM_RASTER,
allowed_drivers: None,
open_options: None,
sibling_files: None,
};
let dataset = Dataset::open_ex("fixtures/byte_no_cf.nc", dataset_options).unwrap();
let dataset = Dataset::open_ex(&fixture, dataset_options).unwrap();

let root_group = dataset.root_group().unwrap();
let md_array = root_group
Expand All @@ -1026,13 +1049,15 @@ mod tests {

#[test]
fn test_attributes() {
let fixture = TempFixture::fixture("cf_nasa_4326.nc");

let dataset_options = DatasetOptions {
open_flags: GdalOpenFlags::GDAL_OF_MULTIDIM_RASTER,
allowed_drivers: None,
open_options: None,
sibling_files: None,
};
let dataset = Dataset::open_ex("fixtures/cf_nasa_4326.nc", dataset_options).unwrap();
let dataset = Dataset::open_ex(&fixture, dataset_options).unwrap();

let root_group = dataset.root_group().unwrap();

Expand Down Expand Up @@ -1077,13 +1102,15 @@ mod tests {

#[test]
fn test_unit() {
let fixture = TempFixture::fixture("cf_nasa_4326.nc");

let dataset_options = DatasetOptions {
open_flags: GdalOpenFlags::GDAL_OF_MULTIDIM_RASTER,
allowed_drivers: None,
open_options: None,
sibling_files: None,
};
let dataset = Dataset::open_ex("fixtures/cf_nasa_4326.nc", dataset_options).unwrap();
let dataset = Dataset::open_ex(&fixture, dataset_options).unwrap();

let root_group = dataset.root_group().unwrap();

Expand Down Expand Up @@ -1114,34 +1141,31 @@ mod tests {

#[test]
fn test_stats() {
{
let dataset_options = DatasetOptions {
open_flags: GdalOpenFlags::GDAL_OF_MULTIDIM_RASTER,
allowed_drivers: None,
open_options: None,
sibling_files: None,
};
let dataset = Dataset::open_ex("fixtures/byte_no_cf.nc", dataset_options).unwrap();
let root_group = dataset.root_group().unwrap();
let array_name = "Band1".to_string();
let options = CslStringList::new(); //Driver specific options determining how the array should be opened. Pass nullptr for default behavior.
let md_array = root_group.open_md_array(&array_name, options).unwrap();

assert!(md_array.get_statistics(false, true).unwrap().is_none());

assert_eq!(
md_array.get_statistics(true, true).unwrap().unwrap(),
MdStatisticsAll {
min: 74.0,
max: 255.0,
mean: 126.76500000000001,
std_dev: 22.928470838675654,
valid_count: 400,
}
);
}
let fixture = TempFixture::fixture("byte_no_cf.nc");

let dataset_options = DatasetOptions {
open_flags: GdalOpenFlags::GDAL_OF_MULTIDIM_RASTER,
allowed_drivers: None,
open_options: None,
sibling_files: None,
};
let dataset = Dataset::open_ex(&fixture, dataset_options).unwrap();
let root_group = dataset.root_group().unwrap();
let array_name = "Band1".to_string();
let options = CslStringList::new(); //Driver specific options determining how the array should be opened. Pass nullptr for default behavior.
let md_array = root_group.open_md_array(&array_name, options).unwrap();

// clean up aux file
std::fs::remove_file("fixtures/byte_no_cf.nc.aux.xml").unwrap();
assert!(md_array.get_statistics(false, true).unwrap().is_none());

assert_eq!(
md_array.get_statistics(true, true).unwrap().unwrap(),
MdStatisticsAll {
min: 74.0,
max: 255.0,
mean: 126.76500000000001,
std_dev: 22.928470838675654,
valid_count: 400,
}
);
}
}
9 changes: 4 additions & 5 deletions src/raster/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::raster::rasterband::ResampleAlg;
use crate::raster::{
ByteBuffer, ColorInterpretation, RasterCreationOption, StatisticsAll, StatisticsMinMax,
};
use crate::test_utils::TempFixture;
use crate::vsi::unlink_mem_file;
use crate::Driver;
use gdal_sys::GDALDataType;
Expand Down Expand Up @@ -806,7 +807,9 @@ fn test_color_table() {

#[test]
fn test_raster_stats() {
let dataset = Dataset::open(fixture!("tinymarble.tif")).unwrap();
let fixture = TempFixture::fixture("tinymarble.tif");

let dataset = Dataset::open(&fixture).unwrap();
let rb = dataset.rasterband(1).unwrap();

assert!(rb.get_statistics(false, false).unwrap().is_none());
Expand All @@ -828,8 +831,4 @@ fn test_raster_stats() {
max: 255.0,
}
);

// clean up aux file
drop(dataset);
std::fs::remove_file(fixture!("tinymarble.tif.aux.xml")).unwrap();
}
Loading

0 comments on commit 748cbe1

Please sign in to comment.