Skip to content

Commit

Permalink
add get_geometries
Browse files Browse the repository at this point in the history
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
  • Loading branch information
asinghvi17 committed Nov 25, 2024
1 parent 2f29e24 commit deafc8c
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions GeometryOpsCore/src/geometry_utils.jl
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit deafc8c

Please sign in to comment.