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 23, 2022
1 parent aecb530 commit 08c8ba8
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions rtree/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,31 @@ def contains(
)
return self._get_ids(it, p_num_results.value)

def __and__(self, other: Index) -> Index:
"""Take the intersection of two Index objects.
:param other: another index
:return: a new 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: Index) -> Index:
"""Take the union of two Index objects.
:param other: another index
:return: a new 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

@overload
def intersection(self, coordinates: Any, objects: Literal[True]) -> Iterator[Item]:
...
Expand Down

0 comments on commit 08c8ba8

Please sign in to comment.