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
d58f82b9
Commit
d58f82b9
authored
5 months ago
by
Joseph Siddons
Browse files
Options
Download
Email Patches
Plain Diff
fix: revert median partition change, split on index. Account for this in insert and delete methods.
parent
1836b2c4
main
10-improve-documentation
v0.11.2
v0.11.1
v0.11.0
v0.10.1
v0.10.0
v0.9.0
v0.8.0
v0.7.1
v0.7.0
v0.6.0
v0.5.0
v0.4.3
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
33 additions
and
13 deletions
+33
-13
GeoSpatialTools/kdtree.py
GeoSpatialTools/kdtree.py
+33
-13
No files found.
GeoSpatialTools/kdtree.py
View file @
d58f82b9
...
...
@@ -49,18 +49,18 @@ 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
#
while (
#
split_index < n_points
#
and getattr(points[split_index], self.variable)
#
== self.partition_value
#
):
#
split_index += 1
self
.
split
=
True
# Left is
<= median
# Left is
points left of midpoint
self
.
child_left
=
KDTree
(
points
[:
split_index
],
depth
+
1
)
# Right is
> median
# Right is
points right of midpoint
self
.
child_right
=
KDTree
(
points
[
split_index
:],
depth
+
1
)
return
None
...
...
@@ -79,8 +79,25 @@ class KDTree:
if
getattr
(
point
,
self
.
variable
)
<
self
.
partition_value
:
return
self
.
child_left
.
insert
(
point
)
el
s
e
:
el
if
getattr
(
point
,
self
.
variable
)
>
self
.
partition_valu
e
:
return
self
.
child_right
.
insert
(
point
)
else
:
r
,
_
=
self
.
query
(
point
)
if
point
in
r
:
return
False
self
.
child_left
.
_insert
(
point
)
return
True
def
_insert
(
self
,
point
:
Record
)
->
None
:
"""Insert a point even if it already exists in the KDTree"""
if
not
self
.
split
:
self
.
points
.
append
(
point
)
return
if
getattr
(
point
,
self
.
variable
)
<=
self
.
partition_value
:
self
.
child_left
.
_insert
(
point
)
else
:
self
.
child_right
.
_insert
(
point
)
return
def
delete
(
self
,
point
:
Record
)
->
bool
:
"""Delete a Record from the KDTree. May unbalance the KDTree"""
...
...
@@ -91,10 +108,13 @@ class KDTree:
except
ValueError
:
return
False
if
getattr
(
point
,
self
.
variable
)
<
self
.
partition_value
:
return
self
.
child_left
.
delete
(
point
)
else
:
return
self
.
child_right
.
delete
(
point
)
if
getattr
(
point
,
self
.
variable
)
<=
self
.
partition_value
:
if
self
.
child_left
.
delete
(
point
):
return
True
if
getattr
(
point
,
self
.
variable
)
>=
self
.
partition_value
:
if
self
.
child_right
.
delete
(
point
):
return
True
return
False
def
query
(
self
,
point
)
->
tuple
[
list
[
Record
],
float
]:
"""Find the nearest Record within the KDTree to a query Record"""
...
...
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