Skip to content

Commit 70ce288

Browse files
OmriTreidelyeesian
authored andcommitted
Fix createCoordSeq + test (#26)
* 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
1 parent efe733d commit 70ce288

File tree

6 files changed

+67
-26
lines changed

6 files changed

+67
-26
lines changed

src/geos_c.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,15 @@ function GEOSContext_setErrorHandler_r(extHandle::GEOSContextHandle_t,ef::GEOSMe
6262
ccall((:GEOSContext_setErrorHandler_r,libgeos),GEOSMessageHandler,(GEOSContextHandle_t,GEOSMessageHandler),extHandle,ef)
6363
end
6464

65-
function GEOSGeomFromWKT(wkt::Ptr{UInt8})
65+
function GEOSGeomFromWKT(wkt::Compat.String)
6666
ccall((:GEOSGeomFromWKT,libgeos),Ptr{GEOSGeometry},(Ptr{UInt8},),wkt)
6767
end
6868

6969
function GEOSGeomToWKT(g::Ptr{GEOSGeometry})
7070
ccall((:GEOSGeomToWKT,libgeos),Ptr{UInt8},(Ptr{GEOSGeometry},),g)
7171
end
7272

73-
function GEOSGeomFromWKT_r(handle::GEOSContextHandle_t,wkt::Ptr{UInt8})
73+
function GEOSGeomFromWKT_r(handle::GEOSContextHandle_t,wkt::Compat.String)
7474
ccall((:GEOSGeomFromWKT_r,libgeos),Ptr{GEOSGeometry},(GEOSContextHandle_t,Ptr{UInt8}),handle,wkt)
7575
end
7676

src/geos_functions.jl

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ GEOMTYPE = Dict( GEOS_POINT => :Point,
1515
GEOS_GEOMETRYCOLLECTION => :GeometryCollection)
1616

1717

18-
function geomFromWKT(geom::Compat.ASCIIString)
19-
result = GEOSGeomFromWKT(pointer(geom))
18+
function geomFromWKT(geom::Compat.String)
19+
result = GEOSGeomFromWKT(geom)
2020
if result == C_NULL
2121
error("LibGEOS: Error in GEOSGeomFromWKT")
2222
end
@@ -27,9 +27,12 @@ geomToWKT(geom::Ptr{GEOSGeometry}) = unsafe_string(GEOSGeomToWKT(geom))
2727
# -----
2828
# Coordinate Sequence functions
2929
# -----
30+
"""
31+
createCoordSeq(size::Integer; ndim::Integer=2) -> Ptr{Ptr{Void}}
3032
31-
# Create a Coordinate sequence with ``size'' coordinates of ``dims'' dimensions (Return NULL on exception)
32-
function createCoordSeq(size::Integer, ndim::Integer)
33+
Create a Coordinate sequence with ``size'' coordinates of ``dims'' dimensions (Return NULL on exception)
34+
"""
35+
function createCoordSeq(size::Integer; ndim::Integer=2)
3336
@assert ndim >= 2
3437
result = GEOSCoordSeq_create(size, ndim)
3538
if result == C_NULL
@@ -139,33 +142,53 @@ function setCoordSeq!(ptr::GEOSCoordSeq, i::Integer, coords::Vector{Float64})
139142
ptr
140143
end
141144

145+
"""
146+
createCoordSeq(x::Real, y::Real) -> Ptr{Ptr{Void}}
147+
148+
Create a createCoordSeq of a single 2D coordinate
149+
"""
142150
function createCoordSeq(x::Real, y::Real)
143-
coordinates = createCoordSeq(1, 2)
151+
coordinates = createCoordSeq(1, ndim=2)
144152
setX!(coordinates, 1, x)
145153
setY!(coordinates, 1, y)
146154
coordinates
147155
end
148156

157+
"""
158+
createCoordSeq(x::Real, y::Real, z::Real) -> Ptr{Ptr{Void}}
159+
160+
Create a createCoordSeq of a single 3D coordinate
161+
"""
149162
function createCoordSeq(x::Real, y::Real, z::Real)
150-
coordinates = createCoordSeq(1, 3)
163+
coordinates = createCoordSeq(1, ndim=3)
151164
setX!(coordinates, 1, x)
152165
setY!(coordinates, 1, y)
153166
setZ!(coordinates, 1, z)
154167
coordinates
155168
end
156169

170+
"""
171+
createCoordSeq(coords::Vector{Float64}) -> Ptr{Ptr{Void}}
172+
173+
Create a createCoordSeq of a single N dimensional coordinate
174+
"""
157175
function createCoordSeq(coords::Vector{Float64})
158176
ndim = length(coords)
159177
@assert ndim >= 2
160-
coordinates = createCoordSeq(1, ndim)
161-
setCoordSeq!(coordinates, 1, coord)
178+
coordinates = createCoordSeq(1, ndim=ndim)
179+
setCoordSeq!(coordinates, 1, coords)
162180
end
163181

182+
"""
183+
createCoordSeq(coords::Vector{Float64}) -> Ptr{Ptr{Void}}
184+
185+
Create a createCoordSeq of a multiple N dimensional coordinate
186+
"""
164187
function createCoordSeq(coords::Vector{Vector{Float64}})
165188
ncoords = length(coords)
166189
@assert ncoords > 0
167190
ndim = length(coords[1])
168-
coordinates = createCoordSeq(ncoords, ndim)
191+
coordinates = createCoordSeq(ncoords, ndim=ndim)
169192
for (i,coord) in enumerate(coords)
170193
setCoordSeq!(coordinates, i, coord)
171194
end

src/geos_operations.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ function geomFromGEOS(ptr::GEOSGeom)
2222
return GeometryCollection(ptr)
2323
end
2424
end
25-
parseWKT(geom::Compat.ASCIIString) = geomFromGEOS(geomFromWKT(geom))
25+
parseWKT(geom::Compat.String) = geomFromGEOS(geomFromWKT(geom))
2626

2727
# -----
2828
# Linear referencing functions -- there are more, but these are probably sufficient for most purposes

test/test_geo_interface.jl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,13 @@ pt = LibGEOS.Point(1.0,2.0)
33
@fact GeoInterface.coordinates(pt) --> roughly([1,2], 1e-5)
44
@fact GeoInterface.geotype(pt) --> :Point
55

6+
pt = LibGEOS.Point(1, 2)
7+
@fact GeoInterface.coordinates(pt) --> roughly([1,2], 1e-5)
8+
@fact GeoInterface.geotype(pt) --> :Point
9+
10+
611
pt = LibGEOS.Point(LibGEOS.geomFromWKT("POINT EMPTY"))
7-
@fact GeoInterface.coordinates(pt) --> roughly([], 1e-5)
12+
@fact GeoInterface.coordinates(pt) --> roughly(Float64[], 1e-5)
813
@fact GeoInterface.geotype(pt) --> :Point
914

1015
mpt = LibGEOS.MultiPoint(LibGEOS.geomFromWKT("MULTIPOINT(0 0, 10 0, 10 10, 11 10)"))

test/test_geos_functions.jl

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -80,25 +80,38 @@ LibGEOS.destroyGeom(expected_)
8080
LibGEOS.destroyGeom(output_)
8181

8282
# GEOSCoordSeqTest
83-
8483
cs_ = LibGEOS.createCoordSeq(5, 3)
85-
@fact LibGEOS.getSize(cs_) --> 5
86-
@fact LibGEOS.getDimensions(cs_) --> 3
84+
@fact LibGEOS.getSize(cs_) --> 1
85+
@fact LibGEOS.getDimensions(cs_) --> 2
86+
@fact LibGEOS.getCoordinates(cs_) --> [5, 3]
87+
88+
cs_2 = LibGEOS.createCoordSeq([5.0, 3.0])
89+
@fact LibGEOS.getSize(cs_2) --> 1
90+
@fact LibGEOS.getDimensions(cs_2) --> 2
91+
@fact LibGEOS.getCoordinates(cs_2) --> [5.0, 3.0]
92+
93+
cs_3 = LibGEOS.createCoordSeq(Vector{Float64}[[5.0, 3.0], [1.0, 2.0], [1.0, 3.0]])
94+
@fact LibGEOS.getSize(cs_3) --> 3
95+
@fact LibGEOS.getDimensions(cs_3) --> 2
96+
@fact LibGEOS.getCoordinates(cs_3)[1] --> [5.0, 3.0]
97+
@fact LibGEOS.getCoordinates(cs_3)[2] --> [1.0, 2.0]
98+
@fact LibGEOS.getCoordinates(cs_3)[3] --> [1.0, 3.0]
99+
87100

88101
for i=1:5
89102
x = i*10.0
90103
y = i*10.0+1.0
91104
z = i*10.0+2.0
92105

93-
LibGEOS.setX!(cs_, i, x)
94-
LibGEOS.setY!(cs_, i, y)
95-
LibGEOS.setZ!(cs_, i, z)
96-
@fact LibGEOS.getX(cs_, i) --> roughly(x, 1e-5)
97-
@fact LibGEOS.getY(cs_, i) --> roughly(y, 1e-5)
98-
@fact LibGEOS.getZ(cs_, i) --> roughly(z, 1e-5)
106+
LibGEOS.setX!(cs_, 1, x)
107+
LibGEOS.setY!(cs_, 1, y)
108+
LibGEOS.setZ!(cs_, 1, z)
109+
@fact LibGEOS.getX(cs_, 1) --> roughly(x, 1e-5)
110+
@fact LibGEOS.getY(cs_, 1) --> roughly(y, 1e-5)
111+
@fact LibGEOS.getZ(cs_, 1) --> roughly(z, 1e-5)
99112
end
100113

101-
cs_ = LibGEOS.createCoordSeq(1, 3)
114+
cs_ = LibGEOS.createCoordSeq(1, ndim=3)
102115
@fact LibGEOS.getSize(cs_) --> 1
103116
@fact LibGEOS.getDimensions(cs_) --> 3
104117
x,y,z = 10.0, 11.0, 12.0
@@ -110,7 +123,7 @@ LibGEOS.setZ!(cs_, 1, z)
110123
@fact LibGEOS.getY(cs_, 1) --> roughly(y, 1e-5)
111124
@fact LibGEOS.getZ(cs_, 1) --> roughly(z, 1e-5)
112125

113-
cs_ = LibGEOS.createCoordSeq(1, 3)
126+
cs_ = LibGEOS.createCoordSeq(1, ndim=3)
114127
@fact LibGEOS.getSize(cs_) --> 1
115128
@fact LibGEOS.getDimensions(cs_) --> 3
116129
x,y,z = 10.0, 11.0, 12.0
@@ -284,7 +297,7 @@ geom1 = LibGEOS.geomFromWKT("LINESTRING(0 0, 5 5, 10 10)")
284297
@fact LibGEOS.isClosed(geom1) --> false
285298
@fact LibGEOS.geomTypeId(geom1) --> LibGEOS.GEOS_LINESTRING
286299
@fact LibGEOS.numPoints(geom1) --> 3
287-
@fact LibGEOS.getLength(geom1) --> roughly(sqrt(100 + 100), 1e-5)
300+
@fact LibGEOS.geomLength(geom1) --> roughly(sqrt(100 + 100), 1e-5)
288301
geom2 = LibGEOS.getPoint(geom1, 1)
289302
@fact LibGEOS.getGeomX(geom2) --> roughly(0.0, 1e-5)
290303
@fact LibGEOS.getGeomY(geom2) --> roughly(0.0, 1e-5)

test/test_geos_operations.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ g1 = parseWKT("LINESTRING(0 0, 5 5, 10 10)")
126126
@fact isClosed(g1) --> false
127127
@fact GeoInterface.geotype(g1) --> :LineString
128128
@fact numPoints(g1) --> 3
129-
@fact getLength(g1) --> roughly(sqrt(100 + 100), 1e-5)
129+
@fact geomLength(g1) --> roughly(sqrt(100 + 100), 1e-5)
130130
@fact GeoInterface.coordinates(startPoint(g1)) --> roughly([0,0], 1e-5)
131131
@fact GeoInterface.coordinates(endPoint(g1)) --> roughly([10,10], 1e-5)
132132

0 commit comments

Comments
 (0)