-
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
Open
ajaypadwal73
wants to merge
10
commits into
apache:main
Choose a base branch
from
ajaypadwal73:feat/geos-functions
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
1addca7
feat: implement ST_BuildArea, ST_DelaunayTriangles, ST_ExteriorRing, …
ajaypadwal73 74c33df
fix: remove unused Geom import in st_buildarea
ajaypadwal73 ebb5257
docs: add reference docs for ST_BuildArea, ST_DelaunayTriangles, ST_E…
ajaypadwal73 87ffb50
test: add Python integration tests for ST_BuildArea, ST_DelaunayTrian…
ajaypadwal73 ceb5a05
test: fix expected values in ST_BuildArea, ST_DelaunayTriangles, ST_P…
ajaypadwal73 e3eae85
feat: add ST_BuildArea, ST_DelaunayTriangles, ST_ExteriorRing, ST_Poi…
ajaypadwal73 c029393
fix(tests): correct expected values in test_st_buildarea, test_st_del…
ajaypadwal73 174d0af
fix: address GEOS function review feedback
ajaypadwal73 f8f4e2a
fix: align buildarea postgis expectation
ajaypadwal73 3bd3f88
ci: skip duplicate sedonafns bootstrap during R check
ajaypadwal73 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,156 @@ | ||||||
| // 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::{Geom, Geometry, GeometryTypes}; | ||||||
| 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) => { | ||||||
| if invoke_scalar(&geom, &mut builder)? { | ||||||
| builder.append_value([]); | ||||||
| } else { | ||||||
| builder.append_null(); | ||||||
| } | ||||||
| } | ||||||
| _ => builder.append_null(), | ||||||
| } | ||||||
| Ok(()) | ||||||
| })?; | ||||||
|
|
||||||
| executor.finish(Arc::new(builder.finish())) | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| fn invoke_scalar(geom: &Geometry, writer: &mut impl std::io::Write) -> Result<bool> { | ||||||
| let geom_type = geom | ||||||
| .geometry_type() | ||||||
| .map_err(|e| DataFusionError::Execution(format!("Failed to get geometry type: {e}")))?; | ||||||
|
|
||||||
| match geom_type { | ||||||
| GeometryTypes::LineString | ||||||
| | GeometryTypes::MultiLineString | ||||||
| | GeometryTypes::GeometryCollection => {} | ||||||
| _ => return Ok(false), | ||||||
| } | ||||||
|
|
||||||
| 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(true) | ||||||
| } | ||||||
|
|
||||||
| #[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("POINT (0 0)").unwrap(); | ||||||
| assert!(result.is_null()); | ||||||
|
|
||||||
| let result = tester | ||||||
| .invoke_scalar("POLYGON ((0 0, 1 0, 1 1, 0 0))") | ||||||
| .unwrap(); | ||||||
| assert!(result.is_null()); | ||||||
|
|
||||||
| 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))"); | ||||||
| } | ||||||
| } | ||||||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.