Skip to content

Add adapter to stream any geo_traits geometry into GeomProcessor #261

@zetacat

Description

@zetacat

Motivation

geozero can stream many concrete geometry types, and has helpers for geo-types. A small adapter would let any geo-traits implementor be processed by geozero without first converting to geo-types. This would help anyone defining their own geometry types that implement geo-traits, not just existing libraries.

Proposal

Provide a lightweight newtype wrapper that implements GeozeroGeometry for any G: geo_traits::GeometryTrait.
This could be gated behind an optional cargo feature.

Example sketch (This is AI-generated and intended for demonstration purposes only, not as a final implementation):

use geozero::{GeomProcessor, GeozeroGeometry, error::Result as GzResult, CoordDimensions};
use geo_traits::{GeometryTrait, GeometryType};

pub struct AsGeozero<G>(pub G);

impl<G> GeozeroGeometry for AsGeozero<G>
where
    G: GeometryTrait<T = f64>,
{
    fn process_geom<P: GeomProcessor>(&self, p: &mut P) -> GzResult<()> {
        match self.0.as_type() {
            GeometryType::Point(pt) => {
                p.point_begin(0)?;
                let c = pt.coord();
                p.xy(c.x(), c.y(), 0)?;
                p.point_end(0)?;
            }
            GeometryType::LineString(ls) => {
                p.linestring_begin(0, ls.num_coords() as usize, false, 0)?;
                for c in ls.coords_iter() { p.xy(c.x(), c.y(), 0)?; }
                p.linestring_end(0, false, 0)?;
            }
            // ...Polygon, Multi*, GeometryCollection, Rect, etc.
        }
        Ok(())
    }

    fn dims(&self) -> CoordDimensions { CoordDimensions::xy() }
    fn srid(&self) -> Option<i32> { None }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions