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

add GeoInterfaceMakie #71

Merged
merged 16 commits into from
Oct 29, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 2 additions & 0 deletions GeoInterfaceMakie/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/Manifest.toml
/docs/build/
21 changes: 21 additions & 0 deletions GeoInterfaceMakie/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2022 Jan Weidner <[email protected]> and contributors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
24 changes: 24 additions & 0 deletions GeoInterfaceMakie/Project.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name = "GeoInterfaceMakie"
uuid = "0edc0954-3250-4c18-859d-ec71c1660c08"
authors = ["JuliaGeo and contributors"]
version = "0.1.0"

[deps]
GeoInterface = "cf35fbd7-0cd7-5166-be24-54bfbe79505f"
GeometryBasics = "5c1252a2-5f33-56bf-86c9-59e7332b4326"
Makie = "ee78f7c6-11fb-53f2-987a-cfe4a2b5a57a"
MakieCore = "20f20a25-4f0e-4fdf-b5d1-57303727442b"

[compat]
julia = "1.7"
MakieCore = "0.4"
Makie = "0.17.13"
GeometryBasics = "0.4.4"
GeoInterface = "1"

[extras]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
LibGEOS = "a90b1aa1-3769-5649-ba7e-abc5a9d163eb"

[targets]
test = ["Test", "LibGEOS"]
19 changes: 19 additions & 0 deletions GeoInterfaceMakie/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# GeoInterfaceMakie

[![Stable](https://img.shields.io/badge/docs-stable-blue.svg)](https://jw3126.github.io/GeoInterfaceMakie.jl/stable/)
[![Dev](https://img.shields.io/badge/docs-dev-blue.svg)](https://jw3126.github.io/GeoInterfaceMakie.jl/dev/)
[![Build Status](https:/jw3126/GeoInterfaceMakie.jl/actions/workflows/CI.yml/badge.svg?branch=main)](https:/jw3126/GeoInterfaceMakie.jl/actions/workflows/CI.yml?query=branch%3Amain)

Makie support for any geometry that implements [GeoInterface](https:/JuliaGeo/GeoInterface.jl).

# Usage
Add Makie support to a type that implements GeoInterface:
```julia
struct MyGeometry
...
end
# overload GeoInterface methods
...
import GeoInterfaceMakie
GeoInterfaceMakie.@enable MyGeometry
```
101 changes: 101 additions & 0 deletions GeoInterfaceMakie/src/GeoInterfaceMakie.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
module GeoInterfaceMakie

using GeoInterface
import MakieCore as MC
import MakieCore
import GeometryBasics as GB
import GeoInterface as GI
import Makie


function _plottype(geom)
plottype_from_geomtrait(GI.geomtrait(geom))
end
function plottype_from_geomtrait(::Union{GI.LineStringTrait, GI.MultiLineStringTrait})
MC.Lines
end
function plottype_from_geomtrait(::Union{GI.PointTrait, GI.MultiPointTrait})
MC.Scatter
end
function plottype_from_geomtrait(::Union{GI.PolygonTrait,GI.MultiPolygonTrait, GI.LinearRingTrait})
Makie.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}}
jw3126 marked this conversation as resolved.
Show resolved Hide resolved
coords = GI.coordinates(geom)
Pt = pttype(geom)
map(Pt, coords)
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
# TODO PolyhedralSurfaceTrait
# TODO GeometryCollectionTrait

function _convert_arguments(::Type{<:Makie.Poly}, geom)::Tuple
geob = basicsgeom(geom)::Union{GB.Polygon, GB.MultiPolygon}
(geob,)
end
function _convert_arguments(::MC.PointBased, geom)::Tuple
pts = points(geom)
(pts,)
end

function expr_enable(Geom)
quote
# import GeoInterfaceMakie.MakieCore
# import GeoInterfaceMakie.Makie
function $Makie.plottype(geom::$Geom)
jw3126 marked this conversation as resolved.
Show resolved Hide resolved
$_plottype(geom)
end
function $MakieCore.convert_arguments(p::Type{<:$Makie.Poly}, geom::$Geom)
$_convert_arguments(p,geom)
end
function $MakieCore.convert_arguments(p::$MakieCore.PointBased, geom::$Geom)
$_convert_arguments(p,geom)
end
end
end
"""

enable(Geom)

Enable Makie based plotting for a type `Geom` that implements the geometry interface
defined in `GeoInterface`.

# Usage
```julia
struct MyGeometry
...
end
# overload GeoInterface for MyGeometry
...

@enable MyGeometry
```
"""
macro enable(Geom)
esc(expr_enable(Geom))
end

# TODO
# function MC.convert_arguments(::SurfaceLike, geom::Geom)::Tuple
# end
# function MC.convert_arguments(::VolumeLike, geom::Geom)::Tuple
# end

end
27 changes: 27 additions & 0 deletions GeoInterfaceMakie/test/runtests.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import GeoInterfaceMakie
using Test
import LibGEOS

GeoInterfaceMakie.@enable(LibGEOS.AbstractGeometry)

using LibGEOS
import GeoInterface as GI
using Makie
@testset "smoketest 2d" begin
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()
geoms = [
unitsquare,
GI.difference(bigsquare, smallsquare),
boundary(unitsquare),
GI.union(smallsquare, unitsquare),
readgeom("POINT(1 0)"),
readgeom("MULTIPOINT(1 2, 2 3, 3 4)"),
]
for (i,geom) in enumerate(geoms)
Makie.plot!(Axis(fig[i,1], title="$(GI.geomtrait(geom))"), geom)
end
fig
end