Skip to content

Commit b5a001b

Browse files
committed
more bugfixes
1 parent 775b1c0 commit b5a001b

File tree

3 files changed

+30
-146
lines changed

3 files changed

+30
-146
lines changed

src/LibGEOS.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ module LibGEOS
1515
disjoint, touches, intersects, crosses, within, contains, overlaps, equals, equalsexact, covers, coveredby,
1616
prepareGeom, prepcontains, prepcontainsproperly, prepcoveredby, prepcovers, prepcrosses,
1717
prepdisjoint, prepintersects, prepoverlaps, preptouches, prepwithin,
18-
isEmpty, isSimple, isRing, hasZ, isClosed, isValid,
19-
normalize!, startPoint, endPoint, geomArea, geomLength, geomDistance, hausdorffdistance, getLength, nearestPoints
18+
isEmpty, isSimple, isRing, hasZ, isClosed, isValid, normalize!, interiorRings, exteriorRing,
19+
numPoints, startPoint, endPoint, area, geomLength, distance, hausdorffdistance, nearestPoints
2020

2121
include("geos_c.jl")
2222

src/geos_functions.jl

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -275,9 +275,12 @@ end
275275
# -----
276276
# Buffer related functions
277277
# -----
278+
# Computes the buffer of a geometry, for both positive and negative buffer distances.
279+
# Since true buffer curves may contain circular arcs, computed buffer polygons can only be approximations to the true geometry.
280+
# The user can control the accuracy of the curve approximation by specifying the number of linear segments with which to approximate a curve.
278281

279-
# always returns a polygon
280-
buffer(ptr::GEOSGeom, width::Float64, quadsegs::Int) = GEOSBuffer(ptr, width, int32(quadsegs))
282+
# Always returns a polygon. The negative or zero-distance buffer of lines and points is always an empty Polygon.
283+
buffer(ptr::GEOSGeom, width::Float64, quadsegs::Int=8) = GEOSBuffer(ptr, width, int32(quadsegs))
281284

282285
# enum GEOSBufCapStyles
283286
# enum GEOSBufJoinStyles
@@ -882,7 +885,7 @@ function getGeometry(ptr::GEOSGeom, n::Int)
882885
if result == C_NULL
883886
error("LibGEOS: Error in GEOSGetGeometryN")
884887
end
885-
result
888+
cloneGeom(result)
886889
end
887890
getGeometries(ptr::GEOSGeom) = GEOSGeom[getGeometry(ptr, i) for i=1:numGeometries(ptr)]
888891

@@ -936,19 +939,19 @@ end
936939
# Return NULL on exception, Geometry must be a Polygon.
937940
# Returned object is a pointer to internal storage: it must NOT be destroyed directly.
938941
function interiorRing(ptr::GEOSGeom, n::Int)
939-
result = GEOSGetInteriorRingN(ptr, int32(n))
942+
result = GEOSGetInteriorRingN(ptr, int32(n-1))
940943
if result == C_NULL
941944
error("LibGEOS: Error in GEOSGetInteriorRingN")
942945
end
943-
result
946+
cloneGeom(result)
944947
end
945948

946949
function interiorRings(ptr::GEOSGeom)
947950
n = numInteriorRings(ptr)
948951
if n == 0
949952
return GEOSGeom[]
950953
else
951-
return GEOSGeom[GEOSGetInteriorRingN(ptr, int32(i)) for i=0:n-1]
954+
return GEOSGeom[interiorRing(ptr, i) for i=1:n]
952955
end
953956
end
954957

@@ -959,7 +962,7 @@ function exteriorRing(ptr::GEOSGeom)
959962
if result == C_NULL
960963
error("LibGEOS: Error in GEOSGetExteriorRing")
961964
end
962-
result
965+
cloneGeom(result)
963966
end
964967

965968
# Return -1 on exception
@@ -1067,17 +1070,6 @@ function hausdorffdistance(g1::GEOSGeom, g2::GEOSGeom, densifyFrac::Float64)
10671070
dist[1]
10681071
end
10691072

1070-
# Call only on LINESTRING
1071-
function getLength(ptr::GEOSGeom)
1072-
len = Array(Float64, 1)
1073-
# Return 0 on exception, 1 otherwise
1074-
result = GEOSGeomGetLength(ptr, pointer(len))
1075-
if result == 0
1076-
error("LibGEOS: Error in GEOSGeomGetLength")
1077-
end
1078-
len[1]
1079-
end
1080-
10811073
# Return 0 on exception, the closest points of the two geometries otherwise.
10821074
# The first point comes from g1 geometry and the second point comes from g2.
10831075
nearestPoints(g1::GEOSGeom, g2::GEOSGeom) = GEOSNearestPoints(g1, g2)

src/geos_operations.jl

Lines changed: 18 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ interpolateNormalized(line::LineString, dist::Float64) = Point(interpolateNormal
3636
# # Topology operations
3737
# # -----
3838
for geom in (:Point, :MultiPoint, :LineString, :MultiLineString, :LinearRing, :Polygon, :MultiPolygon, :GeometryCollection)
39+
@eval buffer(obj::$geom, dist::Float64, quadsegs::Int=8) = Polygon(buffer(obj.ptr, dist, quadsegs))
3940
@eval envelope(obj::$geom) = geomFromGEOS(envelope(obj.ptr))
4041
@eval convexhull(obj::$geom) = geomFromGEOS(convexhull(obj.ptr))
4142
@eval boundary(obj::$geom) = geomFromGEOS(boundary(obj.ptr))
@@ -77,6 +78,8 @@ for geom in (:Point, :MultiPoint, :LineString, :MultiLineString, :LinearRing, :P
7778
@eval simplify(obj::$geom, tol::Float64) = geomFromGEOS(simplify(obj.ptr, tol))
7879
@eval topologyPreserveSimplify(obj::$geom, tol::Float64) = geomFromGEOS(topologyPreserveSimplify(obj.ptr, tol))
7980
@eval uniquePoints(obj::$geom) = MultiPoint(uniquePoints(obj.ptr))
81+
@eval delaunayTriangulationEdges(obj::$geom, tol::Float64=0.0) = MultiLineString(delaunayTriangulation(obj.ptr, tol, true))
82+
@eval delaunayTriangulation(obj::$geom, tol::Float64=0.0) = GeometryCollection(delaunayTriangulation(obj.ptr, tol, false))
8083
end
8184

8285
sharedPaths(obj1::LineString, obj2::LineString) = GeometryCollection(sharedPaths(obj1.ptr, obj2.ptr))
@@ -88,16 +91,6 @@ for g1 in (:Point, :MultiPoint, :LineString, :MultiLineString, :LinearRing, :Pol
8891
end
8992
end
9093

91-
# Return a Delaunay triangulation of the vertex of the given geometry
92-
# @param g the input geometry whose vertex will be used as "sites"
93-
# @param tolerance optional snapping tolerance to use for improved robustness
94-
# @param onlyEdges if non-zero will return a MULTILINESTRING, otherwise it will
95-
# return a GEOMETRYCOLLECTION containing triangular POLYGONs.
96-
for geom in (:Point, :MultiPoint, :LineString, :MultiLineString, :LinearRing, :Polygon, :MultiPolygon, :GeometryCollection)
97-
@eval delaunayTriangulationEdges(obj::$geom, tol::Float64=0.0) = MultiLineString(delaunayTriangulation(obj.ptr, tol, true))
98-
@eval delaunayTriangulation(obj::$geom, tol::Float64=0.0) = GeometryCollection(delaunayTriangulation(obj.ptr, tol, false))
99-
end
100-
10194
# -----
10295
# Binary predicates
10396
# -----
@@ -229,10 +222,8 @@ for geom in (:Point, :MultiPoint, :LineString, :MultiLineString, :LinearRing, :P
229222
@eval isEmpty(obj::$geom) = isEmpty(obj.ptr)
230223
@eval isSimple(obj::$geom) = isSimple(obj.ptr)
231224
@eval isRing(obj::$geom) = isRing(obj.ptr)
225+
@eval isValid(obj::$geom) = isValid(obj.ptr)
232226
@eval hasZ(obj::$geom) = hasZ(obj.ptr)
233-
@eval pointOnSurface(obj::$geom) = Point(pointOnSurface(obj.ptr))
234-
@eval centroid(obj::$geom) = Point(centroid(obj.ptr))
235-
@eval node(obj::$geom) = geomFromGEOS(node(obj.ptr))
236227
end
237228

238229
isClosed(obj::LineString) = isClosed(obj.ptr) # Call only on LINESTRING
@@ -255,14 +246,6 @@ isClosed(obj::LineString) = isClosed(obj.ptr) # Call only on LINESTRING
255246
# # GEOSVALID_ALLOW_SELFTOUCHING_RING_FORMING_HOLE=1
256247
# # };
257248

258-
# function isValid(ptr::GEOSGeom)
259-
# result = GEOSisValid(ptr)
260-
# if result == 0x02
261-
# error("LibGEOS: Error in GEOSisValid")
262-
# end
263-
# bool(result)
264-
# end
265-
266249
# # * return NULL on exception, a string to GEOSFree otherwise
267250
# # GEOSisValidReason
268251

@@ -275,15 +258,6 @@ isClosed(obj::LineString) = isClosed(obj.ptr) # Call only on LINESTRING
275258
# # Geometry info
276259
# # -----
277260

278-
# # Return NULL on exception, result must be freed by caller
279-
# # function geomType(ptr::GEOSGeom)
280-
# # result = GEOSGeomType(ptr)
281-
# # if result == C_NULL
282-
# # error("LibGEOS: Error in GEOSGeomType")
283-
# # end
284-
# # result
285-
# # end
286-
287261
# # May be called on all geometries in GEOS 3.x, returns -1 on error and 1
288262
# # for non-multi geometries. Older GEOS versions only accept
289263
# # GeometryCollections or Multi* geometries here, and are likely to crash
@@ -317,24 +291,6 @@ for geom in (:Point, :MultiPoint, :LineString, :MultiLineString, :LinearRing, :P
317291
@eval normalize!(obj::$geom) = normalize!(obj.ptr)
318292
end
319293

320-
# # Return -1 on exception
321-
# function numInteriorRings(ptr::GEOSGeom)
322-
# result = GEOSGetNumInteriorRings(ptr)
323-
# if result == -1
324-
# error("LibGEOS: Error in GEOSGetNumInteriorRings")
325-
# end
326-
# result
327-
# end
328-
329-
# # Call only on LINESTRING (returns -1 on exception)
330-
# function numPoints(ptr::GEOSGeom)
331-
# result = GEOSGeomGetNumPoints(ptr)
332-
# if result == -1
333-
# error("LibGEOS: Error in GEOSGeomGetNumPoints")
334-
# end
335-
# result
336-
# end
337-
338294
# # Return -1 on exception, Geometry must be a Point.
339295
# function getGeomX(ptr::GEOSGeom)
340296
# x = Array(Float64, 1)
@@ -354,34 +310,8 @@ end
354310
# y[1]
355311
# end
356312

357-
# # Return NULL on exception, Geometry must be a Polygon.
358-
# # Returned object is a pointer to internal storage: it must NOT be destroyed directly.
359-
# function interiorRing(ptr::GEOSGeom, n::Int)
360-
# result = GEOSGetInteriorRingN(ptr, int32(n))
361-
# if result == C_NULL
362-
# error("LibGEOS: Error in GEOSGetInteriorRingN")
363-
# end
364-
# result
365-
# end
366-
367-
# function interiorRings(ptr::GEOSGeom)
368-
# n = numInteriorRings(ptr)
369-
# if n == 0
370-
# return GEOSGeom[]
371-
# else
372-
# return GEOSGeom[GEOSGetInteriorRingN(ptr, int32(i)) for i=0:n-1]
373-
# end
374-
# end
375-
376-
# # Return NULL on exception, Geometry must be a Polygon.
377-
# # Returned object is a pointer to internal storage: it must NOT be destroyed directly.
378-
# function exteriorRing(ptr::GEOSGeom)
379-
# result = GEOSGetExteriorRing(ptr)
380-
# if result == C_NULL
381-
# error("LibGEOS: Error in GEOSGetExteriorRing")
382-
# end
383-
# result
384-
# end
313+
interiorRings(obj::Polygon) = map(LinearRing, interiorRings(obj.ptr))
314+
exteriorRing(obj::Polygon) = LinearRing(exteriorRing(obj.ptr))
385315

386316
# # Return -1 on exception
387317
# function numCoordinates(ptr::GEOSGeom)
@@ -417,63 +347,25 @@ end
417347
# result
418348
# end
419349

350+
numPoints(obj::LineString) = numPoints(obj.ptr) # Call only on LINESTRING
420351
startPoint(obj::LineString) = Point(startPoint(obj.ptr)) # Call only on LINESTRING
421352
endPoint(obj::LineString) = Point(endPoint(obj.ptr)) # Call only on LINESTRING
422353

423354
# # -----
424355
# # Misc functions
425356
# # -----
426-
# function geomArea(ptr::GEOSGeom)
427-
# area = Array(Float64, 1)
428-
# # Return 0 on exception, 1 otherwise
429-
# result = GEOSArea(ptr, pointer(area))
430-
# if result == 0
431-
# error("LibGEOS: Error in GEOSArea")
432-
# end
433-
# area[1]
434-
# end
435-
436-
# function geomLength(ptr::GEOSGeom)
437-
# len = Array(Float64, 1)
438-
# # Return 0 on exception, 1 otherwise
439-
# result = GEOSLength(ptr, pointer(len))
440-
# if result == 0
441-
# error("LibGEOS: Error in GEOSLength")
442-
# end
443-
# len[1]
444-
# end
445-
446-
# function geomDistance(g1::GEOSGeom, g2::GEOSGeom)
447-
# dist = Array(Float64, 1)
448-
# # Return 0 on exception, 1 otherwise
449-
# result = GEOSDistance(g1, g2, pointer(dist))
450-
# if result == 0
451-
# error("LibGEOS: Error in GEOSDistance")
452-
# end
453-
# dist[1]
454-
# end
455-
456-
# function hausdorffdistance(g1::GEOSGeom, g2::GEOSGeom)
457-
# dist = Array(Float64, 1)
458-
# # Return 0 on exception, 1 otherwise
459-
# result = GEOSHausdorffDistance(g1, g2, pointer(dist))
460-
# if result == 0
461-
# error("LibGEOS: Error in GEOSHausdorffDistance")
462-
# end
463-
# dist[1]
464-
# end
465-
466-
# function hausdorffdistance(g1::GEOSGeom, g2::GEOSGeom, densifyFrac::Float64)
467-
# dist = Array(Float64, 1)
468-
# # Return 0 on exception, 1 otherwise
469-
# result = GEOSHausdorffDistanceDensify(g1, g2, densifyFrac, pointer(dist))
470-
# if result == 0
471-
# error("LibGEOS: Error in GEOSHausdorffDistanceDensify")
472-
# end
473-
# dist[1]
474-
# end
357+
for geom in (:Point, :MultiPoint, :LineString, :MultiLineString, :LinearRing, :Polygon, :MultiPolygon, :GeometryCollection)
358+
@eval area(obj::$geom) = geomArea(obj.ptr)
359+
@eval geomLength(obj::$geom) = geomLength(obj.ptr)
360+
end
475361

476-
getLength(obj::LineString) = getLength(obj.ptr) # Call only on LINESTRING
362+
for g1 in (:Point, :MultiPoint, :LineString, :MultiLineString, :LinearRing, :Polygon, :MultiPolygon, :GeometryCollection)
363+
for g2 in (:Point, :MultiPoint, :LineString, :MultiLineString, :LinearRing, :Polygon, :MultiPolygon, :GeometryCollection)
364+
@eval distance(obj1::$g1, obj2::$g2) = geomDistance(obj1.ptr, obj2.ptr)
365+
@eval hausdorffdistance(obj1::$g1, obj2::$g2) = hausdorffdistance(obj1.ptr, obj2.ptr)
366+
@eval hausdorffdistance(obj1::$g1, obj2::$g2, densify::Float64) = hausdorffdistance(obj1.ptr, obj2.ptr, densify)
367+
end
368+
end
477369

478370
# Returns the closest points of the two geometries. The first point comes from g1 geometry and the second point comes from g2.
479371
for g1 in (:Point, :MultiPoint, :LineString, :MultiLineString, :LinearRing, :Polygon, :MultiPolygon, :GeometryCollection)

0 commit comments

Comments
 (0)