Skip to content

Commit

Permalink
Compute intersection bounding box
Browse files Browse the repository at this point in the history
  • Loading branch information
adamjstewart committed Mar 4, 2022
1 parent 0c68607 commit c27cef2
Showing 1 changed file with 35 additions and 4 deletions.
39 changes: 35 additions & 4 deletions rtree/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -668,24 +668,55 @@ def __and__(self, other: Index) -> Index:
:param other: another index
:return: a new index
:raises AssertionError: if self and other have different interleave or dimension
"""
new_idx = Index(properties=self.properties)
assert self.interleaved == other.interleaved
assert self.properties.dimension == other.properties.dimension

i = 0
new_idx = Index(interleaved=self.interleaved, properties=self.properties)

# For each Item in self...
for item1 in self.intersection(self.bounds, objects=True):
# For each Item in other that intersects...
for item2 in other.intersection(item1.bounds, objects=True):
item3 = item1 & item2
new_idx.insert(item3.id, item3.bounds, item3.object)
# Compute the intersection bounding box
bounds = []
for j in range(len(item1.bounds)):
if self.interleaved:
if j < len(item1.bounds) // 2:
bounds.append(max(item1.bounds[j], item2.bounds[j]))
else:
bounds.append(min(item1.bounds[j], item2.bounds[j]))
else:
if j % 2 == 0:
bounds.append(max(item1.bounds[j], item2.bounds[j]))
else:
bounds.append(min(item1.bounds[j], item2.bounds[j]))

new_idx.insert(i, bounds, (item1.object, item2.object))
i += 1

return new_idx

def __or__(self, other: Index) -> Index:
"""Take the union of two Index objects.
:param other: another index
:return: a new index
:raises AssertionError: if self and other have different interleave or dimension
"""
new_idx = Index(properties=self.properties)
assert self.interleaved == other.interleaved
assert self.properties.dimension == other.properties.dimension

new_idx = Index(interleaved=self.interleaved, properties=self.properties)

# For each index...
for old_idx in [self, other]:
# For each item...
for item in old_idx.intersection(old_idx.bounds, objects=True):
new_idx.insert(item.id, item.bounds, item.object)

return new_idx

@overload
Expand Down

0 comments on commit c27cef2

Please sign in to comment.