Skip to content

Commit 5011656

Browse files
authored
add error handler (#42)
* add error handler * add tests for throwing GEOSError on malformed WKT * precompile module
1 parent a8d7ad8 commit 5011656

File tree

2 files changed

+41
-9
lines changed

2 files changed

+41
-9
lines changed

src/LibGEOS.jl

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
__precompile__()
2+
13
module LibGEOS
24

35
if isfile(joinpath(dirname(@__FILE__),"..","deps","deps.jl"))
@@ -24,22 +26,42 @@ module LibGEOS
2426

2527
include("geos_c.jl")
2628

27-
# --- GEOSconnection ---
29+
type GEOSError <: Exception
30+
msg::String
31+
end
32+
Base.showerror(io::IO, err::GEOSError) = print(io, "GEOSError\n\t$(err.msg)")
33+
34+
function geosjl_errorhandler(message::Ptr{UInt8}, userdata)
35+
if unsafe_string(message) == "%s"
36+
throw(GEOSError(unsafe_string(Cstring(userdata))))
37+
else
38+
throw(GEOSError(unsafe_string(message)))
39+
end
40+
end
41+
2842
type GEOSconnection
2943
status::Symbol
3044

3145
function GEOSconnection()
32-
geos_status = new(:Initialized)
33-
initializeGEOS()
34-
finalizer(geos_status, finalizeGEOS)
35-
geos_status
46+
connection = new(:Initialized)
47+
initializeGEOS(
48+
C_NULL,
49+
cfunction(geosjl_errorhandler,Ptr{Void},(Ptr{UInt8},Ptr{Void}))
50+
)
51+
finalizer(connection, finalizeGEOS)
52+
connection
3653
end
3754
end
38-
initializeGEOS() = initGEOS(C_NULL, C_NULL)
39-
finalizeGEOS(status::GEOSconnection) = finishGEOS()
4055

41-
_connection = GEOSconnection()
42-
# --- END GEOSconnection ---
56+
initializeGEOS(notice_f, error_f) = initGEOS(notice_f, error_f)
57+
function finalizeGEOS(connection::GEOSconnection)
58+
connection.status = :Finished
59+
finishGEOS()
60+
end
61+
62+
function __init__()
63+
global const _connection = GEOSconnection()
64+
end
4365

4466
include("geos_functions.jl")
4567
include("geos_types.jl")

test/test_regressions.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,14 @@ facts("LibGEOS regressions") do
1616
mp = LibGEOS.MultiPoint(Vector{Float64}[[0,0],[10,0],[10,10],[11,10]])
1717
@fact GeoInterface.geotype(mp) --> :MultiPoint
1818

19+
# https://github.com/JuliaGeo/LibGEOS.jl/issues/20
20+
# LibGEOS doesn't support Extended WKT
21+
ewkt = "SRID=32756; POLYGON ((30 10, 40 40, 20 40, 10 20, 30 10))"
22+
# ParseException: Unknown type: 'SRID=32756;'
23+
@fact_throws LibGEOS.GEOSError parseWKT(ewkt)
24+
25+
# https://github.com/JuliaGeo/LibGEOS.jl/issues/40
26+
# IllegalArgumentException: Points of LinearRing do not form a closed linestring
27+
@fact_throws LibGEOS.GEOSError parseWKT("POLYGON((-1. 1., 1. 3., 0. 2., -1. 1.5))")
28+
1929
end

0 commit comments

Comments
 (0)