Skip to content

Commit 51e2d36

Browse files
authored
add nearestPoints for prepared geometry (#175)
* add nearestPoints for prepared geometry * Add more nearestPoint tests and include prepared geometry tests of the same * Remove duplicate nearestPoints tests from test_geos_operations.jl
1 parent 1a52f62 commit 51e2d36

File tree

3 files changed

+48
-23
lines changed

3 files changed

+48
-23
lines changed

src/geos_functions.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1481,6 +1481,16 @@ function nearestPoints(obj1::Geometry, obj2::Geometry, context::GEOSContext = ge
14811481
end
14821482
end
14831483

1484+
function nearestPoints(obj1::PreparedGeometry, obj2::Geometry, context::GEOSContext = get_context(obj1))
1485+
points = GEOSPreparedNearestPoints_r(context, obj1, obj2)
1486+
if points == C_NULL
1487+
return Point[]
1488+
else
1489+
return Point[Point(getCoordinates(points, 1, context), context),
1490+
Point(getCoordinates(points, 2, context), context)]
1491+
end
1492+
end
1493+
14841494
# -----
14851495
# Precision functions
14861496
# -----

test/test_geos_functions.jl

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -431,21 +431,48 @@ end
431431
# GEOSNearestPointsTest
432432
geom1_ = LibGEOS.readgeom("POLYGON EMPTY")
433433
geom2_ = LibGEOS.readgeom("POLYGON EMPTY")
434+
prepGeom1_ = LibGEOS.prepareGeom(geom1_)
434435
@test LibGEOS.nearestPoints(geom1_, geom2_) == Point[]
436+
@test LibGEOS.nearestPoints(prepGeom1_, geom2_) == Point[]
435437
LibGEOS.destroyGeom(geom1_)
436438
LibGEOS.destroyGeom(geom2_)
439+
LibGEOS.destroyPreparedGeom(prepGeom1_)
437440

438-
geom1_ = LibGEOS.readgeom("POLYGON((1 1,1 5,5 5,5 1,1 1))")
439-
geom2_ = LibGEOS.readgeom("POLYGON((8 8, 9 9, 9 10, 8 8))")
440-
coords_ = LibGEOS.nearestPoints(geom1_, geom2_)
441-
@test coords_ isa Vector{Point}
442-
@test length(coords_) == 2
443-
@test GeoInterface.coordinates(coords_[1]) [5.0, 5.0] atol = 1e-5
444-
@test GeoInterface.coordinates(coords_[2]) [8.0, 8.0] atol = 1e-5
445-
LibGEOS.destroyGeom(geom1_)
446-
LibGEOS.destroyGeom(geom2_)
447-
LibGEOS.destroyGeom(coords_[1])
448-
LibGEOS.destroyGeom(coords_[2])
441+
for (wkt1_, wkt2_, wkt3_, wkt4_) in [
442+
["POLYGON((1 1,1 5,5 5,5 1,1 1))", "POLYGON((8 8, 9 9, 9 10, 8 8))", "POINT(5 5)", "POINT(8 8)"],
443+
["POLYGON((1 1,1 5,5 5,5 1,1 1))", "POINT(2 2)", "POINT(2 2)", "POINT(2 2)"],
444+
["LINESTRING(1 5,5 5,5 1,1 1)", "POINT(2 2)", "POINT(2 1)", "POINT(2 2)"],
445+
["LINESTRING(0 0,10 10)", "LINESTRING(0 10,10 0)", "POINT(5 5)", "POINT(5 5)"],
446+
["POLYGON((0 0,10 0,10 10,0 10,0 0))", "LINESTRING(8 5,12 5)", "POINT(8 5)", "POINT(8 5)"]
447+
]
448+
geom1_ = LibGEOS.readgeom(wkt1_)
449+
geom2_ = LibGEOS.readgeom(wkt2_)
450+
geom3_ = LibGEOS.readgeom(wkt3_)
451+
geom4_ = LibGEOS.readgeom(wkt4_)
452+
prepGeom1_ = LibGEOS.prepareGeom(geom1_)
453+
454+
coords_ = LibGEOS.nearestPoints(geom1_, geom2_)
455+
@test coords_ isa Vector{Point}
456+
@test length(coords_) == 2
457+
@test LibGEOS.equals(coords_[1], geom3_)
458+
@test LibGEOS.equals(coords_[2], geom4_)
459+
460+
prepCoords_ = LibGEOS.nearestPoints(prepGeom1_, geom2_)
461+
@test prepCoords_ isa Vector{Point}
462+
@test length(prepCoords_) == 2
463+
@test LibGEOS.equals(prepCoords_[1], geom3_)
464+
@test LibGEOS.equals(prepCoords_[2], geom4_)
465+
466+
LibGEOS.destroyGeom(geom1_)
467+
LibGEOS.destroyGeom(geom2_)
468+
LibGEOS.destroyGeom(geom3_)
469+
LibGEOS.destroyGeom(geom4_)
470+
LibGEOS.destroyGeom(coords_[1])
471+
LibGEOS.destroyGeom(coords_[2])
472+
LibGEOS.destroyGeom(prepCoords_[1])
473+
LibGEOS.destroyGeom(prepCoords_[2])
474+
LibGEOS.destroyPreparedGeom(prepGeom1_)
475+
end
449476

450477
# GEOSNodeTest
451478
geom1_ = LibGEOS.readgeom("LINESTRING(0 0, 10 10, 10 0, 0 10)")

test/test_geos_operations.jl

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -141,18 +141,6 @@ end
141141
@test GeoInterface.coordinates(startPoint(g1)) [0, 0] atol = 1e-5
142142
@test GeoInterface.coordinates(endPoint(g1)) [10, 10] atol = 1e-5
143143

144-
# GEOSNearestPointsTest
145-
g1 = readgeom("POLYGON EMPTY")
146-
g2 = readgeom("POLYGON EMPTY")
147-
@test length(nearestPoints(g1, g2)) == 0
148-
149-
g1 = readgeom("POLYGON((1 1,1 5,5 5,5 1,1 1))")
150-
g2 = readgeom("POLYGON((8 8, 9 9, 9 10, 8 8))")
151-
points = nearestPoints(g1, g2)
152-
@test length(points) == 2
153-
@test GeoInterface.coordinates(points[1])[1:2] == [5.0, 5.0]
154-
@test GeoInterface.coordinates(points[2])[1:2] == [8.0, 8.0]
155-
156144
# GEOSNodeTest
157145
g1 = node(readgeom("LINESTRING(0 0, 10 10, 10 0, 0 10)"))
158146
LibGEOS.normalize!(g1)

0 commit comments

Comments
 (0)