|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +use std::sync::Arc; |
| 19 | + |
| 20 | +use arrow_array::builder::BinaryBuilder; |
| 21 | +use datafusion_common::{error::Result, DataFusionError, ScalarValue}; |
| 22 | +use datafusion_expr::ColumnarValue; |
| 23 | +use geos::Geom; |
| 24 | +use sedona_expr::scalar_udf::{ScalarKernelRef, SedonaScalarKernel}; |
| 25 | +use sedona_geometry::wkb_factory::WKB_MIN_PROBABLE_BYTES; |
| 26 | +use sedona_schema::{datatypes::WKB_GEOMETRY, matchers::ArgMatcher}; |
| 27 | + |
| 28 | +use crate::executor::GeosExecutor; |
| 29 | +use crate::geos_to_wkb::write_geos_geometry; |
| 30 | + |
| 31 | +pub fn st_line_merge_impl() -> ScalarKernelRef { |
| 32 | + Arc::new(STLineMerge {}) |
| 33 | +} |
| 34 | + |
| 35 | +#[derive(Debug)] |
| 36 | +struct STLineMerge {} |
| 37 | + |
| 38 | +impl SedonaScalarKernel for STLineMerge { |
| 39 | + fn return_type( |
| 40 | + &self, |
| 41 | + args: &[sedona_schema::datatypes::SedonaType], |
| 42 | + ) -> datafusion_common::Result<Option<sedona_schema::datatypes::SedonaType>> { |
| 43 | + let matcher = ArgMatcher::new( |
| 44 | + vec![ |
| 45 | + ArgMatcher::is_geometry(), |
| 46 | + ArgMatcher::optional(ArgMatcher::is_boolean()), |
| 47 | + ], |
| 48 | + WKB_GEOMETRY, |
| 49 | + ); |
| 50 | + matcher.match_args(args) |
| 51 | + } |
| 52 | + |
| 53 | + fn invoke_batch( |
| 54 | + &self, |
| 55 | + arg_types: &[sedona_schema::datatypes::SedonaType], |
| 56 | + args: &[datafusion_expr::ColumnarValue], |
| 57 | + ) -> datafusion_common::Result<datafusion_expr::ColumnarValue> { |
| 58 | + let executor = GeosExecutor::new(arg_types, args); |
| 59 | + let mut builder = BinaryBuilder::with_capacity( |
| 60 | + executor.num_iterations(), |
| 61 | + WKB_MIN_PROBABLE_BYTES * executor.num_iterations(), |
| 62 | + ); |
| 63 | + |
| 64 | + let directed = match args.get(1) { |
| 65 | + Some(ColumnarValue::Scalar(ScalarValue::Boolean(Some(opt_bool)))) => *opt_bool, |
| 66 | + _ => false, |
| 67 | + }; |
| 68 | + |
| 69 | + executor.execute_wkb_void(|maybe_wkb| { |
| 70 | + match maybe_wkb { |
| 71 | + Some(wkb) => { |
| 72 | + invoke_scalar(&wkb, &mut builder, directed)?; |
| 73 | + builder.append_value([]); |
| 74 | + } |
| 75 | + None => builder.append_null(), |
| 76 | + } |
| 77 | + |
| 78 | + Ok(()) |
| 79 | + })?; |
| 80 | + |
| 81 | + executor.finish(Arc::new(builder.finish())) |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +fn invoke_scalar( |
| 86 | + geos_geom: &geos::Geometry, |
| 87 | + writer: &mut impl std::io::Write, |
| 88 | + directed: bool, |
| 89 | +) -> Result<()> { |
| 90 | + // PostGIS seems to return the original geometry if it is empty |
| 91 | + let is_empty = geos_geom.is_empty().map_err(|e| { |
| 92 | + DataFusionError::Execution(format!("Failed to check if the geometry is empty: {e}")) |
| 93 | + })?; |
| 94 | + if is_empty { |
| 95 | + write_geos_geometry(geos_geom, writer)?; |
| 96 | + return Ok(()); |
| 97 | + } |
| 98 | + |
| 99 | + let result = if directed { |
| 100 | + geos_geom.line_merge_directed() |
| 101 | + } else { |
| 102 | + geos_geom.line_merge() |
| 103 | + }; |
| 104 | + |
| 105 | + let geom = |
| 106 | + result.map_err(|e| DataFusionError::Execution(format!("Failed to merge lines: {e}")))?; |
| 107 | + |
| 108 | + write_geos_geometry(&geom, writer)?; |
| 109 | + |
| 110 | + Ok(()) |
| 111 | +} |
| 112 | + |
| 113 | +#[cfg(test)] |
| 114 | +mod tests { |
| 115 | + use arrow_array::ArrayRef; |
| 116 | + use datafusion_common::ScalarValue; |
| 117 | + use rstest::rstest; |
| 118 | + use sedona_expr::scalar_udf::SedonaScalarUDF; |
| 119 | + use sedona_schema::datatypes::{SedonaType, WKB_GEOMETRY, WKB_VIEW_GEOMETRY}; |
| 120 | + use sedona_testing::create::create_array; |
| 121 | + use sedona_testing::testers::ScalarUdfTester; |
| 122 | + |
| 123 | + use super::*; |
| 124 | + |
| 125 | + #[rstest] |
| 126 | + fn udf(#[values(WKB_GEOMETRY, WKB_VIEW_GEOMETRY)] sedona_type: SedonaType) { |
| 127 | + use arrow_schema::DataType; |
| 128 | + |
| 129 | + let udf = SedonaScalarUDF::from_impl("st_linemerge", st_line_merge_impl()); |
| 130 | + let tester = ScalarUdfTester::new( |
| 131 | + udf.into(), |
| 132 | + vec![sedona_type, SedonaType::Arrow(DataType::Boolean)], |
| 133 | + ); |
| 134 | + tester.assert_return_type(WKB_GEOMETRY); |
| 135 | + |
| 136 | + let input = vec![ |
| 137 | + Some("MULTILINESTRING ((0 0, 1 0), (1 0, 1 1))"), |
| 138 | + Some("MULTILINESTRING ((0 0, 1 0), (1 1, 1 0))"), // opposite direction |
| 139 | + Some("MULTILINESTRING ((0 0, 1 0), (8 8, 9 9))"), // doesn't touch |
| 140 | + ]; |
| 141 | + |
| 142 | + let expected: ArrayRef = create_array( |
| 143 | + &[ |
| 144 | + Some("LINESTRING (0 0, 1 0, 1 1)"), |
| 145 | + Some("LINESTRING (0 0, 1 0, 1 1)"), |
| 146 | + Some("MULTILINESTRING ((0 0, 1 0), (8 8, 9 9))"), |
| 147 | + ], |
| 148 | + &WKB_GEOMETRY, |
| 149 | + ); |
| 150 | + |
| 151 | + assert_eq!( |
| 152 | + &tester |
| 153 | + .invoke_wkb_array_scalar(input.clone(), false) |
| 154 | + .unwrap(), |
| 155 | + &expected |
| 156 | + ); |
| 157 | + |
| 158 | + // If directed is true, lines with opposite directions won't be merged |
| 159 | + |
| 160 | + let expected_directed: ArrayRef = create_array( |
| 161 | + &[ |
| 162 | + Some("LINESTRING (0 0, 1 0, 1 1)"), |
| 163 | + Some("MULTILINESTRING ((0 0, 1 0), (1 1, 1 0))"), |
| 164 | + Some("MULTILINESTRING ((0 0, 1 0), (8 8, 9 9))"), |
| 165 | + ], |
| 166 | + &WKB_GEOMETRY, |
| 167 | + ); |
| 168 | + |
| 169 | + assert_eq!( |
| 170 | + &tester.invoke_wkb_array_scalar(input, true).unwrap(), |
| 171 | + &expected_directed |
| 172 | + ); |
| 173 | + |
| 174 | + // handle NULL |
| 175 | + |
| 176 | + let result = tester |
| 177 | + .invoke_scalar_scalar(ScalarValue::Null, false) |
| 178 | + .unwrap(); |
| 179 | + assert!(result.is_null()); |
| 180 | + } |
| 181 | +} |
0 commit comments