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 all commits
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
62 changes: 62 additions & 0 deletions .github/workflows/CIGeoInterfaceMakie.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
name: GeoInterfaceMakie CI
on:
pull_request:
paths-ignore:
- 'docs/**'
- '*.md'
branches:
- main
- breaking-release
push:
paths-ignore:
- 'docs/**'
- '*.md'
branches:
- main
- breaking-release
tags: '*'

concurrency:
group: makie-${{ github.ref }}
cancel-in-progress: true

jobs:
test:
name: GeoInterfaceMakie Julia ${{ matrix.version }} - ${{ matrix.os }} - ${{ matrix.arch }} - ${{ github.event_name }}
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
version:
- '1.6'
- '1' # automatically expands to the latest stable 1.x release of Julia
os:
- ubuntu-latest
arch:
- x64
steps:
- name: Checkout
uses: actions/checkout@v2
- uses: julia-actions/setup-julia@v1
with:
version: ${{ matrix.version }}
arch: ${{ matrix.arch }}
- uses: julia-actions/cache@v1
- name: Install Julia dependencies
shell: julia --project=monorepo {0}
run: |
jw3126 marked this conversation as resolved.
Show resolved Hide resolved
using Pkg;
# dev mono repo versions
pkg"dev . ./GeoInterfaceMakie"
- name: Run the tests
continue-on-error: true
run: >
julia --color=yes --project=monorepo -e 'using Pkg; Pkg.test("GeoInterfaceMakie", coverage=true)'
&& echo "TESTS_SUCCESSFUL=true" >> $GITHUB_ENV
- name: Exit if tests failed
if: ${{ env.TESTS_SUCCESSFUL != 'true' }}
run: exit 1
- uses: julia-actions/julia-processcoverage@v1
- uses: codecov/codecov-action@v1
with:
file: lcov.info
4 changes: 2 additions & 2 deletions .github/workflows/recipes.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ on:
- 'docs/**'
- '*.md'
branches:
- master
- main
- breaking-release
push:
paths-ignore:
- 'docs/**'
- '*.md'
branches:
- master
- main
- breaking-release
tags: '*'

Expand Down
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.
23 changes: 23 additions & 0 deletions GeoInterfaceMakie/Project.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
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"
MakieCore = "20f20a25-4f0e-4fdf-b5d1-57303727442b"

[compat]
GeoInterface = "1"
GeometryBasics = "0.4.4"
MakieCore = "0.5.1"
julia = "1.6"

[extras]
LibGEOS = "a90b1aa1-3769-5649-ba7e-abc5a9d163eb"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Makie = "ee78f7c6-11fb-53f2-987a-cfe4a2b5a57a"

[targets]
test = ["Test", "LibGEOS", "Makie"]
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
```
114 changes: 114 additions & 0 deletions GeoInterfaceMakie/src/GeoInterfaceMakie.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
module GeoInterfaceMakie

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


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})
MC.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
Pt = pttype(geom)
out = Pt[]
points!(out, GI.geomtrait(geom), geom)
end
@noinline function points!(out, ::GI.AbstractTrait, geom)
for pt in GI.getpoint(geom)
push!(out, _convert(eltype(out), pt))
end
out
end
@noinline function points!(out, ::GI.PointTrait, pt)
push!(out, _convert(eltype(out), pt))
out
end
function _convert(::Type{GB.Point2f}, pt)
x,y = GI.getcoord(pt)
GB.Point2f(x,y)
end
function _convert(::Type{GB.Point3f}, pt)
x,y,z = GI.getcoord(pt)
GB.Point3f(x,y,z)
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

function _convert_arguments(::Type{<:MC.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
function $MC.plottype(geom::$Geom)
$_plottype(geom)
end
function $MC.convert_arguments(p::Type{<:$MC.Poly}, geom::$Geom)
$_convert_arguments(p,geom)
end
function $MC.convert_arguments(p::$MC.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
# Features and Feature collections
# https:/JuliaGeo/GeoInterface.jl/pull/72#issue-1406325596

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

GeoInterfaceMakie.@enable(LibGEOS.AbstractGeometry)

@testset "points" begin
points = GeoInterfaceMakie.points
pt = LibGEOS.Point(1,2,3)
@test points(pt) == [Point3f(1,2,3)]

unitsquare = readgeom("POLYGON((0 0, 0 1, 1 1, 1 0, 0 0))")
@test points(unitsquare) == [
Point2f(0.0, 0.0),
Point2f(0.0, 1.0),
Point2f(1.0, 1.0),
Point2f(1.0, 0.0),
Point2f(0.0, 0.0),
]
end

@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