Skip to content

Commit

Permalink
Add a GeometryCollection convert method for GeoInterface (#207)
Browse files Browse the repository at this point in the history
* Add a GeometryCollection convert

* add basic gc test

* Apply suggestion from code review

Co-authored-by: Rafael Schouten <[email protected]>

---------

Co-authored-by: Rafael Schouten <[email protected]>
  • Loading branch information
asinghvi17 and rafaqz authored Jun 10, 2024
1 parent 0992162 commit d6a9971
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/geo_interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,28 @@ function GI.convert(
Polygon[GI.convert(Polygon, PolygonTrait(), g; context) for g in GI.getgeom(geom)]
return MultiPolygon(polygons)
end
GI.convert(
::Type{GeometryCollection},
::GeometryCollectionTrait,
geom::GeometryCollection;
context = nothing,
) = geom
function GI.convert(
::Type{GeometryCollection},
::GeometryCollectionTrait,
geom;
context = get_global_context(),
)
geometries = map(GI.getgeom(geom)) do g
t = GI.trait(g)
lg = geointerface_geomtype(t)
# We call the full invocation for LibGEOS directly,
# so the context can be passed through, since
# `GI.convert(Mod, x)` does not allow kwargs.
GI.convert(lg, t, g; context)
end
return GeometryCollection(geometries)
end

function GI.convert(
t::Type{<:AbstractGeometry},
Expand Down
3 changes: 3 additions & 0 deletions test/test_geo_interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@ const LG = LibGEOS
# Plots.plot(lr)
# Makie.plot(lr)

gi_gc = GeoInterface.GeometryCollection([ls, mls, pt])
@test GeoInterface.convert(LibGEOS, gi_gc) isa LibGEOS.GeometryCollection

@inferred GeoInterface.ncoord(lr)
@inferred GeoInterface.ngeom(lr)
@inferred GeoInterface.getgeom(lr)
Expand Down

0 comments on commit d6a9971

Please sign in to comment.