Copilot commented on code in PR #597: URL: https://github.com/apache/sedona-db/pull/597#discussion_r2794200102
########## rust/sedona-raster-functions/src/rs_convexhull.rs: ########## @@ -0,0 +1,196 @@ +// 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 crate::executor::RasterExecutor; +use arrow_array::builder::{BinaryBuilder, StringViewBuilder}; +use datafusion_common::DataFusionError; +use datafusion_common::Result; +use datafusion_common::ScalarValue; +use datafusion_expr::{ + scalar_doc_sections::DOC_SECTION_OTHER, ColumnarValue, Documentation, Volatility, +}; +use sedona_expr::item_crs::make_item_crs; +use sedona_expr::scalar_udf::{SedonaScalarKernel, SedonaScalarUDF}; +use sedona_geometry::wkb_factory::write_wkb_polygon; +use sedona_raster::affine_transformation::to_world_coordinate; +use sedona_raster::traits::RasterRef; +use sedona_schema::datatypes::Edges; +use sedona_schema::{datatypes::SedonaType, matchers::ArgMatcher}; + +/// RS_ConvexHull() scalar UDF documentation +/// +/// Returns the convex hull geometry of the raster including the NoDataBandValue band pixels. +/// For regular shaped and non-skewed rasters, this gives more or less the same result as RS_Envelope +/// and hence is only useful for irregularly shaped or skewed rasters. +pub fn rs_convexhull_udf() -> SedonaScalarUDF { + SedonaScalarUDF::new( + "rs_convexhull", + vec![Arc::new(RsConvexHull {})], + Volatility::Immutable, + Some(rs_convexhull_doc()), + ) +} + +fn rs_convexhull_doc() -> Documentation { + Documentation::builder( + DOC_SECTION_OTHER, + "Returns the convex hull geometry of the raster including the NoDataBandValue band pixels. For regular shaped and non-skewed rasters, this gives more or less the same result as RS_Envelope.".to_string(), + "RS_ConvexHull(raster: Raster)".to_string(), + ) + .with_argument("raster", "Raster: Input raster") + .with_sql_example("SELECT RS_ConvexHull(RS_Example())".to_string()) + .build() +} + +#[derive(Debug)] +struct RsConvexHull {} + +impl SedonaScalarKernel for RsConvexHull { + 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()], out_type); + + matcher.match_args(args) + } + + fn invoke_batch( + &self, + arg_types: &[SedonaType], + args: &[ColumnarValue], + ) -> Result<ColumnarValue> { + let executor = RasterExecutor::new(arg_types, args); + // 1 (byte order) + 4 (type) + 4 (num rings) + 4 (num points) + 80 (5 points * 16 bytes) + let bytes_per_poly = 93; + let mut builder = BinaryBuilder::with_capacity( + executor.num_iterations(), + executor.num_iterations() * bytes_per_poly, + ); + let mut crs_builder = StringViewBuilder::with_capacity(executor.num_iterations()); + + executor.execute_raster_void(|_i, raster_opt| { + match raster_opt { + Some(raster) => { + create_convexhull_wkb(raster, &mut builder)?; + builder.append_value([]); Review Comment: The append_value call with an empty slice appears incorrect. The WKB data was already written to the builder in create_convexhull_wkb (line 88), so this line should likely be removed or the create_convexhull_wkb call should not write directly to the builder. ```suggestion ``` ########## rust/sedona-raster-functions/src/rs_envelope.rs: ########## @@ -78,19 +78,38 @@ impl SedonaScalarKernel for RsEnvelope { executor.num_iterations(), executor.num_iterations() * bytes_per_poly, ); + let mut crs_builder = StringViewBuilder::with_capacity(executor.num_iterations()); executor.execute_raster_void(|_i, raster_opt| { match raster_opt { Some(raster) => { create_envelope_wkb(raster, &mut builder)?; builder.append_value([]); Review Comment: The append_value call with an empty slice appears incorrect. The WKB data was already written to the builder in create_envelope_wkb (line 86), so this line should likely be removed or the create_envelope_wkb call should not write directly to the builder. ```suggestion ``` -- 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]
