|
| 1 | +module GeoInterfaceRecipes |
| 2 | + |
| 3 | +using GeoInterface, RecipesBase |
| 4 | + |
| 5 | +const GI = GeoInterface |
| 6 | + |
| 7 | +export @enable_geo_plots |
| 8 | + |
| 9 | +""" |
| 10 | + GeoInterfaceRecipes.@enable_geo_plots(typ) |
| 11 | +
|
| 12 | +Macro to add plot recipes to a geometry type. |
| 13 | +""" |
| 14 | +macro enable_geo_plots(typ) |
| 15 | + quote |
| 16 | + # We recreate the apply_recipe functions manually here |
| 17 | + # as nesting the @recipe macro doesn't work. |
| 18 | + function RecipesBase.apply_recipe(plotattributes::Base.AbstractDict{Base.Symbol, Base.Any}, geom::$(esc(typ))) |
| 19 | + @nospecialize |
| 20 | + series_list = RecipesBase.RecipeData[] |
| 21 | + RecipesBase.is_explicit(plotattributes, :label) || (plotattributes[:label] = :none) |
| 22 | + Base.push!(series_list, RecipesBase.RecipeData(plotattributes, (GeoInterface.geomtrait(geom), geom))) |
| 23 | + return series_list |
| 24 | + end |
| 25 | + function RecipesBase.apply_recipe(plotattributes::Base.AbstractDict{Base.Symbol, Base.Any}, geom::Base.AbstractVector{<:Base.Union{Base.Missing,<:($(esc(typ)))}}) |
| 26 | + @nospecialize |
| 27 | + series_list = RecipesBase.RecipeData[] |
| 28 | + RecipesBase.is_explicit(plotattributes, :label) || (plotattributes[:label] = :none) |
| 29 | + for g in Base.skipmissing(geom) |
| 30 | + Base.push!(series_list, RecipesBase.RecipeData(plotattributes, (GeoInterface.geomtrait(g), g))) |
| 31 | + end |
| 32 | + return series_list |
| 33 | + end |
| 34 | + end |
| 35 | +end |
| 36 | + |
| 37 | +RecipesBase.@recipe function f(t::Union{GI.PointTrait,GI.MultiPointTrait}, geom) |
| 38 | + seriestype --> :scatter |
| 39 | + _coordvecs(t, geom) |
| 40 | +end |
| 41 | + |
| 42 | +RecipesBase.@recipe function f(t::Union{GI.LineStringTrait,GI.MultiLineStringTrait}, geom) |
| 43 | + seriestype --> :path |
| 44 | + _coordvecs(t, geom) |
| 45 | +end |
| 46 | + |
| 47 | +RecipesBase.@recipe function f(t::Union{GI.PolygonTrait,GI.MultiPolygonTrait}, geom) |
| 48 | + seriestype --> :shape |
| 49 | + _coordvecs(t, geom) |
| 50 | +end |
| 51 | + |
| 52 | +RecipesBase.@recipe f(::GI.GeometryCollectionTrait, collection) = collect(getgeom(collection)) |
| 53 | + |
| 54 | +# Convert coordinates to the form used by Plots.jl |
| 55 | +_coordvecs(::GI.PointTrait, geom) = [tuple(GI.coordinates(geom)...)] |
| 56 | +function _coordvecs(::GI.MultiPointTrait, geom) |
| 57 | + n = GI.npoint(geom) |
| 58 | + # We use a fixed conditional instead of dispatch, |
| 59 | + # as `is3d` may not be known at compile-time |
| 60 | + if GI.is3d(geom) |
| 61 | + _geom2coordvecs!(ntuple(_ -> Array{Float64}(undef, n), 3)..., geom) |
| 62 | + else |
| 63 | + _geom2coordvecs!(ntuple(_ -> Array{Float64}(undef, n), 2)..., geom) |
| 64 | + end |
| 65 | +end |
| 66 | +function _coordvecs(::GI.LineStringTrait, geom) |
| 67 | + n = GI.npoint(geom) |
| 68 | + if GI.is3d(geom) |
| 69 | + vecs = ntuple(_ -> Array{Float64}(undef, n), 3) |
| 70 | + return _geom2coordvecs!(vecs..., geom) |
| 71 | + else |
| 72 | + vecs = ntuple(_ -> Array{Float64}(undef, n), 2) |
| 73 | + return _geom2coordvecs!(vecs..., geom) |
| 74 | + end |
| 75 | +end |
| 76 | +function _coordvecs(::GI.MultiLineStringTrait, geom) |
| 77 | + function loop!(vecs, geom) |
| 78 | + i1 = 1 |
| 79 | + for line in GI.getgeom(geom) |
| 80 | + i2 = i1 + GI.npoint(line) - 1 |
| 81 | + vvecs = map(v -> view(v, i1:i2), vecs) |
| 82 | + _geom2coordvecs!(vvecs..., line) |
| 83 | + map(v -> v[i2 + 1] = NaN, vecs) |
| 84 | + i1 = i2 + 2 |
| 85 | + end |
| 86 | + return vecs |
| 87 | + end |
| 88 | + n = GI.npoint(geom) + GI.ngeom(geom) |
| 89 | + if GI.is3d(geom) |
| 90 | + vecs = ntuple(_ -> Array{Float64}(undef, n), 3) |
| 91 | + return loop!(vecs, geom) |
| 92 | + else |
| 93 | + vecs = ntuple(_ -> Array{Float64}(undef, n), 2) |
| 94 | + return loop!(vecs, geom) |
| 95 | + end |
| 96 | +end |
| 97 | +function _coordvecs(::GI.PolygonTrait, geom) |
| 98 | + ring = first(GI.getgeom(geom)) # currently doesn't plot holes |
| 99 | + points = GI.getpoint(ring) |
| 100 | + if GI.is3d(geom) |
| 101 | + return getcoord.(points, 1), getcoord.(points, 2), getcoord.(points, 3) |
| 102 | + else |
| 103 | + return getcoord.(points, 1), getcoord.(points, 2) |
| 104 | + end |
| 105 | +end |
| 106 | +function _coordvecs(::GI.MultiPolygonTrait, geom) |
| 107 | + function loop!(vecs, geom) |
| 108 | + i1 = 1 |
| 109 | + for ring in GI.getring(geom) |
| 110 | + i2 = i1 + GI.npoint(ring) - 1 |
| 111 | + range = i1:i2 |
| 112 | + vvecs = map(v -> view(v, range), vecs) |
| 113 | + _geom2coordvecs!(vvecs..., ring) |
| 114 | + map(v -> v[i2 + 1] = NaN, vecs) |
| 115 | + i1 = i2 + 2 |
| 116 | + end |
| 117 | + return vecs |
| 118 | + end |
| 119 | + n = GI.npoint(geom) + GI.nring(geom) |
| 120 | + if GI.is3d(geom) |
| 121 | + vecs = ntuple(_ -> Array{Float64}(undef, n), 3) |
| 122 | + return loop!(vecs, geom) |
| 123 | + else |
| 124 | + vecs = ntuple(_ -> Array{Float64}(undef, n), 2) |
| 125 | + return loop!(vecs, geom) |
| 126 | + end |
| 127 | +end |
| 128 | + |
| 129 | +_coordvec(n) = Array{Float64}(undef, n) |
| 130 | + |
| 131 | +function _geom2coordvecs!(xs, ys, geom) |
| 132 | + for (i, p) in enumerate(GI.getpoint(geom)) |
| 133 | + xs[i] = GI.x(p) |
| 134 | + ys[i] = GI.y(p) |
| 135 | + end |
| 136 | + return xs, ys |
| 137 | +end |
| 138 | +function _geom2coordvecs!(xs, ys, zs, geom) |
| 139 | + for (i, p) in enumerate(GI.getpoint(geom)) |
| 140 | + xs[i] = GI.x(p) |
| 141 | + ys[i] = GI.y(p) |
| 142 | + zs[i] = GI.z(p) |
| 143 | + end |
| 144 | + return xs, ys, zs |
| 145 | +end |
| 146 | + |
| 147 | +end |
0 commit comments