paleolimbot commented on code in PR #777: URL: https://github.com/apache/sedona-db/pull/777#discussion_r3202392583
########## rust/sedona-functions/src/st_linesubstring.rs: ########## @@ -0,0 +1,164 @@ +// 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::builder::BinaryBuilder; +use arrow_schema::DataType; +use datafusion_common::{error::Result, ScalarValue}; +use datafusion_expr::{ColumnarValue, Volatility}; +use geo_traits::{CoordTrait, GeometryTrait, GeometryType, LineStringTrait}; +use sedona_common::sedona_internal_err; +use sedona_expr::{ + item_crs::ItemCrsKernel, + scalar_udf::{SedonaScalarKernel, SedonaScalarUDF}, +}; +use sedona_schema::{ + datatypes::{SedonaType, WKB_GEOMETRY}, + matchers::ArgMatcher, +}; +use std::{io::Write, sync::Arc}; + +use crate::executor::WkbExecutor; + +#[derive(Debug)] +struct STLineSubstring; +pub fn st_line_substring_udf() -> SedonaScalarUDF { + SedonaScalarUDF::new( + "st_linesubstring", + ItemCrsKernel::wrap_impl(vec![Arc::new(STLineSubstring)]), + Volatility::Immutable, + ) +} + +impl SedonaScalarKernel for STLineSubstring { + fn return_type(&self, args: &[SedonaType]) -> Result<Option<SedonaType>> { + let matcher = ArgMatcher::new( + vec![ + ArgMatcher::is_geometry(), + ArgMatcher::is_numeric(), + ArgMatcher::is_numeric(), + ], + WKB_GEOMETRY, + ); + matcher.match_args(args) + } + + fn invoke_batch( + &self, + arg_types: &[SedonaType], + args: &[ColumnarValue], + ) -> Result<ColumnarValue> { + let executor = WkbExecutor::new(arg_types, args); + let mut builder = BinaryBuilder::new(); + + let start_frac: Option<f64> = match &args[1].cast_to(&DataType::Float64, None)? { + ColumnarValue::Scalar(ScalarValue::Float64(s)) => *s, + _ => None, + }; + + let end_frac: Option<f64> = match &args[2].cast_to(&DataType::Float64, None)? { + ColumnarValue::Scalar(ScalarValue::Float64(e)) => *e, + _ => None, + }; + fn interpolate<C: CoordTrait<T = f64>>(p1: C, p2: C, fraction: f64) -> (f64, f64) { + let x = p1.x() + (p2.x() - p1.x()) * fraction; + let y = p1.y() + (p2.y() - p1.y()) * fraction; + (x, y) + } + executor.execute_wkb_void(|maybe_wkb| { + let mut new_coords = Vec::new(); + if let Some(wkb) = maybe_wkb { + if let GeometryType::LineString(line) = wkb.as_type() { + let num_coords = line.num_coords() as i64; + + let mut cumulative_distances = Vec::with_capacity(num_coords as usize); + let mut total_length = 0.0; + cumulative_distances.push(0.0); + let (s_frac, e_frac) = match (start_frac, end_frac) { + (Some(s), Some(e)) => (s, e), + _ => { + builder.append_null(); + return Ok(()); + } + }; + for i in 0..(num_coords as usize - 1) { + let p1 = line.coord(i).unwrap(); + let p2 = line.coord(i + 1).unwrap(); + + let dist = ((p2.x() - p1.x()).powi(2) + (p2.y() - p1.y()).powi(2)).sqrt(); + total_length += dist; + cumulative_distances.push(total_length); + } + let start_dist = s_frac * total_length; + let end_dist = e_frac * total_length; + + for i in 0..(num_coords as usize - 1) { + let d1 = cumulative_distances[i]; + let d2 = cumulative_distances[i + 1]; + let p1 = line.coord(i).unwrap(); + let p2 = line.coord(i + 1).unwrap(); + + if start_dist >= d1 && start_dist <= d2 { + let segment_len = d2 - d1; + let fraction = if segment_len > 0.0 { + (start_dist - d1) / segment_len + } else { + 0.0 + }; + new_coords.push(interpolate(p1, p2, fraction)); Review Comment: ```suggestion new_coords.push(interpolate(p1, p2, fraction, line.dim(), &mut coords)?); ``` ########## rust/sedona-functions/src/st_linesubstring.rs: ########## @@ -0,0 +1,164 @@ +// 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::builder::BinaryBuilder; +use arrow_schema::DataType; +use datafusion_common::{error::Result, ScalarValue}; +use datafusion_expr::{ColumnarValue, Volatility}; +use geo_traits::{CoordTrait, GeometryTrait, GeometryType, LineStringTrait}; +use sedona_common::sedona_internal_err; +use sedona_expr::{ + item_crs::ItemCrsKernel, + scalar_udf::{SedonaScalarKernel, SedonaScalarUDF}, +}; +use sedona_schema::{ + datatypes::{SedonaType, WKB_GEOMETRY}, + matchers::ArgMatcher, +}; +use std::{io::Write, sync::Arc}; + +use crate::executor::WkbExecutor; + +#[derive(Debug)] +struct STLineSubstring; +pub fn st_line_substring_udf() -> SedonaScalarUDF { + SedonaScalarUDF::new( + "st_linesubstring", + ItemCrsKernel::wrap_impl(vec![Arc::new(STLineSubstring)]), + Volatility::Immutable, + ) +} + +impl SedonaScalarKernel for STLineSubstring { + fn return_type(&self, args: &[SedonaType]) -> Result<Option<SedonaType>> { + let matcher = ArgMatcher::new( + vec![ + ArgMatcher::is_geometry(), + ArgMatcher::is_numeric(), + ArgMatcher::is_numeric(), + ], + WKB_GEOMETRY, + ); + matcher.match_args(args) + } + + fn invoke_batch( + &self, + arg_types: &[SedonaType], + args: &[ColumnarValue], + ) -> Result<ColumnarValue> { + let executor = WkbExecutor::new(arg_types, args); + let mut builder = BinaryBuilder::new(); + + let start_frac: Option<f64> = match &args[1].cast_to(&DataType::Float64, None)? { + ColumnarValue::Scalar(ScalarValue::Float64(s)) => *s, + _ => None, + }; + + let end_frac: Option<f64> = match &args[2].cast_to(&DataType::Float64, None)? { + ColumnarValue::Scalar(ScalarValue::Float64(e)) => *e, + _ => None, + }; + fn interpolate<C: CoordTrait<T = f64>>(p1: C, p2: C, fraction: f64) -> (f64, f64) { + let x = p1.x() + (p2.x() - p1.x()) * fraction; + let y = p1.y() + (p2.y() - p1.y()) * fraction; + (x, y) + } + executor.execute_wkb_void(|maybe_wkb| { + let mut new_coords = Vec::new(); + if let Some(wkb) = maybe_wkb { + if let GeometryType::LineString(line) = wkb.as_type() { + let num_coords = line.num_coords() as i64; + + let mut cumulative_distances = Vec::with_capacity(num_coords as usize); + let mut total_length = 0.0; + cumulative_distances.push(0.0); + let (s_frac, e_frac) = match (start_frac, end_frac) { + (Some(s), Some(e)) => (s, e), + _ => { + builder.append_null(); + return Ok(()); + } + }; + for i in 0..(num_coords as usize - 1) { + let p1 = line.coord(i).unwrap(); + let p2 = line.coord(i + 1).unwrap(); + + let dist = ((p2.x() - p1.x()).powi(2) + (p2.y() - p1.y()).powi(2)).sqrt(); + total_length += dist; + cumulative_distances.push(total_length); + } + let start_dist = s_frac * total_length; + let end_dist = e_frac * total_length; + + for i in 0..(num_coords as usize - 1) { + let d1 = cumulative_distances[i]; + let d2 = cumulative_distances[i + 1]; + let p1 = line.coord(i).unwrap(); + let p2 = line.coord(i + 1).unwrap(); + + if start_dist >= d1 && start_dist <= d2 { + let segment_len = d2 - d1; + let fraction = if segment_len > 0.0 { + (start_dist - d1) / segment_len + } else { + 0.0 + }; + new_coords.push(interpolate(p1, p2, fraction)); + } + + if d1 > start_dist && d1 < end_dist { + new_coords.push((p1.x(), p1.y())); + } + + if end_dist >= d1 && end_dist <= d2 { + let segment_len = d2 - d1; + let fraction = if segment_len > 0.0 { + (end_dist - d1) / segment_len + } else { + 0.0 + }; + new_coords.push(interpolate(p1, p2, fraction)); + } + } + } + } + if !new_coords.is_empty() { + // write byte order (1 = little endian) + builder.write_all(&[1u8])?; + + let type_id: u32 = 2; + builder.write_all(&type_id.to_le_bytes())?; + + let num_points = new_coords.len() as u32; + builder.write_all(&num_points.to_le_bytes())?; + + for (x, y) in new_coords { + builder.write_all(&x.to_le_bytes())?; + builder.write_all(&y.to_le_bytes())?; + } Review Comment: You can use `write_wkb_linestring_header(&mut builder, line.dim(), num_output_coords)?` for this (there are other utility functions in sedona_geometry::wkb_factory that might be useful as well. I've added suggestions above about how to accumulate the coordinates as bytes directly in your implementation (which also simplifies supporting Z and M here). ########## python/sedonadb/tests/functions/test_linesubstring.py: ########## @@ -0,0 +1,46 @@ +# 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. + +import pytest +from sedonadb.testing import geom_or_null, PostGIS, SedonaDB + + [email protected]("eng", [SedonaDB, PostGIS]) [email protected]( + ("geom", "start", "end", "expected"), + [ + (None, 0.0, 1.0, None), Review Comment: ```suggestion (None, 0.0, 1.0, None), ("LINESTRING EMPTY", None, 1.0, None), ("LINESTRING EMPTY, 0.0, None, None), ``` ########## rust/sedona-functions/src/st_linesubstring.rs: ########## @@ -0,0 +1,164 @@ +// 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::builder::BinaryBuilder; +use arrow_schema::DataType; +use datafusion_common::{error::Result, ScalarValue}; +use datafusion_expr::{ColumnarValue, Volatility}; +use geo_traits::{CoordTrait, GeometryTrait, GeometryType, LineStringTrait}; +use sedona_common::sedona_internal_err; +use sedona_expr::{ + item_crs::ItemCrsKernel, + scalar_udf::{SedonaScalarKernel, SedonaScalarUDF}, +}; +use sedona_schema::{ + datatypes::{SedonaType, WKB_GEOMETRY}, + matchers::ArgMatcher, +}; +use std::{io::Write, sync::Arc}; + +use crate::executor::WkbExecutor; + +#[derive(Debug)] +struct STLineSubstring; +pub fn st_line_substring_udf() -> SedonaScalarUDF { + SedonaScalarUDF::new( + "st_linesubstring", + ItemCrsKernel::wrap_impl(vec![Arc::new(STLineSubstring)]), + Volatility::Immutable, + ) +} + +impl SedonaScalarKernel for STLineSubstring { + fn return_type(&self, args: &[SedonaType]) -> Result<Option<SedonaType>> { + let matcher = ArgMatcher::new( + vec![ + ArgMatcher::is_geometry(), + ArgMatcher::is_numeric(), + ArgMatcher::is_numeric(), + ], + WKB_GEOMETRY, + ); + matcher.match_args(args) + } + + fn invoke_batch( + &self, + arg_types: &[SedonaType], + args: &[ColumnarValue], + ) -> Result<ColumnarValue> { + let executor = WkbExecutor::new(arg_types, args); + let mut builder = BinaryBuilder::new(); + + let start_frac: Option<f64> = match &args[1].cast_to(&DataType::Float64, None)? { + ColumnarValue::Scalar(ScalarValue::Float64(s)) => *s, + _ => None, + }; + + let end_frac: Option<f64> = match &args[2].cast_to(&DataType::Float64, None)? { + ColumnarValue::Scalar(ScalarValue::Float64(e)) => *e, + _ => None, + }; + fn interpolate<C: CoordTrait<T = f64>>(p1: C, p2: C, fraction: f64) -> (f64, f64) { + let x = p1.x() + (p2.x() - p1.x()) * fraction; + let y = p1.y() + (p2.y() - p1.y()) * fraction; + (x, y) + } Review Comment: You have a test for Z that's failing because this doesn't handle anything except XY coordinates. I think it should be relatively easy to modify this to work with any dimension...maybe: ```rust fn interpolate<C: CoordTrait<T = f64>>(p1: C, p2: C, fraction: f64, dim: Dimensions, buf: &mut impl Write) -> Result<(), SedonaGeometryError> { for i in 0..dim.size() { let v = p1.nth_unchecked(i) + (p2.nth_unchecked(i) - p1.nth_unchecked(i)) * fraction; buf.write_all(v.to_le_bytes())?; } Ok(()) } ``` ########## rust/sedona-functions/src/st_linesubstring.rs: ########## @@ -0,0 +1,164 @@ +// 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::builder::BinaryBuilder; +use arrow_schema::DataType; +use datafusion_common::{error::Result, ScalarValue}; +use datafusion_expr::{ColumnarValue, Volatility}; +use geo_traits::{CoordTrait, GeometryTrait, GeometryType, LineStringTrait}; +use sedona_common::sedona_internal_err; +use sedona_expr::{ + item_crs::ItemCrsKernel, + scalar_udf::{SedonaScalarKernel, SedonaScalarUDF}, +}; +use sedona_schema::{ + datatypes::{SedonaType, WKB_GEOMETRY}, + matchers::ArgMatcher, +}; +use std::{io::Write, sync::Arc}; + +use crate::executor::WkbExecutor; + +#[derive(Debug)] +struct STLineSubstring; +pub fn st_line_substring_udf() -> SedonaScalarUDF { + SedonaScalarUDF::new( + "st_linesubstring", + ItemCrsKernel::wrap_impl(vec![Arc::new(STLineSubstring)]), + Volatility::Immutable, + ) +} + +impl SedonaScalarKernel for STLineSubstring { + fn return_type(&self, args: &[SedonaType]) -> Result<Option<SedonaType>> { + let matcher = ArgMatcher::new( + vec![ + ArgMatcher::is_geometry(), + ArgMatcher::is_numeric(), + ArgMatcher::is_numeric(), + ], + WKB_GEOMETRY, + ); + matcher.match_args(args) + } + + fn invoke_batch( + &self, + arg_types: &[SedonaType], + args: &[ColumnarValue], + ) -> Result<ColumnarValue> { + let executor = WkbExecutor::new(arg_types, args); + let mut builder = BinaryBuilder::new(); + + let start_frac: Option<f64> = match &args[1].cast_to(&DataType::Float64, None)? { + ColumnarValue::Scalar(ScalarValue::Float64(s)) => *s, + _ => None, + }; + + let end_frac: Option<f64> = match &args[2].cast_to(&DataType::Float64, None)? { + ColumnarValue::Scalar(ScalarValue::Float64(e)) => *e, + _ => None, + }; + fn interpolate<C: CoordTrait<T = f64>>(p1: C, p2: C, fraction: f64) -> (f64, f64) { + let x = p1.x() + (p2.x() - p1.x()) * fraction; + let y = p1.y() + (p2.y() - p1.y()) * fraction; + (x, y) + } + executor.execute_wkb_void(|maybe_wkb| { + let mut new_coords = Vec::new(); + if let Some(wkb) = maybe_wkb { + if let GeometryType::LineString(line) = wkb.as_type() { + let num_coords = line.num_coords() as i64; + + let mut cumulative_distances = Vec::with_capacity(num_coords as usize); + let mut total_length = 0.0; + cumulative_distances.push(0.0); + let (s_frac, e_frac) = match (start_frac, end_frac) { + (Some(s), Some(e)) => (s, e), + _ => { + builder.append_null(); + return Ok(()); + } + }; + for i in 0..(num_coords as usize - 1) { + let p1 = line.coord(i).unwrap(); + let p2 = line.coord(i + 1).unwrap(); + + let dist = ((p2.x() - p1.x()).powi(2) + (p2.y() - p1.y()).powi(2)).sqrt(); + total_length += dist; + cumulative_distances.push(total_length); + } + let start_dist = s_frac * total_length; + let end_dist = e_frac * total_length; + + for i in 0..(num_coords as usize - 1) { + let d1 = cumulative_distances[i]; + let d2 = cumulative_distances[i + 1]; + let p1 = line.coord(i).unwrap(); + let p2 = line.coord(i + 1).unwrap(); + + if start_dist >= d1 && start_dist <= d2 { + let segment_len = d2 - d1; + let fraction = if segment_len > 0.0 { + (start_dist - d1) / segment_len + } else { + 0.0 + }; + new_coords.push(interpolate(p1, p2, fraction)); + } + + if d1 > start_dist && d1 < end_dist { + new_coords.push((p1.x(), p1.y())); Review Comment: ```suggestion write_wkb_coord_trait(p1, &mut coords)?; ``` -- 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]
