Spaces:
Running
Running
File size: 1,619 Bytes
9ee6f68 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | from rtree import index
import numpy as np
from src.utils.config_manager import get_rectangle_query_config
class RectangleQuery2D:
@classmethod
def get_config(cls):
return get_rectangle_query_config()
def __init__(self, dimension=2):
self.property = index.Property()
self.property.dimension = dimension
self.property.overwrite = True
self.index = index.Index(properties=self.property)
self.data = {}
self.current_id = 0
self.rectangle_list = []
def add_rectangle(self, rectangle):
import glog
# glog.info(f"Adding rectangle: {rectangle}")
x1 = np.min(rectangle[:, 0])
y1 = np.min(rectangle[:, 1])
x2 = np.max(rectangle[:, 0])
y2 = np.max(rectangle[:, 1])
self.index.insert(self.current_id, (x1, y1, x2, y2))
self.data[self.current_id] = rectangle
self.rectangle_list.append(rectangle)
self.current_id += 1
def query_rectangle(self, rectangle, tol=None):
"""
Queries the rectangle with an optional tolerance.
Returns the indices of the intersecting rectangles and their corresponding data.
"""
if tol is None:
tol = self.get_config().EPS / 2
x1 = np.min(rectangle[:, 0])
y1 = np.min(rectangle[:, 1])
x2 = np.max(rectangle[:, 0])
y2 = np.max(rectangle[:, 1])
x1 += tol
y1 += tol
x2 -= tol
y2 -= tol
results = list(self.index.intersection((x1, y1, x2, y2)))
return results, [self.data[i] for i in results]
|