Skip to content

Commit

Permalink
Index/Item: add intersection/union support
Browse files Browse the repository at this point in the history
  • Loading branch information
adamjstewart committed Feb 5, 2022
1 parent 6f638cc commit 192ffd8
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions rtree/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,35 @@ def contains(self, coordinates, objects=False):
ctypes.byref(p_num_results))
return self._get_ids(it, p_num_results.value)

def __and__(self, other):
"""Take the intersection of two Index objects.
:param other: another index
:type other: Index
:return: a new index
:rtype: Index
"""
new_idx = Index(properties=self.properties)
for item1 in self.intersection(self.bounds, objects=True):
for item2 in other.intersection(item1.bounds, objects=True):
item3 = item1 & item2
new_idx.insert(item3.id, item3.bounds, item3.object)
return new_idx

def __or__(self, other):
"""Take the union of two Index objects.
:param other: another index
:type other: Index
:return: a new index
:rtype: Index
"""
new_idx = Index(properties=self.properties)
for old_idx in [self, other]:
for item in old_idx.intersection(old_idx.bounds, objects=True):
new_idx.insert(item.id, item.bounds, item.object)
return new_idx

def intersection(self, coordinates, objects=False):
"""Return ids or objects in the index that intersect the given
coordinates.
Expand Down Expand Up @@ -1197,6 +1226,26 @@ def __init__(self, loads, handle, owned=False):
def __gt__(self, other):
return self.id > other.id

def __and__(self, other):
"""Take the intersection of two Item objects.
:param other: another item
:type other: Item
:return: a new item
:rtype: Item
"""
pass

def __or__(self, other):
"""Take the union of two Item objects.
:param other: another item
:type other: Item
:return: a new item
:rtype: Item
"""
pass

@property
def bbox(self):
"""Returns the bounding box of the index entry"""
Expand Down

0 comments on commit 192ffd8

Please sign in to comment.