From deafc8ce5e80fd247f70f493eae5101b4e4edd9c Mon Sep 17 00:00:00 2001 From: Anshul Singhvi Date: Mon, 25 Nov 2024 16:11:33 -0500 Subject: [PATCH] add `get_geometries` a generic utility inspired by similar functions in Rasters.jl and elsewhere - returns a vector of the geometries in a given object, be it a feature collection, table, iterable of geometries, or whatever. Potential next steps: - `get_geometries(x) do geom` that rebuilds the given structure as far as possible - this is the same as `apply(::AbstractGeometryTrait)` though.... - treatment of dataframes with multiple columns --- GeometryOpsCore/src/geometry_utils.jl | 32 +++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/GeometryOpsCore/src/geometry_utils.jl b/GeometryOpsCore/src/geometry_utils.jl index c941273b2..a1e0b2165 100644 --- a/GeometryOpsCore/src/geometry_utils.jl +++ b/GeometryOpsCore/src/geometry_utils.jl @@ -1,3 +1,35 @@ _linearring(geom::GI.LineString) = GI.LinearRing(parent(geom); extent=geom.extent, crs=geom.crs) _linearring(geom::GI.LinearRing) = geom + + +function get_geometries(x) + if GI.isgeometry(x) + return x + # elseif GI.isgeometrycollection(x) + # return GI.getgeom(x) + elseif GI.isfeature(x) + return GI.geometry(x) + elseif GI.isfeaturecollection(x) + return [GI.geometry(f) for f in GI.getfeature(x)] + elseif Tables.istable(x) && Tables.hascolumn(x, first(GI.geometrycolumns(x))) + return Tables.getcolumn(x, first(GI.geometrycolumns(x))) + else + c = collect(x) + if c isa AbstractArray && GI.trait(first(c)) isa GI.AbstractGeometryTrait + return c + else + throw(ArgumentError(""" + Expected a geometry, feature, feature collection, table with geometry column, or iterable of geometries. + Got $(typeof(x)). + + The input must be one of: + - A GeoInterface geometry (has trait <: AbstractGeometryTrait) + - A GeoInterface feature (has trait FeatureTrait) + - A GeoInterface feature collection (has trait FeatureCollectionTrait) + - A Tables.jl table with a geometry column + - An iterable containing geometries + """)) + end + end +end