-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.py
451 lines (377 loc) · 13.3 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
import quadtree as module
import unittest as ut
from shapely.geometry import Polygon
from shapely.geometry import asShape
import json
class Feature(module.Node):
def contains_rectangle(self, rectangle):
# untested, just for convenience: we can use nodes as geometric features
x0,z0,x1,z1 = rectangle
return all([self.contains_point((x0,z0)),
self.contains_point((x0,z1)),
self.contains_point((x1,z0)),
self.contains_point((x1,z1))])
def intersects_rectangle(self, rectangle):
# untested, just for convenience: we can use nodes as geometric features
x0,z0,x1,z1 = rectangle
return any([self.contains_point((x0,z0)),
self.contains_point((x0,z1)),
self.contains_point((x1,z0)),
self.contains_point((x1,z1))])
class TestFeatureWrapper(ut.TestCase):
def setUp(self):
geojson = json.load(open("kings-county.geojson"))
self.feature = module.Feature(geometry=asShape(geojson['features'][0]['geometry']))
self.inside_point = (-73.9517, 40.6472)
self.outside_point = (-73.9156, 40.6129)
def test_accepts_empty_geometry(self):
empty = module.Feature(geometry=Polygon())
def test_empty_geometry_does_not_contain_point(self):
empty = module.Feature(geometry=Polygon())
self.failIf(empty.contains_point((0,0)))
def test_feature_contains_pont_accepts_feature(self):
input_feature = {
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [0.5, 0.5]
},
"properties": {
"name": "Dinagat Islands"
}
}
square = module.Feature(geometry=Polygon([(0,0), (1,0), (1,1), (0,1)]))
self.failUnless(square.contains_point(input_feature))
def test_point_inside(self):
self.failUnless(self.feature.contains_point(self.inside_point))
def test_point_outside(self):
self.failIf(self.feature.contains_point(self.outside_point))
def test_rectangle_intersects(self):
rectangle = tuple(self.inside_point+self.outside_point)
self.failUnless(self.feature.intersects_rectangle(rectangle))
def test_larger_rectangle_intersects(self):
rectangle = self.feature.geometry.bounds
self.failUnless(self.feature.intersects_rectangle(
(rectangle[0]-1,
rectangle[1]-1,
rectangle[2]+1,
rectangle[3]+1)
))
def test_does_not_contain_rectangle(self):
rectangle = tuple(self.inside_point+self.outside_point)
self.failIf(self.feature.contains_rectangle(rectangle))
def test_contains_rectangle(self):
rectangle = (self.inside_point[0], self.inside_point[1], self.inside_point[0]+0.01, self.inside_point[1]+0.01)
self.failUnless(self.feature.contains_rectangle(rectangle))
class TestSquare(ut.TestCase):
def setUp(self):
self.square = module.Feature(Polygon([(0,0), (1,0), (1,1), (0,1)]))
def test_centroid_is_inside(self):
self.failUnless(self.square.contains_point((0.5,0.5)))
def test_side_is_inside(self):
self.failUnless(self.square.contains_point((0.001,0.5)))
def test_corner_is_inside(self):
self.failUnless(self.square.contains_point((0.001,0.001)))
def test_little_square_inside(self):
self.failUnless(self.square.contains_rectangle((0.25, 0.25, 0.75, 0.75)))
def test_offset_square_not_inside(self):
self.failIf(self.square.contains_rectangle((0.5, 0.5, 1.5, 1.5)))
def test_offset_square_intersects(self):
self.failUnless(self.square.intersects_rectangle((0.5, 0.5, 1.5, 1.5)))
class TestNode(ut.TestCase):
def test_empty_node(self):
node = module.Node(None, (0,0,1,1))
class TestAddingPoint(ut.TestCase):
def setUp(self):
self.node = module.Node(None, (0,0,1,1))
self.node.add_point((0.5,0.5))
def test_point_is_inside(self):
self.failUnless(self.node.contains_point((0.5,0.5)))
def test_node_is_leaf(self):
self.assertEqual(self.node.type, module.Node.LEAF)
def test_one_point(self):
self.assertEqual(self.node.number_of_points, 1)
def test_point_is_returned(self):
self.assertEqual(self.node.points[0], (0.5,0.5))
def test_outside_point_raises_exception(self):
def callable():
self.node.add_point((1.1, 1.1))
self.assertRaises(Exception, callable)
class TestContains(ut.TestCase):
def setUp(self):
self.node = module.Node(None, (0,0,1,1))
def test_centroid_is_inside(self):
self.failUnless(self.node.contains_point((0.5,0.5)))
def test_side_is_inside(self):
self.failUnless(self.node.contains_point((0,0.5)))
def test_corner_is_inside(self):
self.failUnless(self.node.contains_point((0,0)))
class TestSubdivide(ut.TestCase):
def test_subdivide(self):
node = module.Node(None, (0,0,1,1), max_points=1)
node.subdivide()
self.assertEqual(node.type, module.Node.BRANCH)
def test_four_children(self):
node = module.Node(None, (0,0,1,1), max_points=1)
node.subdivide()
self.assertEqual(len(node.children), 4)
def test_children_coordinates(self):
node = module.Node(None, (0,0,1,1), max_points=1)
node.subdivide()
children = set([child.rectangle for child in node.children])
candidates = set([ (0,0,0.5,0.5),
(0.5,0,1,0.5),
(0,0.5,0.5,1),
(0.5,0.5,1,1)])
self.assertSetEqual(children, candidates)
def test_point_count_kept(self):
node = module.Node(None, (0,0,1,1), max_points=4)
node.add_point((0.25,0.25))
node.add_point((0.75,0.25))
node.add_point((0.25,0.75))
node.add_point((0.75,0.75))
old = node.number_of_points
node.subdivide()
self.assertEqual(node.number_of_points, old)
def test_points_are_split(self):
node = module.Node(None, (0,0,1,1), max_points=4)
node.add_point((0.25,0.25))
node.add_point((0.75,0.25))
node.add_point((0.25,0.75))
node.add_point((0.75,0.75))
node.subdivide()
for child in node.children:
self.assertEqual(child.number_of_points, 1)
class TestAutoSplitting(ut.TestCase):
def setUp(self):
self.node = module.Node(None, (0,0,1,1), max_points=1)
self.node.add_point((0.25,0.25))
self.node.add_point((0.75,0.75))
def test_node_is_split(self):
self.assertEqual(self.node.type, module.Node.BRANCH)
def test_two_points(self):
self.assertEqual(self.node.number_of_points, 2)
def test_both_points_are_inside(self):
self.failUnless(self.node.contains_point((0.25,0.25)))
self.failUnless(self.node.contains_point((0.75,0.75)))
def test_many_points_at_same_location(self):
self.node.add_point((0.25,0.25))
self.assertEqual(self.node.number_of_points, 3)
def test_points_at_same_location_do_not_split(self):
node = module.Node(None, (0,0,1,1), max_points=1)
node.add_point((0.25,0.25))
node.add_point((0.25,0.25))
self.assertEqual(node.type, module.Node.LEAF)
def test_points_at_same_location_go_to_same_square(self):
node = module.Node(None, (0,0,1,1), max_points=1)
node.add_point((0.25,0.25))
node.add_point((0.25,0.25))
node.subdivide()
self.assertEqual(node.children[0].number_of_points, 2)
class TestFeatureOverlap(ut.TestCase):
def setUp(self):
self.square = Feature(None, (0.5, 0.5, 1.5, 1.5))
self.node = module.Node(None, (0,0,1,1), max_points=3)
def test_empty_geometry_returns_zero(self):
empty = module.Feature(geometry=Polygon())
self.assertEqual(self.node.count_overlapping_points(empty), 0)
def test_zero_point(self):
self.assertEqual(self.node.count_overlapping_points(self.square), 0)
def test_zero_overlap(self):
self.node.add_point((0.25, 0.25))
self.assertEqual(self.node.count_overlapping_points(self.square), 0)
def test_one_of_two_overlap(self):
self.node.add_point((0.25, 0.25))
self.node.add_point((0.75, 0.75))
self.assertEqual(self.node.count_overlapping_points(self.square), 1)
def test_child_overlaps(self):
node = self.node
node.add_point((0.25,0.25))
node.add_point((0.75,0.25))
node.add_point((0.25,0.75))
node.add_point((0.75,0.75))
self.assertEqual(node.count_overlapping_points(self.square), 1)
def test_multiple_children_partially_overlap(self):
node = self.node
other_square = Feature(None, (0.49, 0.49, 1.49, 1.49))
node.add_point((0.25,0.25))
node.add_point((0.75,0.25))
node.add_point((0.25,0.75))
node.add_point((0.75,0.75))
self.assertEqual(node.count_overlapping_points(self.square), 1)
def test_count(self):
xs = range(100)
ys = range(100)
node = module.Node(None, (0,0,1,1), max_points=1)
for x in xs:
for y in ys:
node.add_point((x/100.0,y/100.0))
feature = Feature(None, (0.5, 0.5, 1.5, 1.5))
self.assertEqual(node.count_overlapping_points(feature), 2500)
class TestQuadTree(ut.TestCase):
def setUp(self):
self.points = [
(0,0),
(0.5,0.5),
(0.75,0.25),
(1,1)
]
self.quadtree = module.QuadTree(self.points)
def test_all_points_within_quadtree(self):
for point in self.points:
self.failUnless(self.quadtree.contains_point(point))
def test_rectangle_is_bounding_box(self):
self.assertEqual(self.quadtree.rectangle, (0,0,1,1))
def test_has_four_points(self):
self.assertEqual(self.quadtree.number_of_points, 4)
def test_known_values(self):
rectangle = Feature(None, (0.25,0,1.25,0.75))
self.assertEqual(self.quadtree.count_overlapping_points(rectangle), 2)
class TestWalk(ut.TestCase):
def test_returns_one_point(self):
points = [
(0,0),
]
quadtree = module.QuadTree(points)
self.assertSetEqual(set(quadtree.walk()), set(points))
def test_returns_four_points(self):
points = [
(0,0),
(0.5,0.5),
(0.75,0.25),
(1,1)
]
quadtree = module.QuadTree(points)
self.assertSetEqual(set(quadtree.walk()), set(points))
def test_returns_many_points(self):
points = []
for x in range(100):
for y in range(100):
points.append((float(x), float(y)))
quadtree = module.QuadTree(points)
self.assertSetEqual(set(quadtree.walk()), set(points))
class TestMetaData(ut.TestCase):
def setUp(self):
self.node = module.Node(None, (0,0,1,1), max_points=3)
def test_add_point_accepts_geometry(self):
geometry = {"type": "Point", "coordinates": [0.5, 0.5]}
self.node.add_point(geometry)
self.assertListEqual(list(self.node.points[0]), [0.5, 0.5])
def test_add_point_accepts_feature(self):
feature = {
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [0.5, 0.5]
},
"properties": {
"name": "Dinagat Islands"
}
}
self.node.add_point(feature)
self.assertListEqual(list(self.node.points[0]), [0.5, 0.5])
def test_get_all_points_returns_feature(self):
self.node.add_point((0.5, 0.5))
feature = self.node.get_all_points()[0]
self.assertEqual(feature['type'], 'Feature')
self.assertEqual(feature['geometry']['type'], 'Point')
self.assertListEqual(feature['geometry']['coordinates'], [0.5, 0.5])
self.failUnless('properties' in feature)
def test_get_all_points_returns_all(self):
self.node.add_point((0.5, 0.5))
self.node.add_point((0.75, 0.75))
self.assertEqual(self.node.number_of_points, len(self.node.get_all_points()))
def test_get_all_points_returns_children(self):
self.node.add_point((0.25, 0.25))
self.node.add_point((0.75, 0.75))
self.node.subdivide()
self.assertEqual(self.node.number_of_points, len(self.node.get_all_points()))
def test_children_hold_features(self):
self.node.add_point((0.25, 0.25))
self.node.add_point((0.75, 0.75))
self.node.subdivide()
features = []
for child in self.node.children:
features.extend(child.get_all_points())
self.assertEqual(len(features), 2)
def test_get_all_points_returns_data(self):
self.node.add_point((0.5, 0.5))
input_feature = {
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [0.5, 0.5]
},
"properties": {
"name": "Dinagat Islands"
}
}
output_feature = self.node.get_all_points()[0]
self.assertDictEqual(input_feature, output_feature)
def test_get_overlapping_points_returns_data(self):
self.node.add_point((0.25, 0.25))
input_feature = {
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [0.75, 0.75]
},
"properties": {
"name": "Dinagat Islands"
}
}
self.node.add_point(input_feature)
square = Feature(None, (0.5, 0.5, 1.5, 1.5))
self.assertDictEqual(input_feature, self.node.get_overlapping_points(square)[0])
def test_get_overlapping_points_same_as_count(self):
xs = range(100)
ys = range(100)
node = module.Node(None, (0,0,1,1), max_points=1)
for x in xs:
for y in ys:
node.add_point((x/100.0,y/100.0))
feature = Feature(None, (0.5, 0.5, 1.5, 1.5))
self.assertEqual(node.count_overlapping_points(feature), len(node.get_overlapping_points(feature)))
def test_contains_pont_accepts_feature(self):
input_feature = {
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [0.5, 0.5]
},
"properties": {
"name": "Dinagat Islands"
}
}
self.failUnless(self.node.contains_point(input_feature))
def test_contains_pont_accepts_geometry(self):
input_feature = {
"type": "Point",
"coordinates": [0.5, 0.5]
}
self.failUnless(self.node.contains_point(input_feature))
def test_features_can_initialize_quadtree(self):
feature1 = {
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [0.25, 0.25]
},
"properties": {
"name": "Dinagat Islands"
}
}
feature2 = {
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [0.75, 0.75]
},
"properties": {
"name": "Dinagat Islands"
}
}
quadtree = module.QuadTree([feature1, feature2])
self.assertEqual(quadtree.rectangle, (0.25, 0.25, 0.75, 0.75))
if __name__ == '__main__':
ut.main()