paleolimbot commented on code in PR #811:
URL: https://github.com/apache/sedona-db/pull/811#discussion_r3185798692


##########
rust/sedona-raster-gdal/src/utils.rs:
##########
@@ -0,0 +1,458 @@
+// 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.
+
+//! Utility functions for loading raster data via GDAL.
+
+use arrow_array::StructArray;
+use datafusion_common::error::Result;
+use datafusion_common::exec_datafusion_err;
+use sedona_gdal::dataset::Dataset;
+use sedona_gdal::spatial_ref::SpatialRef;
+
+use sedona_raster::builder::RasterBuilder;
+use sedona_raster::traits::BandMetadata;
+use sedona_schema::raster::{BandDataType, StorageType};
+
+use crate::gdal_common::{gdal_to_band_data_type, 
RasterMetadataFromGdalGeoTransform};
+
+/// Append a GDAL dataset as a single in-db raster to the provided 
[`RasterBuilder`].
+pub fn append_as_indb_raster(dataset: &Dataset, builder: &mut RasterBuilder) 
-> Result<()> {
+    let (width, height) = dataset.raster_size();
+
+    let geotransform = dataset
+        .geo_transform()
+        .map_err(|e| exec_datafusion_err!("Failed to get geotransform: {}", 
e))?;
+
+    let metadata = geotransform.to_raster_metadata(width, height);
+
+    let crs = dataset
+        .spatial_ref()
+        .ok()
+        .and_then(|sr: SpatialRef| sr.to_projjson().ok());
+
+    builder
+        .start_raster(&metadata, crs.as_deref())
+        .map_err(|e| exec_datafusion_err!("Failed to start raster: {}", e))?;
+
+    let band_count = dataset.raster_count();
+    for band_idx in 1..=band_count {
+        let band = dataset
+            .rasterband(band_idx)
+            .map_err(|e| exec_datafusion_err!("Failed to get band {}: {}", 
band_idx, e))?;
+
+        let gdal_type = band.band_type();
+        let band_data_type = gdal_to_band_data_type(gdal_type)
+            .map_err(|_| exec_datafusion_err!("Unsupported band data type: 
{:?}", gdal_type))?;
+
+        // Get nodata value
+        let nodata_bytes = match band_data_type {
+            BandDataType::UInt64 => band
+                .no_data_value_u64()
+                .map(|no_data| no_data.to_le_bytes().to_vec()),
+            BandDataType::Int64 => band
+                .no_data_value_i64()
+                .map(|no_data| no_data.to_le_bytes().to_vec()),
+            _ => band
+                .no_data_value()
+                .map(|no_data| 
crate::gdal_common::nodata_f64_to_bytes(no_data, &band_data_type)),
+        };
+
+        let band_metadata = BandMetadata {
+            nodata_value: nodata_bytes,
+            storage_type: StorageType::InDb,
+            datatype: band_data_type,
+            outdb_url: None,
+            outdb_band_id: None,
+        };
+
+        builder
+            .start_band(band_metadata)
+            .map_err(|e| exec_datafusion_err!("Failed to start band: {}", e))?;
+
+        let band_data = read_band_data(dataset, band_idx, width, height, 
band_data_type)?;
+        builder.band_data_writer().append_value(&band_data);

Review Comment:
   If I'm reading this correctly, this will heap allocate + load a potentially 
large `Vec<u8>`, copy it into another allocation, and then discard it.
   
   Can we do something like this?
   
   ```rust
   let mut writer = builder.band_data_writer();
   visit_band_data(dataset, band_idx, width, height, band_data_type, |item| {
       writer.write_unsafe(item);
   })?;
   ```
   
   (or `chunk` instead of `item` if the per-item overhead for that is too high)
   
   



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