| 1 |
Testing Rtree |
|---|
| 2 |
================ |
|---|
| 3 |
|
|---|
| 4 |
Make an instance, index stored in memory |
|---|
| 5 |
|
|---|
| 6 |
>>> from rtree import Rtree |
|---|
| 7 |
>>> index = Rtree() |
|---|
| 8 |
|
|---|
| 9 |
Add 100 largish boxes randomly distributed over the domain |
|---|
| 10 |
|
|---|
| 11 |
>>> for i, coords in enumerate(boxes15): |
|---|
| 12 |
... index.add(i, coords) |
|---|
| 13 |
|
|---|
| 14 |
Find likely items |
|---|
| 15 |
|
|---|
| 16 |
>>> hits = [n for n in index.intersection((0, 0, 60, 60))] |
|---|
| 17 |
>>> len(hits) |
|---|
| 18 |
10 |
|---|
| 19 |
|
|---|
| 20 |
Find the three items nearest to this one |
|---|
| 21 |
>>> hits = [n for n in index.nearest((0,0,10,10), 3)] |
|---|
| 22 |
>>> len(hits) |
|---|
| 23 |
3 |
|---|
| 24 |
>>> hits |
|---|
| 25 |
[76L, 48L, 19L] |
|---|
| 26 |
|
|---|
| 27 |
Delete index members |
|---|
| 28 |
|
|---|
| 29 |
>>> for i, coords in enumerate(boxes15): |
|---|
| 30 |
... index.delete(i, coords) |
|---|
| 31 |
|
|---|
| 32 |
Check that we have deleted stuff |
|---|
| 33 |
|
|---|
| 34 |
>>> hits = 0 |
|---|
| 35 |
>>> hits = [n for n in index.intersection((0, 0, 60, 60))] |
|---|
| 36 |
>>> len(hits) |
|---|
| 37 |
0 |
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 |
Disk-based indexes |
|---|
| 41 |
------------------ |
|---|
| 42 |
|
|---|
| 43 |
# Clean up leftover data |
|---|
| 44 |
>>> import os |
|---|
| 45 |
>>> try: |
|---|
| 46 |
... os.remove('/tmp/default.dat') |
|---|
| 47 |
... os.remove('/tmp/default.idx') |
|---|
| 48 |
... os.remove('pagesize3.dat') |
|---|
| 49 |
... os.remove('pagesize3.idx') |
|---|
| 50 |
... os.remove('testing.dat') |
|---|
| 51 |
... os.remove('testing.idx') |
|---|
| 52 |
... except OSError: |
|---|
| 53 |
... pass |
|---|
| 54 |
|
|---|
| 55 |
# Default page size 1 |
|---|
| 56 |
>>> index = Rtree("/tmp/default") |
|---|
| 57 |
>>> for i, coords in enumerate(boxes15): |
|---|
| 58 |
... index.add(i, coords) |
|---|
| 59 |
>>> hits = [n for n in index.intersection((0, 0, 60, 60))] |
|---|
| 60 |
>>> len(hits) |
|---|
| 61 |
10 |
|---|
| 62 |
|
|---|
| 63 |
# Page size 3 |
|---|
| 64 |
>>> index = Rtree("pagesize3", pagesize=3) |
|---|
| 65 |
>>> for i, coords in enumerate(boxes15): |
|---|
| 66 |
... index.add(i, coords) |
|---|
| 67 |
>>> hits = [n for n in index.intersection((0, 0, 60, 60))] |
|---|
| 68 |
>>> len(hits) |
|---|
| 69 |
10 |
|---|
| 70 |
|
|---|
| 71 |
Test invalid name |
|---|
| 72 |
>>> index = Rtree("bogus/foo") |
|---|
| 73 |
Traceback (most recent call last): |
|---|
| 74 |
... |
|---|
| 75 |
IOError: Unable to open file 'bogus/foo' for index storage |
|---|
| 76 |
|
|---|
| 77 |
# Load a persisted index |
|---|
| 78 |
>>> _ = os.system("cp /tmp/default.dat testing.dat") |
|---|
| 79 |
>>> _ = os.system("cp /tmp/default.idx testing.idx") |
|---|
| 80 |
|
|---|
| 81 |
>>> index = Rtree("testing") |
|---|
| 82 |
>>> hits = [n for n in index.intersection((0, 0, 60, 60))] |
|---|
| 83 |
>>> len(hits) |
|---|
| 84 |
10 |
|---|
| 85 |
|
|---|
| 86 |
# Clean up leftover data |
|---|
| 87 |
>>> import os |
|---|
| 88 |
>>> try: |
|---|
| 89 |
... os.remove('/tmp/default.dat') |
|---|
| 90 |
... os.remove('/tmp/default.idx') |
|---|
| 91 |
... os.remove('pagesize3.dat') |
|---|
| 92 |
... os.remove('pagesize3.idx') |
|---|
| 93 |
... os.remove('testing.dat') |
|---|
| 94 |
... os.remove('testing.idx') |
|---|
| 95 |
... except OSError: |
|---|
| 96 |
... pass |
|---|