-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgeobox2_tests.py
137 lines (109 loc) · 4.24 KB
/
geobox2_tests.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
"""
Tests for geobox2. Can be run with nosests, e.g:
$ nosetests geobox2_tests.py
"""
import geobox2
import decimal
def testboundingbox():
lat = _dec(42.270872)
lon = _dec(-83.726329)
a2 = geobox2.Geobox(lat, lon)
scope = _dec(1.0)
top, left, bottom, right = a2.bounding_box(lat, lon, scope)
assert top > lat, "top > lat"
assert bottom < lat, "bottom < lat"
assert left < lon, "left < lon"
assert right > lon, "right > lon"
eq_(top, _dec(43.0), "top")
eq_(bottom, _dec(42.0), "bottom")
eq_(left, _dec(-84.0), "left")
eq_(right, _dec(-83.0), "right")
class TestAppend(object):
def setup(self):
self.oldscopes = geobox2.SCOPE_SIZES
geobox2.SCOPE_SIZES = [_dec(1.0)]
self.center = "43.000|-84.000|42.000|-83.000"
self.left = "43.000|-85.000|42.000|-84.000"
self.right = "43.000|-83.000|42.000|-82.000"
self.down = "42.000|-84.000|41.000|-83.000"
self.up = "44.000|-84.000|43.000|-83.000"
self.leftdown = "42.000|-85.000|41.000|-84.000"
self.leftup = "44.000|-85.000|43.000|-84.000"
self.rightdown = "42.000|-83.000|41.000|-82.000"
self.rightup = "44.000|-83.000|43.000|-82.000"
def teardown(self):
geobox2.SCOPE_SIZES = self.oldscopes
def testonebox(self):
"with point in middle of scope, should be no adjacent boxes added"
lat = _dec(42.5)
lon = _dec(-83.5)
pt = geobox2.Geobox(lat, lon)
boxes = pt.storage_geoboxes()
eq_([self.center], boxes)
def testappendleft(self):
"with point near left edge, we expect an extra box to the left"
lat = _dec(42.5)
lon = _dec(-83.8)
pt = geobox2.Geobox(lat, lon)
boxes = pt.storage_geoboxes()
eq_([self.center, self.left], boxes)
def testappendright(self):
"with point near right edge, we expect an extra box to the right"
lat = _dec(42.5)
lon = _dec(-83.2)
pt = geobox2.Geobox(lat, lon)
boxes = pt.storage_geoboxes()
eq_([self.center, self.right], boxes)
def testappendup(self):
"with point near top edge, we expect an extra box to the top"
lat = _dec(42.8)
lon = _dec(-83.5)
pt = geobox2.Geobox(lat, lon)
boxes = pt.storage_geoboxes()
eq_([self.center, self.up], boxes)
def testappenddown(self):
"with point near bottom edge, we expect an extra box to the bottom"
lat = _dec(42.2)
lon = _dec(-83.5)
pt = geobox2.Geobox(lat, lon)
boxes = pt.storage_geoboxes()
eq_([self.center, self.down], boxes)
def testappendbottomleft(self):
"with point near bottom left edge, we expect extra boxes for the corner"
lat = _dec(42.2)
lon = _dec(-83.8)
pt = geobox2.Geobox(lat, lon)
boxes = pt.storage_geoboxes()
eq_(sorted([self.center, self.left, self.down, self.leftdown]), sorted(boxes))
def testappendtopleft(self):
"with point near top left edge, we expect extra boxes for the corner"
lat = _dec(42.8)
lon = _dec(-83.8)
pt = geobox2.Geobox(lat, lon)
boxes = pt.storage_geoboxes()
eq_(sorted([self.center, self.left, self.up, self.leftup]), sorted(boxes))
def testappendbottomright(self):
"with point near bottom right edge, we expect extra boxes for the corner"
lat = _dec(42.2)
lon = _dec(-83.2)
pt = geobox2.Geobox(lat, lon)
boxes = pt.storage_geoboxes()
eq_(sorted([self.center, self.right, self.down, self.rightdown]), sorted(boxes))
def testappendtopright(self):
"with point near top left edge, we expect extra boxes for the corner"
lat = _dec(42.8)
lon = _dec(-83.2)
pt = geobox2.Geobox(lat, lon)
boxes = pt.storage_geoboxes()
eq_(sorted([self.center, self.right, self.up, self.rightup]), sorted(boxes))
def eq_(a, b, msg=None):
"""Shorthand for 'assert a == b, "%r != %r" % (a, b)
"""
def details():
if msg:
return "\n%s\n%s\n !=\n%s" % (msg, a, b)
else:
return "\n%s\n !=\n%s" % (a, b)
assert a == b, details()
def _dec(num):
return decimal.Decimal(str(num))