Skip to content

Commit

Permalink
Merge #300
Browse files Browse the repository at this point in the history
300: Require matching data types in MDArray::read_into_slice r=jdroenner a=lnicola

- [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.
---

We don't support `GEDTC_COMPOUND` anyway because of the `GdalType` bound. ~~This is stricter than it could be, but should be fine as a quick workaround for #290. We should pass in the data type of the buffer, but it wasn't obvious to me how to do that.~~

Fixes #290

Co-authored-by: Laurențiu Nicola <lnicola@dend.ro>
  • Loading branch information
bors[bot] and lnicola authored Sep 2, 2022
2 parents cb0ba5d + 1fe8629 commit 260494b
Showing 1 changed file with 22 additions and 9 deletions.
31 changes: 22 additions & 9 deletions src/raster/mdarray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ use gdal_sys::{
GDALAttributeReadAsIntArray, GDALAttributeReadAsString, GDALAttributeReadAsStringArray,
GDALAttributeRelease, GDALDataType, GDALDatasetH, GDALDimensionGetIndexingVariable,
GDALDimensionGetName, GDALDimensionGetSize, GDALDimensionHS, GDALDimensionRelease,
GDALExtendedDataTypeClass, GDALExtendedDataTypeGetClass, GDALExtendedDataTypeGetName,
GDALExtendedDataTypeGetNumericDataType, GDALExtendedDataTypeH, GDALExtendedDataTypeRelease,
GDALGroupGetAttribute, GDALGroupGetDimensions, GDALGroupGetGroupNames,
GDALGroupGetMDArrayNames, GDALGroupGetName, GDALGroupH, GDALGroupOpenGroup,
GDALGroupOpenMDArray, GDALGroupRelease, GDALMDArrayGetAttribute, GDALMDArrayGetDataType,
GDALMDArrayGetDimensionCount, GDALMDArrayGetDimensions, GDALMDArrayGetNoDataValueAsDouble,
GDALMDArrayGetSpatialRef, GDALMDArrayGetStatistics, GDALMDArrayGetTotalElementsCount,
GDALMDArrayGetUnit, GDALMDArrayH, GDALMDArrayRelease, OSRDestroySpatialReference, VSIFree,
GDALExtendedDataTypeClass, GDALExtendedDataTypeCreate, GDALExtendedDataTypeGetClass,
GDALExtendedDataTypeGetName, GDALExtendedDataTypeGetNumericDataType, GDALExtendedDataTypeH,
GDALExtendedDataTypeRelease, GDALGroupGetAttribute, GDALGroupGetDimensions,
GDALGroupGetGroupNames, GDALGroupGetMDArrayNames, GDALGroupGetName, GDALGroupH,
GDALGroupOpenGroup, GDALGroupOpenMDArray, GDALGroupRelease, GDALMDArrayGetAttribute,
GDALMDArrayGetDataType, GDALMDArrayGetDimensionCount, GDALMDArrayGetDimensions,
GDALMDArrayGetNoDataValueAsDouble, GDALMDArrayGetSpatialRef, GDALMDArrayGetStatistics,
GDALMDArrayGetTotalElementsCount, GDALMDArrayGetUnit, GDALMDArrayH, GDALMDArrayRelease,
OSRDestroySpatialReference, VSIFree,
};
use libc::c_void;
use std::ffi::CString;
Expand Down Expand Up @@ -160,7 +161,14 @@ impl<'a> MDArray<'a> {
let n_dst_buffer_alloc_size = 0;

let rv = unsafe {
let data_type = GDALMDArrayGetDataType(self.c_mdarray);
let data_type = GDALExtendedDataTypeCreate(T::gdal_type());

if !self.datatype().class().is_numeric() {
return Err(GdalError::UnsupportedMdDataType {
data_type: self.datatype().class(),
method_name: "GDALMDArrayRead",
});
}

let rv = gdal_sys::GDALMDArrayRead(
self.c_mdarray,
Expand Down Expand Up @@ -920,6 +928,9 @@ mod tests {
let values = md_array.read_as::<u8>(vec![0, 0], vec![20, 20]).unwrap();

assert_eq!(&values[..4], &[181, 181, 156, 148]);

let values = md_array.read_as::<u16>(vec![0, 0], vec![20, 20]).unwrap();
assert_eq!(&values[..4], &[181, 181, 156, 148]);
}

#[test]
Expand All @@ -946,6 +957,8 @@ mod tests {

// check that we don't get a `SIGSEV` here
assert!(non_string_array.read_as_string_array().is_err());

assert!(string_array.read_as::<u8>(vec![0, 0], vec![1, 2]).is_err());
}

#[test]
Expand Down

0 comments on commit 260494b

Please sign in to comment.