Skip to content

Commit d6a9971

Browse files
asinghvi17rafaqz
andauthored
Add a GeometryCollection convert method for GeoInterface (#207)
* 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]>
1 parent 0992162 commit d6a9971

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

src/geo_interface.jl

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,28 @@ function GI.convert(
176176
Polygon[GI.convert(Polygon, PolygonTrait(), g; context) for g in GI.getgeom(geom)]
177177
return MultiPolygon(polygons)
178178
end
179+
GI.convert(
180+
::Type{GeometryCollection},
181+
::GeometryCollectionTrait,
182+
geom::GeometryCollection;
183+
context = nothing,
184+
) = geom
185+
function GI.convert(
186+
::Type{GeometryCollection},
187+
::GeometryCollectionTrait,
188+
geom;
189+
context = get_global_context(),
190+
)
191+
geometries = map(GI.getgeom(geom)) do g
192+
t = GI.trait(g)
193+
lg = geointerface_geomtype(t)
194+
# We call the full invocation for LibGEOS directly,
195+
# so the context can be passed through, since
196+
# `GI.convert(Mod, x)` does not allow kwargs.
197+
GI.convert(lg, t, g; context)
198+
end
199+
return GeometryCollection(geometries)
200+
end
179201

180202
function GI.convert(
181203
t::Type{<:AbstractGeometry},

test/test_geo_interface.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,9 @@ const LG = LibGEOS
129129
# Plots.plot(lr)
130130
# Makie.plot(lr)
131131

132+
gi_gc = GeoInterface.GeometryCollection([ls, mls, pt])
133+
@test GeoInterface.convert(LibGEOS, gi_gc) isa LibGEOS.GeometryCollection
134+
132135
@inferred GeoInterface.ncoord(lr)
133136
@inferred GeoInterface.ngeom(lr)
134137
@inferred GeoInterface.getgeom(lr)

0 commit comments

Comments
 (0)