Kontinuation commented on code in PR #787: URL: https://github.com/apache/sedona-db/pull/787#discussion_r3169208502
########## rust/sedona-raster-gdal/src/gdal_common.rs: ########## @@ -0,0 +1,752 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +use sedona_gdal::dataset::Dataset; +use sedona_gdal::errors::GdalError; +use sedona_gdal::gdal::Gdal; +use sedona_gdal::gdal_dyn_bindgen::{GDAL_OF_RASTER, GDAL_OF_READONLY, GDAL_OF_VERBOSE_ERROR}; +use sedona_gdal::mem::MemDatasetBuilder; +use sedona_gdal::raster::types::DatasetOptions; +use sedona_gdal::raster::types::GdalDataType; + +use sedona_raster::traits::RasterRef; +use sedona_schema::raster::{BandDataType, StorageType}; + +use datafusion_common::{ + arrow_datafusion_err, exec_datafusion_err, exec_err, DataFusionError, Result, +}; + +/// Execute a closure with a reference to the global [`Gdal`] handle, +/// converting initialization errors to [`DataFusionError`]. +pub(crate) fn with_gdal<F, R>(f: F) -> Result<R> +where + F: FnOnce(&Gdal) -> Result<R>, +{ + match sedona_gdal::global::with_global_gdal(f) { + Ok(inner_result) => inner_result, + Err(init_err) => Err(DataFusionError::External(Box::new(init_err))), + } +} + +/// Converts a BandDataType to the corresponding GDAL data type. +pub fn band_data_type_to_gdal(band_type: &BandDataType) -> GdalDataType { + match band_type { + BandDataType::UInt8 => GdalDataType::UInt8, + BandDataType::Int8 => GdalDataType::Int8, + BandDataType::UInt16 => GdalDataType::UInt16, + BandDataType::Int16 => GdalDataType::Int16, + BandDataType::UInt32 => GdalDataType::UInt32, + BandDataType::Int32 => GdalDataType::Int32, + BandDataType::UInt64 => GdalDataType::UInt64, + BandDataType::Int64 => GdalDataType::Int64, + BandDataType::Float32 => GdalDataType::Float32, + BandDataType::Float64 => GdalDataType::Float64, + } +} + +/// Converts a GDAL data type to the corresponding BandDataType. +pub fn gdal_to_band_data_type(gdal_type: GdalDataType) -> Result<BandDataType> { + match gdal_type { + GdalDataType::UInt8 => Ok(BandDataType::UInt8), + GdalDataType::Int8 => Ok(BandDataType::Int8), + GdalDataType::UInt16 => Ok(BandDataType::UInt16), + GdalDataType::Int16 => Ok(BandDataType::Int16), + GdalDataType::UInt32 => Ok(BandDataType::UInt32), + GdalDataType::Int32 => Ok(BandDataType::Int32), + GdalDataType::UInt64 => Ok(BandDataType::UInt64), + GdalDataType::Int64 => Ok(BandDataType::Int64), + GdalDataType::Float32 => Ok(BandDataType::Float32), + GdalDataType::Float64 => Ok(BandDataType::Float64), + _ => Err(DataFusionError::NotImplemented(format!( + "GDAL data type {:?} is not supported", + gdal_type + ))), + } +} + +/// Returns the byte size of a GDAL data type. +pub fn gdal_type_byte_size(gdal_type: GdalDataType) -> usize { + gdal_type.byte_size() +} + +/// Interprets bytes according to the band data type and returns the value as `f64`. +/// +/// Returns an error if `bytes` does not have the expected length for `band_type`. +pub fn bytes_to_f64(bytes: &[u8], band_type: &BandDataType) -> Result<f64> { + macro_rules! read_le_f64 { + ($t:ty, $n:expr) => {{ + let arr: [u8; $n] = bytes.try_into().map_err(|_| { + exec_datafusion_err!( + "Invalid byte slice length for type {}, expected: {}, actual: {}", + stringify!($t), + $n, + bytes.len() + ) + })?; + Ok(<$t>::from_le_bytes(arr) as f64) + }}; + } + + match band_type { + BandDataType::UInt8 => { + if bytes.len() != 1 { + return exec_err!( + "Invalid byte length for UInt8: expected 1, got {}", + bytes.len() + ); + } + Ok(bytes[0] as f64) + } + BandDataType::Int8 => { + if bytes.len() != 1 { + return exec_err!( + "Invalid byte length for Int8: expected 1, got {}", + bytes.len() + ); + } + Ok(bytes[0] as i8 as f64) + } + BandDataType::UInt16 => read_le_f64!(u16, 2), + BandDataType::Int16 => read_le_f64!(i16, 2), + BandDataType::UInt32 => read_le_f64!(u32, 4), + BandDataType::Int32 => read_le_f64!(i32, 4), + BandDataType::UInt64 => read_le_f64!(u64, 8), + BandDataType::Int64 => read_le_f64!(i64, 8), Review Comment: I have patched these arms to return Err to inform the developer to handle these types specially. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
