forked from tilezen/vector-datasource
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_collision_rank.py
162 lines (130 loc) · 4.81 KB
/
test_collision_rank.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
from unittest import TestCase
class CollisionRankTest(TestCase):
def test_simple_assignment(self):
# ranker should assign us a rank.
from vectordatasource.collision import CollisionRanker
ranker = CollisionRanker([
{'kind': 'foo'},
])
shape = None
props = {'kind': 'foo'}
fid = 1
rank = ranker((shape, props, fid))
self.assertEqual(rank, 1)
def test_contiguous_ranks(self):
# ranker should assign ranks contiguously in order
from vectordatasource.collision import CollisionRanker
ranker = CollisionRanker([
{'kind': 'foo'},
{'kind': 'bar'},
])
shape = None
props = {'kind': 'bar'}
fid = 1
rank = ranker((shape, props, fid))
# bar is the second in the list, so we should get 2 in response.
self.assertEqual(rank, 2)
def test_complex_filter(self):
from vectordatasource.collision import CollisionRanker
ranker = CollisionRanker([
{'kind': 'foo', 'population': {'min': 1000}},
{'kind': 'foo'},
])
# check that filter expressions can be used to modify the behaviour.
for population, expected_rank in [(999, 2), (1000, 1)]:
shape = None
props = {'kind': 'foo', 'population': population}
fid = 1
rank = ranker((shape, props, fid))
self.assertEqual(rank, expected_rank)
def test_reserved_blocks(self):
# test that if we overflow the reserved block by having too many items
# in the preceding section, then the code fails with an error.
from vectordatasource.collision import CollisionRanker
with self.assertRaises(AssertionError):
CollisionRanker([
{'foo': 'bar'},
{'baz': 'bat'},
{'_reserved': {'from': 2, 'to': 10}},
])
def test_skips_reserved_blocks(self):
# test that we skip reserved blocks in the numbering
from vectordatasource.collision import CollisionRanker
ranker = CollisionRanker([
# should be 1
{'kind': 'foo'},
# should skip 2-9 as unused
# should skip 10-20 as reserved
{'_reserved': {'from': 10, 'to': 20}},
# should be 21
{'kind': 'bar'},
])
shape = None
props = {'kind': 'bar'}
fid = 1
rank = ranker((shape, props, fid))
self.assertEqual(rank, 21)
def test_transform_adds_layer(self):
"""
Tests that the "$layer" pseudo-property is being injected by the
add_collision_rank post-processor. We use the $-prefix so that it
doesn't clash with the "layer" property, which represents something
close to z-order for features.
"""
from ModestMaps.Core import Coordinate
from shapely.geometry import Point
from tilequeue.process import Context
from tilequeue.tile import coord_to_bounds
from tilequeue.tile import num2deg
from vectordatasource.collision import CollisionRanker
from vectordatasource.transform import add_collision_rank
z, x, y = (16, 0, 0)
ranker = CollisionRanker([
{"$layer": "foo"},
])
shape = Point(*num2deg(x + 0.5, y + 0.5, z))
feature_layers = [
dict(
layer_datum=dict(name='foo'),
features=[
(shape, {}, 1),
],
),
]
nominal_zoom = z
padded_bounds = coord_to_bounds(Coordinate(zoom=z, column=x, row=y))
params = {}
resources = {'ranker': ranker}
ctx = Context(
feature_layers,
nominal_zoom,
padded_bounds,
params,
resources,
log=None,
)
# NOTE: modifies layers in-place, doesn't return anything
add_collision_rank(ctx)
# we should have assigned the collision rank because of the
# $layer match
_, props, _ = feature_layers[0]['features'][0]
self.assertEqual(props.get('collision_rank'), 1)
def test_reserved_count(self):
"""
Test that we can reserve a specific number of collision rank indices
without needing to specify the exact start/end.
"""
from vectordatasource.collision import CollisionRanker
ranker = CollisionRanker([
# should be 1
{'kind': 'foo'},
# should skip 2-10 (count 9) as reserved
{'_reserved': {'count': 9}},
# should be 11
{'kind': 'bar'},
])
shape = None
props = {'kind': 'bar'}
fid = 1
rank = ranker((shape, props, fid))
self.assertEqual(rank, 11)