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
NOCSurfaceProcesses
GeoSpatialTools
Commits
87449a8a
Commit
87449a8a
authored
4 months ago
by
Joseph Siddons
Browse files
Options
Download
Email Patches
Plain Diff
feat: add method to remove Record from QuadTree and OctTree classes
parent
5366dd38
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
120 additions
and
0 deletions
+120
-0
GeoSpatialTools/octtree.py
GeoSpatialTools/octtree.py
+35
-0
GeoSpatialTools/quadtree.py
GeoSpatialTools/quadtree.py
+27
-0
test/test_octtree.py
test/test_octtree.py
+35
-0
test/test_quadtree.py
test/test_quadtree.py
+23
-0
No files found.
GeoSpatialTools/octtree.py
View file @
87449a8a
...
...
@@ -591,6 +591,41 @@ class OctTree:
return
True
return
False
def
remove
(
self
,
point
:
SpaceTimeRecord
)
->
bool
:
"""
Remove a SpaceTimeRecord from the OctTree if it is in the OctTree.
Returns True if the SpaceTimeRecord is removed.
"""
if
not
self
.
boundary
.
contains
(
point
):
return
False
if
point
in
self
.
points
:
self
.
points
.
remove
(
point
)
return
True
if
not
self
.
divided
:
return
False
if
self
.
northwestback
.
remove
(
point
):
return
True
elif
self
.
northeastback
.
remove
(
point
):
return
True
elif
self
.
southwestback
.
remove
(
point
):
return
True
elif
self
.
southeastback
.
remove
(
point
):
return
True
elif
self
.
northwestfwd
.
remove
(
point
):
return
True
elif
self
.
northeastfwd
.
remove
(
point
):
return
True
elif
self
.
southwestfwd
.
remove
(
point
):
return
True
elif
self
.
southeastfwd
.
remove
(
point
):
return
True
return
False
def
query
(
self
,
rect
:
SpaceTimeRectangle
,
...
...
This diff is collapsed.
Click to expand it.
GeoSpatialTools/quadtree.py
View file @
87449a8a
...
...
@@ -404,6 +404,33 @@ class QuadTree:
return
True
return
False
def
remove
(
self
,
point
:
Record
)
->
bool
:
"""
Remove a Record from the QuadTree if it is in the QuadTree.
Returns True if the Record is removed.
"""
if
not
self
.
boundary
.
contains
(
point
):
return
False
if
point
in
self
.
points
:
self
.
points
.
remove
(
point
)
return
True
if
not
self
.
divided
:
return
False
if
self
.
northwest
.
remove
(
point
):
return
True
elif
self
.
northeast
.
remove
(
point
):
return
True
elif
self
.
southwest
.
remove
(
point
):
return
True
elif
self
.
southeast
.
remove
(
point
):
return
True
return
False
def
query
(
self
,
rect
:
Rectangle
,
...
...
This diff is collapsed.
Click to expand it.
test/test_octtree.py
View file @
87449a8a
...
...
@@ -223,6 +223,41 @@ class TestOctTree(unittest.TestCase):
]
assert
res
==
expected
def
test_remove
(
self
):
d
=
datetime
(
2023
,
3
,
24
,
12
,
0
)
dt
=
timedelta
(
days
=
10
)
start
=
d
-
dt
end
=
d
+
dt
boundary
=
Rectangle
(
0
,
20
,
0
,
8
,
start
,
end
)
otree
=
OctTree
(
boundary
,
capacity
=
3
)
points
:
list
[
Record
]
=
[
Record
(
10
,
4
,
d
,
"main"
),
Record
(
12
,
1
,
d
+
timedelta
(
hours
=
3
),
"main2"
),
Record
(
3
,
7
,
d
-
timedelta
(
days
=
3
),
"main3"
),
Record
(
13
,
2
,
d
+
timedelta
(
hours
=
17
),
"southeastfwd"
),
Record
(
3
,
6
,
d
-
timedelta
(
days
=
1
),
"northwestback"
),
Record
(
10
,
4
,
d
,
"northwestback"
),
Record
(
18
,
2
,
d
+
timedelta
(
days
=
23
),
"not added"
),
Record
(
11
,
7
,
d
+
timedelta
(
hours
=
2
),
"northeastfwd"
),
]
to_remove
=
points
[
4
]
for
point
in
points
:
otree
.
insert
(
point
)
# TEST: query works before remove
q_res
=
otree
.
nearby_points
(
to_remove
,
dist
=
0.1
,
t_dist
=
timedelta
(
minutes
=
5
)
)
assert
len
(
q_res
)
==
1
# TEST: point is removed and query fails
assert
otree
.
remove
(
to_remove
)
q_res
=
otree
.
nearby_points
(
to_remove
,
dist
=
0.1
,
t_dist
=
timedelta
(
minutes
=
5
)
)
assert
len
(
q_res
)
==
0
def
test_query
(
self
):
d
=
datetime
(
2023
,
3
,
24
,
12
,
0
)
dt
=
timedelta
(
days
=
10
)
...
...
This diff is collapsed.
Click to expand it.
test/test_quadtree.py
View file @
87449a8a
...
...
@@ -103,6 +103,29 @@ class TestQuadTree(unittest.TestCase):
]
assert
res
==
expected
def
test_remove
(
self
):
boundary
=
Rectangle
(
0
,
20
,
0
,
8
)
qtree
=
QuadTree
(
boundary
,
capacity
=
3
)
points
:
list
[
Record
]
=
[
Record
(
10
,
5
),
Record
(
19
,
1
),
Record
(
0
,
0
),
Record
(
-
2
,
-
9.2
),
Record
(
12.8
,
2.1
),
]
to_remove
=
points
[
2
]
for
point
in
points
:
qtree
.
insert
(
point
)
q_res
=
qtree
.
nearby_points
(
to_remove
,
dist
=
0.1
)
# TEST: get the point
assert
len
(
q_res
)
==
1
# TEST: Point is removed
assert
qtree
.
remove
(
to_remove
)
q_res
=
qtree
.
nearby_points
(
to_remove
,
dist
=
0.1
)
assert
len
(
q_res
)
==
0
def
test_query
(
self
):
boundary
=
Rectangle
(
0
,
20
,
0
,
8
)
qtree
=
QuadTree
(
boundary
,
capacity
=
3
)
...
...
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