Repository: JuliaGeo/LibGEOS.jl
Pull request #150 (2022-12-15 00:04:43)

improve show
```julia
julia> Point(1,2,3) # before
Point(Ptr{Nothing} @0x000000000511a220, LibGEOS.GEOSContext(Ptr{Nothing} @0x0000000001e06780))

julia> Point(1,2,3) # after
readgeom("POINT Z (1 2 3)")::Point

julia> Polygon([[[1.0,2], [3.0,4], [5.0,6], [1.0,2]]]) # before
Polygon(Ptr{Nothing} @0x0000000002add7e0, LibGEOS.GEOSContext(Ptr{Nothing} @0x0000000001e06780))

julia> Polygon([[[1.0,2], [3.0,4], [5.0,6], [1.0,2]]]) # after
readgeom("POLYGON ((1 2, 3 4, 5 6, 1 2))")::Polygon
```

evetion: Thanks, this is an improvement! 

Could you add the use of the compact keyword? As in https://github.com/yeesian/ArchGDAL.jl/blob/master/src/base/display.jl#L254. This will help with performance and displaying in tables.
visr: Indeed a great improvement. I assume that you went for `readgeom("POINT Z (1 2 3)")::Point` to make it easy to recreate the geometry. It does feel a bit verbose though. What about just showing the WKT, `POINT Z (1 2 3)` It looks like shapely does this as well: https://shapely.readthedocs.io/en/stable/manual.html#Point.

And (this could be a separate PR), but it would be cool to support other mime types, like `application/geo+json`, which jupyter supports: https://docs.jupyter.org/en/latest/reference/mimetype.html.

jw3126: > Indeed a great improvement. I assume that you went for readgeom("POINT Z (1 2 3)")::Point to make it easy to recreate the geometry. It does feel a bit verbose though. What about just showing the WKT, POINT Z (1 2 3) It looks like shapely does this as well: https://shapely.readthedocs.io/en/stable/manual.html#Point.

Yes I had the same ideas, it is a tradeoff and I had a slight preference for convenient recreation, but I can change it if you feel strongly about this.
jw3126: @evetion done
@visr I changed according to your suggestion
jw3126: ```julia
julia> p = Polygon([[[0.0, 0.0] for _ in 1:10000]]); show(IOContext(stdout, :compact=>true),p)
POLYGON ((0 0, 0 0, 0 0, 0 0, 0 0, 0 0,  ... 0 0, 0 0, 0 0, 0 0, 0 0, 0 0))
```
evetion: I would opt for never calling `writegeom`/showing WKT at all when :compact is set to true, as in the ArchGDAL code (it only displays the geometry type then).

Patch:
diff --git a/src/geos_types.jl b/src/geos_types.jl
index ed24b85..2988045 100644
--- a/src/geos_types.jl
+++ b/src/geos_types.jl
@@ -1,5 +1,15 @@
 abstract type AbstractGeometry end
 
+function Base.show(io::IO, geo::AbstractGeometry)
+    compact = get(io, :compact, false)
+    if compact
+        print(io, typeof(geo), "(...)")
+    else
+        s = writegeom(geo)
+        print(io, s)
+    end
+end
+
 """
 
     get_context(geom::AbstractGeometry)::GEOSContext


Test patch:
diff --git a/test/test_misc.jl b/test/test_misc.jl
index 9c56950..282da38 100644
--- a/test/test_misc.jl
+++ b/test/test_misc.jl
@@ -36,3 +36,25 @@ end
     @test_throws ArgumentError LibGEOS.intersects(p1, q2)
     @test_throws ArgumentError LibGEOS.intersects(p2, q1)
 end
+
+@testset "show it like you build it" begin
+    for geo in [
+        readgeom("POINT(0 0)")
+        readgeom("MULTIPOINT(0 0, 5 0, 10 0)")
+        readgeom("LINESTRING (130 240, 650 240)")
+        readgeom("POLYGON EMPTY")
+        readgeom("POLYGON ((10 10, 20 40, 90 90, 90 10, 10 10))")
+        readgeom("MULTILINESTRING ((5 0, 10 0), (0 0, 5 0))")
+        readgeom("GEOMETRYCOLLECTION (LINESTRING (1 2, 2 2), LINESTRING (2 1, 1 1), POLYGON ((0.5 1, 1 2, 1 1, 0.5 1)), POLYGON ((9 2, 9.5 1, 2 1, 2 2, 9 2)))")
+        ]
+        geo2 = readgeom(sprint(show, geo))
+        @test LibGEOS.equals(geo, geo2)
+    end
+    p = Polygon([[[0.0, 0.0] for _ in 1:10000]])
+    buf = IOBuffer()
+    print(IOContext(buf, :compact=>true), p)
+    seekstart(buf)
+    s = read(buf, String)
+    @test length(s) < 30
+    @test occursin("Polygon(...)", s)
+end