-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
154 lines (128 loc) · 5.78 KB
/
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# -*- coding: utf-8 -*-
"""tests.py
"""
import os
from io import BytesIO
from queue import Queue
from threading import Thread
from collections import namedtuple
from re import match
from functools import reduce
from PIL import Image
from requests import get, Response
from numpy import pi, arctan, exp, log, tan, asarray, arange, extract, ptp, min, max
from .constants import *
from .util import *
from .pins import *
from .tiles import *
def tests():
"""unit tests"""
# check constants entered correctly
assert EARTH_RADIUS == 6378137.0, f"Earth Radius: {EARTH_RADIUS}"
assert EARTH_CIRCUMFERENCE == 40075016.68557849
assert TILE_SIZE == 256
assert ORIGIN_SHIFT == 20037508.342789244
assert INITIAL_RESOLUTION == 156543.03392804097
assert LAT_LIMIT * R2D == 85.0511287798066
# functions
print("testing resolution")
assert resolution(zoom=2) == 39135.75848201024
assert resolution(zoom=18) == 0.5971642834779395
"""
print("testing contenttype")
wiki_png = "https://upload.wikimedia.org/wikipedia/en/thumb/8/80/Wikipedia-logo-v2.svg/526px-Wikipedia-logo-v2.svg.png"
wiki_svg = "https://upload.wikimedia.org/wikipedia/en/8/80/Wikipedia-logo-v2.svg"
cnn_url = "http://cnn.com"
png_resp = get(wiki_png)
svg_resp = get(wiki_svg)
cnn_resp = get(cnn_url)
assert contenttype(png_resp) == ("image", "png", None)
assert contenttype(cnn_resp) == ("text", "html", "charset=utf-8")
print("testing is_image")
assert is_image(png_resp)
assert is_image(svg_resp)
assert not is_image(cnn_resp)
print("testing get_pic")
assert get_pic(cnn_resp) == None
assert not get_pic(png_resp) == None
assert get_pic(svg_resp) == None
print("testing pin_bounds")
# ?
print("testing tile_up")
# covered by get_bounded_map
print("testing get_bounded_map")
sw = Pin(latitude=42.44175005091515, longitude=-83.87692461385079)
ne = Pin(latitude=42.54204474908484, longitude=-83.51926364614918)
bounds = (sw, ne)
img_small = get_bounded_map(bounds, detail=2)
img_large = get_bounded_map(bounds, detail=4)
assert img_small.size == (521, 198)
assert img_large.size == (2084, 792)
img_small.show()
img_large.show()
"""
# test Pin class
chicago = Pin(*CHICAGO) # load from (latitude, longitude)
chicago_m = Pin.from_merc(*chicago.merc) # to / from mercator
chicago_p = Pin.from_pixels(*chicago.pixel_coord(10), 10) # to / from pix
assert chicago.merc == (-9757153.368030429, 5138536.587247468)
assert chicago.merc_meters == (-9757153.368030429, 5138536.587247468)
assert chicago.merc_sphere == (-1.52978108937303, 0.8056485126060271)
assert chicago.merc_norm == (-0.24347222222222226, 0.12822294317588237)
assert chicago.world == (65.6711111111111, 95.17492654697412)
assert chicago.lambda_phi == (-1.52978108937303, 0.730420291959627)
assert chicago.lat_lng == (41.85, -87.65)
assert chicago_m.lat_lng == (41.849999999999994, -87.65)
assert chicago_p.lat_lng == (41.85012764855735, -87.65029907226562)
assert chicago.pixel_coord(zoom=0) == (65, 95)
assert chicago.pixel_coord(zoom=1) == (131, 190)
assert chicago.pixel_coord(zoom=2) == (262, 380)
assert chicago.pixel_coord(zoom=3) == (525, 761)
assert chicago.pixel_coord(zoom=4) == (1050, 1522)
assert chicago.pixel_coord(zoom=5) == (2101, 3045)
assert chicago.pixel_coord(zoom=6) == (4202, 6091)
assert chicago.pixel_coord(zoom=7) == (8405, 12182)
assert chicago.pixel_coord(zoom=8) == (16811, 24364)
assert chicago.pixel_coord(zoom=9) == (33623, 48729)
assert chicago.pixel_coord(zoom=10) == (67247, 97459)
assert chicago.pixel_coord(zoom=11) == (134494, 194918)
assert chicago.pixel_coord(zoom=12) == (268988, 389836)
# for Z in range(13):
# print("assert chicago.tile_coord(zoom={}) == {}".format(Z, chicago.tile_coord(Z)))
assert chicago.tile_coord(zoom=0) == (0, 0)
assert chicago.tile_coord(zoom=1) == (0, 0)
assert chicago.tile_coord(zoom=2) == (1, 1)
assert chicago.tile_coord(zoom=3) == (2, 2)
assert chicago.tile_coord(zoom=4) == (4, 5)
assert chicago.tile_coord(zoom=5) == (8, 11)
assert chicago.tile_coord(zoom=6) == (16, 23)
assert chicago.tile_coord(zoom=7) == (32, 47)
assert chicago.tile_coord(zoom=8) == (65, 95)
assert chicago.tile_coord(zoom=9) == (131, 190)
assert chicago.tile_coord(zoom=10) == (262, 380)
assert chicago.tile_coord(zoom=11) == (525, 761)
assert chicago.tile_coord(zoom=12) == (1050, 1522)
# for Z in range(13):
# print("assert chicago.subpixel_coord(zoom={}) == {}".format(Z, chicago.subpixel_coord(Z)))
assert chicago.subpixel_coord(zoom=0) == (65, 95)
assert chicago.subpixel_coord(zoom=1) == (131, 190)
assert chicago.subpixel_coord(zoom=2) == (6, 124)
assert chicago.subpixel_coord(zoom=3) == (13, 249)
assert chicago.subpixel_coord(zoom=4) == (26, 242)
assert chicago.subpixel_coord(zoom=5) == (53, 229)
assert chicago.subpixel_coord(zoom=6) == (106, 203)
assert chicago.subpixel_coord(zoom=7) == (213, 150)
assert chicago.subpixel_coord(zoom=8) == (171, 44)
assert chicago.subpixel_coord(zoom=9) == (87, 89)
assert chicago.subpixel_coord(zoom=10) == (175, 179)
assert chicago.subpixel_coord(zoom=11) == (94, 102)
assert chicago.subpixel_coord(zoom=12) == (188, 204)
# test Tile class
chi_tile = Tile.from_pin(chicago, zoom=13)
# assert Tile.new_tile_q.qsize() == 1
# assert chi_tile.img is None
Tile.new_tile_q.join() # wait for html to arrive
assert chi_tile.img is not None
print("yay, winner!")
if __name__ == "__main__":
tests()