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


##########
c/sedona-gdal/src/vrt.rs:
##########
@@ -0,0 +1,324 @@
+// 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::{GdalError, Result};
+use crate::gdal_api::{call_gdal_api, GdalApi};
+use crate::raster::RasterBand;
+use crate::{gdal_dyn_bindgen::*, 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 a new empty VRT dataset with the given dimensions.
+    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(GdalError::NullPointer {
+                method_name: "VRTCreate",
+                msg: String::new(),
+            });
+        }
+
+        Ok(VrtDataset {
+            dataset: Dataset::new_owned(api, c_dataset),
+        })
+    }
+
+    /// Consume this VRT and return the underlying `Dataset`, transferring 
ownership.
+    pub fn as_dataset(self) -> Dataset {
+        let VrtDataset { dataset } = self;
+        dataset
+    }
+
+    /// Add a new band to the VRT dataset.
+    ///
+    /// Returns the 1-based index of the newly created band on success.
+    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 band from the VRT dataset as a `VrtRasterBand`.
+    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> {
+    /// Returns the raw GDAL raster band handle.
+    pub fn c_rasterband(&self) -> GDALRasterBandH {
+        self.band.c_rasterband()
+    }
+
+    /// Adds a simple source to this VRT band.
+    ///
+    /// # Arguments
+    /// * `source_band` - The source raster band to read from
+    /// * `src_window` - Source window as `(x_offset, y_offset, x_size, 
y_size)` in pixels
+    /// * `dst_window` - Destination window as `(x_offset, y_offset, x_size, 
y_size)` in pixels
+    /// * `resampling` - Optional resampling method (e.g. "near", "bilinear", 
"cubic").
+    /// * `nodata` - Optional nodata value for the source. If None, uses 
`NODATA_UNSET`.
+    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.and_then(|s| CString::new(s).ok());
+
+        let resampling_ptr = c_resampling
+            .as_ref()
+            .map(|s| s.as_ptr())
+            .unwrap_or(null_mut());

Review Comment:
   Fixed this by correctly propagate error.



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