james-willis commented on code in PR #1068: URL: https://github.com/apache/sedona-db/pull/1068#discussion_r3647977369
########## rust/sedona-raster-gdal/src/rs_reproject_match.rs: ########## @@ -0,0 +1,538 @@ +// 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. + +//! RS_ReprojectMatch UDF - Reproject a raster onto a reference raster's grid. +//! +//! Reprojects the input raster onto the reference raster's CRS, pixel grid, and +//! envelope: the output always has the *same* extent, resolution, dimensions, +//! and CRS as the reference (in the spirit of `rioxarray`'s `reproject_match`). +//! The input's band count/order, per-band data type, and nodata are preserved. +//! Pixel values are recomputed by GDAL's warp (`GDALReprojectImage`); output +//! cells the reprojected input footprint does not cover are filled with nodata. +//! +//! The reference raster contributes only its grid — transform, dimensions, and +//! CRS — never its pixels. + +use std::sync::Arc; + +use arrow_array::ArrayRef; +use arrow_schema::DataType; +use datafusion_common::cast::as_string_array; +use datafusion_common::config::ConfigOptions; +use datafusion_common::error::Result; +use datafusion_common::{exec_err, ScalarValue}; +use datafusion_expr::{ColumnarValue, Volatility}; + +use sedona_common::sedona_internal_err; +use sedona_expr::scalar_udf::{SedonaScalarKernel, SedonaScalarUDF}; +use sedona_gdal::geo_transform::GeoTransform; +use sedona_gdal::raster::types::ResampleAlg; +use sedona_raster::array::RasterRefImpl; +use sedona_raster::builder::RasterBuilder; +use sedona_raster::traits::RasterRef; +use sedona_raster_functions::rs_ensure_loaded::{ + NEEDS_PIXELS_METADATA_KEY, RETURNS_BYTES_METADATA_KEY, +}; +use sedona_raster_functions::RasterExecutor; +use sedona_schema::datatypes::{SedonaType, RASTER}; +use sedona_schema::matchers::ArgMatcher; + +use crate::gdal_common::{raster_ref_to_gdal_mem, with_gdal, GdalBandLayout}; +use crate::gdal_dataset_provider::configure_thread_local_options; +use crate::utils::{append_warped_nd_from_dataset, WarpGrid}; + +/// RS_ReprojectMatch() scalar UDF implementation. +/// +/// Reprojects `raster` onto `reference`'s CRS + grid + envelope. +/// +/// Signatures (matching Apache Sedona (Spark)): +/// - `RS_ReprojectMatch(raster, reference)` — 2 args (algorithm defaults to +/// `NearestNeighbor`) +/// - `RS_ReprojectMatch(raster, reference, algorithm)` — 3 args +pub fn rs_reproject_match_udf() -> SedonaScalarUDF { + SedonaScalarUDF::new( + "rs_reprojectmatch", + vec![ + Arc::new(RsReprojectMatch { arg_count: 2 }), + Arc::new(RsReprojectMatch { arg_count: 3 }), + ], + Volatility::Immutable, + ) + // Reads band pixels (so the planner materializes OutDb rasters via + // RS_EnsureLoaded first) and emits a fresh InDb raster (so its output is + // already loaded and isn't wrapped again). + .with_metadata(NEEDS_PIXELS_METADATA_KEY, "true") + .with_metadata(RETURNS_BYTES_METADATA_KEY, "true") +} + +/// Kernel implementation for RS_ReprojectMatch. +#[derive(Debug)] +struct RsReprojectMatch { + /// Number of arguments in the matched signature (2 or 3). + arg_count: usize, +} + +impl SedonaScalarKernel for RsReprojectMatch { + fn return_type(&self, args: &[SedonaType]) -> Result<Option<SedonaType>> { + let matchers = match self.arg_count { + 2 => vec![ArgMatcher::is_raster(), ArgMatcher::is_raster()], + 3 => vec![ + ArgMatcher::is_raster(), + ArgMatcher::is_raster(), + ArgMatcher::is_string(), + ], + _ => { + return sedona_internal_err!( + "RS_ReprojectMatch: unexpected arg_count {}", + self.arg_count + ); + } + }; + ArgMatcher::new(matchers, RASTER).match_args(args) + } + + fn invoke_batch( + &self, + arg_types: &[SedonaType], + args: &[ColumnarValue], + ) -> Result<ColumnarValue> { + self.invoke_batch_from_args(arg_types, args, &SedonaType::Arrow(DataType::Null), 0, None) + } + + fn invoke_batch_from_args( + &self, + arg_types: &[SedonaType], + args: &[ColumnarValue], + _return_type: &SedonaType, + _num_rows: usize, + config_options: Option<&ConfigOptions>, + ) -> Result<ColumnarValue> { + let num_iterations = RasterExecutor::num_iterations_over(args); + + // Algorithm string at index 2 (when arg_count == 3); otherwise the + // Spark default `NearestNeighbor`. Expand to an array so a per-row + // column and a scalar are handled identically. + let algorithm_array = if self.arg_count >= 3 { + args[2] + .clone() + .cast_to(&DataType::Utf8, None)? Review Comment: done -- 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]
