Skip to content

Commit 52d8010

Browse files
eliascarvjuliohm
andauthored
Search methods: handle different units (#1064)
* Search methods: handle different units * Minor adjustments * Minor adjustments --------- Co-authored-by: Júlio Hoffimann <[email protected]>
1 parent 23a2317 commit 52d8010

File tree

4 files changed

+39
-8
lines changed

4 files changed

+39
-8
lines changed

src/neighborsearch/ball.jl

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,13 @@ BallSearch(geoms, ball) = BallSearch(GeometrySet(geoms), ball)
2727

2828
function search(pₒ::Point, method::BallSearch; mask=nothing)
2929
tree = method.tree
30-
dmax = radius(method.ball)
3130

32-
inds = inrange(tree, ustrip.(to(pₒ)), ustrip(dmax))
31+
# adjust units of query point and radius
32+
u = unit(lentype(method.domain))
33+
r = ustrip(u, radius(method.ball))
34+
x = ustrip.(u, to(pₒ))
35+
36+
inds = inrange(tree, x, r)
3337

3438
if isnothing(mask)
3539
inds

src/neighborsearch/kball.jl

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,18 @@ KBallSearch(geoms, k, ball) = KBallSearch(GeometrySet(geoms), k, ball)
3030
maxneighbors(method::KBallSearch) = method.k
3131

3232
function searchdists!(neighbors, distances, pₒ::Point, method::KBallSearch; mask=nothing)
33-
u = unit(lentype(pₒ))
3433
tree = method.tree
35-
dmax = radius(method.ball)
3634
k = method.k
3735

38-
inds, dists = knn(tree, ustrip.(to(pₒ)), k, true)
36+
# adjust units of query point and radius
37+
u = unit(lentype(method.domain))
38+
r = ustrip(u, radius(method.ball))
39+
x = ustrip.(u, to(pₒ))
40+
41+
inds, dists = knn(tree, x, k, true)
3942

4043
# keep neighbors inside ball
41-
keep = dists .≤ ustrip(dmax)
44+
keep = dists .≤ r
4245

4346
# possibly mask some of the neighbors
4447
isnothing(mask) || (keep .*= mask[inds])

src/neighborsearch/knearest.jl

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,14 @@ KNearestSearch(geoms, k; metric=Euclidean()) = KNearestSearch(GeometrySet(geoms)
2828
maxneighbors(method::KNearestSearch) = method.k
2929

3030
function searchdists!(neighbors, distances, pₒ::Point, method::KNearestSearch; mask=nothing)
31-
u = unit(lentype(pₒ))
3231
tree = method.tree
3332
k = method.k
3433

35-
inds, dists = knn(tree, ustrip.(to(pₒ)), k, true)
34+
# adjust units of query point
35+
u = unit(lentype(method.domain))
36+
x = ustrip.(u, to(pₒ))
37+
38+
inds, dists = knn(tree, x, k, true)
3639

3740
if isnothing(mask)
3841
nneigh = k

test/neighborsearch.jl

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@
2121
n = search(cart(9, 9), s)
2222
@test Set(n) == Set([89, 90, 99, 100])
2323

24+
# different units
25+
s = BallSearch(𝒟, MetricBall(T(10) * u"dm"))
26+
n = search(Point(T(900) * u"cm", T(900) * u"cm"), s)
27+
@test Set(n) == Set([100, 99, 90])
28+
n = search(Point(T(9000) * u"mm", T(9000) * u"mm"), s)
29+
@test Set(n) == Set([100, 99, 90])
30+
2431
# non MinkowskiMetric example
2532
𝒟 = CartesianGrid((360, 180), T.((0.0, -90.0)), T.((1.0, 1.0)))
2633
s = BallSearch(𝒟, MetricBall(T(150), Haversine(T(6371))))
@@ -56,6 +63,13 @@ end
5663
@test nn == 3
5764
@test Set(n[1:nn]) == Set([100, 99, 90])
5865

66+
# different units
67+
s = KNearestSearch(𝒟, 3)
68+
n = search(Point(T(900) * u"cm", T(900) * u"cm"), s)
69+
@test Set(n) == Set([100, 99, 90])
70+
n = search(Point(T(9000) * u"mm", T(9000) * u"mm"), s)
71+
@test Set(n) == Set([100, 99, 90])
72+
5973
# construct from vector of geometries
6074
s = KNearestSearch(randpoint2(100), 3)
6175
@test s isa KNearestSearch
@@ -105,6 +119,13 @@ end
105119
@test length(n) == 4
106120
@test length(d) == 4
107121

122+
# different units
123+
s = KBallSearch(𝒟, 10, MetricBall(T(10) * u"dm"))
124+
n = search(Point(T(500) * u"cm", T(500) * u"cm"), s)
125+
@test Set(n) == Set([56, 66, 55, 57, 46])
126+
n = search(Point(T(5000) * u"mm", T(5000) * u"mm"), s)
127+
@test Set(n) == Set([56, 66, 55, 57, 46])
128+
108129
# construct from vector of geometries
109130
s = KBallSearch(randpoint2(100), 10, MetricBall(T(1)))
110131
@test s isa KBallSearch

0 commit comments

Comments
 (0)