Kontinuation commented on code in PR #698:
URL: https://github.com/apache/sedona-db/pull/698#discussion_r3001472692


##########
c/sedona-gdal/src/vrt.rs:
##########
@@ -0,0 +1,326 @@
+// 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.
+
+//! GDAL VRT (Virtual Raster) API wrappers.
+
+use std::ffi::CString;
+use std::ops::{Deref, DerefMut};
+use std::ptr::null_mut;
+
+use crate::cpl::CslStringList;
+use crate::dataset::Dataset;
+use crate::errors::Result;
+use crate::gdal_api::{call_gdal_api, GdalApi};
+use crate::raster::rasterband::RasterBand;
+use crate::{gdal_dyn_bindgen::*, raster::types::GdalDataType};
+
+/// Special value indicating that nodata is not set for a VRT source.
+/// Matches `VRT_NODATA_UNSET` from GDAL's `gdal_vrt.h`.
+pub const NODATA_UNSET: f64 = -1234.56;
+
+/// A VRT (Virtual Raster) dataset.
+pub struct VrtDataset {
+    dataset: Dataset,
+}
+
+unsafe impl Send for VrtDataset {}
+
+impl VrtDataset {
+    /// Create an empty VRT dataset with the given raster size.
+    pub fn create(api: &'static GdalApi, x_size: usize, y_size: usize) -> 
Result<Self> {
+        let x: i32 = x_size.try_into()?;
+        let y: i32 = y_size.try_into()?;
+        let c_dataset = unsafe { call_gdal_api!(api, VRTCreate, x, y) };
+
+        if c_dataset.is_null() {
+            return Err(api.last_null_pointer_err("VRTCreate"));
+        }
+
+        Ok(VrtDataset {
+            dataset: Dataset::new(api, c_dataset),
+        })
+    }
+
+    /// Return the underlying `Dataset`, transferring ownership.
+    pub fn as_dataset(self) -> Dataset {
+        let VrtDataset { dataset } = self;
+        dataset
+    }
+
+    /// Add a band to this VRT dataset.
+    /// Return the 1-based index of the new band.
+    pub fn add_band(&mut self, data_type: GdalDataType, options: 
Option<&[&str]>) -> Result<usize> {
+        let csl = 
CslStringList::try_from_iter(options.unwrap_or(&[]).iter().copied())?;
+
+        // Preserve null semantics: pass null when no options given.
+        let opts_ptr = if csl.is_empty() {
+            null_mut()
+        } else {
+            csl.as_ptr()
+        };
+
+        let rv = unsafe {
+            call_gdal_api!(
+                self.dataset.api(),
+                GDALAddBand,
+                self.dataset.c_dataset(),
+                data_type.to_c(),
+                opts_ptr
+            )
+        };
+
+        if rv != CE_None {
+            return Err(self.dataset.api().last_cpl_err(rv as u32));
+        }
+
+        Ok(self.raster_count())
+    }
+
+    /// Fetch a VRT band by 1-indexed band number.
+    pub fn rasterband(&self, band_index: usize) -> Result<VrtRasterBand<'_>> {
+        let band = self.dataset.rasterband(band_index)?;
+        Ok(VrtRasterBand { band })
+    }
+}
+
+impl Deref for VrtDataset {
+    type Target = Dataset;
+
+    fn deref(&self) -> &Self::Target {
+        &self.dataset
+    }
+}
+
+impl DerefMut for VrtDataset {
+    fn deref_mut(&mut self) -> &mut Self::Target {
+        &mut self.dataset
+    }
+}
+
+impl AsRef<Dataset> for VrtDataset {
+    fn as_ref(&self) -> &Dataset {
+        &self.dataset
+    }
+}
+
+/// A raster band within a VRT dataset.
+pub struct VrtRasterBand<'a> {
+    band: RasterBand<'a>,
+}
+
+impl<'a> VrtRasterBand<'a> {
+    /// Return the raw GDAL raster band handle.
+    pub fn c_rasterband(&self) -> GDALRasterBandH {
+        self.band.c_rasterband()
+    }
+
+    /// Add a simple source to this VRT band.
+    /// Map a source window to a destination window, with optional resampling 
and nodata.
+    pub fn add_simple_source(
+        &self,
+        source_band: &RasterBand<'a>,
+        src_window: (i32, i32, i32, i32),
+        dst_window: (i32, i32, i32, i32),
+        resampling: Option<&str>,
+        nodata: Option<f64>,
+    ) -> Result<()> {
+        let c_resampling = resampling.map(CString::new).transpose()?;
+
+        let resampling_ptr = c_resampling
+            .as_ref()
+            .map(|s| s.as_ptr())
+            .unwrap_or(null_mut());
+

Review Comment:
   Thanks, addressed. The fallback resampling pointer now uses `null()` to 
match the const-qualified GDAL parameter.



-- 
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]

Reply via email to