Skip to content

Commit f4be137

Browse files
visryeesian
authored andcommitted
refactor getX and friends (#67)
They were using a single element Array where a Ref is more appropriate. A quick test unexpectedly showed no performance gain however. This also removes `getX!`, since there is no benefit in having a mutating version of a function that grabs a scalar. Also removes a bit of commented code, that is implemented already in another file.
1 parent 6688054 commit f4be137

File tree

3 files changed

+91
-164
lines changed

3 files changed

+91
-164
lines changed

Project.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "LibGEOS"
22
uuid = "a90b1aa1-3769-5649-ba7e-abc5a9d163eb"
3-
version = "v0.5.0"
3+
version = "0.5.0"
44

55
[deps]
66
BinaryProvider = "b99e7846-7c00-51b0-8f62-c81ae34c0232"
@@ -11,6 +11,6 @@ Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
1111

1212
[compat]
1313
BinaryProvider = "≥ 0.5.3"
14+
CEnum = "≥ 0.2.0"
1415
GeoInterface = "≥ 0.4.0"
1516
julia = "≥ 1.0.0"
16-
CEnum = "≥ 0.2.0"

src/geos_functions.jl

Lines changed: 89 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -90,53 +90,24 @@ function setZ!(ptr::GEOSCoordSeq, i::Integer, value::Real, context::GEOSContext
9090
result
9191
end
9292

93-
# Get ordinate values from a Coordinate Sequence (Return 0 on exception)
94-
function getX!(ptr::GEOSCoordSeq, index::Integer, coord::Vector{Float64}, context::GEOSContext = _context)
95-
result = GEOSCoordSeq_getX_r(context.ptr, ptr, index-1, pointer(coord))
93+
"Get size info from a Coordinate Sequence (Return 0 on exception)"
94+
function getSize(ptr::GEOSCoordSeq, context::GEOSContext = _context)
95+
out = Ref{UInt32}()
96+
result = GEOSCoordSeq_getSize_r(context.ptr, ptr, out)
9697
if result == 0
97-
error("LibGEOS: Error in GEOSCoordSeq_getX")
98+
error("LibGEOS: Error in GEOSCoordSeq_getSize")
9899
end
99-
result
100+
Int(out[])
100101
end
101102

102-
function getY!(ptr::GEOSCoordSeq, index::Integer, coord::Vector{Float64}, context::GEOSContext = _context)
103-
result = GEOSCoordSeq_getY_r(context.ptr, ptr, index-1, pointer(coord))
103+
"Get dimensions info from a Coordinate Sequence (Return 0 on exception)"
104+
function getDimensions(ptr::GEOSCoordSeq, context::GEOSContext = _context)
105+
out = Ref{UInt32}()
106+
result = GEOSCoordSeq_getDimensions_r(context.ptr, ptr, out)
104107
if result == 0
105-
error("LibGEOS: Error in GEOSCoordSeq_getY")
106-
end
107-
result
108-
end
109-
110-
function getZ!(ptr::GEOSCoordSeq, index::Integer, coord::Vector{Float64}, context::GEOSContext = _context)
111-
result = GEOSCoordSeq_getZ_r(context.ptr, ptr, index-1, pointer(coord))
112-
if result == 0
113-
error("LibGEOS: Error in GEOSCoordSeq_getZ")
114-
end
115-
result
116-
end
117-
118-
let out = Array{UInt32}(undef, 1)
119-
global getSize
120-
function getSize(ptr::GEOSCoordSeq, context::GEOSContext = _context)
121-
# Get size info from a Coordinate Sequence (Return 0 on exception)
122-
result = GEOSCoordSeq_getSize_r(context.ptr, ptr, pointer(out))
123-
if result == 0
124-
error("LibGEOS: Error in GEOSCoordSeq_getSize")
125-
end
126-
Int(out[1])
127-
end
128-
end
129-
130-
let out = Array{UInt32}(undef, 1)
131-
global getDimensions
132-
function getDimensions(ptr::GEOSCoordSeq, context::GEOSContext = _context)
133-
# Get dimensions info from a Coordinate Sequence (Return 0 on exception)
134-
result = GEOSCoordSeq_getDimensions_r(context.ptr, ptr, pointer(out))
135-
if result == 0
136-
error("LibGEOS: Error in GEOSCoordSeq_getDimensions")
137-
end
138-
Int(out[1])
108+
error("LibGEOS: Error in GEOSCoordSeq_getDimensions")
139109
end
110+
Int(out[])
140111
end
141112

142113
# convenience functions
@@ -202,12 +173,13 @@ function createCoordSeq(coords::Vector{Vector{Float64}}, context::GEOSContext =
202173
coordinates
203174
end
204175

205-
let out = Array{Float64}(undef, 1)
206-
global getX
207-
function getX(ptr::GEOSCoordSeq, i::Integer, context::GEOSContext = _context)
208-
getX!(ptr, i, out, context)
209-
out[1]
176+
function getX(ptr::GEOSCoordSeq, i::Integer, context::GEOSContext = _context)
177+
out = Ref{Float64}()
178+
result = GEOSCoordSeq_getX_r(context.ptr, ptr, i-1, out)
179+
if result == 0
180+
error("LibGEOS: Error in GEOSCoordSeq_getX")
210181
end
182+
out[]
211183
end
212184

213185
function getX(ptr::GEOSCoordSeq, context::GEOSContext = _context)
@@ -221,13 +193,13 @@ function getX(ptr::GEOSCoordSeq, context::GEOSContext = _context)
221193
xcoords
222194
end
223195

224-
let out = Array{Float64}(undef, 1)
225-
global getY
226-
function getY(ptr::GEOSCoordSeq, i::Integer, context::GEOSContext = _context)
227-
out = Array{Float64}(undef, 1)
228-
getY!(ptr, i, out, context)
229-
out[1]
196+
function getY(ptr::GEOSCoordSeq, i::Integer, context::GEOSContext = _context)
197+
out = Ref{Float64}()
198+
result = GEOSCoordSeq_getY_r(context.ptr, ptr, i-1, out)
199+
if result == 0
200+
error("LibGEOS: Error in GEOSCoordSeq_getY")
230201
end
202+
out[]
231203
end
232204

233205
function getY(ptr::GEOSCoordSeq, context::GEOSContext = _context)
@@ -241,12 +213,13 @@ function getY(ptr::GEOSCoordSeq, context::GEOSContext = _context)
241213
ycoords
242214
end
243215

244-
let out = Array{Float64}(undef, 1)
245-
global getZ
246-
function getZ(ptr::GEOSCoordSeq, i::Integer, context::GEOSContext = _context)
247-
getZ!(ptr, i, out, context)
248-
out[1]
216+
function getZ(ptr::GEOSCoordSeq, i::Integer, context::GEOSContext = _context)
217+
out = Ref{Float64}()
218+
result = GEOSCoordSeq_getZ_r(context.ptr, ptr, i-1, out)
219+
if result == 0
220+
error("LibGEOS: Error in GEOSCoordSeq_getZ")
249221
end
222+
out[]
250223
end
251224

252225
function getZ(ptr::GEOSCoordSeq, context::GEOSContext = _context)
@@ -966,26 +939,22 @@ function numPoints(ptr::GEOSGeom, context::GEOSContext = _context)
966939
end
967940

968941
# Return -1 on exception, Geometry must be a Point.
969-
let out = Array{Float64}(undef, 1)
970-
global getGeomX
971-
function getGeomX(ptr::GEOSGeom, context::GEOSContext = _context)
972-
result = GEOSGeomGetX_r(context.ptr, ptr, pointer(out))
973-
if result == -1
974-
error("LibGEOS: Error in GEOSGeomGetX")
975-
end
976-
out[1]
942+
function getGeomX(ptr::GEOSGeom, context::GEOSContext = _context)
943+
out = Ref{Float64}()
944+
result = GEOSGeomGetX_r(context.ptr, ptr, out)
945+
if result == -1
946+
error("LibGEOS: Error in GEOSGeomGetX")
977947
end
948+
out[]
978949
end
979950

980-
let out = Array{Float64}(undef, 1)
981-
global getGeomY
982-
function getGeomY(ptr::GEOSGeom, context::GEOSContext = _context)
983-
result = GEOSGeomGetY_r(context.ptr, ptr, pointer(out))
984-
if result == -1
985-
error("LibGEOS: Error in GEOSGeomGetY")
986-
end
987-
out[1]
951+
function getGeomY(ptr::GEOSGeom, context::GEOSContext = _context)
952+
out = Ref{Float64}()
953+
result = GEOSGeomGetY_r(context.ptr, ptr, out)
954+
if result == -1
955+
error("LibGEOS: Error in GEOSGeomGetY")
988956
end
957+
out[]
989958
end
990959

991960
# Return NULL on exception, Geometry must be a Polygon.
@@ -1074,64 +1043,54 @@ end
10741043
# -----
10751044
# Misc functions
10761045
# -----
1077-
let out = Array{Float64}(undef, 1)
1078-
global geomArea
1079-
function geomArea(ptr::GEOSGeom, context::GEOSContext = _context)
1080-
# Return 0 on exception, 1 otherwise
1081-
result = GEOSArea_r(context.ptr, ptr, pointer(out))
1082-
if result == 0
1083-
error("LibGEOS: Error in GEOSArea")
1084-
end
1085-
out[1]
1086-
end
1087-
end
1088-
1089-
let out = Array{Float64}(undef, 1)
1090-
global geomLength
1091-
function geomLength(ptr::GEOSGeom, context::GEOSContext = _context)
1092-
# Return 0 on exception, 1 otherwise
1093-
result = GEOSLength_r(context.ptr, ptr, pointer(out))
1094-
if result == 0
1095-
error("LibGEOS: Error in GEOSLength")
1096-
end
1097-
out[1]
1098-
end
1099-
end
1100-
1101-
let out = Array{Float64}(undef, 1)
1102-
global geomDistance
1103-
function geomDistance(g1::GEOSGeom, g2::GEOSGeom, context::GEOSContext = _context)
1104-
# Return 0 on exception, 1 otherwise
1105-
result = GEOSDistance_r(context.ptr, g1, g2, pointer(out))
1106-
if result == 0
1107-
error("LibGEOS: Error in GEOSDistance")
1108-
end
1109-
out[1]
1110-
end
1111-
end
1112-
1113-
let out = Array{Float64}(undef, 1)
1114-
global hausdorffdistance
1115-
function hausdorffdistance(g1::GEOSGeom, g2::GEOSGeom, context::GEOSContext = _context)
1116-
# Return 0 on exception, 1 otherwise
1117-
result = GEOSHausdorffDistance_r(context.ptr, g1, g2, pointer(out))
1118-
if result == 0
1119-
error("LibGEOS: Error in GEOSHausdorffDistance")
1120-
end
1121-
out[1]
1122-
end
1123-
end
1124-
1125-
let out = Array{Float64}(undef, 1)
1126-
global hausdorffdistance
1127-
function hausdorffdistance(g1::GEOSGeom, g2::GEOSGeom, densifyFrac::Real, context::GEOSContext = _context)
1128-
# Return 0 on exception, 1 otherwise
1129-
result = GEOSHausdorffDistanceDensify_r(context.ptr, g1, g2, densifyFrac, pointer(out))
1130-
if result == 0
1131-
error("LibGEOS: Error in GEOSHausdorffDistanceDensify")
1132-
end
1133-
out[1]
1046+
function geomArea(ptr::GEOSGeom, context::GEOSContext = _context)
1047+
out = Ref{Float64}()
1048+
# Return 0 on exception, 1 otherwise
1049+
result = GEOSArea_r(context.ptr, ptr, out)
1050+
if result == 0
1051+
error("LibGEOS: Error in GEOSArea")
1052+
end
1053+
out[]
1054+
end
1055+
1056+
function geomLength(ptr::GEOSGeom, context::GEOSContext = _context)
1057+
out = Ref{Float64}()
1058+
# Return 0 on exception, 1 otherwise
1059+
result = GEOSLength_r(context.ptr, ptr, out)
1060+
if result == 0
1061+
error("LibGEOS: Error in GEOSLength")
1062+
end
1063+
out[]
1064+
end
1065+
1066+
function geomDistance(g1::GEOSGeom, g2::GEOSGeom, context::GEOSContext = _context)
1067+
out = Ref{Float64}()
1068+
# Return 0 on exception, 1 otherwise
1069+
result = GEOSDistance_r(context.ptr, g1, g2, out)
1070+
if result == 0
1071+
error("LibGEOS: Error in GEOSDistance")
1072+
end
1073+
out[]
1074+
end
1075+
1076+
function hausdorffdistance(g1::GEOSGeom, g2::GEOSGeom, context::GEOSContext = _context)
1077+
out = Ref{Float64}()
1078+
# Return 0 on exception, 1 otherwise
1079+
result = GEOSHausdorffDistance_r(context.ptr, g1, g2, out)
1080+
if result == 0
1081+
error("LibGEOS: Error in GEOSHausdorffDistance")
1082+
end
1083+
out[]
1084+
end
1085+
1086+
function hausdorffdistance(g1::GEOSGeom, g2::GEOSGeom, densifyFrac::Real, context::GEOSContext = _context)
1087+
out = Ref{Float64}()
1088+
# Return 0 on exception, 1 otherwise
1089+
result = GEOSHausdorffDistanceDensify_r(context.ptr, g1, g2, densifyFrac, out)
1090+
if result == 0
1091+
error("LibGEOS: Error in GEOSHausdorffDistanceDensify")
11341092
end
1093+
out[]
11351094
end
11361095

11371096
# Return 0 on exception, the closest points of the two geometries otherwise.

src/geos_operations.jl

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -234,41 +234,9 @@ for geom in (:Point, :MultiPoint, :LineString, :MultiLineString, :LinearRing, :P
234234
@eval normalize!(obj::$geom) = normalize!(obj.ptr)
235235
end
236236

237-
# # Return -1 on exception, Geometry must be a Point.
238-
# let out = Array{Float64}(1)
239-
# global getGeomX
240-
# function getGeomX(ptr::GEOSGeom)
241-
# result = GEOSGeomGetX(ptr, pointer(out))
242-
# if result == -1
243-
# error("LibGEOS: Error in GEOSGeomGetX")
244-
# end
245-
# out[1]
246-
# end
247-
# end
248-
249-
# let out = Array{Float64}(1)
250-
# global getGeomY
251-
# function getGeomY(ptr::GEOSGeom)
252-
# result = GEOSGeomGetY(ptr, pointer(out))
253-
# if result == -1
254-
# error("LibGEOS: Error in GEOSGeomGetY")
255-
# end
256-
# out[1]
257-
# end
258-
# end
259-
260237
interiorRings(obj::Polygon) = map(LinearRing, interiorRings(obj.ptr))
261238
exteriorRing(obj::Polygon) = LinearRing(exteriorRing(obj.ptr))
262239

263-
# # Return -1 on exception
264-
# function numCoordinates(ptr::GEOSGeom)
265-
# result = GEOSGetNumCoordinates(ptr)
266-
# if result == -1
267-
# error("LibGEOS: Error in GEOSGetNumCoordinates")
268-
# end
269-
# result
270-
# end
271-
272240
# # Geometry must be a LineString, LinearRing or Point (Return NULL on exception)
273241
# function getCoordSeq(ptr::GEOSGeom)
274242
# result = GEOSGeom_getCoordSeq(ptr)

0 commit comments

Comments
 (0)