Skip to content

Commit

Permalink
feat: add more geospatial functions
Browse files Browse the repository at this point in the history
  • Loading branch information
sunng87 committed Nov 1, 2024
1 parent ae63046 commit f7a9ec0
Show file tree
Hide file tree
Showing 7 changed files with 601 additions and 1 deletion.
151 changes: 151 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion src/common/function/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ workspace = true

[features]
default = ["geo"]
geo = ["geohash", "h3o", "s2"]
geo = ["geohash", "h3o", "s2", "wkt", "geo-types", "dep:geo"]

[dependencies]
api.workspace = true
Expand All @@ -29,6 +29,8 @@ datafusion.workspace = true
datatypes.workspace = true
derive_more = { version = "1", default-features = false, features = ["display"] }
geohash = { version = "0.13", optional = true }
geo-types = { version = "0.7", optional = true }
geo = { version = "0.29", optional = true }
h3o = { version = "0.6", optional = true }
jsonb.workspace = true
num = "0.4"
Expand All @@ -44,6 +46,7 @@ sql.workspace = true
statrs = "0.16"
store-api.workspace = true
table.workspace = true
wkt = { version = "0.11", optional = true }

[dev-dependencies]
ron = "0.7"
Expand Down
15 changes: 15 additions & 0 deletions src/common/function/src/scalars/geo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ pub(crate) mod encoding;
mod geohash;
mod h3;
mod helpers;
mod measure;
mod relation;
mod s2;
mod wkt;

use crate::function_registry::FunctionRegistry;

Expand Down Expand Up @@ -65,5 +68,17 @@ impl GeoFunctions {
registry.register(Arc::new(s2::S2CellLevel));
registry.register(Arc::new(s2::S2CellToToken));
registry.register(Arc::new(s2::S2CellParent));

// spatial data type
registry.register(Arc::new(wkt::LatLngToPointWkt));

// spatial relation
registry.register(Arc::new(relation::STContains));
registry.register(Arc::new(relation::STWithin));
registry.register(Arc::new(relation::STIntersects));

// spatial measure
registry.register(Arc::new(measure::STDistance));
registry.register(Arc::new(measure::STDistanceSphere));
}
}
17 changes: 17 additions & 0 deletions src/common/function/src/scalars/geo/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
// 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 common_error::ext::{BoxedError, PlainError};
use common_error::status_code::StatusCode;
use common_query::error::{self, Result};
use geo_types::geometry::Geometry;
use snafu::ResultExt;
use wkt::TryFromWkt;

macro_rules! ensure_columns_len {
($columns:ident) => {
Expand Down Expand Up @@ -73,3 +79,14 @@ macro_rules! ensure_and_coerce {
}

pub(super) use ensure_and_coerce;

pub(super) fn parse_wkt(s: &str) -> Result<Geometry> {
Geometry::try_from_wkt_str(s)
.map_err(|e| {
BoxedError::new(PlainError::new(
format!("Fail to parse WKT: {}", e),
StatusCode::EngineExecuteQuery,
))
})
.context(error::ExecuteSnafu)
}
Loading

0 comments on commit f7a9ec0

Please sign in to comment.