Skip to content

Commit a959d15

Browse files
authored
Add temporary svec util (#1186)
1 parent 7959564 commit a959d15

File tree

6 files changed

+22
-17
lines changed

6 files changed

+22
-17
lines changed

src/neighborsearch.jl

-7
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,3 @@ end
9595
include("neighborsearch/ball.jl")
9696
include("neighborsearch/knearest.jl")
9797
include("neighborsearch/kball.jl")
98-
99-
# -----------------
100-
# HELPER FUNCTIONS
101-
# -----------------
102-
103-
# NearestNeighbors.jl only accepts AbstractVector
104-
_svector(c::CRS) = SVector(CoordRefSystems.raw(c))

src/neighborsearch/ball.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ end
2020

2121
function BallSearch(domain::D, ball::B) where {D<:Domain,B<:MetricBall}
2222
m = metric(ball)
23-
xs = [_svector(coords(centroid(domain, i))) for i in 1:nelements(domain)]
23+
xs = [svec(centroid(domain, i)) for i in 1:nelements(domain)]
2424
tree = m isa MinkowskiMetric ? KDTree(xs, m) : BallTree(xs, m)
2525
BallSearch{D,B,typeof(tree)}(domain, ball, tree)
2626
end
@@ -36,7 +36,7 @@ function search(pₒ::Point, method::BallSearch; mask=nothing)
3636
r = ustrip(u, radius(method.ball))
3737

3838
# adjust CRS of query point
39-
x = _svector(convert(C, coords(pₒ)))
39+
x = svec(convert(C, coords(pₒ)))
4040

4141
inds = inrange(tree, x, r)
4242

src/neighborsearch/kball.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ end
2222

2323
function KBallSearch(domain::D, k::Int, ball::B) where {D<:Domain,B<:MetricBall}
2424
m = metric(ball)
25-
xs = [_svector(coords(centroid(domain, i))) for i in 1:nelements(domain)]
25+
xs = [svec(centroid(domain, i)) for i in 1:nelements(domain)]
2626
tree = m isa MinkowskiMetric ? KDTree(xs, m) : BallTree(xs, m)
2727
KBallSearch{D,B,typeof(tree)}(domain, k, ball, tree)
2828
end
@@ -41,7 +41,7 @@ function searchdists!(neighbors, distances, pₒ::Point, method::KBallSearch; ma
4141
r = ustrip(u, radius(method.ball))
4242

4343
# adjust CRS of query point
44-
x = _svector(convert(C, coords(pₒ)))
44+
x = svec(convert(C, coords(pₒ)))
4545

4646
inds, dists = knn(tree, x, k, true)
4747

src/neighborsearch/knearest.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ struct KNearestSearch{D<:Domain,T} <: BoundedNeighborSearchMethod
1818
end
1919

2020
function KNearestSearch(domain::D, k::Int; metric=Euclidean()) where {D<:Domain}
21-
xs = [_svector(coords(centroid(domain, i))) for i in 1:nelements(domain)]
21+
xs = [svec(centroid(domain, i)) for i in 1:nelements(domain)]
2222
tree = metric isa MinkowskiMetric ? KDTree(xs, metric) : BallTree(xs, metric)
2323
KNearestSearch{D,typeof(tree)}(domain, k, tree)
2424
end
@@ -34,7 +34,7 @@ function searchdists!(neighbors, distances, pₒ::Point, method::KNearestSearch;
3434
k = method.k
3535

3636
# adjust CRS of query point
37-
x = _svector(convert(C, coords(pₒ)))
37+
x = svec(convert(C, coords(pₒ)))
3838

3939
inds, dists = knn(tree, x, k, true)
4040

src/traversing/source.jl

+3-4
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ function traverse(domain, path::SourcePath)
2323
assertion(length(sources) nelements(domain), "more sources than points in object")
2424

2525
# fit search tree
26-
xs = [ustrip.(to(centroid(domain, s))) for s in sources]
27-
kdtree = KDTree(xs)
26+
kdtree = KDTree([svec(centroid(domain, s)) for s in sources])
2827

2928
# other locations that are not sources
3029
others = setdiff(1:nelements(domain), sources)
@@ -35,8 +34,8 @@ function traverse(domain, path::SourcePath)
3534
# compute distances to sources
3635
dists = []
3736
for batch in batches
38-
coords = [ustrip.(to(centroid(domain, b))) for b in batch]
39-
_, ds = knn(kdtree, coords, length(sources), true)
37+
xs = [svec(centroid(domain, b)) for b in batch]
38+
_, ds = knn(kdtree, xs, length(sources), true)
4039
append!(dists, ds)
4140
end
4241

src/utils/crs.jl

+13
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,19 @@ ignoring the original units of the coordinate reference system.
3333
flat(p::Point) = Point(flat(coords(p)))
3434
flat(c::CRS) = Cartesian{datum(c)}(CoordRefSystems.raw(c))
3535

36+
"""
37+
svec(p)
38+
39+
Return `SVector` with raw coordinates of point `p`.
40+
41+
### Notes
42+
43+
This utility function exists because NearestNeighbors.jl
44+
currently only accepts coordinates of type `AbstractVector`.
45+
"""
46+
svec(p::Point) = svec(coords(p))
47+
svec(c::CRS) = SVector(CoordRefSystems.raw(c))
48+
3649
"""
3750
coordsum(points; weights=nothing)
3851

0 commit comments

Comments
 (0)