Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

create multi geometries from vector of single geometries #87

Closed
visr opened this issue Apr 23, 2021 · 0 comments · Fixed by #136
Closed

create multi geometries from vector of single geometries #87

visr opened this issue Apr 23, 2021 · 0 comments · Fixed by #136

Comments

@visr
Copy link
Member

visr commented Apr 23, 2021

using LibGEOS, GeoInterface

# 3 overlapping circles in a triangle
circles = [
    buffer(Point(0.0, 0.0), 1.0),
    buffer(Point(1.0, 0.0), 1.0),
    buffer(Point(0.5, 1.0), 1.0),
]

# now if from these 3 polygons we want to create a single MultiPolygon
# you might expect this to work:
MultiPolygon(circles)
# but you get -> ERROR: LoadError: MethodError: no method matching MultiPolygon(::Vector{Polygon})
# going through the GeoInterface constructor works, but will be less efficient
MultiPolygon(GeoInterface.coordinates(circles))

mutable struct MultiPolygon <: GeoInterface.AbstractMultiPolygon
ptr::GEOSGeom
function MultiPolygon(ptr::GEOSGeom)
multipolygon = new(ptr)
finalizer(destroyGeom, multipolygon)
multipolygon
end
MultiPolygon(multipolygon::Vector{Vector{Vector{Vector{Float64}}}}) =
MultiPolygon(createCollection(GEOS_MULTIPOLYGON,
GEOSGeom[createPolygon(createLinearRing(coords[1]),
GEOSGeom[createLinearRing(c) for c in coords[2:end]])
for coords in multipolygon]))
MultiPolygon(obj::T) where {T<:GeoInterface.AbstractMultiPolygon} = MultiPolygon(GeoInterface.coordinates(obj))
end

This probably goes for all multi geometries, not just multipolygon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant