From 08c8ba8daa0e7bfac1c09a50826de7e3364b9ac5 Mon Sep 17 00:00:00 2001 From: "Adam J. Stewart" Date: Sat, 5 Feb 2022 11:40:44 -0600 Subject: [PATCH] Index/Item: add intersection/union support --- rtree/index.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/rtree/index.py b/rtree/index.py index 864aa697..15e1093e 100644 --- a/rtree/index.py +++ b/rtree/index.py @@ -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]: ...