|
| 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::{DataFusionError, Result}; |
| 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::{ |
| 27 | + datatypes::{SedonaType, WKB_GEOMETRY}, |
| 28 | + matchers::ArgMatcher, |
| 29 | +}; |
| 30 | + |
| 31 | +use crate::executor::GeosExecutor; |
| 32 | + |
| 33 | +/// ST_MinimumClearanceLine() implementation using the geos crate |
| 34 | +pub fn st_minimum_clearance_line_impl() -> ScalarKernelRef { |
| 35 | + Arc::new(STMinimumClearanceLine {}) |
| 36 | +} |
| 37 | + |
| 38 | +#[derive(Debug)] |
| 39 | +struct STMinimumClearanceLine {} |
| 40 | + |
| 41 | +impl SedonaScalarKernel for STMinimumClearanceLine { |
| 42 | + fn return_type(&self, args: &[SedonaType]) -> Result<Option<SedonaType>> { |
| 43 | + let matcher = ArgMatcher::new(vec![ArgMatcher::is_geometry()], WKB_GEOMETRY); |
| 44 | + |
| 45 | + matcher.match_args(args) |
| 46 | + } |
| 47 | + |
| 48 | + fn invoke_batch( |
| 49 | + &self, |
| 50 | + arg_types: &[SedonaType], |
| 51 | + args: &[ColumnarValue], |
| 52 | + ) -> Result<ColumnarValue> { |
| 53 | + let executor = GeosExecutor::new(arg_types, args); |
| 54 | + let mut builder = BinaryBuilder::with_capacity( |
| 55 | + executor.num_iterations(), |
| 56 | + WKB_MIN_PROBABLE_BYTES * executor.num_iterations(), |
| 57 | + ); |
| 58 | + |
| 59 | + executor.execute_wkb_void(|maybe_wkb| { |
| 60 | + match maybe_wkb { |
| 61 | + Some(wkb) => { |
| 62 | + invoke_scalar(&wkb, &mut builder)?; |
| 63 | + builder.append_value([]); |
| 64 | + } |
| 65 | + _ => builder.append_null(), |
| 66 | + } |
| 67 | + |
| 68 | + Ok(()) |
| 69 | + })?; |
| 70 | + |
| 71 | + executor.finish(Arc::new(builder.finish())) |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +fn invoke_scalar(geos_geom: &geos::Geometry, writer: &mut impl std::io::Write) -> Result<()> { |
| 76 | + let geometry = geos_geom.minimum_clearance_line().map_err(|e| { |
| 77 | + DataFusionError::Execution(format!( |
| 78 | + "Failed to calculate geometry's minimum clearance line: {e}" |
| 79 | + )) |
| 80 | + })?; |
| 81 | + |
| 82 | + let wkb = geometry |
| 83 | + .to_wkb() |
| 84 | + .map_err(|e| DataFusionError::Execution(format!("Failed to convert to wkb: {e}")))?; |
| 85 | + |
| 86 | + writer.write_all(wkb.as_ref())?; |
| 87 | + Ok(()) |
| 88 | +} |
| 89 | + |
| 90 | +#[cfg(test)] |
| 91 | +mod tests { |
| 92 | + use rstest::rstest; |
| 93 | + use sedona_expr::scalar_udf::SedonaScalarUDF; |
| 94 | + use sedona_schema::datatypes::WKB_VIEW_GEOMETRY; |
| 95 | + use sedona_testing::{create::create_array, testers::ScalarUdfTester}; |
| 96 | + |
| 97 | + use super::*; |
| 98 | + |
| 99 | + #[rstest] |
| 100 | + fn udf(#[values(WKB_GEOMETRY, WKB_VIEW_GEOMETRY)] sedona_type: SedonaType) { |
| 101 | + let udf = SedonaScalarUDF::from_kernel( |
| 102 | + "st_minimumclearanceline", |
| 103 | + st_minimum_clearance_line_impl(), |
| 104 | + ); |
| 105 | + let tester = ScalarUdfTester::new(udf.into(), vec![sedona_type.clone()]); |
| 106 | + tester.assert_return_type(WKB_GEOMETRY); |
| 107 | + |
| 108 | + let input_wkt = vec![ |
| 109 | + None, |
| 110 | + Some("POLYGON ((0 0, 1 0, 1 1, 0.5 3.2e-4, 0 0))"), |
| 111 | + Some("MULTIPOLYGON(((26 125, 26 200, 126 200, 126 125, 26 125 ),( 51 150, 101 150, 76 175, 51 150 )),(( 151 100, 151 200, 176 175, 151 100 )))"), |
| 112 | + Some("LINESTRING (5 107, 54 84, 101 100)"), |
| 113 | + Some("POLYGON((0 0,0 3,3 3,3 0,0 0),(1 1,1 2,2 2,2 1,1 1))"), |
| 114 | + Some("POLYGON((0 0,0 1,0 1,1 1,1 0,0 0,0 0))"), |
| 115 | + Some("LINESTRING (0 0, 1 1, 2 2)"), |
| 116 | + Some("MULTIPOLYGON(((0.5 0.5,0 0,0 1,0.5 0.5)),((0.5 0.5,1 1,1 0,0.5 0.5)),((2.5 2.5,2 2,2 3,2.5 2.5)),((2.5 2.5,3 3,3 2,2.5 2.5)))"), |
| 117 | + Some("POINT (1 1)"), |
| 118 | + Some("GEOMETRYCOLLECTION(POINT(1 1),MULTIPOLYGON(((0 2,1 1,0 0,0 2)),((2 0,1 1,2 2,2 0))))"), |
| 119 | + Some("POLYGON EMPTY"), |
| 120 | + Some("POLYGON((0 0,3 0,3 3,2 1,1 3,0 3,0 0))"), |
| 121 | + ]; |
| 122 | + let expected = create_array( |
| 123 | + &[ |
| 124 | + None, |
| 125 | + Some("LINESTRING(0.5 0.00032,0.5 0)"), |
| 126 | + Some("LINESTRING(76 175,76 150)"), |
| 127 | + Some("LINESTRING(54 84,101 100)"), |
| 128 | + Some("LINESTRING(1 1,1 2)"), |
| 129 | + Some("LINESTRING(0 0,0 1)"), |
| 130 | + Some("LINESTRING(0 0,1 1)"), |
| 131 | + Some("LINESTRING(2.5 2.5,3 2.5)"), |
| 132 | + Some("LINESTRING EMPTY"), |
| 133 | + Some("LINESTRING(1 1,2 1)"), |
| 134 | + Some("LINESTRING EMPTY"), |
| 135 | + Some("LINESTRING(1 3,0 3)"), |
| 136 | + ], |
| 137 | + &WKB_GEOMETRY, |
| 138 | + ); |
| 139 | + |
| 140 | + assert_eq!(&tester.invoke_wkb_array(input_wkt).unwrap(), &expected); |
| 141 | + } |
| 142 | +} |
0 commit comments