Kontinuation commented on code in PR #253: URL: https://github.com/apache/sedona-db/pull/253#discussion_r2471696275
########## rust/sedona-raster/src/builder.rs: ########## @@ -0,0 +1,1600 @@ +// 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_array::BinaryViewArray; +use arrow_array::{ + builder::{ + BinaryBuilder, BinaryViewBuilder, Float64Builder, ListBuilder, StringBuilder, + StringViewBuilder, StructBuilder, UInt32Builder, UInt64Builder, + }, + Array, BinaryArray, Float64Array, ListArray, StringArray, StringViewArray, StructArray, + UInt32Array, UInt64Array, +}; +use arrow_schema::{ArrowError, DataType, Field}; +use datafusion_common::error::Result; +use sedona_schema::raster::{ + band_indices, band_metadata_indices, bounding_box_indices, column, metadata_indices, + raster_indices, BandDataType, RasterSchema, StorageType, +}; + +/// Builder for constructing raster arrays with zero-copy band data writing +pub struct RasterBuilder { + main_builder: StructBuilder, +} + +/// Metadata for a raster +#[derive(Debug, Clone)] +pub struct RasterMetadata { + pub width: u64, + pub height: u64, + pub upperleft_x: f64, + pub upperleft_y: f64, + pub scale_x: f64, + pub scale_y: f64, + pub skew_x: f64, + pub skew_y: f64, +} + +/// Bounding box coordinates +#[derive(Debug, Clone)] +pub struct BoundingBox { + pub min_x: f64, + pub min_y: f64, + pub max_x: f64, + pub max_y: f64, +} + +/// Metadata for a single band +#[derive(Debug, Clone)] +pub struct BandMetadata { + pub nodata_value: Option<Vec<u8>>, + pub storage_type: StorageType, + pub datatype: BandDataType, + /// URL for OutDb reference (only used when storage_type == OutDbRef) + pub outdb_url: Option<String>, + /// Band ID within the OutDb resource (only used when storage_type == OutDbRef) + pub outdb_band_id: Option<u32>, +} + +impl RasterBuilder { + /// Create a new raster builder with the specified capacity + pub fn new(capacity: usize) -> Self { + let metadata_builder = StructBuilder::from_fields( + match RasterSchema::metadata_type() { + DataType::Struct(fields) => fields, + _ => panic!("Expected struct type for metadata"), + }, + capacity, + ); + + let crs_builder = StringViewBuilder::new(); + + let bbox_builder = StructBuilder::from_fields( + match RasterSchema::bounding_box_type() { + DataType::Struct(fields) => fields, + _ => panic!("Expected struct type for bounding box"), + }, + capacity, + ); + + let band_struct_builder = StructBuilder::from_fields( + match RasterSchema::band_type() { + DataType::Struct(fields) => fields, + _ => panic!("Expected struct type for band"), + }, + 0, + ); + + let bands_builder = ListBuilder::new(band_struct_builder).with_field(Field::new( + column::BAND, + RasterSchema::band_type(), + false, + )); + + // Now create the main builder with pre-built components + let main_builder = StructBuilder::new( + RasterSchema::fields(), + vec![ + Box::new(metadata_builder), + Box::new(crs_builder), + Box::new(bbox_builder), + Box::new(bands_builder), + ], + ); + + Self { main_builder } + } + + /// Start a new raster with metadata, optional CRS, and optional bounding box + /// + /// This is the unified method for starting a raster with all optional parameters. + /// + /// # Arguments + /// * `metadata` - Raster metadata (dimensions, geotransform parameters) + /// * `crs` - Optional coordinate reference system as string + /// * `bbox` - Optional bounding box coordinates + /// + /// # Examples + /// ``` + /// use sedona_raster::builder::{RasterBuilder, RasterMetadata, BoundingBox}; + /// + /// let mut builder = RasterBuilder::new(10); + /// let metadata = RasterMetadata { + /// width: 100, height: 100, + /// upperleft_x: 0.0, upperleft_y: 0.0, + /// scale_x: 1.0, scale_y: -1.0, + /// skew_x: 0.0, skew_y: 0.0, + /// }; + /// + /// // From RasterMetadata struct with separate bounding box + /// let bbox = BoundingBox { min_x: 0.0, min_y: 0.0, max_x: 100.0, max_y: 100.0 }; + /// builder.start_raster(&metadata, Some("EPSG:4326"), Some(&bbox)).unwrap(); + /// + /// // Minimal - just metadata + /// builder.start_raster(&metadata, None, None).unwrap(); + /// ``` + pub fn start_raster( + &mut self, + metadata: &dyn MetadataRef, + crs: Option<&str>, + bbox: Option<&BoundingBox>, + ) -> Result<(), ArrowError> { + self.append_metadata_from_ref(metadata)?; + self.append_crs(crs)?; + self.append_bounding_box(bbox)?; + + Ok(()) + } + + /// Start a new band - this must be called before writing band data + pub fn start_band(&mut self, band_metadata: BandMetadata) -> Result<(), ArrowError> { + let bands_builder = self + .main_builder + .field_builder::<ListBuilder<StructBuilder>>(raster_indices::BANDS) + .unwrap(); + let band_builder = bands_builder.values(); + + // Get the metadata builder and populate its fields + { + let metadata_builder = band_builder + .field_builder::<StructBuilder>(band_indices::METADATA) Review Comment: +1. I also favor this approach. -- 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]
