Skip to content

Commit 2ab9320

Browse files
authored
add recipes for features and feature collections (#72)
1 parent 5be9ee5 commit 2ab9320

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

GeoInterfaceRecipes/src/GeoInterfaceRecipes.jl

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ macro enable_geo_plots(typ)
1919
@nospecialize
2020
series_list = RecipesBase.RecipeData[]
2121
RecipesBase.is_explicit(plotattributes, :label) || (plotattributes[:label] = :none)
22-
Base.push!(series_list, RecipesBase.RecipeData(plotattributes, (GeoInterface.geomtrait(geom), geom)))
22+
Base.push!(series_list, RecipesBase.RecipeData(plotattributes, (GeoInterface.trait(geom), geom)))
2323
return series_list
2424
end
2525
function RecipesBase.apply_recipe(plotattributes::Base.AbstractDict{Base.Symbol, Base.Any}, geom::Base.AbstractVector{<:Base.Union{Base.Missing,<:($(esc(typ)))}})
2626
@nospecialize
2727
series_list = RecipesBase.RecipeData[]
2828
RecipesBase.is_explicit(plotattributes, :label) || (plotattributes[:label] = :none)
2929
for g in Base.skipmissing(geom)
30-
Base.push!(series_list, RecipesBase.RecipeData(plotattributes, (GeoInterface.geomtrait(g), g)))
30+
Base.push!(series_list, RecipesBase.RecipeData(plotattributes, (GeoInterface.trait(g), g)))
3131
end
3232
return series_list
3333
end
@@ -51,6 +51,11 @@ end
5151

5252
RecipesBase.@recipe f(::GI.GeometryCollectionTrait, collection) = collect(getgeom(collection))
5353

54+
# Features
55+
RecipesBase.@recipe f(t::GI.FeatureTrait, feature) = GI.geometry(feature)
56+
57+
RecipesBase.@recipe f(t::GI.FeatureCollectionTrait, fc) = collect(GI.getfeature(fc))
58+
5459
# Convert coordinates to the form used by Plots.jl
5560
_coordvecs(::GI.PointTrait, geom) = [tuple(GI.coordinates(geom)...)]
5661
function _coordvecs(::GI.MultiPointTrait, geom)
@@ -126,6 +131,7 @@ function _coordvecs(::GI.MultiPolygonTrait, geom)
126131
end
127132
end
128133

134+
129135
_coordvec(n) = Array{Float64}(undef, n)
130136

131137
function _geom2coordvecs!(xs, ys, geom)

GeoInterfaceRecipes/test/runtests.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,12 @@ struct MyMultiPoint{N} <: MyAbstractGeom{N} end
1414
struct MyMultiCurve{N} <: MyAbstractGeom{N} end
1515
struct MyMultiPolygon{N} <: MyAbstractGeom{N} end
1616
struct MyCollection{N} <: MyAbstractGeom{N} end
17+
struct MyFeature end
18+
struct MyFeatureCollection end
1719

1820
GeoInterfaceRecipes.@enable_geo_plots MyAbstractGeom
21+
GeoInterfaceRecipes.@enable_geo_plots MyFeature
22+
GeoInterfaceRecipes.@enable_geo_plots MyFeatureCollection
1923

2024
GeoInterface.isgeometry(::MyAbstractGeom) = true
2125
GeoInterface.is3d(::GeoInterface.AbstractGeometryTrait, ::MyAbstractGeom{N}) where {N} = N == 3
@@ -54,6 +58,13 @@ GeoInterface.ncoord(::GeoInterface.GeometryCollectionTrait, geom::MyCollection{N
5458
GeoInterface.ngeom(::GeoInterface.GeometryCollectionTrait, geom::MyCollection) = 4
5559
GeoInterface.getgeom(::GeoInterface.GeometryCollectionTrait, geom::MyCollection{N}, i) where {N} = MyMultiPolygon{N}()
5660

61+
GeoInterface.trait(::MyFeature) = FeatureTrait()
62+
GeoInterface.trait(::MyFeatureCollection) = FeatureCollectionTrait()
63+
GeoInterface.getfeature(::GeoInterface.FeatureCollectionTrait, geom::MyFeatureCollection, i) = MyFeature()
64+
GeoInterface.getfeature(::GeoInterface.FeatureCollectionTrait, geom::MyFeatureCollection) = [MyFeature(), MyFeature()]
65+
GeoInterface.geometry(geom::MyFeature) = rand((MyPolygon{2}(), MyMultiPolygon{2}()))
66+
GeoInterface.nfeature(::GeoInterface.FeatureTrait, geom::MyFeature) = 1
67+
5768
@testset "plot" begin
5869
# We just check if they actually run
5970
# 2d
@@ -72,4 +83,6 @@ GeoInterface.getgeom(::GeoInterface.GeometryCollectionTrait, geom::MyCollection{
7283
plot(MyPolygon{3}())
7384
plot(MyMultiPolygon{3}())
7485
plot(MyCollection{3}())
86+
plot(MyFeature())
87+
plot(MyFeatureCollection())
7588
end

0 commit comments

Comments
 (0)