petern48 commented on code in PR #469: URL: https://github.com/apache/sedona-db/pull/469#discussion_r2654914835
########## rust/sedona-geo/src/st_asgeojson.rs: ########## @@ -0,0 +1,148 @@ +// 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 crate::to_geo::GeoTypesExecutor; +use arrow_array::builder::StringBuilder; +use arrow_schema::DataType; +use datafusion_common::error::{DataFusionError, Result}; +use datafusion_expr::ColumnarValue; +use sedona_expr::scalar_udf::{ScalarKernelRef, SedonaScalarKernel}; +use sedona_schema::{datatypes::SedonaType, matchers::ArgMatcher}; + +/// ST_AsGeoJSON() kernel implementation using GeoTypesExecutor +pub fn st_asgeojson_impl() -> ScalarKernelRef { + Arc::new(STAsGeoJSON {}) +} + +#[derive(Debug)] +struct STAsGeoJSON {} + +impl SedonaScalarKernel for STAsGeoJSON { + fn return_type(&self, args: &[SedonaType]) -> Result<Option<SedonaType>> { + let matcher = ArgMatcher::new( + vec![ArgMatcher::is_geometry()], + SedonaType::Arrow(DataType::Utf8), + ); + + matcher.match_args(args) + } + + fn invoke_batch( + &self, + arg_types: &[SedonaType], + args: &[ColumnarValue], + ) -> Result<ColumnarValue> { + let executor = GeoTypesExecutor::new(arg_types, args); + + // Minimal GeoJSON: {"type":"Point","coordinates":[]} + let min_probable_geojson_size = executor.num_iterations() * 33; + + // Initialize an output builder of the appropriate type + let mut builder = + StringBuilder::with_capacity(executor.num_iterations(), min_probable_geojson_size); + + executor.execute_wkb_void(|maybe_geom| { + match maybe_geom { + Some(geom) => { + // Convert geo_types::Geometry to geojson::Geometry + let geojson_geom: geojson::Geometry = (&geom).into(); Review Comment: Opened a quick fix for POLYGON EMPTY in geojson: https://github.com/georust/geojson/pull/262. Though we should just move forward with the workaround. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
