root/Rtree/trunk/tests/index.txt

Revision 1613, 7.9 KB (checked in by hobu, 5 months ago)

shut off .leaves() test due to precision issues

Line 
1Testing Rtree
2================
3
4    >>> from rtree import index
5    >>> from rtree.index import Rtree
6
7Ensure libspatialindex version is >= 1.4.0
8    >>> index.__version__.split('.')[1] >= 4
9    True
10   
11Make an instance, index stored in memory
12   
13    >>> p = index.Property()
14   
15    >>> idx = index.Index(properties=p)
16    >>> idx
17    <rtree.index.Index object at 0x...>
18   
19Add 100 largish boxes randomly distributed over the domain
20   
21    >>> for i, coords in enumerate(boxes15):
22    ...     idx.add(i, coords)
23   
24    >>> 0 in idx.intersection((0, 0, 60, 60))
25    True
26    >>> hits = list(idx.intersection((0, 0, 60, 60)))
27    >>> len(hits)
28    10
29    >>> hits
30    [0L, 4L, 16L, 27L, 35L, 40L, 47L, 50L, 76L, 80L]
31
32Insert an object into the index that can be pickled
33
34    >>> idx.insert(4321, (34.3776829412, 26.7375853734, 49.3776829412, 41.7375853734), obj=42)
35
36Fetch our straggler that contains a pickled object   
37    >>> hits = idx.intersection((0, 0, 60, 60), objects=True)
38    >>> for i in hits:
39    ...     if i.id == 4321:
40    ...         i.object
41    ...         i.bbox
42    42
43    [34.3776829412, 26.737585373400002, 49.3776829412, 41.737585373400002]
44
45
46Find the three items nearest to this one
47    >>> hits = list(idx.nearest((0,0,10,10), 3))
48    >>> hits
49    [76L, 48L, 19L]
50    >>> len(hits)
51    3
52   
53
54Default order is [xmin, ymin, xmax, ymax]
55    >>> idx.bounds
56    [-186.673789279, -96.717721818399994, 184.76138755599999, 96.604369977800005]
57
58To get in order [xmin, xmax, ymin, ymax (... for n-d indexes)] use the kwarg:
59    >>> idx.get_bounds(coordinate_interleaved=False)
60    [-186.673789279, 184.76138755599999, -96.717721818399994, 96.604369977800005]
61
62Delete index members
63
64    >>> for i, coords in enumerate(boxes15):
65    ...     idx.delete(i, coords)
66
67Delete our straggler too
68    >>> idx.delete(4321, (34.3776829412, 26.7375853734, 49.3776829412, 41.7375853734) )
69
70Check that we have deleted stuff
71
72    >>> hits = 0
73    >>> hits = list(idx.intersection((0, 0, 60, 60)))
74    >>> len(hits)
75    0
76   
77#    >>> assert(idx.valid())
78
79Check that nearest returns *all* of the items that are nearby
80    >>> idx2 = Rtree()
81    >>> idx2
82    <rtree.index.Index object at 0x...>
83
84    >>> locs = [(14, 10, 14, 10),
85    ...         (16, 10, 16, 10)]
86   
87    >>> for i, (minx, miny, maxx, maxy) in enumerate(locs):
88    ...        idx2.add(i, (minx, miny, maxx, maxy))
89
90    >>> sorted(idx2.nearest((15, 10, 15, 10), 1))
91    [0L, 1L]
92
93
94Check that nearest returns *all* of the items that are nearby (with objects)
95    >>> idx2 = Rtree()
96    >>> idx2
97    <rtree.index.Index object at 0x...>
98
99    >>> locs = [(14, 10, 14, 10),
100    ...         (16, 10, 16, 10)]
101   
102    >>> for i, (minx, miny, maxx, maxy) in enumerate(locs):
103    ...        idx2.add(i, (minx, miny, maxx, maxy), obj={'a': 42})
104
105    >>> sorted([(i.id, i.object) for i in idx2.nearest((15, 10, 15, 10), 1, objects=True)])
106    [(0L, {'a': 42}), (1L, {'a': 42})]
107
108
109    >>> idx2 = Rtree()
110    >>> idx2
111    <rtree.index.Index object at 0x...>
112           
113    >>> locs = [(2, 4), (6, 8), (10, 12), (11, 13), (15, 17), (13, 20)]
114   
115    >>> for i, (start, stop) in enumerate(locs):
116    ...        idx2.add(i, (start, 1, stop, 1))
117   
118    >>> sorted(idx2.nearest((13, 0, 20, 2), 1))
119    [3L, 4L, 5L]
120
121Default page size 4096
122
123    >>> idx3 = Rtree(u"defaultidx")
124    >>> for i, coords in enumerate(boxes15):
125    ...     idx3.add(i, coords)
126    >>> hits = list(idx3.intersection((0, 0, 60, 60)))
127    >>> len(hits)
128    10
129
130Make sure to delete the index or the file is not flushed and it
131will be invalid
132
133    >>> del idx3
134
135Page size 3
136
137    >>> idx4 = Rtree("pagesize3", pagesize=3)
138    >>> for i, coords in enumerate(boxes15):
139    ...     idx4.add(i, coords)
140    >>> hits = list(idx4.intersection((0, 0, 60, 60)))
141    >>> len(hits)
142    10
143   
144    >>> idx4.close()
145    >>> del idx4
146   
147Test invalid name
148
149    >>> inv = Rtree("bogus/foo")
150    Traceback (most recent call last):
151    ...
152    IOError: Unable to open file 'bogus/foo.idx' for index storage
153
154Load a persisted index
155
156    >>> import shutil
157    >>> _ = shutil.copy("defaultidx.dat", "testing.dat")
158    >>> _ = shutil.copy("defaultidx.idx", "testing.idx")
159
160    # >>> import pdb;pdb.set_trace()
161
162    >>> idx = Rtree("testing")
163    >>> hits = list(idx.intersection((0, 0, 60, 60)))
164    >>> len(hits)
165    10
166
167Make a 3D index
168    >>> p = index.Property()
169    >>> p.dimension = 3
170   
171
172with interleaved=False, the order of input and output is:
173(xmin, xmax, ymin, ymax, zmin, zmax)
174
175    >>> idx3d = index.Index(properties=p, interleaved=False)
176    >>> idx3d
177    <rtree.index.Index object at 0x...>
178   
179    >>> idx3d.insert(1, (0, 0, 60, 60, 22, 22.0))
180   
181    >>> 1 in idx3d.intersection((-1, 1, 58, 62, 22, 24))
182    True
183
184
185Make a 4D index
186    >>> p = index.Property()
187    >>> p.dimension = 4
188   
189
190with interleaved=False, the order of input and output is: (xmin, xmax, ymin, ymax, zmin, zmax, kmin, kmax)
191
192    >>> idx4d = index.Index(properties=p, interleaved=False)
193    >>> idx4d
194    <rtree.index.Index object at 0x...>
195   
196    >>> idx4d.insert(1, (0, 0, 60, 60, 22, 22.0, 128, 142))
197   
198    >>> 1 in idx4d.intersection((-1, 1, 58, 62, 22, 24, 120, 150))
199    True
200
201Check that we can make an index with custom filename extensions
202
203    >>> p = index.Property()
204    >>> p.dat_extension = 'data'
205    >>> p.idx_extension = 'index'
206   
207    >>> idx_cust = Rtree('custom', properties=p)
208    >>> for i, coords in enumerate(boxes15):
209    ...     idx_cust.add(i, coords)
210    >>> hits = list(idx_cust.intersection((0, 0, 60, 60)))
211    >>> len(hits)
212    10
213   
214    >>> del idx_cust
215   
216Reopen the index
217    >>> p2 = index.Property()
218    >>> p2.dat_extension = 'data'
219    >>> p2.idx_extension = 'index'
220   
221    >>> idx_cust2 = Rtree('custom', properties=p2)   
222    >>> hits = list(idx_cust2.intersection((0, 0, 60, 60)))
223    >>> len(hits)
224    10
225   
226    >>> del idx_cust2
227
228Adding the same id twice does not overwrite existing data
229
230    >>> r = Rtree()
231    >>> r.add(1, (2, 2))
232    >>> r.add(1, (3, 3))
233    >>> list(r.intersection((0, 0, 5, 5)))
234    [1L, 1L]
235
236A stream of data need that needs to be an iterator that will raise a
237StopIteration. The order depends on the interleaved kwarg sent to the
238constructor.
239
240The object can be None, but you must put a place holder of 'None' there.
241
242    >>> p = index.Property()
243    >>> def data_gen(interleaved=True):
244    ...    for i, (minx, miny, maxx, maxy) in enumerate(boxes15):
245    ...        if interleaved:
246    ...            yield (i, (minx, miny, maxx, maxy), 42)
247    ...        else:
248    ...            yield (i, (minx, maxx, miny, maxy), 42)
249
250    >>> strm_idx = index.Rtree(data_gen(), properties = p)
251
252    >>> hits = list(strm_idx.intersection((0, 0, 60, 60)))
253
254    >>> len(hits)
255    10
256   
257   
258    >>> sorted(hits)
259    [0L, 4L, 16L, 27L, 35L, 40L, 47L, 50L, 76L, 80L]
260
261    >>> hits = list(strm_idx.intersection((0, 0, 60, 60), objects=True))
262    >>> len(hits)
263    10
264   
265    >>> hits[0].object
266    42
267
268Try streaming against a persisted index without interleaving.
269    >>> strm_idx = index.Rtree('streamed', data_gen(interleaved=False), properties = p, interleaved=False)
270
271Note the arguments to intersection must be xmin, xmax, ymin, ymax for interleaved=False
272    >>> hits = list(strm_idx.intersection((0, 60, 0, 60)))
273    >>> len(hits)
274    10
275   
276    >>> sorted(hits)
277    [0L, 4L, 16L, 27L, 35L, 40L, 47L, 50L, 76L, 80L]
278
279    >>> hits = list(strm_idx.intersection((0, 60, 0, 60), objects=True))
280    >>> len(hits)
281    10
282   
283    >>> hits[0].object
284    42
285
286    >>> hits = list(strm_idx.intersection((0, 60, 0, 60), objects='raw'))
287    >>> hits[0]
288    42
289    >>> len(hits)
290    10
291   
292    >>> strm_idx.count((0, 60, 0, 60))
293    10L
294   
295    >>> del strm_idx
296
297    >>> p = index.Property()
298    >>> p.leaf_capacity = 100
299    >>> p.fill_factor = 0.5
300    >>> p.index_capacity = 10
301    >>> p.near_minimum_overlap_factor = 7
302    >>> idx = index.Index(data_gen(interleaved=False), properties = p, interleaved=False)
303
304    >>> leaves = idx.leaves()
305
306    >>> del idx
Note: See TracBrowser for help on using the browser.