-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtiles.py
351 lines (253 loc) · 11.4 KB
/
tiles.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
from math import pi, cos, sin, log, exp, atan
from typing import List, Tuple, Dict, Optional, Any, Union
from shapely.geometry import Polygon
# DEG_TO_RAD:float = pi / 180
# RAD_TO_DEG:float = 180 / pi
DEG_TO_RAD = pi / 180
RAD_TO_DEG = 180 / pi
def constrain(lower_limit:float, x:float, upper_limit:float) -> float:
"""Constrains x to the [lower_limit, upper_limit] segment."""
ans = max(lower_limit, x)
ans = min(ans, upper_limit)
return ans
class GoogleProjection:
"""
This class converts from LonLat to pixel and vice versa. For that, it pre
calculates some values for each zoom level, which are store in 3 arrays.
For information about the formulas in lon_lat2pixel() and pixel2lon_lat(), see
https://en.wikipedia.org/wiki/Mercator_projection#Mathematics_of_the_Mercator_projection
"""
# see also https://alastaira.wordpress.com/2011/01/23/the-google-maps-bing-maps-spherical-mercator-projection/
def __init__(self, levels:int=18) -> None:
self.pixels_per_degree:List[float] = []
self.pixels_per_radian:List[float] = []
self.center_pixel:List[Tuple[int, int]] = [] # pixel for (0, 0)
# self.world_size:List[int] = [] # world size in pixels
# NOTE: no support for high res tiles
world_size:int = 256 # size in pixels of the image representing the whole world
for d in range(levels + 1): # type: int
center:int = world_size // 2
self.pixels_per_degree.append(world_size / 360.0)
self.pixels_per_radian.append(world_size / (2 * pi))
self.center_pixel.append( (center, center) )
# the world doubles in size on each zoom level
world_size *= 2
# it's LonLat! (lon, lat)
def lon_lat2pixel(self, lon_lat:Tuple[float, float], zoom:int) -> Tuple[int, int]:
lon, lat = lon_lat
center_x, center_y = self.center_pixel[zoom]
# x is easy because it's linear to the longitude
x = center_x + round(lon * self.pixels_per_degree[zoom])
# y is... what?
f = constrain(-0.9999, sin(DEG_TO_RAD * lat), 0.9999)
y = center_y + round(0.5 * log((1 + f) / (1 - f)) * -self.pixels_per_radian[zoom])
return (x, y)
def pixel2lon_lat(self, px:Tuple[int, int], zoom:int) -> Tuple[float,float]:
x, y = px
center_x, center_y = self.center_pixel[zoom]
# longitude is linear to x
lon = (x - center_x) / self.pixels_per_degree[zoom]
angle = (y - center_y) / -self.pixels_per_radian[zoom] # angle in radians
lat = RAD_TO_DEG * (2 * atan(exp(angle)) - 0.5 * pi)
return (lon, lat)
# enough ZLs for a lifetime
tileproj = GoogleProjection(30)
class Tile:
# def __init__(self, z:int, x:int, y:int, metatile:Optional[MetaTile]=None) -> None:
def __init__(self, z:int, x:int, y:int, metatile=None) -> None:
# NOTE: there are 3 sets of coordinates and their vertical component grow in different directions
# (z,),x,y are tile coords, relative to the upper left corner, so y grows downwards
self.z = z
self.x = x
self.y = y
# self.meta_index:Optional[Tuple[int, int]] = None
self.meta_index = None
self.meta_pixel_coords = None
if metatile is not None:
self.meta_index = (x - metatile.x, y - metatile.y)
self.meta_pixel_coords = ()
self.size = metatile.tile_size
else:
# TODO: no support for hi res tiles
self.size = 256
# pixel_pos is based on (z,),x,y; it's relative to the world at this ZL and also grows downwards
self.pixel_pos = (self.x * self.size, self.y * self.size)
# corners are another way to express pixel_pos; same direction
# ((x0, y0), (x1, y1))
self.corners = ( self.pixel_pos,
(self.pixel_pos[0] + self.size,
self.pixel_pos[1] + self.size) )
# but coords are LongLat, and Lat grows upwards, so wehn calling these functions,
# we have to swap the lat's
long0, lat1 = tileproj.pixel2lon_lat(self.corners[0], self.z)
long1, lat0 = tileproj.pixel2lon_lat(self.corners[1], self.z)
self.coords = ( (long0, lat0), (long1, lat1) )
polygon_points = [ (self.coords[i][0], self.coords[j][1])
for i, j in ((0, 0), (1, 0), (1, 1), (0, 1), (0, 0)) ]
self.polygon = Polygon(polygon_points)
self.image_size = (self.size, self.size)
self.data: Optional[bytes] = None
self.is_empty = None # Optional[bool]
def __eq__(self, other):
return ( self.z == other.z and self.x == other.x and self.y == other.y )
def __repr__(self):
return "Tile(%d, %d, %d, %r)" % (self.z, self.x, self.y, self.meta_index)
def __iter__(self):
return self.iter()
def iter(self):
"""Returns a generator over the 'coords'."""
yield self.z
yield self.x
yield self.y
class PixelTile:
"""It's a (meta) tile with arbitrary pixel bounds."""
def __init__(self, z, center_x, center_y, size):
self.z = z
# keep the original coords, mostly for printing; meta_index is used for cutting
self.x = center_x
self.y = center_y
# make sure it's rendered
self.is_empty = False
self.render = True
half_size = size // 2
# (x, y)
self.pixel_pos = (center_x - half_size, center_y - half_size)
# debug(self.pixel_pos)
# (w, h)
self.image_size = (size, size)
# debug(self.image_size)
self.tiles = [ self ]
# ((x0, y0), (x1, y1))
self.corners = ( self.pixel_pos,
(self.pixel_pos[0] + self.image_size[0],
self.pixel_pos[1] + self.image_size[1]) )
# we have to swap the lat's
long0, lat1 = tileproj.pixel2lon_lat(self.corners[0], self.z)
long1, lat0 = tileproj.pixel2lon_lat(self.corners[1], self.z)
self.coords = ( (long0, lat0), (long1, lat1) )
polygon_points = [ (self.coords[i][0], self.coords[j][1])
for i, j in ((0, 0), (1, 0), (1, 1), (0, 1), (0, 0)) ]
self.polygon = Polygon(polygon_points)
# times
self.render_time = 0
self.serializing_time = 0
self.deserializing_time = 0
self.saving_time = 0
# Tile emulation
self.meta_index = (0, 0)
def __repr__(self) -> str:
return "PixelTile(%d,%d,%d)" % (self.z, self.x, self.y)
def child(self, tile:Tile):
return None
def children(self):
return []
# TODO: move to BaseTile
def __iter__(self):
return self.iter()
def iter(self):
"""Returns a generator over the 'coords'."""
yield self.z
yield self.x
yield self.y
def times(self):
return (self.render_time, self.serializing_time, self.deserializing_time,
self.saving_time)
# TODO: MetaTile factory
# Children = List[MetaTile]
class MetaTile:
def __init__(self, z:int, x:int, y:int, wanted_size:int, tile_size:int) -> None:
self.z = z
self.x = x
self.y = y
self.wanted_size = wanted_size # in tiles
self.size = min(2**z, wanted_size)
self.tile_size = tile_size
self.is_empty = True # to simplify code in store_metatile()
self.render = True # this is going to be reset by store_metatile()
# NOTE: children are not precomputed because it's recursive with no bounds
# see children()
# self._children:Optional[Children] = None
self._children = None
self.tiles = [ Tile(self.z, self.x + i, self.y + j, self)
for i in range(self.size) for j in range(self.size) ]
self.im: Optional[bytes] = None
# (x, y)
self.pixel_pos = (self.x * self.tile_size, self.y * self.tile_size)
# (w, h)
self.image_size = (self.size * self.tile_size, self.size * self.tile_size)
# ((x0, y0), (x1, y1))
self.corners = ( self.pixel_pos,
(self.pixel_pos[0] + self.image_size[0],
self.pixel_pos[1] + self.image_size[1]) )
# we have to swap the lat's
long0, lat1 = tileproj.pixel2lon_lat(self.corners[0], self.z)
long1, lat0 = tileproj.pixel2lon_lat(self.corners[1], self.z)
self.coords = ( (long0, lat0), (long1, lat1) )
polygon_points = [ (self.coords[i][0], self.coords[j][1])
for i, j in ((0, 0), (1, 0), (1, 1), (0, 1), (0, 0)) ]
self.polygon = Polygon(polygon_points)
# times
self.render_time:float = 0
self.serializing_time:float = 0
self.deserializing_time = 0
self.saving_time = 0
@staticmethod
def from_tile(tile: Tile, wanted_size):
size = min(2**tile.z, wanted_size)
x = tile.x // size * size
y = tile.y // size * size
return MetaTile(tile.z, x, y, wanted_size, tile.size)
# see https://github.com/python/mypy/issues/2783#issuecomment-276596902
# def __eq__(self, other:MetaTile) -> bool: # type: ignore
def __eq__(self, other) -> bool: # type: ignore
return ( self.z == other.z and self.x == other.x and self.y == other.y
and self.size == other.size )
def __repr__(self) -> str:
return "MetaTile(%d,%d,%d)" % (self.z, self.x, self.y)
# def children(self) -> Children:
def children(self):
if self._children is None:
if self.size == self.wanted_size:
self._children = [ MetaTile(self.z + 1,
2 * self.x + i * self.size,
2 * self.y + j * self.size,
self.wanted_size, self.tile_size)
for i in range(2) for j in range(2) ]
else:
self._children = [ MetaTile(self.z + 1, 2 * self.x, 2 * self.y,
self.wanted_size, self.tile_size) ]
debug((self, self._children))
return self._children
def __contains__(self, other:Tile) -> bool:
if isinstance(other, Tile):
if other.z == self.z-1:
return ( self.x <= 2*other.x < self.x+self.size and
self.y <= 2*other.y < self.y+self.size )
else:
return ( self.x <= other.x < self.x+self.size and
self.y <= other.y < self.y+self.size )
else:
# TODO: more?
return False
# def child(self, tile:Tile) -> MetaTile:
def child(self, tile:Tile):
"""Returns the child MetaTile were tile fits."""
if tile in self:
# there's only one
return [ child for child in self.children() if tile in child ][0]
else:
raise ValueError("%r not in %r" % (tile, self))
def __hash__(self):
return hash((self.z, self.x, self.y, self.size))
def times(self):
return (self.render_time, self.serializing_time, self.deserializing_time,
self.saving_time)
def tile_spec2zxy(tile_spec): # str -> Tuple[int, int, int]
if ',' in tile_spec:
data = tile_spec.split(',')
elif '/' in tile_spec:
data = tile_spec.split('/')
else:
raise ValueError("METATILE not in form Z,X,Y or Z/X/Y.")
z, x, y = map(int, data)
return (z, x, y)