Skip to content

Commit f92eda4

Browse files
authored
Merge pull request #170 from jw3126/lineMerge
fix lineMerge
2 parents 4928bb3 + 27a7a29 commit f92eda4

File tree

6 files changed

+70
-26
lines changed

6 files changed

+70
-26
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "LibGEOS"
22
uuid = "a90b1aa1-3769-5649-ba7e-abc5a9d163eb"
33
license = "MIT"
4-
version = "0.8.2"
4+
version = "0.8.3"
55

66
[deps]
77
CEnum = "fa961155-64e5-5f13-b03f-caf6b980ea82"

src/geos_functions.jl

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -663,20 +663,36 @@ end
663663
# GEOSPolygonizer_getCutEdges
664664
# GEOSPolygonize_full
665665

666-
function lineMerge(obj::Geometry, context::GEOSContext = get_context(obj))::MultiLineString
666+
function lineMerge(obj::Geometry, context::GEOSContext = get_context(obj))
667667
result = GEOSLineMerge_r(context, obj)
668668
if result == C_NULL
669669
error("LibGEOS: Error in GEOSLineMerge")
670670
end
671-
MultiLineString(result, context)
671+
geomFromGEOS(result, context)
672+
end
673+
674+
function maximumInscribedCircle(obj::Geometry, tolerance, context::GEOSContext = get_context(obj))
675+
result = GEOSMaximumInscribedCircle_r(context, obj, tolerance)
676+
if result == C_NULL
677+
error("LibGEOS: Error in GEOSMaximumInscribedCircle")
678+
end
679+
geomFromGEOS(result, context)
680+
end
681+
682+
function isValidReason(obj::Geometry, context::GEOSContext = get_context(obj))
683+
result = GEOSisValidReason_r(context, obj)
684+
if result == C_NULL
685+
error("LibGEOS: Error in GEOSisValidReason")
686+
end
687+
result
672688
end
673689

674690
function simplify(obj::Geometry, tol::Real, context::GEOSContext = get_context(obj))
675691
result = GEOSSimplify_r(context, obj, tol)
676692
if result == C_NULL
677693
error("LibGEOS: Error in GEOSSimplify")
678694
end
679-
geomFromGEOS(result)
695+
geomFromGEOS(result, context)
680696
end
681697

682698
function topologyPreserveSimplify(obj::Geometry, tol::Real, context::GEOSContext = get_context(obj))

src/geos_types.jl

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@ function Base.show(io::IO, geo::AbstractGeometry)
1010
end
1111
end
1212

13+
function open_issue_if_conversion_makes_sense(T::Type, id)
14+
msg = """LibGEOS: Can't convert a pointer to an element with a GeomType ID id = $id (GEOSGeomType = $(GEOSGeomTypes(id)))
15+
to a geometry of type $T (yet).
16+
Please open an issue if you think this conversion makes sense.")
17+
"""
18+
error(msg)
19+
end
20+
1321
mutable struct Point <: AbstractGeometry
1422
ptr::GEOSGeom
1523
context::GEOSContext
@@ -19,8 +27,7 @@ mutable struct Point <: AbstractGeometry
1927
point = if id == GEOS_POINT
2028
point = new(cloneGeom(obj, context), context)
2129
else
22-
error("LibGEOS: Can't convert a pointer to an element with a GeomType ID of $id to a point (yet).
23-
Please open an issue if you think this conversion makes sense.")
30+
open_issue_if_conversion_makes_sense(Point, id)
2431
end
2532
finalizer(destroyGeom, point)
2633
point
@@ -50,8 +57,7 @@ mutable struct MultiPoint <: AbstractGeometry
5057
context
5158
)
5259
else
53-
error("LibGEOS: Can't convert a pointer to an element with a GeomType ID of $id to a multipoint (yet).
54-
Please open an issue if you think this conversion makes sense.")
60+
open_issue_if_conversion_makes_sense(MultiPoint, id)
5561
end
5662
finalizer(destroyGeom, multipoint)
5763
multipoint
@@ -83,8 +89,7 @@ mutable struct LineString <: AbstractGeometry
8389
line = if id == GEOS_LINESTRING
8490
new(cloneGeom(obj, context), context)
8591
else
86-
error("LibGEOS: Can't convert a pointer to an element with a GeomType ID of $id to a linestring (yet).
87-
Please open an issue if you think this conversion makes sense.")
92+
open_issue_if_conversion_makes_sense(LineString, id)
8893
end
8994
finalizer(destroyGeom, line)
9095
line
@@ -115,8 +120,7 @@ mutable struct MultiLineString <: AbstractGeometry
115120
[cloneGeom(obj, context)],
116121
context), context)
117122
else
118-
error("LibGEOS: Can't convert a pointer to an element with a GeomType ID of $id to a multi-linestring (yet).
119-
Please open an issue if you think this conversion makes sense.")
123+
open_issue_if_conversion_makes_sense(MultiLineString, id)
120124
end
121125
finalizer(destroyGeom, multiline)
122126
multiline
@@ -147,8 +151,7 @@ mutable struct LinearRing <: AbstractGeometry
147151
ring = if id == GEOS_LINEARRING
148152
new(cloneGeom(obj, context), context)
149153
else
150-
error("LibGEOS: Can't convert a pointer to an element with a GeomType ID of $id to a linear ring (yet).
151-
Please open an issue if you think this conversion makes sense.")
154+
open_issue_if_conversion_makes_sense(LinearRing, id)
152155
end
153156
finalizer(destroyGeom, ring)
154157
ring
@@ -174,8 +177,7 @@ mutable struct Polygon <: AbstractGeometry
174177
elseif id == GEOS_LINEARRING
175178
new(cloneGeom(createPolygon(obj, context), context), context)
176179
else
177-
error("LibGEOS: Can't convert a pointer to an element with a GeomType ID of $id to a polygon (yet).
178-
Please open an issue if you think this conversion makes sense.")
180+
open_issue_if_conversion_makes_sense(Polygon, id)
179181
end
180182
finalizer(destroyGeom, polygon)
181183
polygon
@@ -214,8 +216,7 @@ mutable struct MultiPolygon <: AbstractGeometry
214216
context
215217
)
216218
else
217-
error("LibGEOS: Can't convert a pointer to an element with a GeomType ID of $id to a multi-polygon (yet).
218-
Please open an issue if you think this conversion makes sense.")
219+
open_issue_if_conversion_makes_sense(MultiPolygon, id)
219220
end
220221
finalizer(destroyGeom, multipolygon)
221222
multipolygon
@@ -254,8 +255,7 @@ mutable struct GeometryCollection <: AbstractGeometry
254255
geometrycollection = if id == GEOS_GEOMETRYCOLLECTION
255256
new(cloneGeom(obj, context), context)
256257
else
257-
error("LibGEOS: Can't convert a pointer to an element with a GeomType ID of $id to a geometry collection (yet).
258-
Please open an issue if you think this conversion makes sense.")
258+
open_issue_if_conversion_makes_sense(GeometryCollection, id)
259259
end
260260
finalizer(destroyGeom, geometrycollection)
261261
geometrycollection

test/test_geos_functions.jl

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using Test
22
using LibGEOS
3+
import GeoInterface
4+
using Extents
35

46
@testset "WKTWriter" begin
57
# default writing options
@@ -905,13 +907,20 @@ end
905907
@test p isa Polygon
906908

907909
lss = readgeom("MULTILINESTRING((0 0, 0 1), (0 1, 0 2))")
908-
@test lineMerge(lss) == readgeom("MULTILINESTRING ((0 0, 0 1, 0 2))")
910+
@test lineMerge(lss) == readgeom("LINESTRING (0 0, 0 1, 0 2)")
909911

910-
geo_invalid = readgeom("POLYGON((0 0, 0 1, 1 1, 1 0, -1 1, 0 0))")
911-
@test !LibGEOS.isValid(geo_invalid)
912-
geo_valid = LibGEOS.makeValid(geo_invalid)
913-
@test geo_valid isa LibGEOS.MultiPolygon
914-
@test LibGEOS.isValid(geo_valid)
912+
lss = readgeom("MULTILINESTRING((0 0, 0 1), (0 2, 0 3), (0 3, 0 4))")
913+
@test lineMerge(lss) == readgeom("MULTILINESTRING ((0 0, 0 1), (0 2, 0 3, 0 4))")
914+
915+
lss = readgeom("MULTILINESTRING EMPTY")
916+
@test lineMerge(lss) == readgeom("GEOMETRYCOLLECTION EMPTY")
917+
918+
lss = readgeom("LINESTRING EMPTY")
919+
@test lineMerge(lss) == readgeom("GEOMETRYCOLLECTION EMPTY")
915920

916921
@test LibGEOS.reverse(readgeom("LINESTRING(0 0, 1 1)")) == readgeom("LINESTRING(1 1, 0 0)")
922+
923+
geo = readgeom("POLYGON((0 0, 0 1, 1 1, 1 0, 0 0))")
924+
mic = LibGEOS.maximumInscribedCircle(geo, 1e-4)
925+
@test mic == readgeom("LINESTRING (0.5 0.5, 0 0.5)")
917926
end

test/test_geos_types.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
11

2+
@testset "open_issue_if_conversion_makes_sense" begin
3+
polygon = readgeom("POLYGON EMPTY")
4+
GC.@preserve polygon begin
5+
res = @test_throws Exception Point(polygon.ptr, polygon.context)
6+
msg = res.value.msg
7+
@test occursin("GEOS_POLYGON", msg)
8+
@test occursin("Point", msg)
9+
end
10+
end
11+
212
# Function to test if a geomerty is valid and if its type matches the geometry ID and has the correct dimensions
313
function testValidTypeDims(geom::LibGEOS.Geometry, typeid::LibGEOS.GEOSGeomTypes, dims::Integer)
414
@test LibGEOS.isValid(geom)

test/test_invalid_geometry.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
using Test
2+
using LibGEOS
3+
14
@testset "LibGEOS invalid geometry" begin
25
# LibGEOS shouldn't crash but error out on invalid geometry
36

@@ -11,4 +14,10 @@
1114
@test !LibGEOS.isValid(polygon)
1215
@test LibGEOS.GEOSisValidReason_r(LibGEOS.get_global_context(), polygon) ==
1316
"Hole lies outside shell[15 15]"
17+
@test LibGEOS.isValidReason(polygon) == "Hole lies outside shell[15 15]"
18+
19+
geo_valid = LibGEOS.makeValid(polygon)
20+
@test geo_valid isa LibGEOS.MultiPolygon
21+
@test LibGEOS.isValid(geo_valid)
22+
@test LibGEOS.isValidReason(geo_valid) == "Valid Geometry"
1423
end

0 commit comments

Comments
 (0)