Skip to content

Commit

Permalink
Fix createCoordSeq + test (#26)
Browse files Browse the repository at this point in the history
* Fix createCoordSeq + test

* fix ambiguity in createCoordSeq(size::Integer; ndim::Integer)

* fix segfault in Point(2,5)
* more test coverage for createCoordSeq

* fix test for 0.4 support

* switch to new signatures

* bugfix

* Fix CoordSeq test

Don't set coordinates beyond index 1 in a size 1 CoordSeq
  • Loading branch information
OmriTreidel authored and yeesian committed Mar 29, 2017
1 parent efe733d commit 70ce288
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 26 deletions.
4 changes: 2 additions & 2 deletions src/geos_c.jl
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,15 @@ function GEOSContext_setErrorHandler_r(extHandle::GEOSContextHandle_t,ef::GEOSMe
ccall((:GEOSContext_setErrorHandler_r,libgeos),GEOSMessageHandler,(GEOSContextHandle_t,GEOSMessageHandler),extHandle,ef)
end

function GEOSGeomFromWKT(wkt::Ptr{UInt8})
function GEOSGeomFromWKT(wkt::Compat.String)
ccall((:GEOSGeomFromWKT,libgeos),Ptr{GEOSGeometry},(Ptr{UInt8},),wkt)
end

function GEOSGeomToWKT(g::Ptr{GEOSGeometry})
ccall((:GEOSGeomToWKT,libgeos),Ptr{UInt8},(Ptr{GEOSGeometry},),g)
end

function GEOSGeomFromWKT_r(handle::GEOSContextHandle_t,wkt::Ptr{UInt8})
function GEOSGeomFromWKT_r(handle::GEOSContextHandle_t,wkt::Compat.String)
ccall((:GEOSGeomFromWKT_r,libgeos),Ptr{GEOSGeometry},(GEOSContextHandle_t,Ptr{UInt8}),handle,wkt)
end

Expand Down
41 changes: 32 additions & 9 deletions src/geos_functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ GEOMTYPE = Dict( GEOS_POINT => :Point,
GEOS_GEOMETRYCOLLECTION => :GeometryCollection)


function geomFromWKT(geom::Compat.ASCIIString)
result = GEOSGeomFromWKT(pointer(geom))
function geomFromWKT(geom::Compat.String)
result = GEOSGeomFromWKT(geom)
if result == C_NULL
error("LibGEOS: Error in GEOSGeomFromWKT")
end
Expand All @@ -27,9 +27,12 @@ geomToWKT(geom::Ptr{GEOSGeometry}) = unsafe_string(GEOSGeomToWKT(geom))
# -----
# Coordinate Sequence functions
# -----
"""
createCoordSeq(size::Integer; ndim::Integer=2) -> Ptr{Ptr{Void}}
# Create a Coordinate sequence with ``size'' coordinates of ``dims'' dimensions (Return NULL on exception)
function createCoordSeq(size::Integer, ndim::Integer)
Create a Coordinate sequence with ``size'' coordinates of ``dims'' dimensions (Return NULL on exception)
"""
function createCoordSeq(size::Integer; ndim::Integer=2)
@assert ndim >= 2
result = GEOSCoordSeq_create(size, ndim)
if result == C_NULL
Expand Down Expand Up @@ -139,33 +142,53 @@ function setCoordSeq!(ptr::GEOSCoordSeq, i::Integer, coords::Vector{Float64})
ptr
end

"""
createCoordSeq(x::Real, y::Real) -> Ptr{Ptr{Void}}
Create a createCoordSeq of a single 2D coordinate
"""
function createCoordSeq(x::Real, y::Real)
coordinates = createCoordSeq(1, 2)
coordinates = createCoordSeq(1, ndim=2)
setX!(coordinates, 1, x)
setY!(coordinates, 1, y)
coordinates
end

"""
createCoordSeq(x::Real, y::Real, z::Real) -> Ptr{Ptr{Void}}
Create a createCoordSeq of a single 3D coordinate
"""
function createCoordSeq(x::Real, y::Real, z::Real)
coordinates = createCoordSeq(1, 3)
coordinates = createCoordSeq(1, ndim=3)
setX!(coordinates, 1, x)
setY!(coordinates, 1, y)
setZ!(coordinates, 1, z)
coordinates
end

"""
createCoordSeq(coords::Vector{Float64}) -> Ptr{Ptr{Void}}
Create a createCoordSeq of a single N dimensional coordinate
"""
function createCoordSeq(coords::Vector{Float64})
ndim = length(coords)
@assert ndim >= 2
coordinates = createCoordSeq(1, ndim)
setCoordSeq!(coordinates, 1, coord)
coordinates = createCoordSeq(1, ndim=ndim)
setCoordSeq!(coordinates, 1, coords)
end

"""
createCoordSeq(coords::Vector{Float64}) -> Ptr{Ptr{Void}}
Create a createCoordSeq of a multiple N dimensional coordinate
"""
function createCoordSeq(coords::Vector{Vector{Float64}})
ncoords = length(coords)
@assert ncoords > 0
ndim = length(coords[1])
coordinates = createCoordSeq(ncoords, ndim)
coordinates = createCoordSeq(ncoords, ndim=ndim)
for (i,coord) in enumerate(coords)
setCoordSeq!(coordinates, i, coord)
end
Expand Down
2 changes: 1 addition & 1 deletion src/geos_operations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function geomFromGEOS(ptr::GEOSGeom)
return GeometryCollection(ptr)
end
end
parseWKT(geom::Compat.ASCIIString) = geomFromGEOS(geomFromWKT(geom))
parseWKT(geom::Compat.String) = geomFromGEOS(geomFromWKT(geom))

# -----
# Linear referencing functions -- there are more, but these are probably sufficient for most purposes
Expand Down
7 changes: 6 additions & 1 deletion test/test_geo_interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@ pt = LibGEOS.Point(1.0,2.0)
@fact GeoInterface.coordinates(pt) --> roughly([1,2], 1e-5)
@fact GeoInterface.geotype(pt) --> :Point

pt = LibGEOS.Point(1, 2)
@fact GeoInterface.coordinates(pt) --> roughly([1,2], 1e-5)
@fact GeoInterface.geotype(pt) --> :Point


pt = LibGEOS.Point(LibGEOS.geomFromWKT("POINT EMPTY"))
@fact GeoInterface.coordinates(pt) --> roughly([], 1e-5)
@fact GeoInterface.coordinates(pt) --> roughly(Float64[], 1e-5)
@fact GeoInterface.geotype(pt) --> :Point

mpt = LibGEOS.MultiPoint(LibGEOS.geomFromWKT("MULTIPOINT(0 0, 10 0, 10 10, 11 10)"))
Expand Down
37 changes: 25 additions & 12 deletions test/test_geos_functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -80,25 +80,38 @@ LibGEOS.destroyGeom(expected_)
LibGEOS.destroyGeom(output_)

# GEOSCoordSeqTest

cs_ = LibGEOS.createCoordSeq(5, 3)
@fact LibGEOS.getSize(cs_) --> 5
@fact LibGEOS.getDimensions(cs_) --> 3
@fact LibGEOS.getSize(cs_) --> 1
@fact LibGEOS.getDimensions(cs_) --> 2
@fact LibGEOS.getCoordinates(cs_) --> [5, 3]

cs_2 = LibGEOS.createCoordSeq([5.0, 3.0])
@fact LibGEOS.getSize(cs_2) --> 1
@fact LibGEOS.getDimensions(cs_2) --> 2
@fact LibGEOS.getCoordinates(cs_2) --> [5.0, 3.0]

cs_3 = LibGEOS.createCoordSeq(Vector{Float64}[[5.0, 3.0], [1.0, 2.0], [1.0, 3.0]])
@fact LibGEOS.getSize(cs_3) --> 3
@fact LibGEOS.getDimensions(cs_3) --> 2
@fact LibGEOS.getCoordinates(cs_3)[1] --> [5.0, 3.0]
@fact LibGEOS.getCoordinates(cs_3)[2] --> [1.0, 2.0]
@fact LibGEOS.getCoordinates(cs_3)[3] --> [1.0, 3.0]


for i=1:5
x = i*10.0
y = i*10.0+1.0
z = i*10.0+2.0

LibGEOS.setX!(cs_, i, x)
LibGEOS.setY!(cs_, i, y)
LibGEOS.setZ!(cs_, i, z)
@fact LibGEOS.getX(cs_, i) --> roughly(x, 1e-5)
@fact LibGEOS.getY(cs_, i) --> roughly(y, 1e-5)
@fact LibGEOS.getZ(cs_, i) --> roughly(z, 1e-5)
LibGEOS.setX!(cs_, 1, x)
LibGEOS.setY!(cs_, 1, y)
LibGEOS.setZ!(cs_, 1, z)
@fact LibGEOS.getX(cs_, 1) --> roughly(x, 1e-5)
@fact LibGEOS.getY(cs_, 1) --> roughly(y, 1e-5)
@fact LibGEOS.getZ(cs_, 1) --> roughly(z, 1e-5)
end

cs_ = LibGEOS.createCoordSeq(1, 3)
cs_ = LibGEOS.createCoordSeq(1, ndim=3)
@fact LibGEOS.getSize(cs_) --> 1
@fact LibGEOS.getDimensions(cs_) --> 3
x,y,z = 10.0, 11.0, 12.0
Expand All @@ -110,7 +123,7 @@ LibGEOS.setZ!(cs_, 1, z)
@fact LibGEOS.getY(cs_, 1) --> roughly(y, 1e-5)
@fact LibGEOS.getZ(cs_, 1) --> roughly(z, 1e-5)

cs_ = LibGEOS.createCoordSeq(1, 3)
cs_ = LibGEOS.createCoordSeq(1, ndim=3)
@fact LibGEOS.getSize(cs_) --> 1
@fact LibGEOS.getDimensions(cs_) --> 3
x,y,z = 10.0, 11.0, 12.0
Expand Down Expand Up @@ -284,7 +297,7 @@ geom1 = LibGEOS.geomFromWKT("LINESTRING(0 0, 5 5, 10 10)")
@fact LibGEOS.isClosed(geom1) --> false
@fact LibGEOS.geomTypeId(geom1) --> LibGEOS.GEOS_LINESTRING
@fact LibGEOS.numPoints(geom1) --> 3
@fact LibGEOS.getLength(geom1) --> roughly(sqrt(100 + 100), 1e-5)
@fact LibGEOS.geomLength(geom1) --> roughly(sqrt(100 + 100), 1e-5)
geom2 = LibGEOS.getPoint(geom1, 1)
@fact LibGEOS.getGeomX(geom2) --> roughly(0.0, 1e-5)
@fact LibGEOS.getGeomY(geom2) --> roughly(0.0, 1e-5)
Expand Down
2 changes: 1 addition & 1 deletion test/test_geos_operations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ g1 = parseWKT("LINESTRING(0 0, 5 5, 10 10)")
@fact isClosed(g1) --> false
@fact GeoInterface.geotype(g1) --> :LineString
@fact numPoints(g1) --> 3
@fact getLength(g1) --> roughly(sqrt(100 + 100), 1e-5)
@fact geomLength(g1) --> roughly(sqrt(100 + 100), 1e-5)
@fact GeoInterface.coordinates(startPoint(g1)) --> roughly([0,0], 1e-5)
@fact GeoInterface.coordinates(endPoint(g1)) --> roughly([10,10], 1e-5)

Expand Down

0 comments on commit 70ce288

Please sign in to comment.