diff --git a/src/wrappers.jl b/src/wrappers.jl index 4c15c8d..6a7b6b0 100644 --- a/src/wrappers.jl +++ b/src/wrappers.jl @@ -172,7 +172,9 @@ for (geomtype, trait, childtype, child_trait, length_check, nesting) in ( # But not if geom is already a WrapperGeometry convert(::Type{$geomtype}, ::$trait, geom::$geomtype) = geom - function Base.show(io::IO, ::MIME"text/plain", geom::$geomtype{Z, M, T, E, C}; show_mz::Bool = true, screen_ncols::Int = displaysize(io)[2]) where {Z, M, T, E <: Union{Nothing,Extents.Extent}, C} + function Base.show(io::IO, ::MIME"text/plain", geom::$geomtype{Z, M, T, E, C}; + show_mz::Bool = true, screen_ncols::Int = displaysize(io)[2] + ) where {Z, M, T, E <: Union{Nothing,Extents.Extent}, C} compact = get(io, :compact, false) spacing = compact ? "" : " " show_mz &= !compact @@ -188,7 +190,7 @@ for (geomtype, trait, childtype, child_trait, length_check, nesting) in ( end end - str = "$($geomtype)" + str = string($geomtype) if show_mz str *= "{$Z,$(spacing)$M}" end @@ -234,7 +236,7 @@ for (geomtype, trait, childtype, child_trait, length_check, nesting) in ( str *= "]" else - str *= _nice_geom_str(g, false, compact, screen_ncols - currently_used_space) + str *= _nice_geom_str(parent(geom), false, compact, screen_ncols - currently_used_space) end str *= extent_str @@ -449,7 +451,7 @@ function Base.:(==)(g1::Point, g2::Point) end Base.:(!=)(g1::Point, g2::Point) = !(g1 == g2) -function Base.show(io::IO, ::MIME"text/plain", point::Point{Z, M, T, C}; show_mz::Bool = true) where {Z,M,T,C} +function Base.show(io::IO, ::MIME"text/plain", point::Point{Z, M, T, C}; show_mz::Bool = true, screen_ncols::Int = displaysize(io)[2]) where {Z,M,T,C} print(io, "Point") this_crs = crs(point) @@ -652,9 +654,9 @@ _child_feature_error() = throw(ArgumentError("child objects must be features")) isfeaturecollection(fc::Type{<:FeatureCollection}) = true trait(fc::FeatureCollection) = FeatureCollectionTrait() -nfeature(::FeatureCollectionTrait, fc::FeatureCollection) = +nfeature(t::FeatureCollectionTrait, fc::FeatureCollection) = _parent_is_fc(fc) ? nfeature(t, parent(fc)) : length(parent(fc)) -getfeature(::FeatureCollectionTrait, fc::FeatureCollection) = +getfeature(t::FeatureCollectionTrait, fc::FeatureCollection) = _parent_is_fc(fc) ? getfeature(t, parent(fc)) : parent(fc) getfeature(t::FeatureCollectionTrait, fc::FeatureCollection, i::Integer) = _parent_is_fc(fc) ? getfeature(t, parent(fc), i) : parent(fc)[i] @@ -665,4 +667,4 @@ crs(fc::FeatureCollection) = _parent_is_fc(x) = isfeaturecollection(parent(x)) -end # module \ No newline at end of file +end # module diff --git a/test/test_wrappers.jl b/test/test_wrappers.jl index f8e25e4..26a783f 100644 --- a/test/test_wrappers.jl +++ b/test/test_wrappers.jl @@ -2,18 +2,18 @@ using Test, GeoFormatTypes, Extents import GeoInterface as GI using GeoInterface.Wrappers -# use this to test our string representations for geoms -buf = IOBuffer() -compact_buf = IOContext(buf, :compact => true) - # checks that our string display for geoms in regular/compact form is as expected function test_display(geom, expected_str, expected_compact_str) # checks non-compact string repr - show(buf, MIME"text/plain"(), geom) - @test expected_str == String(take!(buf)) + generated_str = sprint() do io + show(io, MIME"text/plain"(), geom) + end + @test expected_str == generated_str # checks compact string repr - show(compact_buf, MIME"text/plain"(), geom) - @test expected_compact_str == String(take!(buf)) + generated_compact_str = sprint() do io + show(IOContext(io, :compact => true), MIME"text/plain"(), geom) + end + @test expected_compact_str == generated_compact_str end # Point @@ -365,6 +365,34 @@ test_display(fc, "FeatureCollection([Feature(MultiPolygon{false, false}([Polygon vecfc = GI.FeatureCollection([(geometry=(1,2), a=1, b=2)]) @test GI.getfeature(vecfc, 1) == (geometry=(1,2), a=1, b=2) + + +struct MaPointRappa + x::Float64 + y::Float64 +end + +@testset "Wrapped geometry printing" begin + + GI.geomtrait(::MaPointRappa) = GI.PointTrait() + GI.ncoord(::GI.PointTrait, ::MaPointRappa) = 2 + GI.x(::GI.PointTrait, p::MaPointRappa) = p.x + GI.y(::GI.PointTrait, p::MaPointRappa) = p.y + + + test_display(GI.Point(MaPointRappa(1.0, 2.0)), "Point{false, false}((1.0, 2.0))", "Point((1.0,2.0))") + + GI.geomtrait(::Vector{MaPointRappa}) = GI.LineStringTrait() + GI.npoint(::GI.LineStringTrait, v::Vector{MaPointRappa}) = length(v) + GI.getpoint(::GI.LineStringTrait, v::Vector{MaPointRappa}, i::Integer) = v[i] + + test_display( + GI.LineString([MaPointRappa(1.0, 2.0), MaPointRappa(3.0, 4.0)]), + "LineString{false, false}([MaPointRappa(1.0, 2.0), MaPointRappa(3.0, 4.0)])", + "LineString([MaPointRappa(1.0, 2.0),MaPointRappa(3.0, 4.0)])" # FIXME: this should not show the point type! + ) +end + # TODO # # Triangle