-
Notifications
You must be signed in to change notification settings - Fork 56
feat: implement ST_BuildArea, ST_DelaunayTriangles, ST_ExteriorRing, ST_PointOnSurface, ST_NumInteriorRing alias #990
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 3 commits
1addca7
74c33df
ebb5257
87ffb50
ceb5a05
e3eae85
c029393
174d0af
f8f4e2a
3bd3f88
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,134 @@ | ||||||
| // 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 arrow_array::builder::BinaryBuilder; | ||||||
| use datafusion_common::{error::Result, DataFusionError}; | ||||||
| use datafusion_expr::ColumnarValue; | ||||||
| use geos::Geometry; | ||||||
| use sedona_expr::{ | ||||||
| item_crs::ItemCrsKernel, | ||||||
| scalar_udf::{ScalarKernelRef, SedonaScalarKernel}, | ||||||
| }; | ||||||
| use sedona_geometry::wkb_factory::WKB_MIN_PROBABLE_BYTES; | ||||||
| use sedona_schema::{ | ||||||
| datatypes::{SedonaType, WKB_GEOGRAPHY, WKB_GEOMETRY}, | ||||||
| matchers::ArgMatcher, | ||||||
| }; | ||||||
|
|
||||||
| use crate::executor::GeosExecutor; | ||||||
| use crate::geos_to_wkb::write_geos_geometry; | ||||||
|
|
||||||
| /// ST_BuildArea() implementation using the geos crate | ||||||
| pub fn st_build_area_impl() -> Vec<ScalarKernelRef> { | ||||||
| ItemCrsKernel::wrap_impl(vec![ | ||||||
| Arc::new(STBuildArea { | ||||||
| matcher: ArgMatcher::new(vec![ArgMatcher::is_geometry()], WKB_GEOMETRY), | ||||||
| }), | ||||||
| Arc::new(STBuildArea { | ||||||
| matcher: ArgMatcher::new(vec![ArgMatcher::is_geography()], WKB_GEOGRAPHY), | ||||||
| }), | ||||||
| ]) | ||||||
| } | ||||||
|
|
||||||
| #[derive(Debug)] | ||||||
| struct STBuildArea { | ||||||
| matcher: ArgMatcher, | ||||||
| } | ||||||
|
|
||||||
| impl SedonaScalarKernel for STBuildArea { | ||||||
| fn return_type(&self, args: &[SedonaType]) -> Result<Option<SedonaType>> { | ||||||
| self.matcher.match_args(args) | ||||||
| } | ||||||
|
|
||||||
| fn invoke_batch( | ||||||
| &self, | ||||||
| arg_types: &[SedonaType], | ||||||
| args: &[ColumnarValue], | ||||||
| ) -> Result<ColumnarValue> { | ||||||
| let executor = GeosExecutor::new(arg_types, args); | ||||||
| let mut builder = BinaryBuilder::with_capacity( | ||||||
| executor.num_iterations(), | ||||||
| WKB_MIN_PROBABLE_BYTES * executor.num_iterations(), | ||||||
| ); | ||||||
| executor.execute_wkb_void(|maybe_geom| { | ||||||
| match maybe_geom { | ||||||
| Some(geom) => { | ||||||
| invoke_scalar(&geom, &mut builder)?; | ||||||
| builder.append_value([]); | ||||||
| } | ||||||
| _ => builder.append_null(), | ||||||
| } | ||||||
| Ok(()) | ||||||
| })?; | ||||||
|
|
||||||
| executor.finish(Arc::new(builder.finish())) | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| fn invoke_scalar(geom: &Geometry, writer: &mut impl std::io::Write) -> Result<()> { | ||||||
| let result = geom | ||||||
| .build_area() | ||||||
| .map_err(|e| DataFusionError::Execution(format!("ST_BuildArea failed: {e}")))?; | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| write_geos_geometry(&result, writer)?; | ||||||
| Ok(()) | ||||||
| } | ||||||
|
|
||||||
| #[cfg(test)] | ||||||
| mod tests { | ||||||
| use datafusion_common::ScalarValue; | ||||||
| use rstest::rstest; | ||||||
| use sedona_expr::scalar_udf::SedonaScalarUDF; | ||||||
| use sedona_schema::datatypes::{ | ||||||
| WKB_GEOGRAPHY, WKB_GEOGRAPHY_ITEM_CRS, WKB_GEOMETRY, WKB_GEOMETRY_ITEM_CRS, | ||||||
| }; | ||||||
| use sedona_testing::testers::ScalarUdfTester; | ||||||
|
|
||||||
| use super::*; | ||||||
|
|
||||||
| #[rstest] | ||||||
| fn udf(#[values(WKB_GEOMETRY, WKB_GEOGRAPHY)] sedona_type: SedonaType) { | ||||||
| let udf = SedonaScalarUDF::from_impl("st_buildarea", st_build_area_impl()); | ||||||
| let tester = ScalarUdfTester::new(udf.into(), vec![sedona_type.clone()]); | ||||||
|
|
||||||
| tester.assert_return_type(sedona_type.clone()); | ||||||
|
|
||||||
| let result = tester | ||||||
| .invoke_scalar("LINESTRING (0 0, 1 0, 1 1, 0 1, 0 0)") | ||||||
| .unwrap(); | ||||||
| tester.assert_scalar_result_equals(result, "POLYGON ((0 0, 0 1, 1 1, 1 0, 0 0))"); | ||||||
|
|
||||||
| let result = tester.invoke_scalar(ScalarValue::Null).unwrap(); | ||||||
| assert!(result.is_null()); | ||||||
| } | ||||||
|
|
||||||
| #[rstest] | ||||||
| fn udf_invoke_item_crs( | ||||||
| #[values(WKB_GEOMETRY_ITEM_CRS.clone(), WKB_GEOGRAPHY_ITEM_CRS.clone())] | ||||||
| sedona_type: SedonaType, | ||||||
| ) { | ||||||
| let udf = SedonaScalarUDF::from_impl("st_buildarea", st_build_area_impl()); | ||||||
| let tester = ScalarUdfTester::new(udf.into(), vec![sedona_type.clone()]); | ||||||
| tester.assert_return_type(sedona_type); | ||||||
|
|
||||||
| let result = tester | ||||||
| .invoke_scalar("LINESTRING (0 0, 1 0, 1 1, 0 1, 0 0)") | ||||||
| .unwrap(); | ||||||
| tester.assert_scalar_result_equals(result, "POLYGON ((0 0, 0 1, 1 1, 1 0, 0 0))"); | ||||||
| } | ||||||
| } | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,152 @@ | ||
| // 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 arrow_array::builder::BinaryBuilder; | ||
| use arrow_schema::DataType; | ||
| use datafusion_common::{cast::as_float64_array, DataFusionError, Result}; | ||
| use datafusion_expr::ColumnarValue; | ||
| use geos::{Geom, Geometry}; | ||
| use sedona_expr::{ | ||
| item_crs::ItemCrsKernel, | ||
| scalar_udf::{ScalarKernelRef, SedonaScalarKernel}, | ||
| }; | ||
| use sedona_geometry::wkb_factory::WKB_MIN_PROBABLE_BYTES; | ||
| use sedona_schema::{ | ||
| datatypes::{SedonaType, WKB_GEOMETRY}, | ||
| matchers::ArgMatcher, | ||
| }; | ||
|
|
||
| use crate::executor::GeosExecutor; | ||
| use crate::geos_to_wkb::write_geos_geometry; | ||
|
|
||
| /// ST_DelaunayTriangles(geom) — no tolerance, returns polygons | ||
| pub fn st_delaunay_triangles_impl() -> Vec<ScalarKernelRef> { | ||
| ItemCrsKernel::wrap_impl(STDelaunayTriangles { tolerance: false }) | ||
| } | ||
|
|
||
| /// ST_DelaunayTriangles(geom, tolerance) — with tolerance | ||
| pub fn st_delaunay_triangles_tolerance_impl() -> Vec<ScalarKernelRef> { | ||
| ItemCrsKernel::wrap_impl(STDelaunayTriangles { tolerance: true }) | ||
| } | ||
|
|
||
| #[derive(Debug)] | ||
| struct STDelaunayTriangles { | ||
| tolerance: bool, | ||
| } | ||
|
|
||
| impl SedonaScalarKernel for STDelaunayTriangles { | ||
| fn return_type(&self, args: &[SedonaType]) -> Result<Option<SedonaType>> { | ||
| let arg_matchers = if self.tolerance { | ||
| vec![ArgMatcher::is_geometry(), ArgMatcher::is_numeric()] | ||
| } else { | ||
| vec![ArgMatcher::is_geometry()] | ||
| }; | ||
| let matcher = ArgMatcher::new(arg_matchers, WKB_GEOMETRY); | ||
| matcher.match_args(args) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you implement this using two separate While you're here, is it easy to add support for the "flags" parameter? We can't support the 2 (TIN) option but I believe GEOS has the MULTILINESTRING output option. |
||
| } | ||
|
|
||
| fn invoke_batch( | ||
| &self, | ||
| arg_types: &[SedonaType], | ||
| args: &[ColumnarValue], | ||
| ) -> Result<ColumnarValue> { | ||
| let executor = GeosExecutor::new(arg_types, args); | ||
| let mut builder = BinaryBuilder::with_capacity( | ||
| executor.num_iterations(), | ||
| WKB_MIN_PROBABLE_BYTES * executor.num_iterations(), | ||
| ); | ||
|
|
||
| if self.tolerance { | ||
| let tolerance_value = args[1] | ||
| .cast_to(&DataType::Float64, None)? | ||
| .to_array(executor.num_iterations())?; | ||
| let tolerance_array = as_float64_array(&tolerance_value)?; | ||
| let mut tolerance_iter = tolerance_array.iter(); | ||
|
|
||
| executor.execute_wkb_void(|maybe_geom| { | ||
| match (maybe_geom, tolerance_iter.next().unwrap()) { | ||
| (Some(geom), Some(tol)) => { | ||
| invoke_scalar(&geom, tol, &mut builder)?; | ||
| builder.append_value([]); | ||
| } | ||
| _ => builder.append_null(), | ||
| } | ||
| Ok(()) | ||
| })?; | ||
| } else { | ||
| executor.execute_wkb_void(|maybe_geom| { | ||
| match maybe_geom { | ||
| Some(geom) => { | ||
| invoke_scalar(&geom, 0.0, &mut builder)?; | ||
| builder.append_value([]); | ||
| } | ||
| _ => builder.append_null(), | ||
| } | ||
| Ok(()) | ||
| })?; | ||
| } | ||
|
|
||
| executor.finish(Arc::new(builder.finish())) | ||
| } | ||
| } | ||
|
|
||
| fn invoke_scalar(geom: &Geometry, tolerance: f64, writer: &mut impl std::io::Write) -> Result<()> { | ||
| let result = geom | ||
| .delaunay_triangulation(tolerance, false) | ||
| .map_err(|e| DataFusionError::Execution(format!("ST_DelaunayTriangles failed: {e}")))?; | ||
| write_geos_geometry(&result, writer)?; | ||
| Ok(()) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use datafusion_common::ScalarValue; | ||
| use rstest::rstest; | ||
| use sedona_expr::scalar_udf::SedonaScalarUDF; | ||
| use sedona_schema::datatypes::{WKB_GEOMETRY, WKB_GEOMETRY_ITEM_CRS}; | ||
| use sedona_testing::testers::ScalarUdfTester; | ||
|
|
||
| use super::*; | ||
|
|
||
| #[rstest] | ||
| fn udf_no_tolerance(#[values(WKB_GEOMETRY)] sedona_type: SedonaType) { | ||
| let udf = SedonaScalarUDF::from_impl("st_delaunaytriangles", st_delaunay_triangles_impl()); | ||
| let tester = ScalarUdfTester::new(udf.into(), vec![sedona_type.clone()]); | ||
|
|
||
|
paleolimbot marked this conversation as resolved.
|
||
| tester.assert_return_type(WKB_GEOMETRY); | ||
|
|
||
| let result = tester | ||
| .invoke_scalar("MULTIPOINT ((0 0), (1 1), (0 1))") | ||
| .unwrap(); | ||
| tester.assert_scalar_result_equals( | ||
| result, | ||
| "GEOMETRYCOLLECTION (POLYGON ((0 1, 0 0, 1 1, 0 1)))", | ||
| ); | ||
|
|
||
| let result = tester.invoke_scalar(ScalarValue::Null).unwrap(); | ||
| assert!(result.is_null()); | ||
| } | ||
|
|
||
| #[rstest] | ||
| fn udf_invoke_item_crs(#[values(WKB_GEOMETRY_ITEM_CRS.clone())] sedona_type: SedonaType) { | ||
| let udf = SedonaScalarUDF::from_impl("st_delaunaytriangles", st_delaunay_triangles_impl()); | ||
| let tester = ScalarUdfTester::new(udf.into(), vec![sedona_type.clone()]); | ||
| tester.assert_return_type(sedona_type); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for missing this, but we can't support Geography with this one because the definition of "enclosing" isn't the same on the sphere.