Skip to content

Commit

Permalink
Extended test coverage. Fixed compat in Project.toml.
Browse files Browse the repository at this point in the history
  • Loading branch information
evetion committed May 24, 2022
1 parent 96f9731 commit e2892d0
Show file tree
Hide file tree
Showing 5 changed files with 162 additions and 11 deletions.
3 changes: 3 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@ Tables = "bd369af6-aec1-5ad0-b16a-f7cc5008161c"
CEnum = "0.4"
ColorTypes = "0.10, 0.11"
DiskArrays = "0.3"
Extents = "0.1"
GDAL = "1.3"
GeoFormatTypes = "0.3, 0.4"
GeoInterface = "1"
GeoInterfaceRecipes = "1.0"
ImageCore = "0.8, 0.9"
Tables = "1"
julia = "1.6"
6 changes: 3 additions & 3 deletions src/geointerface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -281,15 +281,15 @@ let pointtypes = (wkbPoint, wkbPoint25D, wkbPointM, wkbPointZM),
)
end

function GeoInterface.asbinary(::GeometryTraits, a::AbstractGeometry)
function GeoInterface.asbinary(::GeometryTraits, geom::AbstractGeometry)
return toWKB(geom)
end

function GeoInterface.astext(::GeometryTraits, a::AbstractGeometry)
function GeoInterface.astext(::GeometryTraits, geom::AbstractGeometry)
return toWKT(geom)
end

function Base.convert(::Type{T}, geom::X) where {T<:IGeometry,X}
function Base.convert(::Type{T}, geom) where {T<:IGeometry}
return Base.convert(T, GeoInterface.geomtrait(geom), geom)
end
function Base.convert(
Expand Down
1 change: 1 addition & 0 deletions test/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
DiskArrays = "3c3547ce-8d99-4f5e-a174-61eb10b00ae3"
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
Downloads = "f43a241f-c20a-4ad4-852c-f6b1247861c6"
Extents = "411431e0-e8b7-467b-b5e0-f676ba4f2910"
GDAL = "add2ef01-049f-52c4-9ee2-e494f65e021a"
GeoFormatTypes = "68eda718-8dee-11e9-39e7-89f7f65f511f"
GeoInterface = "cf35fbd7-0cd7-5166-be24-54bfbe79505f"
Expand Down
19 changes: 19 additions & 0 deletions test/test_geometry.jl
Original file line number Diff line number Diff line change
Expand Up @@ -827,4 +827,23 @@ import GeoFormatTypes as GFT
@test !GI.is3d(point)
end
end

@testset "GeoInterface conversion" begin
struct MyPoint end
struct MyLine end

GI.isgeometry(::MyPoint) = true
GI.geomtrait(::MyPoint) = GI.PointTrait()
GI.ncoord(::GI.PointTrait, geom::MyPoint) = 2
GI.getcoord(::GI.PointTrait, geom::MyPoint, i) = [1, 2][i]

GI.isgeometry(::MyLine) = true
GI.geomtrait(::MyLine) = GI.LineStringTrait()
GI.ngeom(::GI.LineStringTrait, geom::MyLine) = 2
GI.getgeom(::GI.LineStringTrait, geom::MyLine, i) = MyPoint()

geom = MyLine()
ag_geom = convert(Type{AG.IGeometry}, geom)
GI.coordinates(ag_geom) == [[1, 2], [1, 2]]
end
end
144 changes: 136 additions & 8 deletions test/test_geos_operations.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Test
import ArchGDAL as AG
using Extents

@testset "test_geos_operations.jl" begin
function equivalent_to_wkt(geom::AG.Geometry, wkt::String)
Expand Down Expand Up @@ -166,6 +167,129 @@ import ArchGDAL as AG
end
end
end
function test_method_simple(
f::Function,
wkt1::AbstractString,
wkt2::AbstractString,
wkt3::AbstractString,
)
AG.fromWKT(wkt1) do geom1
AG.fromWKT(wkt2) do geom2
@test AG.toWKT(f(geom1, geom2)) == wkt3
end
end
end

function test_predicate(f::Function, wkt1, wkt2, result::Bool)
AG.fromWKT(wkt1) do geom1
AG.fromWKT(wkt2) do geom2
@test f(geom1, geom2) == result
end
end
end

@testset "GeoInterface" begin
test_predicate(
GI.intersects,
"POLYGON((1 1,1 5,5 5,5 1,1 1))",
"POINT(2 2)",
true,
)
test_predicate(
GI.equals,
"POLYGON((1 1,1 5,5 5,5 1,1 1))",
"POINT(2 2)",
false,
)
test_predicate(
GI.disjoint,
"POLYGON((1 1,1 5,5 5,5 1,1 1))",
"POINT(2 2)",
false,
)
test_predicate(
GI.touches,
"POLYGON((1 1,1 5,5 5,5 1,1 1))",
"POINT(1 1)",
true,
)
test_predicate(
GI.crosses,
"POLYGON((1 1,1 5,5 5,5 1,1 1))",
"POINT(2 2)",
false,
)
test_predicate(
GI.within,
"POLYGON((1 1,1 5,5 5,5 1,1 1))",
"POINT(2 2)",
false,
)
test_predicate(
GI.contains,
"POLYGON((1 1,1 5,5 5,5 1,1 1))",
"POINT(2 2)",
true,
)
test_predicate(
GI.overlaps,
"POLYGON((1 1,1 5,5 5,5 1,1 1))",
"POINT(2 2)",
false,
)
test_method_simple(
GI.union,
"POLYGON((1 1,1 5,5 5,5 1,1 1))",
"POINT(2 2)",
"POLYGON ((1 5,5 5,5 1,1 1,1 5))",
)
test_method_simple(
GI.intersection,
"POLYGON((1 1,1 5,5 5,5 1,1 1))",
"POINT(2 2)",
"POINT (2 2)",
)
test_method_simple(
GI.difference,
"POLYGON((1 1,1 5,5 5,5 1,1 1))",
"POINT(2 2)",
"POLYGON ((1 5,5 5,5 1,1 1,1 5))",
)
test_method_simple(
GI.symdifference,
"POLYGON((1 1,1 5,5 5,5 1,1 1))",
"POINT(2 2)",
"POLYGON ((1 5,5 5,5 1,1 1,1 5))",
)
@test GI.distance(
AG.fromWKT("POLYGON((1 1,1 5,5 5,5 1,1 1))"),
AG.fromWKT("POINT(2 2)"),
) == 0.0

@test GI.length(AG.fromWKT("LINESTRING(0 0, 10 0)")) == 10

@test GI.area(AG.fromWKT("POLYGON((0 0, 10 0, 10 10, 0 10, 0 0))")) ==
100

@test GI.coordinates(
GI.buffer(AG.fromWKT("POLYGON((0 0, 10 0, 10 10, 0 10, 0 0))"), 1),
)[1][1] == [-1.0, 0.0]

@test AG.toWKT(
GI.convexhull(AG.fromWKT("POLYGON((0 0, 10 0, 10 10, 0 10, 0 0))")),
) == "POLYGON ((0 0,0 10,10 10,10 0,0 0))"

@test GI.extent(AG.fromWKT("POLYGON((0 0, 10 0, 10 10, 0 10, 0 0))")) ==
Extent(X = (0.0, 10.0), Y = (0.0, 10.0), Z = (0.0, 0.0))

@test GI.asbinary(
AG.fromWKT("POLYGON((0 0, 10 0, 10 10, 0 10, 0 0))"),
)[1:10] ==
UInt8[0x01, 0x03, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x05]

@test GI.astext(AG.fromWKT("POLYGON((0 0, 10 0, 10 10, 0 10, 0 0))")) ==
"POLYGON ((0 0,10 0,10 10,0 10,0 0))"
end

@testset "Intersection" begin
test_method(
Expand All @@ -192,14 +316,12 @@ import ArchGDAL as AG
"POLYGON((0 1,0 2,10 2,10 1,0 1))",
"GEOMETRYCOLLECTION (POLYGON ((1 2,1 1,0.5 1.0,1 2)),POLYGON ((9.5 1.0,2 1,2 2,9 2,9.5 1.0)),LINESTRING (1 2,2 2),LINESTRING (2 1,1 1))",
)
end

function test_predicate(f::Function, wkt1, wkt2, result::Bool)
AG.fromWKT(wkt1) do geom1
AG.fromWKT(wkt2) do geom2
@test f(geom1, geom2) == result
end
end
test_method_simple(
GI.intersection,
"MULTIPOLYGON(((0 0,5 10,10 0,0 0),(1 1,1 2,2 2,2 1,1 1),(100 100,100 102,102 102,102 100,100 100)))",
"POLYGON((0 1,0 2,10 2,10 1,0 1))",
"GEOMETRYCOLLECTION (POLYGON ((1 2,1 1,0.5 1.0,1 2)),POLYGON ((9.5 1.0,2 1,2 2,9 2,9.5 1.0)),LINESTRING (1 2,2 2),LINESTRING (2 1,1 1))",
)
end

@testset "Intersects" begin
Expand Down Expand Up @@ -228,6 +350,12 @@ import ArchGDAL as AG
"MULTIPOLYGON(((0 0,0 10,10 10,10 0,0 0)))",
true,
)
test_predicate(
GI.intersects,
"POLYGON((1 1,1 2,2 2,2 1,1 1))",
"MULTIPOLYGON(((0 0,0 10,10 10,10 0,0 0)))",
true,
)
end

@testset "Within" begin
Expand Down

0 comments on commit e2892d0

Please sign in to comment.