32 lines
890 B
Python
32 lines
890 B
Python
from rtree import index
|
|
from pprint import pprint
|
|
import os, sys
|
|
import rtree
|
|
|
|
|
|
# Create a new index
|
|
idx = rtree.index.Index()
|
|
|
|
# Insert a rectangle (minx, miny, maxx, maxy) with a unique identifier
|
|
idx.insert(0, (0, 0, 0, 0))
|
|
idx.insert(1, (30, 30, 30, 30))
|
|
idx.insert(2, (40, 40, 40, 40))
|
|
|
|
# Query all rectangles that intersect with the given rectangle
|
|
result = idx.intersection((2, 2, 8, 8))
|
|
# for id in result:
|
|
# print(id) # prints: 0
|
|
|
|
# Query all rectangles that are completely contained within the given rectangle
|
|
# result = idx.intersection((1, 1, 9, 9), objects=True)
|
|
# for id, bounds in result:
|
|
# print(id, bounds) # prints: (0, (0, 0, 10, 10))
|
|
|
|
# # Query all rectangles that contain the given point
|
|
result = idx.nearest((40, 35), 1)
|
|
for id in result:
|
|
print(id) # prints: (0, (0, 0, 10, 10))
|
|
|
|
# # Delete a rectangle by its identifier
|
|
# idx.delete(0, (0, 0, 10, 10))
|