Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
Joseph Siddons
GeoSpatialTools
Commits
55a83edd
Commit
55a83edd
authored
5 months ago
by
Joseph Siddons
Browse files
Options
Download
Email Patches
Plain Diff
fix(kdtree): increment split index if next index is above median
parent
cdffc25e
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
17 additions
and
3 deletions
+17
-3
GeoSpatialTools/kdtree.py
GeoSpatialTools/kdtree.py
+17
-3
No files found.
GeoSpatialTools/kdtree.py
View file @
55a83edd
...
...
@@ -48,18 +48,32 @@ class KDTree:
points
.
sort
(
key
=
lambda
p
:
getattr
(
p
,
self
.
variable
))
split_index
=
n_points
//
2
self
.
partition_value
=
getattr
(
points
[
split_index
-
1
],
self
.
variable
)
while
(
split_index
<
n_points
and
getattr
(
points
[
split_index
],
self
.
variable
)
==
self
.
partition_value
):
split_index
+=
1
self
.
partition_value
=
getattr
(
points
[
split_index
],
self
.
variable
)
self
.
split
=
True
# Left is <= median
self
.
child_left
=
KDTree
(
points
[:
split_index
],
depth
+
1
)
# Right is > median
self
.
child_right
=
KDTree
(
points
[
split_index
:],
depth
+
1
)
return
None
def
insert
(
self
,
point
:
Record
)
->
bool
:
"""Insert a Record into the KDTree. May unbalance the KDTree"""
"""
Insert a Record into the KDTree. May unbalance the KDTree.
The point will not be inserted if it is already in the KDTree.
"""
if
not
self
.
split
:
if
point
in
self
.
points
:
return
False
self
.
points
.
append
(
point
)
return
True
...
...
@@ -83,7 +97,7 @@ class KDTree:
return
self
.
child_right
.
delete
(
point
)
def
query
(
self
,
point
)
->
tuple
[
Record
|
None
,
float
]:
"""Find the nearest Record within the KDTree to a
_
query Record"""
"""Find the nearest Record within the KDTree to a query Record"""
if
point
.
lon
<
0
:
point2
=
Record
(
point
.
lon
+
360
,
point
.
lat
)
else
:
...
...
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment