Skip to content
6 changes: 5 additions & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,11 @@ jobs:
- name: Install dependencies
shell: bash
run: |
sudo apt-get update && sudo apt-get install -y libgeos-dev
sudo apt-get update && sudo apt-get install -y \
libgeos-dev \
libgdal-dev \
gdal-bin \
pkg-config

- name: Build
if: matrix.name == 'build'
Expand Down
46 changes: 46 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ members = [
"rust/sedona-datasource",
"rust/sedona-expr",
"rust/sedona-functions",
"rust/sedona-gdal",
"rust/sedona-geo-generic-alg",
"rust/sedona-geo-traits-ext",
"rust/sedona-geo",
Expand Down Expand Up @@ -91,7 +92,7 @@ datafusion-physical-expr = { version = "50.2.0" }
datafusion-physical-plan = { version = "50.2.0" }
dirs = "6.0.0"
env_logger = "0.11"
fastrand = "2.0"
fastrand = "2.3"
futures = { version = "0.3" }
object_store = { version = "0.12.0", default-features = false }
float_next_after = "1"
Expand All @@ -100,6 +101,9 @@ mimalloc = { version = "0.1", default-features = false }
libmimalloc-sys = { version = "0.1", default-features = false }
once_cell = "1.20"

gdal = { version = "0.19", features = ["bindgen"] }
gdal-sys = { version = "0.12", features = ["bindgen"] }

geos = { git="https://github.com/georust/geos.git", rev="47afbad2483e489911ddb456417808340e9342c3", features = ["geo", "v3_11_0"] }

geo-types = "0.7.17"
Expand Down
48 changes: 48 additions & 0 deletions rust/sedona-gdal/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# 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.

[package]
name = "sedona-gdal"
version.workspace = true
homepage.workspace = true
repository.workspace = true
description.workspace = true
readme.workspace = true
edition.workspace = true
rust-version.workspace = true

[lints.clippy]
result_large_err = "allow"

[dev-dependencies]
approx = { workspace = true }
rstest = { workspace = true }
sedona-testing = { workspace = true, features = ["criterion"] }
criterion = { workspace = true}
tempfile = { workspace = true }

[dependencies]
arrow = { workspace = true }
arrow-array = { workspace = true }
arrow-schema = { workspace = true }
datafusion-common = { workspace = true }
datafusion-expr = { workspace = true }
gdal = {workspace = true}
gdal-sys = {workspace = true}
sedona-common = { workspace = true }
sedona-raster = { workspace = true }
sedona-schema = { workspace = true }
145 changes: 145 additions & 0 deletions rust/sedona-gdal/src/dataset.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
// 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 arrow_schema::ArrowError;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The errors in this module are not from Arrow. Is ExecutionError or ExternalError a better alternative?

use gdal::raster::GdalDataType;
use gdal::Dataset;
use gdal::Metadata;
use sedona_schema::raster::BandDataType;

/// Extract geotransform components from a GDAL dataset
/// Returns (upper_left_x, upper_left_y, scale_x, scale_y, skew_x, skew_y)
pub fn geotransform_components(
dataset: &Dataset,
) -> Result<(f64, f64, f64, f64, f64, f64), ArrowError> {
let geotransform = dataset
.geo_transform()
.map_err(|e| ArrowError::ParseError(format!("Failed to get geotransform: {e}")))?;
Ok((
geotransform[0], // Upper-left X coordinate
geotransform[3], // Upper-left Y coordinate
geotransform[1], // scale_x (pixel width)
geotransform[5], // scale_y (pixel height, usually negative)
geotransform[2], // skew_x (X-direction skew)
geotransform[4], // skew_y (Y-direction skew)
))
}

/// Extract tile size from a GDAL dataset
pub fn tile_size(dataset: &Dataset) -> (Option<usize>, Option<usize>) {
let tile_width = match dataset.metadata_item("TILEWIDTH", "") {
Some(val) => val.parse::<usize>().ok(),
None => None,
};
let tile_height = match dataset.metadata_item("TILEHEIGHT", "") {
Some(val) => val.parse::<usize>().ok(),
None => None,
};

(tile_width, tile_height)
}

pub fn to_banddatatype(gdal_data_type: GdalDataType) -> Result<BandDataType, ArrowError> {
match gdal_data_type {
GdalDataType::UInt8 => Ok(BandDataType::UInt8),
GdalDataType::UInt16 => Ok(BandDataType::UInt16),
GdalDataType::Int16 => Ok(BandDataType::Int16),
GdalDataType::UInt32 => Ok(BandDataType::UInt32),
GdalDataType::Int32 => Ok(BandDataType::Int32),
GdalDataType::Float32 => Ok(BandDataType::Float32),
GdalDataType::Float64 => Ok(BandDataType::Float64),
_ => Err(ArrowError::InvalidArgumentError(format!(
"Unsupported GDAL data type: {:?}",
gdal_data_type
))),
}
}

#[cfg(test)]
mod tests {
use super::*;
use gdal::DriverManager;

#[test]
fn test_geotransform_components() {
let driver = DriverManager::get_driver_by_name("MEM").unwrap();
let mut dataset = driver
.create_with_band_type::<u8, _>("", 100, 100, 1)
.unwrap();

let (upper_left_x, upper_left_y, pixel_width, pixel_height, rotation_x, rotation_y) =
(10.0, 20.0, 1.5, -2.5, 0.1, 0.2);

// Add some basic georeferencing
dataset
.set_geo_transform(&[
upper_left_x,
pixel_width,
rotation_x,
upper_left_y,
rotation_y,
pixel_height,
])
.unwrap();

let (ulx, uly, sx, sy, rx, ry) = geotransform_components(&dataset).unwrap();
assert_eq!(ulx, upper_left_x);
assert_eq!(uly, upper_left_y);
assert_eq!(sx, pixel_width);
assert_eq!(sy, pixel_height);
assert_eq!(rx, rotation_x);
assert_eq!(ry, rotation_y);
}

#[test]
fn test_tile_size() -> Result<(), ArrowError> {
let driver = DriverManager::get_driver_by_name("MEM")
.map_err(|e| ArrowError::ParseError(format!("Failed to get MEM driver: {e}")))?;
let mut dataset = driver
.create_with_band_type::<u8, _>("", 256, 512, 1)
.map_err(|e| ArrowError::ParseError(format!("Failed to create dataset: {e}")))?;

// Set real tile size metadata
dataset
.set_metadata_item("TILEWIDTH", "256", "")
.map_err(|e| ArrowError::ParseError(format!("Failed to set TILEWIDTH: {e}")))?;
dataset
.set_metadata_item("TILEHEIGHT", "512", "")
.map_err(|e| ArrowError::ParseError(format!("Failed to set TILEHEIGHT: {e}")))?;

let (tile_width, tile_height) = tile_size(&dataset);
assert!(tile_width.is_some());
assert_eq!(tile_width.unwrap(), 256);
assert_eq!(tile_height.unwrap(), 512);
Ok(())
}

#[test]
fn test_to_banddatatype() {
assert_eq!(
to_banddatatype(GdalDataType::UInt8).unwrap(),
BandDataType::UInt8
);
let result = to_banddatatype(GdalDataType::Unknown);
assert!(result.is_err());
assert!(result
.err()
.unwrap()
.to_string()
.contains("Unsupported GDAL data type"));
}
}
19 changes: 19 additions & 0 deletions rust/sedona-gdal/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// 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.

pub mod dataset;
pub mod raster_io;
Loading