-
Notifications
You must be signed in to change notification settings - Fork 17
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
Add package extension on RecipesBase #143
Open
evetion
wants to merge
8
commits into
main
Choose a base branch
from
feat/recipesbaseext
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
ff9a2c3
Add package extension on RecipesBase.
evetion 12eb2f7
Add the actual new files.
evetion 1d19350
Update Project.toml
evetion 3de5510
Dump 1.6.
evetion 8ce5609
Move code to extension, update Project.toml.
evetion 7f84c34
Merge main.
evetion 556b1e9
Don't export plot macros in GeoInterface.
evetion 398afd7
Enable geoplot.
evetion File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,7 +19,6 @@ jobs: | |
matrix: | ||
version: | ||
- "1" | ||
- "1.6" | ||
- "nightly" | ||
os: | ||
- ubuntu-latest | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,186 +1,17 @@ | ||
module GeoInterfaceRecipes | ||
|
||
using GeoInterface, RecipesBase | ||
import GeoInterface: @enable_plots | ||
|
||
const GI = GeoInterface | ||
# This is now an empty package, but loading both GeoInterface and RecipesBase | ||
# will trigger the extension which loads the recipes | ||
|
||
export @enable_geo_plots | ||
|
||
RecipesBase.@recipe function f(t::Union{GI.PointTrait,GI.MultiPointTrait}, geom) | ||
seriestype --> :scatter | ||
_coordvecs(t, geom) | ||
end | ||
|
||
RecipesBase.@recipe function f(t::Union{GI.AbstractLineStringTrait,GI.MultiLineStringTrait}, geom) | ||
seriestype --> :path | ||
_coordvecs(t, geom) | ||
end | ||
|
||
RecipesBase.@recipe function f(t::Union{GI.PolygonTrait,GI.MultiPolygonTrait,GI.LinearRingTrait}, geom) | ||
seriestype --> :shape | ||
_coordvecs(t, geom) | ||
end | ||
|
||
RecipesBase.@recipe f(::GI.GeometryCollectionTrait, collection) = collect(getgeom(collection)) | ||
|
||
# Features | ||
RecipesBase.@recipe f(t::GI.FeatureTrait, feature) = GI.geometry(feature) | ||
|
||
RecipesBase.@recipe f(t::GI.FeatureCollectionTrait, fc) = collect(GI.getfeature(fc)) | ||
|
||
# Convert coordinates to the form used by Plots.jl | ||
_coordvecs(::GI.PointTrait, geom) = [tuple(GI.coordinates(geom)...)] | ||
function _coordvecs(::GI.MultiPointTrait, geom) | ||
n = GI.npoint(geom) | ||
# We use a fixed conditional instead of dispatch, | ||
# as `is3d` may not be known at compile-time | ||
if GI.is3d(geom) | ||
_geom2coordvecs!(ntuple(_ -> Array{Float64}(undef, n), 3)..., geom) | ||
else | ||
_geom2coordvecs!(ntuple(_ -> Array{Float64}(undef, n), 2)..., geom) | ||
end | ||
end | ||
function _coordvecs(::GI.AbstractLineStringTrait, geom) | ||
n = GI.npoint(geom) | ||
if GI.is3d(geom) | ||
vecs = ntuple(_ -> Array{Float64}(undef, n), 3) | ||
return _geom2coordvecs!(vecs..., geom) | ||
else | ||
vecs = ntuple(_ -> Array{Float64}(undef, n), 2) | ||
return _geom2coordvecs!(vecs..., geom) | ||
end | ||
end | ||
function _coordvecs(::GI.MultiLineStringTrait, geom) | ||
function loop!(vecs, geom) | ||
i1 = 1 | ||
for line in GI.getgeom(geom) | ||
i2 = i1 + GI.npoint(line) - 1 | ||
vvecs = map(v -> view(v, i1:i2), vecs) | ||
_geom2coordvecs!(vvecs..., line) | ||
map(v -> v[i2 + 1] = NaN, vecs) | ||
i1 = i2 + 2 | ||
end | ||
return vecs | ||
end | ||
n = GI.npoint(geom) + GI.ngeom(geom) | ||
if GI.is3d(geom) | ||
vecs = ntuple(_ -> Array{Float64}(undef, n), 3) | ||
return loop!(vecs, geom) | ||
else | ||
vecs = ntuple(_ -> Array{Float64}(undef, n), 2) | ||
return loop!(vecs, geom) | ||
end | ||
end | ||
function _coordvecs(::GI.LinearRingTrait, geom) | ||
points = GI.getpoint(geom) | ||
if GI.is3d(geom) | ||
return getcoord.(points, 1), getcoord.(points, 2), getcoord.(points, 3) | ||
else | ||
return getcoord.(points, 1), getcoord.(points, 2) | ||
end | ||
end | ||
function _coordvecs(::GI.PolygonTrait, geom) | ||
ring = first(GI.getgeom(geom)) # currently doesn't plot holes | ||
points = GI.getpoint(ring) | ||
if GI.is3d(geom) | ||
return getcoord.(points, 1), getcoord.(points, 2), getcoord.(points, 3) | ||
else | ||
return getcoord.(points, 1), getcoord.(points, 2) | ||
end | ||
end | ||
function _coordvecs(::GI.MultiPolygonTrait, geom) | ||
function loop!(vecs, geom) | ||
i1 = 1 | ||
for ring in GI.getring(geom) | ||
i2 = i1 + GI.npoint(ring) - 1 | ||
range = i1:i2 | ||
vvecs = map(v -> view(v, range), vecs) | ||
_geom2coordvecs!(vvecs..., ring) | ||
map(v -> v[i2 + 1] = NaN, vecs) | ||
i1 = i2 + 2 | ||
end | ||
return vecs | ||
end | ||
n = GI.npoint(geom) + GI.nring(geom) | ||
if GI.is3d(geom) | ||
vecs = ntuple(_ -> Array{Float64}(undef, n), 3) | ||
return loop!(vecs, geom) | ||
else | ||
vecs = ntuple(_ -> Array{Float64}(undef, n), 2) | ||
return loop!(vecs, geom) | ||
end | ||
end | ||
# Backwards compatible | ||
var"@enable" = var"@enable_plots" | ||
var"@enable_geo_plots" = var"@enable_plots" | ||
|
||
|
||
_coordvec(n) = Array{Float64}(undef, n) | ||
|
||
function _geom2coordvecs!(xs, ys, geom) | ||
for (i, p) in enumerate(GI.getpoint(geom)) | ||
xs[i] = GI.x(p) | ||
ys[i] = GI.y(p) | ||
end | ||
return xs, ys | ||
end | ||
function _geom2coordvecs!(xs, ys, zs, geom) | ||
for (i, p) in enumerate(GI.getpoint(geom)) | ||
xs[i] = GI.x(p) | ||
ys[i] = GI.y(p) | ||
zs[i] = GI.z(p) | ||
end | ||
return xs, ys, zs | ||
end | ||
|
||
function expr_enable(typ) | ||
quote | ||
# We recreate the apply_recipe functions manually here | ||
# as nesting the @recipe macro doesn't work. | ||
function RecipesBase.apply_recipe(plotattributes::Base.AbstractDict{Base.Symbol, Base.Any}, geom::$(esc(typ))) | ||
@nospecialize | ||
series_list = RecipesBase.RecipeData[] | ||
RecipesBase.is_explicit(plotattributes, :label) || (plotattributes[:label] = :none) | ||
Base.push!(series_list, RecipesBase.RecipeData(plotattributes, (GeoInterface.trait(geom), geom))) | ||
return series_list | ||
end | ||
function RecipesBase.apply_recipe(plotattributes::Base.AbstractDict{Base.Symbol, Base.Any}, geom::Base.AbstractVector{<:Base.Union{Base.Missing,<:($(esc(typ)))}}) | ||
@nospecialize | ||
series_list = RecipesBase.RecipeData[] | ||
RecipesBase.is_explicit(plotattributes, :label) || (plotattributes[:label] = :none) | ||
for g in Base.skipmissing(geom) | ||
Base.push!(series_list, RecipesBase.RecipeData(plotattributes, (GeoInterface.trait(g), g))) | ||
end | ||
return series_list | ||
end | ||
end | ||
end | ||
|
||
""" | ||
GeoInterfaceRecipes.@enable(GeometryType) | ||
|
||
Macro to add plot recipes to a geometry type. | ||
|
||
# Usage | ||
|
||
```julia | ||
struct MyGeometry | ||
... | ||
end | ||
# overload GeoInterface for MyGeometry | ||
... | ||
|
||
# Enable Plots.jl plotting | ||
GeoInterfaceRecipes.@enable_geo_plots MyGeometry | ||
``` | ||
""" | ||
macro enable(typ) | ||
expr_enable(typ) | ||
end | ||
|
||
# Compat | ||
macro enable_geo_plots(typ) | ||
expr_enable(typ) | ||
end | ||
|
||
# Enable Plots.jl for GeoInterface wrappers | ||
@enable GeoInterface.Wrappers.WrapperGeometry | ||
export @enable_plots | ||
export @enable | ||
export @enable_geo_plots | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.