alamb commented on code in PR #8943: URL: https://github.com/apache/arrow-rs/pull/8943#discussion_r2586643038
########## parquet-geospatial/src/types.rs: ########## @@ -0,0 +1,393 @@ +// 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 arrow::error::Result; +use arrow_schema::{ArrowError, DataType, extension::ExtensionType}; +use serde::{Deserialize, Serialize}; + +/// Hints at the likely Parquet geospatial logical type represented by a [`Metadata`]. +/// +/// Based on the `algorithm` field: +/// - [`Hint::Geometry`]: WKB format with linear/planar edge interpolation +/// - [`Hint::Geography`]: WKB format with explicit non-linear/non-planar edge interpolation +/// +/// See the [Parquet Geospatial specification](https://github.com/apache/parquet-format/blob/master/Geospatial.md) +/// for more details. +#[derive(Copy, Clone, Debug, Serialize, Deserialize)] +pub enum Hint { + /// Geospatial features in WKB format with linear/planar edge interpolation + Geometry, + /// Geospatial features in WKB format with explicit non-linear/non-planar edge interpolation + Geography, +} + +/// The metadata associated with a [`WkbType`]. +#[derive(Clone, Debug, Default, Serialize, Deserialize)] +pub struct Metadata { + /// The Coordinate Reference System (CRS) of the [`WkbType`], if present. + /// + /// This may be a raw string value (e.g., "EPSG:3857") or a JSON object (e.g., PROJJSON). + /// Note: Common lon/lat CRS representations (EPSG:4326, OGC:CRS84) are canonicalized + /// to `None` during serialization to match Parquet conventions. + #[serde(skip_serializing_if = "Option::is_none")] + pub crs: Option<serde_json::Value>, + /// The edge interpolation algorithm of the [`WkbType`], if present. + #[serde(skip_serializing_if = "Option::is_none")] + pub algorithm: Option<String>, +} + +impl Metadata { + /// Constructs a new [`Metadata`] with the given CRS and algorithm. + /// + /// If a CRS is provided, and can be parsed as JSON, it will be stored as a JSON object instead + /// of its string representation. + pub fn new(crs: Option<String>, algorithm: Option<String>) -> Self { Review Comment: It seems like this doesn't need an owned String -- as it parses the result into `Json::Value` Thus I suggest something like ```suggestion pub fn new(crs: Option<&str>, algorithm: Option<String>) -> Self { ``` ########## parquet-geospatial/src/types.rs: ########## @@ -0,0 +1,393 @@ +// 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 arrow::error::Result; +use arrow_schema::{ArrowError, DataType, extension::ExtensionType}; +use serde::{Deserialize, Serialize}; + +/// Hints at the likely Parquet geospatial logical type represented by a [`Metadata`]. +/// +/// Based on the `algorithm` field: +/// - [`Hint::Geometry`]: WKB format with linear/planar edge interpolation +/// - [`Hint::Geography`]: WKB format with explicit non-linear/non-planar edge interpolation +/// +/// See the [Parquet Geospatial specification](https://github.com/apache/parquet-format/blob/master/Geospatial.md) +/// for more details. +#[derive(Copy, Clone, Debug, Serialize, Deserialize)] +pub enum Hint { + /// Geospatial features in WKB format with linear/planar edge interpolation + Geometry, + /// Geospatial features in WKB format with explicit non-linear/non-planar edge interpolation + Geography, +} + +/// The metadata associated with a [`WkbType`]. +#[derive(Clone, Debug, Default, Serialize, Deserialize)] +pub struct Metadata { + /// The Coordinate Reference System (CRS) of the [`WkbType`], if present. + /// + /// This may be a raw string value (e.g., "EPSG:3857") or a JSON object (e.g., PROJJSON). + /// Note: Common lon/lat CRS representations (EPSG:4326, OGC:CRS84) are canonicalized + /// to `None` during serialization to match Parquet conventions. + #[serde(skip_serializing_if = "Option::is_none")] + pub crs: Option<serde_json::Value>, + /// The edge interpolation algorithm of the [`WkbType`], if present. + #[serde(skip_serializing_if = "Option::is_none")] + pub algorithm: Option<String>, +} + +impl Metadata { + /// Constructs a new [`Metadata`] with the given CRS and algorithm. + /// + /// If a CRS is provided, and can be parsed as JSON, it will be stored as a JSON object instead + /// of its string representation. + pub fn new(crs: Option<String>, algorithm: Option<String>) -> Self { + let crs = crs.map(|c| match serde_json::from_str(&c) { + Ok(crs) => crs, + Err(_) => serde_json::Value::String(c), + }); + + Self { crs, algorithm } + } + + /// Returns a [`Hint`] to the likely underlying Logical Type that this [`Metadata`] represents. + pub fn type_hint(&self) -> Hint { + match &self.algorithm { + Some(s) if s.to_lowercase() == "planar" => Hint::Geometry, + Some(_) => Hint::Geography, + None => Hint::Geometry, + } + } +} + +/// Well-Known Binary (WKB) [`ExtensionType`] for geospatial data. +/// +/// Represents the canonical Arrow Extension Type for storing GeoArrow data. +#[derive(Debug, Default)] +pub struct WkbType(Metadata); + +impl WkbType { + /// Constructs a new [`WkbType`] with the given [`Metadata`]. + /// + /// If `None` is provided, default (empty) metadata is used. + pub fn new(metadata: Option<Metadata>) -> Self { + Self(metadata.unwrap_or_default()) + } +} + +impl ExtensionType for WkbType { + const NAME: &'static str = "geoarrow.wkb"; Review Comment: Is "geoarrow" the right extension type for the parquet reader to use? I ask this in ignorance as now there is a geometry type in Parquet, but it seems like the geoarrow is still listed as a "Community Extension Type" https://arrow.apache.org/docs/format/CanonicalExtensions.html#community-extension-types I have no particular preference here, FWIW, and if we can use this crate to drive standardization on "geoarrow.wkb" that seems good to me but I wanted to ask ########## parquet-geospatial/src/types.rs: ########## @@ -0,0 +1,393 @@ +// 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 arrow::error::Result; Review Comment: For some reason ArrowError is in `arrow_schema` but the Result typedef is in the arrow crate: https://docs.rs/arrow/latest/src/arrow/error.rs.html#20-23 > If we want to avoid the dependency altogether there aren't that many Result<T> uses here and I can easily type alias or use std::Result<T, ArrowError> for those as well. THis is what I would suggest ########## parquet-geospatial/src/types.rs: ########## @@ -0,0 +1,393 @@ +// 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 arrow::error::Result; +use arrow_schema::{ArrowError, DataType, extension::ExtensionType}; +use serde::{Deserialize, Serialize}; + +/// Hints at the likely Parquet geospatial logical type represented by a [`Metadata`]. +/// +/// Based on the `algorithm` field: +/// - [`Hint::Geometry`]: WKB format with linear/planar edge interpolation +/// - [`Hint::Geography`]: WKB format with explicit non-linear/non-planar edge interpolation +/// +/// See the [Parquet Geospatial specification](https://github.com/apache/parquet-format/blob/master/Geospatial.md) +/// for more details. +#[derive(Copy, Clone, Debug, Serialize, Deserialize)] +pub enum Hint { + /// Geospatial features in WKB format with linear/planar edge interpolation + Geometry, + /// Geospatial features in WKB format with explicit non-linear/non-planar edge interpolation + Geography, +} + +/// The metadata associated with a [`WkbType`]. +#[derive(Clone, Debug, Default, Serialize, Deserialize)] +pub struct Metadata { + /// The Coordinate Reference System (CRS) of the [`WkbType`], if present. + /// + /// This may be a raw string value (e.g., "EPSG:3857") or a JSON object (e.g., PROJJSON). + /// Note: Common lon/lat CRS representations (EPSG:4326, OGC:CRS84) are canonicalized + /// to `None` during serialization to match Parquet conventions. + #[serde(skip_serializing_if = "Option::is_none")] + pub crs: Option<serde_json::Value>, + /// The edge interpolation algorithm of the [`WkbType`], if present. + #[serde(skip_serializing_if = "Option::is_none")] + pub algorithm: Option<String>, +} + +impl Metadata { + /// Constructs a new [`Metadata`] with the given CRS and algorithm. + /// + /// If a CRS is provided, and can be parsed as JSON, it will be stored as a JSON object instead + /// of its string representation. + pub fn new(crs: Option<String>, algorithm: Option<String>) -> Self { + let crs = crs.map(|c| match serde_json::from_str(&c) { + Ok(crs) => crs, + Err(_) => serde_json::Value::String(c), + }); + + Self { crs, algorithm } + } + + /// Returns a [`Hint`] to the likely underlying Logical Type that this [`Metadata`] represents. + pub fn type_hint(&self) -> Hint { + match &self.algorithm { + Some(s) if s.to_lowercase() == "planar" => Hint::Geometry, + Some(_) => Hint::Geography, + None => Hint::Geometry, + } + } +} + +/// Well-Known Binary (WKB) [`ExtensionType`] for geospatial data. +/// +/// Represents the canonical Arrow Extension Type for storing GeoArrow data. Review Comment: Maybe it is worth a link to GeoArrow here https://github.com/geoarrow/geoarrow -- 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]
