Skip to content

Move GeoInterface.jl to an extension #263

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

Merged
merged 3 commits into from
Jul 7, 2025
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
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
fail-fast: false
matrix:
version:
- '1.6'
- '1.10'
- '1'
os:
- ubuntu-latest
Expand Down
12 changes: 9 additions & 3 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,18 @@ version = "0.5.9"
[deps]
EarCut_jll = "5ae413db-bbd1-5e63-b57d-d24a61df00f5"
Extents = "411431e0-e8b7-467b-b5e0-f676ba4f2910"
GeoInterface = "cf35fbd7-0cd7-5166-be24-54bfbe79505f"
IterTools = "c8e1da08-722c-5040-9ed9-7db0dc04731e"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
PrecompileTools = "aea7be01-6a6a-4083-8856-8a6e6704d82a"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"

[weakdeps]
GeoInterface = "cf35fbd7-0cd7-5166-be24-54bfbe79505f"

[extensions]
GeometryBasicsGeoInterfaceExt = "GeoInterface"

[compat]
Aqua = "0.8"
EarCut_jll = "2"
Expand All @@ -26,14 +31,15 @@ PrecompileTools = "1.0"
Random = "<0.0.1,1"
StaticArrays = "0.6, 1"
Test = "<0.0.1,1"
julia = "1.6"
julia = "1.10"

[extras]
Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595"
GeoJSON = "61d90e0f-e114-555e-ac52-39dfb47a3ef9"
GeoInterface = "cf35fbd7-0cd7-5166-be24-54bfbe79505f"
OffsetArrays = "6fe1bfb0-de20-5000-8ca7-80f57d26f881"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["Aqua", "GeoJSON", "Test", "Random", "OffsetArrays"]
test = ["Aqua", "GeoInterface", "GeoJSON", "OffsetArrays", "Random", "Test"]
27 changes: 13 additions & 14 deletions src/geointerface.jl → ext/GeometryBasicsGeoInterfaceExt.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
module GeometryBasicsGeoInterfaceExt

using GeoInterface, GeometryBasics

import GeometryBasics: geointerface_geomtype
using GeometryBasics: Ngon

# Implementation of trait based interface from https://github.com/JuliaGeo/GeoInterface.jl/

GeoInterface.isgeometry(::Type{<:AbstractGeometry}) = true
Expand Down Expand Up @@ -104,12 +111,6 @@ function GeoInterface.convert(::Type{Point}, type::PointTrait, geom)
return Point{2,T}(x, y)
end
end

# without a function barrier you get a lot of allocations from runtime types
function _collect_with_type(::Type{PT}, geom) where {PT <: Point{2}}
return [PT(GeoInterface.x(p), GeoInterface.y(p)) for p in getgeom(geom)]
end

function GeoInterface.convert(::Type{LineString}, type::LineStringTrait, geom)
g1 = getgeom(geom, 1)
x, y = GeoInterface.x(g1), GeoInterface.y(g1)
Expand All @@ -122,7 +123,6 @@ function GeoInterface.convert(::Type{LineString}, type::LineStringTrait, geom)
return LineString(_collect_with_type(Point{2, T}, geom))
end
end

function GeoInterface.convert(::Type{Polygon}, type::PolygonTrait, geom)
t = LineStringTrait()
exterior = GeoInterface.convert(LineString, t, GeoInterface.getexterior(geom))
Expand All @@ -133,7 +133,6 @@ function GeoInterface.convert(::Type{Polygon}, type::PolygonTrait, geom)
return Polygon(exterior, interiors)
end
end

function GeoInterface.convert(::Type{MultiPoint}, type::MultiPointTrait, geom)
g1 = getgeom(geom, 1)
x, y = GeoInterface.x(g1), GeoInterface.y(g1)
Expand All @@ -146,18 +145,18 @@ function GeoInterface.convert(::Type{MultiPoint}, type::MultiPointTrait, geom)
return MultiPoint([Point{2,T}(GeoInterface.x(p), GeoInterface.y(p)) for p in getgeom(geom)])
end
end

function GeoInterface.convert(::Type{MultiLineString}, type::MultiLineStringTrait, geom)
t = LineStringTrait()
return MultiLineString(map(l -> GeoInterface.convert(LineString, t, l), getgeom(geom)))
end

function GeoInterface.convert(::Type{MultiPolygon}, type::MultiPolygonTrait, geom)
t = PolygonTrait()
return MultiPolygon(map(poly -> GeoInterface.convert(Polygon, t, poly), getgeom(geom)))
end

function Extents.extent(rect::Rect2)
(xmin, ymin), (xmax, ymax) = extrema(rect)
return Extents.Extent(X=(xmin, xmax), Y=(ymin, ymax))
end
# without a function barrier you get a lot of allocations from runtime types
function _collect_with_type(::Type{PT}, geom) where {PT <: Point{2}}
return [PT(GeoInterface.x(p), GeoInterface.y(p)) for p in getgeom(geom)]
end

end
7 changes: 4 additions & 3 deletions src/GeometryBasics.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
module GeometryBasics

using IterTools, LinearAlgebra, StaticArrays
using GeoInterface
import Extents
using EarCut_jll
import Base: *
Expand All @@ -27,8 +26,6 @@ include("triangulation.jl")
include("lines.jl")
include("boundingboxes.jl")

include("geointerface.jl")

export AbstractGeometry, GeometryPrimitive
export Mat, Point, Vec
export LineFace, Polytope, Line, NgonFace, convert_simplex
Expand Down Expand Up @@ -71,4 +68,8 @@ if Base.VERSION >= v"1.8"
include("precompiles.jl")
end

# Needed for GeometryBasicsGeoInterfaceExt.
# In future this can go away as can use Module dispatch.
function geointerface_geomtype end

end # module
9 changes: 9 additions & 0 deletions src/primitives/rectangles.jl
Original file line number Diff line number Diff line change
Expand Up @@ -582,3 +582,12 @@ function faces(::Rect3)
(3, 4, 8, 7), (1, 3, 7, 5), (6, 8, 4, 2)
]
end

function Extents.extent(rect::Rect2)
(xmin, ymin), (xmax, ymax) = extrema(rect)
return Extents.Extent(X=(xmin, xmax), Y=(ymin, ymax))
end
function Extents.extent(rect::Rect3)
(xmin, ymin, zmin), (xmax, ymax, zmax) = extrema(rect)
return Extents.Extent(X=(xmin, xmax), Y=(ymin, ymax), Z=(zmin, zmax))
end
7 changes: 6 additions & 1 deletion test/geointerface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,9 @@ end
ext = extent(rect)
@test ext.X == (0.0f0, 1.0f0)
@test ext.Y == (0.0f0, 1.0f0)
end
rect = Rect3f(Vec3f(0), Vec3f(1.0))
ext = extent(rect)
@test ext.X == (0.0f0, 1.0f0)
@test ext.Y == (0.0f0, 1.0f0)
@test ext.Z == (0.0f0, 1.0f0)
end