Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
8 changes: 4 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ on:
push:
branches:
- master
tags: '*'
tags: "*"
pull_request:
branches:
- master
Expand All @@ -15,8 +15,8 @@ jobs:
fail-fast: false
matrix:
version:
- '1.3'
- '1.7'
- "1.6"
- "1"
os:
- ubuntu-latest
- macOS-latest
Expand Down Expand Up @@ -54,7 +54,7 @@ jobs:
- uses: actions/checkout@v2
- uses: julia-actions/setup-julia@v1
with:
version: '1.7'
version: "1.7"
- run: |
julia --project=docs -e '
using Pkg
Expand Down
5 changes: 3 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
name = "GeometryBasics"
uuid = "5c1252a2-5f33-56bf-86c9-59e7332b4326"
authors = ["SimonDanisch <[email protected]>"]
version = "0.4.2"
version = "0.4.3"

[deps]
EarCut_jll = "5ae413db-bbd1-5e63-b57d-d24a61df00f5"
GeoInterface = "cf35fbd7-0cd7-5166-be24-54bfbe79505f"
IterTools = "c8e1da08-722c-5040-9ed9-7db0dc04731e"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
Expand All @@ -16,7 +17,7 @@ IterTools = "1.3.0"
StaticArrays = "0.12, 1.0"
StructArrays = "0.6"
Tables = "0.2, 1"
julia = "1.3"
julia = "1.6"

[extras]
OffsetArrays = "6fe1bfb0-de20-5000-8ca7-80f57d26f881"
Expand Down
3 changes: 2 additions & 1 deletion src/GeometryBasics.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module GeometryBasics

using StaticArrays, Tables, StructArrays, IterTools, LinearAlgebra
using StaticArrays, Tables, StructArrays, IterTools, LinearAlgebra, GeoInterface
using EarCut_jll

using Base: @propagate_inbounds
Expand All @@ -25,6 +25,7 @@ include("lines.jl")
include("boundingboxes.jl")

include("deprecated.jl")
include("geointerface.jl")

export AbstractGeometry, GeometryPrimitive
export Mat, Point, Vec
Expand Down
74 changes: 74 additions & 0 deletions src/geointerface.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# Implementation of trait based interface from https://github.com/JuliaGeo/GeoInterface.jl/

GeoInterface.isgeometry(::Type{<:AbstractGeometry}) = true
GeoInterface.isgeometry(::Type{<:AbstractFace}) = true
GeoInterface.isgeometry(::Type{<:AbstractPoint}) = true
GeoInterface.isgeometry(::Type{<:AbstractVector{<:AbstractGeometry}}) = true
GeoInterface.isgeometry(::Type{<:AbstractVector{<:AbstractPoint}}) = true
GeoInterface.isgeometry(::Type{<:AbstractVector{<:LineString}}) = true
GeoInterface.isgeometry(::Type{<:AbstractVector{<:AbstractPolygon}}) = true
GeoInterface.isgeometry(::Type{<:AbstractVector{<:AbstractFace}}) = true
GeoInterface.isgeometry(::Type{<:Mesh}) = true

GeoInterface.geomtrait(::Point) = GeoInterface.PointTrait()
GeoInterface.geomtrait(::Line) = GeoInterface.LineTrait()
GeoInterface.geomtrait(::LineString) = GeoInterface.LineStringTrait()
GeoInterface.geomtrait(::Polygon) = GeoInterface.PolygonTrait()
GeoInterface.geomtrait(::MultiPoint) = GeoInterface.MultiPointTrait()
GeoInterface.geomtrait(::MultiLineString) = GeoInterface.MultiLineStringTrait()
GeoInterface.geomtrait(::MultiPolygon) = GeoInterface.MultiPolygonTrait()
GeoInterface.geomtrait(::Ngon) = GeoInterface.PolygonTrait()
GeoInterface.geomtrait(::AbstractMesh) = GeoInterface.PolyhedralSurfaceTrait()

GeoInterface.geomtrait(::Simplex{Dim,T,1}) where {Dim,T} = GeoInterface.PointTrait()
GeoInterface.geomtrait(::Simplex{Dim,T,2}) where {Dim,T} = GeoInterface.LineStringTrait()
GeoInterface.geomtrait(::Simplex{Dim,T,3}) where {Dim,T} = GeoInterface.PolygonTrait()

GeoInterface.ncoord(::GeoInterface.PointTrait, g::Point) = length(g)
GeoInterface.getcoord(::GeoInterface.PointTrait, g::Point, i::Int) = g[i]

GeoInterface.ngeom(::GeoInterface.LineTrait, g::Line) = length(g)
GeoInterface.getgeom(::GeoInterface.LineTrait, g::Line, i::Int) = g[i]

GeoInterface.ngeom(::GeoInterface.LineStringTrait, g::LineString) = length(g)
function GeoInterface.getgeom(::GeoInterface.LineStringTrait, g::LineString, i::Int)
return coordinates(g)[i]
end

GeoInterface.ngeom(::GeoInterface.PolygonTrait, g::Polygon) = length(g.interiors) + 1 # +1 for exterior
function GeoInterface.getgeom(::GeoInterface.PolygonTrait, g::Polygon, i::Int)
return i > 1 ? g.interiors[i - 1] : g.exterior
end

GeoInterface.ngeom(::GeoInterface.MultiPointTrait, g::MultiPoint) = length(g)
GeoInterface.getgeom(::GeoInterface.MultiPointTrait, g::MultiPoint, i::Int) = g[i]

function GeoInterface.ngeom(::GeoInterface.MultiLineStringTrait, g::MultiLineString)
return length(g)
end
function GeoInterface.getgeom(::GeoInterface.MultiLineStringTrait, g::MultiLineString,
i::Int)
return g[i]
end

GeoInterface.ngeom(::GeoInterface.MultiPolygonTrait, g::MultiPolygon) = length(g)
GeoInterface.getgeom(::GeoInterface.MultiPolygonTrait, g::MultiPolygon, i::Int) = g[i]

function GeoInterface.ncoord(::GeoInterface.AbstractGeometryTrait,
::Simplex{Dim,T,N,P}) where {Dim,T,N,P}
return Dim
end
function GeoInterface.ncoord(::GeoInterface.AbstractGeometryTrait,
::AbstractGeometry{Dim,T}) where {Dim,T}
return Dim
end
function GeoInterface.ngeom(::GeoInterface.AbstractGeometryTrait,
::Simplex{Dim,T,N,P}) where {Dim,T,N,P}
return N
end
GeoInterface.ngeom(::PolygonTrait, ::Ngon) = 1 # can't have any holes
GeoInterface.getgeom(::PolygonTrait, g::Ngon, _) = LineString(g.points)

GeoInterface.ncoord(::PolyhedralSurfaceTrait, ::Mesh{Dim,T,E,V} where {Dim,T,E,V}) = Dim
GeoInterface.ngeom(::PolyhedralSurfaceTrait, g::AbstractMesh) = length(g)
GeoInterface.getgeom(::PolyhedralSurfaceTrait, g::AbstractMesh, i) = g[i]
27 changes: 27 additions & 0 deletions test/geointerface.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
@testset "Basic types" begin
point = Point(2, 3)
GeoInterface.testgeometry(point)

mp = MultiPoint([point, point])
GeoInterface.testgeometry(mp)

linestring = LineString(Point{2,Int}[(10, 10), (20, 20), (10, 40)])
GeoInterface.testgeometry(linestring)

multilinestring = MultiLineString([linestring, linestring])
GeoInterface.testgeometry(multilinestring)

poly = Polygon(rand(Point{2,Float32}, 5))
GeoInterface.testgeometry(poly)

triangle = Triangle(point, point, point)
GeoInterface.testgeometry(triangle)

polys = MultiPolygon([poly, poly])
GeoInterface.testgeometry(polys)
end

@testset "Mesh" begin
mesh = triangle_mesh(Sphere(Point3f(0), 1))
GeoInterface.testgeometry(mesh)
end
5 changes: 5 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ using Test, Random, StructArrays, Tables, StaticArrays, OffsetArrays
using GeometryBasics
using LinearAlgebra
using GeometryBasics: attributes
using GeoInterface

@testset "GeometryBasics" begin

Expand Down Expand Up @@ -715,4 +716,8 @@ end
include("fixed_arrays.jl")
end

@testset "GeoInterface" begin
include("geointerface.jl")
end

end # testset "GeometryBasics"