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

Use GI.convert for Makie plots, allow arrays #108

Merged
merged 5 commits into from
Aug 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 21 additions & 55 deletions GeoInterfaceMakie/src/GeoInterfaceMakie.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,76 +18,42 @@ end
function plottype_from_geomtrait(::Union{GI.PolygonTrait,GI.MultiPolygonTrait, GI.LinearRingTrait})
MC.Poly
end
function pttype(geom)
if GI.is3d(geom)
GB.Point3f
else
GB.Point2f
end
end
function points(geom)::Union{Vector{GB.Point2f}, Vector{GB.Point3f}}
Pt = pttype(geom)
out = Pt[]
points!(out, GI.geomtrait(geom), geom)
end
@noinline function points!(out, ::GI.AbstractTrait, geom)
for pt in GI.getpoint(geom)
push!(out, _convert(eltype(out), pt))
end
out
end
@noinline function points!(out, ::GI.PointTrait, pt)
push!(out, _convert(eltype(out), pt))
out
end
function _convert(::Type{GB.Point2f}, pt)
x,y = GI.getcoord(pt)
GB.Point2f(x,y)
end
function _convert(::Type{GB.Point3f}, pt)
x,y,z = GI.getcoord(pt)
GB.Point3f(x,y,z)
end

function basicsgeom(geom)
t = GI.geomtrait(geom)
T = basicsgeomtype(t)
GI.convert(T, t, geom)
end

basicsgeomtype(::GI.PointTrait) = GB.Point
basicsgeomtype(::GI.MultiPointTrait) = GB.MultiPoint
basicsgeomtype(::GI.PolygonTrait) = GB.Polygon
basicsgeomtype(::GI.MultiPolygonTrait) = GB.MultiPolygon
basicsgeomtype(::GI.LineStringTrait) = GB.LineString
basicsgeomtype(::GI.MultiLineStringTrait) = GB.MultiLineString

function _convert_arguments(t::Type{<:MC.Poly}, geom)::Tuple
geob = basicsgeom(geom)::Union{GB.Polygon, GB.MultiPolygon}
MC.convert_arguments(t,geob)
end
function _convert_arguments(t::Type{<:MC.Lines}, geom)::Tuple
geob = basicsgeom(geom)
function _convert_arguments(t, geom)::Tuple
geob = GI.convert(GB, geom)
MC.convert_arguments(t, geob)
end
function _convert_arguments(::MC.PointBased, geom)::Tuple
pts = points(geom)
(pts,)
function _convert_array_arguments(t, geoms)::Tuple
geob = map(geom -> GI.convert(GB, geom), geoms)
MC.convert_arguments(t, geob)
end

function expr_enable(Geom)
quote
function $MC.plottype(geom::$Geom)
$_plottype(geom)
end
# TODO: this method doesn't seem to do anything
function $MC.plottype(geom::AbstractArray{<:$Geom})
$_plottype(first(geom))
end
function $MC.convert_arguments(p::Type{<:$MC.Poly}, geom::$Geom)
$_convert_arguments(p,geom)
$_convert_arguments(p, geom)
end
function $MC.convert_arguments(p::Type{<:$MC.Poly}, geoms::AbstractArray{<:$Geom})
$_convert_array_arguments(p, geoms)
end
function $MC.convert_arguments(p::$MC.PointBased, geom::$Geom)
$_convert_arguments(p,geom)
$_convert_arguments(p, geom)
end
function $MC.convert_arguments(p::$MC.PointBased, geoms::AbstractArray{<:$Geom})
$_convert_array_arguments(p, geoms)
end
function $MC.convert_arguments(p::Type{<:$MC.Lines}, geom::$Geom)
$_convert_arguments(p,geom)
$_convert_arguments(p, geom)
end
function $MC.convert_arguments(p::Type{<:$MC.Lines}, geoms::AbstractArray{<:$Geom})
$_convert_array_arguments(p, geoms)
end
end
end
Expand Down
40 changes: 19 additions & 21 deletions GeoInterfaceMakie/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,6 @@ using Makie

GeoInterfaceMakie.@enable(LibGEOS.AbstractGeometry)

@testset "points" begin
points = GeoInterfaceMakie.points
pt = LibGEOS.Point(1,2,3)
@test points(pt) == [Point3f(1,2,3)]

unitsquare = readgeom("POLYGON((0 0, 0 1, 1 1, 1 0, 0 0))")
@test points(unitsquare) == [
Point2f(0.0, 0.0),
Point2f(0.0, 1.0),
Point2f(1.0, 1.0),
Point2f(1.0, 0.0),
Point2f(0.0, 0.0),
]
end

@testset "Makie plotting LibGEOS MultiLineString shows additional lines #83" begin
mls = readgeom("MULTILINESTRING ((0 0,3 0,3 3,0 3,0 0),(1 1,2 1,2 2,1 2,1 1))")
expected = [[0.0, 0.0], [3.0, 0.0], [3.0, 3.0], [0.0, 3.0], [0.0, 0.0],
Expand All @@ -35,17 +20,30 @@ end
unitsquare = readgeom("POLYGON((0 0, 0 1, 1 1, 1 0, 0 0))")
bigsquare = readgeom("POLYGON((0 0, 11 0, 11 11, 0 11, 0 0))")
smallsquare = readgeom("POLYGON((5 5, 8 5, 8 8, 5 8, 5 5))")
fig = Figure()
multipolygon = GI.union(smallsquare, unitsquare)
point = readgeom("POINT(1 0)")
multipoint = readgeom("MULTIPOINT(1 2, 2 3, 3 4)")
geoms = [
unitsquare,
GI.difference(bigsquare, smallsquare),
boundary(unitsquare),
GI.union(smallsquare, unitsquare),
readgeom("POINT(1 0)"),
readgeom("MULTIPOINT(1 2, 2 3, 3 4)"),
multipolygon,
point,
multipoint,
]
for (i,geom) in enumerate(geoms)
Makie.plot!(Axis(fig[i,1], title="$(GI.geomtrait(geom))"), geom)
fig = Figure()
for (i, geom) in enumerate(geoms)
Makie.plot!(Axis(fig[i, 1], title="$(GI.geomtrait(geom))"), geom)
if geom == multipoint
# `plot!` wont even work with the GeometryBasics version of this
continue
elseif geom == multipolygon
# `plot!` wont work with the GeometryBasics version of this either
# But `poly!` does
Makie.poly!(Axis(fig[i, 2], title="Vector of $(GI.geomtrait(geom))"), [geom, geom])
else
Makie.plot!(Axis(fig[i, 2], title="Vector of $(GI.geomtrait(geom))"), [geom, geom])
end
end
fig
end
Loading