-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3835461
commit ed7622d
Showing
13 changed files
with
396 additions
and
255 deletions.
There are no files selected for viewing
This file contains 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,73 @@ | ||
use std::any::Any; | ||
use std::sync::OnceLock; | ||
|
||
use arrow_schema::DataType; | ||
use datafusion::logical_expr::scalar_doc_sections::DOC_SECTION_OTHER; | ||
use datafusion::logical_expr::{ColumnarValue, Documentation, ScalarUDFImpl, Signature}; | ||
use geoarrow::algorithm::native::BoundingRectArray; | ||
use geoarrow::ArrayBase; | ||
|
||
use crate::data_types::{any_single_geometry_type_input, parse_to_native_array, GEOMETRY_TYPE}; | ||
use crate::error::GeoDataFusionResult; | ||
|
||
#[derive(Debug)] | ||
pub(super) struct Envelope { | ||
signature: Signature, | ||
} | ||
|
||
impl Envelope { | ||
pub fn new() -> Self { | ||
Self { | ||
signature: any_single_geometry_type_input(), | ||
} | ||
} | ||
} | ||
|
||
static DOCUMENTATION: OnceLock<Documentation> = OnceLock::new(); | ||
|
||
impl ScalarUDFImpl for Envelope { | ||
fn as_any(&self) -> &dyn Any { | ||
self | ||
} | ||
|
||
fn name(&self) -> &str { | ||
"st_envelope" | ||
} | ||
|
||
fn signature(&self) -> &Signature { | ||
&self.signature | ||
} | ||
|
||
fn return_type(&self, _arg_types: &[DataType]) -> datafusion::error::Result<DataType> { | ||
Ok(GEOMETRY_TYPE.into()) | ||
} | ||
|
||
fn invoke(&self, args: &[ColumnarValue]) -> datafusion::error::Result<ColumnarValue> { | ||
Ok(envelope_impl(args)?) | ||
} | ||
|
||
fn documentation(&self) -> Option<&Documentation> { | ||
Some(DOCUMENTATION.get_or_init(|| { | ||
Documentation::builder() | ||
.with_doc_section(DOC_SECTION_OTHER) | ||
.with_description( | ||
"Computes a point which is the geometric center of mass of a geometry.", | ||
) | ||
.with_argument("g1", "geometry") | ||
.build() | ||
.unwrap() | ||
})) | ||
} | ||
} | ||
|
||
fn envelope_impl(args: &[ColumnarValue]) -> GeoDataFusionResult<ColumnarValue> { | ||
let array = ColumnarValue::values_to_arrays(args)? | ||
.into_iter() | ||
.next() | ||
.unwrap(); | ||
let native_array = parse_to_native_array(array)?; | ||
// Since a RectArray is a valid normal geometry type for us, we don't have to cast it to a | ||
// Geometry array. That just has overhead. | ||
let output = native_array.as_ref().bounding_rect()?; | ||
Ok(output.into_array_ref().into()) | ||
} |
This file contains 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,8 @@ | ||
mod envelope; | ||
|
||
use datafusion::prelude::SessionContext; | ||
|
||
/// Register all provided [geo] functions for constructing geometries | ||
pub fn register_constructors(ctx: &SessionContext) { | ||
ctx.register_udf(envelope::Envelope::new().into()); | ||
} |
This file contains 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,70 @@ | ||
use std::any::Any; | ||
use std::sync::OnceLock; | ||
|
||
use arrow_schema::DataType; | ||
use datafusion::logical_expr::scalar_doc_sections::DOC_SECTION_OTHER; | ||
use datafusion::logical_expr::{ColumnarValue, Documentation, ScalarUDFImpl, Signature}; | ||
use geoarrow::algorithm::native::BoundingRectArray; | ||
use geoarrow::ArrayBase; | ||
|
||
use crate::data_types::{any_single_geometry_type_input, parse_to_native_array, BOX2D_TYPE}; | ||
use crate::error::GeoDataFusionResult; | ||
|
||
#[derive(Debug)] | ||
pub(super) struct Box2D { | ||
signature: Signature, | ||
} | ||
|
||
impl Box2D { | ||
pub fn new() -> Self { | ||
Self { | ||
signature: any_single_geometry_type_input(), | ||
} | ||
} | ||
} | ||
|
||
static DOCUMENTATION: OnceLock<Documentation> = OnceLock::new(); | ||
|
||
impl ScalarUDFImpl for Box2D { | ||
fn as_any(&self) -> &dyn Any { | ||
self | ||
} | ||
|
||
fn name(&self) -> &str { | ||
"st_box2d" | ||
} | ||
|
||
fn signature(&self) -> &Signature { | ||
&self.signature | ||
} | ||
|
||
fn return_type(&self, _arg_types: &[DataType]) -> datafusion::error::Result<DataType> { | ||
Ok(BOX2D_TYPE.into()) | ||
} | ||
|
||
fn invoke(&self, args: &[ColumnarValue]) -> datafusion::error::Result<ColumnarValue> { | ||
Ok(box2d_impl(args)?) | ||
} | ||
|
||
fn documentation(&self) -> Option<&Documentation> { | ||
Some(DOCUMENTATION.get_or_init(|| { | ||
Documentation::builder() | ||
.with_doc_section(DOC_SECTION_OTHER) | ||
.with_description("Returns a box2d representing the 2D extent of the geometry.") | ||
.with_argument("geom", "geometry") | ||
.build() | ||
.unwrap() | ||
})) | ||
} | ||
} | ||
|
||
// Note: this is exactly the same impl as ST_Envelope. Perhaps we should use an alias instead | ||
fn box2d_impl(args: &[ColumnarValue]) -> GeoDataFusionResult<ColumnarValue> { | ||
let array = ColumnarValue::values_to_arrays(args)? | ||
.into_iter() | ||
.next() | ||
.unwrap(); | ||
let native_array = parse_to_native_array(array)?; | ||
let output = native_array.as_ref().bounding_rect()?; | ||
Ok(output.into_array_ref().into()) | ||
} |
This file contains 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 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,13 @@ | ||
mod r#box; | ||
mod extrema; | ||
|
||
use datafusion::prelude::SessionContext; | ||
|
||
/// Register all provided bounding box functions | ||
pub fn register_constructors(ctx: &SessionContext) { | ||
ctx.register_udf(extrema::XMin::new().into()); | ||
ctx.register_udf(extrema::YMin::new().into()); | ||
ctx.register_udf(extrema::XMax::new().into()); | ||
ctx.register_udf(extrema::YMax::new().into()); | ||
ctx.register_udf(r#box::Box2D::new().into()); | ||
} |
Oops, something went wrong.