Skip to content

Commit

Permalink
add error handler (#42)
Browse files Browse the repository at this point in the history
* add error handler

* add tests for throwing GEOSError on malformed WKT

* precompile module
  • Loading branch information
yeesian authored Aug 30, 2017
1 parent a8d7ad8 commit 5011656
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 9 deletions.
40 changes: 31 additions & 9 deletions src/LibGEOS.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
__precompile__()

module LibGEOS

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

include("geos_c.jl")

# --- GEOSconnection ---
type GEOSError <: Exception
msg::String
end
Base.showerror(io::IO, err::GEOSError) = print(io, "GEOSError\n\t$(err.msg)")

function geosjl_errorhandler(message::Ptr{UInt8}, userdata)
if unsafe_string(message) == "%s"
throw(GEOSError(unsafe_string(Cstring(userdata))))
else
throw(GEOSError(unsafe_string(message)))
end
end

type GEOSconnection
status::Symbol

function GEOSconnection()
geos_status = new(:Initialized)
initializeGEOS()
finalizer(geos_status, finalizeGEOS)
geos_status
connection = new(:Initialized)
initializeGEOS(
C_NULL,
cfunction(geosjl_errorhandler,Ptr{Void},(Ptr{UInt8},Ptr{Void}))
)
finalizer(connection, finalizeGEOS)
connection
end
end
initializeGEOS() = initGEOS(C_NULL, C_NULL)
finalizeGEOS(status::GEOSconnection) = finishGEOS()

_connection = GEOSconnection()
# --- END GEOSconnection ---
initializeGEOS(notice_f, error_f) = initGEOS(notice_f, error_f)
function finalizeGEOS(connection::GEOSconnection)
connection.status = :Finished
finishGEOS()
end

function __init__()
global const _connection = GEOSconnection()
end

include("geos_functions.jl")
include("geos_types.jl")
Expand Down
10 changes: 10 additions & 0 deletions test/test_regressions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,14 @@ facts("LibGEOS regressions") do
mp = LibGEOS.MultiPoint(Vector{Float64}[[0,0],[10,0],[10,10],[11,10]])
@fact GeoInterface.geotype(mp) --> :MultiPoint

# https://github.com/JuliaGeo/LibGEOS.jl/issues/20
# LibGEOS doesn't support Extended WKT
ewkt = "SRID=32756; POLYGON ((30 10, 40 40, 20 40, 10 20, 30 10))"
# ParseException: Unknown type: 'SRID=32756;'
@fact_throws LibGEOS.GEOSError parseWKT(ewkt)

# https://github.com/JuliaGeo/LibGEOS.jl/issues/40
# IllegalArgumentException: Points of LinearRing do not form a closed linestring
@fact_throws LibGEOS.GEOSError parseWKT("POLYGON((-1. 1., 1. 3., 0. 2., -1. 1.5))")

end

0 comments on commit 5011656

Please sign in to comment.