Skip to content
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

return tuple points from GeoInterface methods #193

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
32 changes: 27 additions & 5 deletions src/geo_interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,33 @@ GeoInterface.getgeom(::MultiLineStringTrait, geom::MultiLineString, i) =
getGeometry(geom, i)::LineString
GeoInterface.getgeom(::MultiPolygonTrait, geom::MultiPolygon, i) =
getGeometry(geom, i)::Polygon
GeoInterface.getgeom(
::Union{LineStringTrait,LinearRingTrait},
geom::Union{LineString,LinearRing},
i,
) = getPoint(geom, i)
function GeoInterface.getgeom(::Union{GI.LineStringTrait,GI.LinearRingTrait}, geom::Union{LineString,LinearRing}, i)
refs = Ref{Float64}(), Ref{Float64}(), Ref{Float64}() # 3 Refs is faster than a Vector
rafaqz marked this conversation as resolved.
Show resolved Hide resolved
seq = getCoordSeq(geom::Union{LineString, LinearRing})
return _get_tuple_point(geom, seq, refs, i)
end
function GeoInterface.getgeom(::AbstractGeometryTrait, geom::Union{LineString,LinearRing})
context = get_context(geom)
seq = getCoordSeq(geom, context)
n = getSize(seq, context) # Faster thatn `GI.ngeom(geom)` when we already have `seq`
# Preallocate refse
refs = Ref{Float64}(), Ref{Float64}(), Ref{Float64}()
# Pretetermin if there is a z coordinate
hasz = hasZ(geom, context)
# We specify Unit32 in the generator to avoid unnecesary conversion later
return (_get_tuple_point(geom, seq, refs, i, context, hasz) for i in UInt32(1):UInt32(n))
end

function _get_tuple_point(geom, seq, (x, y, z), i, context=get_context(geom), hasz=hasZ(geom, context))
if hasz
GEOSCoordSeq_getXYZ_r(context, seq, i - UInt32(1), x, y, z)
return x[], y[], Z[]
else
GEOSCoordSeq_getXY_r(context, seq, i - UInt32(1), x, y)
return x[], y[]
end
end

GeoInterface.getgeom(t::AbstractPointTrait, geom::PreparedGeometry) = nothing
function GeoInterface.getgeom(::PolygonTrait, geom::Polygon, i::Int)
if i == 1
Expand Down
8 changes: 5 additions & 3 deletions test/test_geo_interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,10 @@ const LG = LibGEOS
@test GeoInterface.geomtrait(ls) == LineStringTrait()
p = GeoInterface.ngeom(ls) == 4
p = GeoInterface.getgeom(ls, 3)
@test p isa LibGEOS.Point
@test p isa Tuple{Float64,Float64}
@test GeoInterface.coordinates(p) == [9, 2]
@test GeoInterface.testgeometry(ls)
@test GeoInterface.testgeometry(ls)
@test collect(GeoInterface.getpoint(ls)) == [(8, 1), (9, 1), (9, 2), (8, 2)]
@test GeoInterface.is3d(ls) == false
Plots.plot(ls)
Makie.plot(ls)
Expand Down Expand Up @@ -121,10 +122,11 @@ const LG = LibGEOS
@test GeoInterface.geomtrait(lr) == LinearRingTrait()
@test GeoInterface.ngeom(lr) == 5
p = GeoInterface.getgeom(lr, 3)
@test p isa LibGEOS.Point
@test p isa Tuple{Float64,Float64}
@test GeoInterface.coordinates(p) == [9, 2]
@test GeoInterface.is3d(lr) == false
@test GeoInterface.testgeometry(lr)
@test collect(GeoInterface.getpoint(lr)) == [(8, 1), (9, 1), (9, 2), (8, 2), (8, 1)]
# Cannot convert LinearRingTrait to series data for plotting
# Plots.plot(lr)
# Makie.plot(lr)
Expand Down
Loading