Kontinuation commented on code in PR #614: URL: https://github.com/apache/sedona-db/pull/614#discussion_r2834214060
########## rust/sedona-raster-functions/src/rs_pixel_functions.rs: ########## @@ -0,0 +1,504 @@ +// 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 std::sync::Arc; +use std::vec; + +use crate::executor::RasterExecutor; +use arrow_array::builder::{BinaryBuilder, StringViewBuilder}; +use arrow_array::{cast::AsArray, types::Int32Type}; +use datafusion_common::{DataFusionError, Result, ScalarValue}; +use datafusion_expr::{ColumnarValue, Volatility}; +use sedona_expr::item_crs::make_item_crs; +use sedona_expr::scalar_udf::{SedonaScalarKernel, SedonaScalarUDF}; +use sedona_geometry::wkb_factory::{write_wkb_point, write_wkb_polygon, WKB_MIN_PROBABLE_BYTES}; +use sedona_raster::affine_transformation::{to_world_coordinate, AffineMatrix}; +use sedona_raster::traits::RasterRef; +use sedona_schema::datatypes::Edges; +use sedona_schema::{datatypes::SedonaType, matchers::ArgMatcher}; + +// =========================================================================== +// RS_PixelAsPoint +// =========================================================================== + +/// RS_PixelAsPoint(raster, colX, rowY) scalar UDF implementation +/// +/// Returns the upper-left corner of the specified pixel as a Point geometry. +/// The pixel coordinates are 1-based. Extrapolates for out-of-bounds coordinates. +pub fn rs_pixelaspoint_udf() -> SedonaScalarUDF { + SedonaScalarUDF::new( + "rs_pixelaspoint", + vec![Arc::new(RsPixelAsPoint {})], + Volatility::Immutable, + ) +} + +#[derive(Debug)] +struct RsPixelAsPoint {} + +impl SedonaScalarKernel for RsPixelAsPoint { + fn return_type(&self, args: &[SedonaType]) -> Result<Option<SedonaType>> { + let out_type = SedonaType::new_item_crs(&SedonaType::Wkb(Edges::Planar, None))?; + let matcher = ArgMatcher::new( + vec![ + ArgMatcher::is_raster(), + ArgMatcher::is_integer(), + ArgMatcher::is_integer(), + ], + out_type, + ); + matcher.match_args(args) + } + + fn invoke_batch( + &self, + arg_types: &[SedonaType], + args: &[ColumnarValue], + ) -> Result<ColumnarValue> { + let executor = RasterExecutor::new(arg_types, args); + let col_array = args[1].clone().into_array(executor.num_iterations())?; + let col_array = col_array.as_primitive::<Int32Type>(); + let row_array = args[2].clone().into_array(executor.num_iterations())?; + let row_array = row_array.as_primitive::<Int32Type>(); + + let bytes_per_point = WKB_MIN_PROBABLE_BYTES; + let mut builder = BinaryBuilder::with_capacity( + executor.num_iterations(), + executor.num_iterations() * bytes_per_point, + ); + let mut crs_builder = StringViewBuilder::with_capacity(executor.num_iterations()); + + let mut col_iter = col_array.iter(); + let mut row_iter = row_array.iter(); + executor.execute_raster_void(|_, raster_opt| { + let col_x = col_iter.next().unwrap(); + let row_y = row_iter.next().unwrap(); + match (raster_opt, col_x, row_y) { + (Some(raster), Some(col_x), Some(row_y)) => { + // Convert to 0-based for the affine transform + let (wx, wy) = + to_world_coordinate(raster, (col_x - 1) as i64, (row_y - 1) as i64); + + write_wkb_point(&mut builder, (wx, wy)) + .map_err(|e| DataFusionError::External(e.into()))?; + builder.append_value([]); Review Comment: This is incorrect. `builder.append_value([])` is needed for sealing the previously written binary value. -- 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]
