|
| 1 | +module GeoInterfaceMakie |
| 2 | + |
| 3 | +using GeoInterface |
| 4 | +import MakieCore as MC |
| 5 | +import GeometryBasics as GB |
| 6 | +import GeoInterface as GI |
| 7 | + |
| 8 | + |
| 9 | +function _plottype(geom) |
| 10 | + plottype_from_geomtrait(GI.geomtrait(geom)) |
| 11 | +end |
| 12 | +function plottype_from_geomtrait(::Union{GI.LineStringTrait, GI.MultiLineStringTrait}) |
| 13 | + MC.Lines |
| 14 | +end |
| 15 | +function plottype_from_geomtrait(::Union{GI.PointTrait, GI.MultiPointTrait}) |
| 16 | + MC.Scatter |
| 17 | +end |
| 18 | +function plottype_from_geomtrait(::Union{GI.PolygonTrait,GI.MultiPolygonTrait, GI.LinearRingTrait}) |
| 19 | + MC.Poly |
| 20 | +end |
| 21 | +function pttype(geom) |
| 22 | + if GI.is3d(geom) |
| 23 | + GB.Point3f |
| 24 | + else |
| 25 | + GB.Point2f |
| 26 | + end |
| 27 | +end |
| 28 | +function points(geom)::Union{Vector{GB.Point2f}, Vector{GB.Point3f}} |
| 29 | + Pt = pttype(geom) |
| 30 | + out = Pt[] |
| 31 | + points!(out, GI.geomtrait(geom), geom) |
| 32 | +end |
| 33 | +@noinline function points!(out, ::GI.AbstractTrait, geom) |
| 34 | + for pt in GI.getpoint(geom) |
| 35 | + push!(out, _convert(eltype(out), pt)) |
| 36 | + end |
| 37 | + out |
| 38 | +end |
| 39 | +@noinline function points!(out, ::GI.PointTrait, pt) |
| 40 | + push!(out, _convert(eltype(out), pt)) |
| 41 | + out |
| 42 | +end |
| 43 | +function _convert(::Type{GB.Point2f}, pt) |
| 44 | + x,y = GI.getcoord(pt) |
| 45 | + GB.Point2f(x,y) |
| 46 | +end |
| 47 | +function _convert(::Type{GB.Point3f}, pt) |
| 48 | + x,y,z = GI.getcoord(pt) |
| 49 | + GB.Point3f(x,y,z) |
| 50 | +end |
| 51 | + |
| 52 | +function basicsgeom(geom) |
| 53 | + t = GI.geomtrait(geom) |
| 54 | + T = basicsgeomtype(t) |
| 55 | + GI.convert(T, t, geom) |
| 56 | +end |
| 57 | + |
| 58 | +basicsgeomtype(::GI.PointTrait) = GB.Point |
| 59 | +basicsgeomtype(::GI.MultiPointTrait) = GB.MultiPoint |
| 60 | +basicsgeomtype(::GI.PolygonTrait) = GB.Polygon |
| 61 | +basicsgeomtype(::GI.MultiPolygonTrait) = GB.MultiPolygon |
| 62 | +basicsgeomtype(::GI.LineStringTrait) = GB.LineString |
| 63 | +basicsgeomtype(::GI.MultiLineStringTrait) = GB.MultiLineString |
| 64 | + |
| 65 | +function _convert_arguments(::Type{<:MC.Poly}, geom)::Tuple |
| 66 | + geob = basicsgeom(geom)::Union{GB.Polygon, GB.MultiPolygon} |
| 67 | + (geob,) |
| 68 | +end |
| 69 | +function _convert_arguments(::MC.PointBased, geom)::Tuple |
| 70 | + pts = points(geom) |
| 71 | + (pts,) |
| 72 | +end |
| 73 | + |
| 74 | +function expr_enable(Geom) |
| 75 | + quote |
| 76 | + function $MC.plottype(geom::$Geom) |
| 77 | + $_plottype(geom) |
| 78 | + end |
| 79 | + function $MC.convert_arguments(p::Type{<:$MC.Poly}, geom::$Geom) |
| 80 | + $_convert_arguments(p,geom) |
| 81 | + end |
| 82 | + function $MC.convert_arguments(p::$MC.PointBased, geom::$Geom) |
| 83 | + $_convert_arguments(p,geom) |
| 84 | + end |
| 85 | + end |
| 86 | +end |
| 87 | + |
| 88 | +""" |
| 89 | +
|
| 90 | + @enable(Geom) |
| 91 | +
|
| 92 | +Enable Makie based plotting for a type `Geom` that implements the geometry interface |
| 93 | +defined in `GeoInterface`. |
| 94 | +
|
| 95 | +# Usage |
| 96 | +```julia |
| 97 | +struct MyGeometry |
| 98 | +... |
| 99 | +end |
| 100 | +# overload GeoInterface for MyGeometry |
| 101 | +... |
| 102 | +
|
| 103 | +@enable MyGeometry |
| 104 | +``` |
| 105 | +""" |
| 106 | +macro enable(Geom) |
| 107 | + esc(expr_enable(Geom)) |
| 108 | +end |
| 109 | + |
| 110 | +# TODO |
| 111 | +# Features and Feature collections |
| 112 | +# https://github.com/JuliaGeo/GeoInterface.jl/pull/72#issue-1406325596 |
| 113 | + |
| 114 | +end |
0 commit comments