Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
2 changes: 2 additions & 0 deletions src/GeometryBasics.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module GeometryBasics

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

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

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

export AbstractGeometry, GeometryPrimitive
export Mat, Point, Vec
Expand Down
4 changes: 0 additions & 4 deletions src/basic_types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ Fixed Size Polygon, e.g.
- ...
"""
struct Ngon{Dim,T<:Real,N,Point<:AbstractPoint{Dim,T}} <: AbstractPolygon{Dim,T}

points::SVector{N,Point}
end

Expand Down Expand Up @@ -137,7 +136,6 @@ It applies to infinite dimensions. The structure of this type is designed
to allow embedding in higher-order spaces by parameterizing on `T`.
"""
struct Simplex{Dim,T<:Real,N,Point<:AbstractPoint{Dim,T}} <: Polytope{Dim,T}

points::SVector{N,Point}
end

Expand Down Expand Up @@ -328,7 +326,6 @@ Base.size(mp::MultiPolygon) = size(mp.polygons)

struct MultiLineString{Dim,T<:Real,Element<:LineString{Dim,T},A<:AbstractVector{Element}} <:
AbstractVector{Element}

linestrings::A
end

Expand All @@ -347,7 +344,6 @@ A collection of points
"""
struct MultiPoint{Dim,T<:Real,P<:AbstractPoint{Dim,T},A<:AbstractVector{P}} <:
AbstractVector{P}

points::A
end

Expand Down
79 changes: 79 additions & 0 deletions src/geointerface.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# 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) + 1 # n line segments + 1
function GeoInterface.getgeom(::GeoInterface.LineStringTrait, g::LineString, i::Int)
return GeometryBasics.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)::typeof(g.exterior)
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(::GeoInterface.PolygonTrait, ::Ngon) = 1 # can't have any holes
GeoInterface.getgeom(::GeoInterface.PolygonTrait, g::Ngon, _) = LineString(g.points)

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

mp = MultiPoint([point, point])
GeoInterface.testgeometry(mp)
@test GeoInterface.ngeom(mp) == 2
@test GeoInterface.getgeom(mp, 2) == point
@test GeoInterface.coordinates(mp) == [[2, 3], [2, 3]]

linestring = LineString(Point{2,Int}[(10, 10), (20, 20), (10, 40)])
GeoInterface.testgeometry(linestring)
@test GeoInterface.ngeom(linestring) == 3
@test GeoInterface.getgeom(linestring, 1) == Point(10, 10)
@test GeoInterface.getgeom(linestring, 2) == Point(20, 20)
@test GeoInterface.getgeom(linestring, 3) == Point(10, 40)
@test GeoInterface.coordinates(linestring) == [[10, 10], [20, 20], [10, 40]]

multilinestring = MultiLineString([linestring, linestring])
GeoInterface.testgeometry(multilinestring)
@test GeoInterface.coordinates(multilinestring) ==
[[[10, 10], [20, 20], [10, 40]], [[10, 10], [20, 20], [10, 40]]]

poly = Polygon(rand(Point{2,Float32}, 5), [rand(Point{2,Float32}, 5)])
GeoInterface.testgeometry(poly)
@test length(GeoInterface.coordinates(poly)) == 2
@test length(GeoInterface.coordinates(poly)[1]) == 5

triangle = Triangle(point, point, point)
GeoInterface.testgeometry(triangle)
@test length(GeoInterface.coordinates(triangle)) == 1
@test length(GeoInterface.coordinates(triangle)[1]) == 3

polys = MultiPolygon([poly, poly])
GeoInterface.testgeometry(polys)
@test length(GeoInterface.coordinates(polys)) == 2
@test length(GeoInterface.coordinates(polys)[1]) == 2
@test length(GeoInterface.coordinates(polys)[1][1]) == 5
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"