petern48 commented on code in PR #469:
URL: https://github.com/apache/sedona-db/pull/469#discussion_r2650189224


##########
rust/sedona-functions/src/st_asgeojson.rs:
##########
@@ -0,0 +1,456 @@
+// 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::executor::WkbExecutor;
+use arrow_array::builder::StringBuilder;
+use arrow_schema::DataType;
+use datafusion_common::error::{DataFusionError, Result};
+use datafusion_expr::{
+    scalar_doc_sections::DOC_SECTION_OTHER, ColumnarValue, Documentation, 
Volatility,
+};
+use geo_traits::to_geo::{
+    ToGeoLineString, ToGeoMultiLineString, ToGeoMultiPoint, ToGeoMultiPolygon, 
ToGeoPoint,
+    ToGeoPolygon,
+};
+use geo_traits::{GeometryCollectionTrait, GeometryTrait, GeometryType};
+use geo_types::Geometry;
+use sedona_expr::scalar_udf::{SedonaScalarKernel, SedonaScalarUDF};
+use sedona_schema::{datatypes::SedonaType, matchers::ArgMatcher};
+
+/// Output format type for GeoJSON
+#[derive(Debug, Clone, Copy, PartialEq)]
+enum GeoJsonType {
+    Simple,
+    Feature,
+    FeatureCollection,
+}
+
+impl GeoJsonType {
+    fn from_str(s: &str) -> Result<Self> {
+        match s.to_lowercase().as_str() {
+            "simple" => Ok(GeoJsonType::Simple),
+            "feature" => Ok(GeoJsonType::Feature),
+            "featurecollection" => Ok(GeoJsonType::FeatureCollection),
+            _ => Err(DataFusionError::Execution(format!(
+                "Invalid GeoJSON type '{}'. Valid options are: 'Simple', 
'Feature', 'FeatureCollection'",
+                s
+            ))),
+        }
+    }
+}
+
+/// ST_AsGeoJSON() scalar UDF implementation
+///
+/// An implementation of GeoJSON writing using the geojson crate.
+pub fn st_asgeojson_udf() -> SedonaScalarUDF {
+    SedonaScalarUDF::new(
+        "st_asgeojson",
+        vec![Arc::new(STAsGeoJSON {}), Arc::new(STAsGeoJSONWithType {})],
+        Volatility::Immutable,
+        Some(st_asgeojson_doc()),
+    )
+}
+
+fn st_asgeojson_doc() -> Documentation {
+    Documentation::builder(
+        DOC_SECTION_OTHER,
+        "Return the GeoJSON representation of a geometry or geography",
+        "ST_AsGeoJSON (A: Geometry [, type: String])",
+    )
+    .with_argument("geom", "geometry: Input geometry or geography")
+    .with_argument(
+        "type",
+        "string (optional): Output type - 'Simple' (default), 'Feature', or 
'FeatureCollection'",
+    )
+    .with_sql_example("SELECT ST_AsGeoJSON(ST_Point(1.0, 2.0))")
+    .with_sql_example("SELECT ST_AsGeoJSON(ST_Point(1.0, 2.0), 'Feature')")
+    .with_sql_example("SELECT ST_AsGeoJSON(ST_Point(1.0, 2.0), 
'FeatureCollection')")

Review Comment:
   I'm not sure if you already discussed this or not, but I'll point out that 
this follows [the Sedona 
API](https://sedona.apache.org/latest/api/sql/Function/#st_asgeojson). The 
[PostGIS one](https://postgis.net/docs/ST_AsGeoJSON.html) is completely 
different.
   
   ```
   text ST_AsGeoJSON(record feature, text geom_column="", integer 
maxdecimaldigits=9, boolean pretty_bool=false, text id_column='');
   
   text ST_AsGeoJSON(geometry geom, integer maxdecimaldigits=9, integer 
options=8);
   
   text ST_AsGeoJSON(geography geog, integer maxdecimaldigits=9, integer 
options=0);
   ```
   
   We have generally followed the PostGIS behavior for all previous functions 
that I know of.



-- 
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]

Reply via email to