Skip to content

Commit

Permalink
add nearestPoints for prepared geometry (#175)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
jaakkor2 authored Jul 6, 2023
1 parent 1a52f62 commit 51e2d36
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 23 deletions.
10 changes: 10 additions & 0 deletions src/geos_functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1481,6 +1481,16 @@ function nearestPoints(obj1::Geometry, obj2::Geometry, context::GEOSContext = ge
end
end

function nearestPoints(obj1::PreparedGeometry, obj2::Geometry, context::GEOSContext = get_context(obj1))
points = GEOSPreparedNearestPoints_r(context, obj1, obj2)
if points == C_NULL
return Point[]
else
return Point[Point(getCoordinates(points, 1, context), context),
Point(getCoordinates(points, 2, context), context)]
end
end

# -----
# Precision functions
# -----
Expand Down
49 changes: 38 additions & 11 deletions test/test_geos_functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -431,21 +431,48 @@ end
# GEOSNearestPointsTest
geom1_ = LibGEOS.readgeom("POLYGON EMPTY")
geom2_ = LibGEOS.readgeom("POLYGON EMPTY")
prepGeom1_ = LibGEOS.prepareGeom(geom1_)
@test LibGEOS.nearestPoints(geom1_, geom2_) == Point[]
@test LibGEOS.nearestPoints(prepGeom1_, geom2_) == Point[]
LibGEOS.destroyGeom(geom1_)
LibGEOS.destroyGeom(geom2_)
LibGEOS.destroyPreparedGeom(prepGeom1_)

geom1_ = LibGEOS.readgeom("POLYGON((1 1,1 5,5 5,5 1,1 1))")
geom2_ = LibGEOS.readgeom("POLYGON((8 8, 9 9, 9 10, 8 8))")
coords_ = LibGEOS.nearestPoints(geom1_, geom2_)
@test coords_ isa Vector{Point}
@test length(coords_) == 2
@test GeoInterface.coordinates(coords_[1]) [5.0, 5.0] atol = 1e-5
@test GeoInterface.coordinates(coords_[2]) [8.0, 8.0] atol = 1e-5
LibGEOS.destroyGeom(geom1_)
LibGEOS.destroyGeom(geom2_)
LibGEOS.destroyGeom(coords_[1])
LibGEOS.destroyGeom(coords_[2])
for (wkt1_, wkt2_, wkt3_, wkt4_) in [
["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)"],
["POLYGON((1 1,1 5,5 5,5 1,1 1))", "POINT(2 2)", "POINT(2 2)", "POINT(2 2)"],
["LINESTRING(1 5,5 5,5 1,1 1)", "POINT(2 2)", "POINT(2 1)", "POINT(2 2)"],
["LINESTRING(0 0,10 10)", "LINESTRING(0 10,10 0)", "POINT(5 5)", "POINT(5 5)"],
["POLYGON((0 0,10 0,10 10,0 10,0 0))", "LINESTRING(8 5,12 5)", "POINT(8 5)", "POINT(8 5)"]
]
geom1_ = LibGEOS.readgeom(wkt1_)
geom2_ = LibGEOS.readgeom(wkt2_)
geom3_ = LibGEOS.readgeom(wkt3_)
geom4_ = LibGEOS.readgeom(wkt4_)
prepGeom1_ = LibGEOS.prepareGeom(geom1_)

coords_ = LibGEOS.nearestPoints(geom1_, geom2_)
@test coords_ isa Vector{Point}
@test length(coords_) == 2
@test LibGEOS.equals(coords_[1], geom3_)
@test LibGEOS.equals(coords_[2], geom4_)

prepCoords_ = LibGEOS.nearestPoints(prepGeom1_, geom2_)
@test prepCoords_ isa Vector{Point}
@test length(prepCoords_) == 2
@test LibGEOS.equals(prepCoords_[1], geom3_)
@test LibGEOS.equals(prepCoords_[2], geom4_)

LibGEOS.destroyGeom(geom1_)
LibGEOS.destroyGeom(geom2_)
LibGEOS.destroyGeom(geom3_)
LibGEOS.destroyGeom(geom4_)
LibGEOS.destroyGeom(coords_[1])
LibGEOS.destroyGeom(coords_[2])
LibGEOS.destroyGeom(prepCoords_[1])
LibGEOS.destroyGeom(prepCoords_[2])
LibGEOS.destroyPreparedGeom(prepGeom1_)
end

# GEOSNodeTest
geom1_ = LibGEOS.readgeom("LINESTRING(0 0, 10 10, 10 0, 0 10)")
Expand Down
12 changes: 0 additions & 12 deletions test/test_geos_operations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -141,18 +141,6 @@ end
@test GeoInterface.coordinates(startPoint(g1)) [0, 0] atol = 1e-5
@test GeoInterface.coordinates(endPoint(g1)) [10, 10] atol = 1e-5

# GEOSNearestPointsTest
g1 = readgeom("POLYGON EMPTY")
g2 = readgeom("POLYGON EMPTY")
@test length(nearestPoints(g1, g2)) == 0

g1 = readgeom("POLYGON((1 1,1 5,5 5,5 1,1 1))")
g2 = readgeom("POLYGON((8 8, 9 9, 9 10, 8 8))")
points = nearestPoints(g1, g2)
@test length(points) == 2
@test GeoInterface.coordinates(points[1])[1:2] == [5.0, 5.0]
@test GeoInterface.coordinates(points[2])[1:2] == [8.0, 8.0]

# GEOSNodeTest
g1 = node(readgeom("LINESTRING(0 0, 10 10, 10 0, 0 10)"))
LibGEOS.normalize!(g1)
Expand Down

0 comments on commit 51e2d36

Please sign in to comment.