Skip to content

Commit

Permalink
passing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaqz committed Feb 24, 2023
1 parent d034836 commit d860b69
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 25 deletions.
16 changes: 10 additions & 6 deletions src/geointerface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -282,16 +282,20 @@ let pointtypes = (wkbPoint, wkbPoint25D, wkbPointM, wkbPointZM),

function GeoInterface.convert(
::Type{T},
type::GeometryTraits,
trait::GeometryTraits,
geom,
) where {T<:IGeometry}
f = get(lookup_method, typeof(type), nothing)
isnothing(f) && error(
"Cannot convert an object of $(typeof(geom)) with the $(typeof(type)) trait (yet). Please report an issue.",
)
return f(GeoInterface.coordinates(geom))
f = get(lookup_method, typeof(trait), nothing)
isnothing(f) && _convert_error(geom, trait)
coords = GeoInterface.coordinates(geom)
@show typeof(coords)
return f(coords)
end

@noinline _convert_error(geom, trait) = error(
"Cannot convert an object of $(typeof(geom)) with the $(typeof(trait)) trait (yet). Please report an issue.",
)

function GeoInterface.geomtrait(
geom::Union{map(T -> AbstractGeometry{T}, pointtypes)...},
)
Expand Down
10 changes: 5 additions & 5 deletions src/ogr/geometry.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# Local convert method converts any GeoInterface geometry
# to the equivalent ArchGDAL geometry
# to the equivalent ArchGDAL geometry.
# `geom` can be points, so this is a hot path that needs to be fast.
function to_gdal(geom)
GeoInterface.isgeometry(geom) || _not_a_geom_error()
trait = GeoInterface.geomtrait(geom)
typ = geointerface_geomtype(trait)
GeoInterface.convert(typ, trait, geom)
return GeoInterface.convert(typ, trait, geom)
end

@noinline _not_a_geom_error() =
Expand Down Expand Up @@ -942,11 +943,11 @@ multisurfaces (multipolygons).
"""
pointonsurface(geom::AbstractGeometry)::IGeometry =
IGeometry(GDAL.ogr_g_pointonsurface(geom))
pointonsurface(g1, g2) = pointonsurface(to_gdal(g1), to_gdal(g2))
pointonsurface(g) = pointonsurface(to_gdal(g))

unsafe_pointonsurface(geom::AbstractGeometry)::Geometry =
Geometry(GDAL.ogr_g_pointonsurface(geom))
unsafe_pointonsurface(g1, g2) = unsafe_pointonsurface(to_gdal(g1), to_gdal(g2))
unsafe_pointonsurface(g) = unsafe_pointonsurface(to_gdal(g1))

"""
difference(g1, g2)
Expand Down Expand Up @@ -1697,7 +1698,6 @@ MULTICURVE or MULTISURFACE in it, by approximating curve geometries.
* `options`: options as a null-terminated list of strings or NULL.
See OGRGeometryFactory::curveToLineString() for valid options.
"""

function lineargeom(
geom::AbstractGeometry,
stepsize::Real = 0;
Expand Down
4 changes: 3 additions & 1 deletion src/spatialref.jl
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,10 @@ function reproject(
)
if GI.isgeometry(geom)
reproject(to_gdal(geom), sourcecrs, targetcrs; kwargs...)
elseif geom isa AbstractArray
elseif geom isa AbstractArray && (length(geom) > 0) && GI.isgeometry(first(geom))
reproject(to_gdal.(geom), Ref(sourcecrs), Ref(targetcrs); kwargs...)
else
throw(ArgumentError("geom is not a GeoInterface compatible geometry"))
end
end

Expand Down
36 changes: 23 additions & 13 deletions test/test_geos_operations.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using Test
import ArchGDAL as AG
import GeoInterface as GI
import GeoInterface
using Extents
const GI = GeoInterface


@testset "test_geos_operations.jl" begin
function equivalent_to_wkt(geom::AG.Geometry, wkt::String)
Expand Down Expand Up @@ -123,13 +125,15 @@ using Extents
@test AG.toWKT(result) == wkt2
end
@test AG.toWKT(f(geom)) == wkt2
wrapped_geom = GI.convert(GI, geom)
@test AG.toWKT(f(wrapped_geom)) == wkt2
if parentmodule(f) != GeoInterface && GI.ngeom(geom) != 0
wrapped_geom = GI.convert(GI, geom)
@test AG.toWKT(f(wrapped_geom)) == wkt2
end
end
end

@testset "Centroid" begin
test_method(AG.centroid, "POINT(10 0)", "POINT (10 0)")
test_method_gi(AG.centroid, "POINT(10 0)", "POINT (10 0)")
test_method(AG.centroid, "LINESTRING(0 0, 10 0)", "POINT (5 0)")
test_method(
AG.centroid,
Expand Down Expand Up @@ -167,9 +171,11 @@ using Extents
@test AG.toWKT(result) == wkt3
end
@test AG.toWKT(f(geom1, geom2)) == wkt3
wrapped_geom1 = GI.convert(GI, geom1)
wrapped_geom2 = GI.convert(GI, geom2)
@test AG.toWKT(f(wrapped_geom1, wrapped_geom2)) == wkt3
if parentmodule(f) != GeoInterface && GI.ngeom(geom1) != 0 && GI.ngeom(geom2) != 0
wrapped_geom1 = GI.convert(GI, geom1)
wrapped_geom2 = GI.convert(GI, geom2)
@test AG.toWKT(f(wrapped_geom1, wrapped_geom2)) == wkt3
end
end
end
end
Expand All @@ -182,9 +188,11 @@ using Extents
AG.fromWKT(wkt1) do geom1
AG.fromWKT(wkt2) do geom2
@test AG.toWKT(f(geom1, geom2)) == wkt3
wrapped_geom1 = GI.convert(GI, geom1)
wrapped_geom2 = GI.convert(GI, geom2)
@test AG.toWKT(f(wrapped_geom1, wrapped_geom2)) == wkt3
if parentmodule(f) != GeoInterface && GI.ngeom(geom1) != 0 && GI.ngeom(geom2) != 0
wrapped_geom1 = GI.convert(GI, geom1)
wrapped_geom2 = GI.convert(GI, geom2)
@test AG.toWKT(f(wrapped_geom1, wrapped_geom2)) == wkt3
end
end
end
end
Expand All @@ -195,9 +203,11 @@ using Extents
# Test GDAL geoms
@test f(geom1, geom2) == result
# Test GeoInterface geoms
wrapped_geom1 = GI.convert(GI, geom1)
wrapped_geom2 = GI.convert(GI, geom2)
@test f(geom1, geom2) == result
if parentmodule(f) != GeoInterface && GI.ngeom(geom1) != 0 && GI.ngeom(geom2) != 0
wrapped_geom1 = GI.convert(GI, geom1)
wrapped_geom2 = GI.convert(GI, geom2)
@test f(geom1, geom2) == result
end
end
end
end
Expand Down

0 comments on commit d860b69

Please sign in to comment.