-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_tiles_multiprocess.py
373 lines (319 loc) · 10.6 KB
/
generate_tiles_multiprocess.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
# pylint: disable=W0707
import json
import logging
import math
import os
import shutil
import sys
import threading
from argparse import ArgumentParser
from queue import Queue
import mapnik
from mbutil import disk_to_mbtiles
DEG_TO_RAD = math.pi / 180
RAD_TO_DEG = 180 / math.pi
MERC_MAX_LATITUDE = 85.05112877980659
MAPNIK_LONGLAT_PROJ = "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"
class GoogleProjection:
def __init__(self, levels, tile_size):
self.Bc = []
self.Cc = []
self.zc = []
self.Ac = []
c = tile_size
for _ in range(0, levels):
e = c / 2
self.Bc.append(c / 360.0)
self.Cc.append(c / (2 * math.pi))
self.zc.append((e, e))
self.Ac.append(c)
c *= 2
def from_ll_to_pixel(self, ll, zoom):
d = self.zc[zoom]
e = round(d[0] + ll[0] * self.Bc[zoom])
f = min(max(math.sin(DEG_TO_RAD * ll[1]), -0.9999), 0.9999)
g = round(d[1] + 0.5 * math.log((1 + f) / (1 - f)) * -self.Cc[zoom])
return e, g
def from_pixel_to_ll(self, px, zoom):
e = self.zc[zoom]
f = (px[0] - e[0]) / self.Bc[zoom]
g = (px[1] - e[1]) / -self.Cc[zoom]
h = RAD_TO_DEG * (2 * math.atan(math.exp(g)) - 0.5 * math.pi)
return f, h
class RenderThread:
def __init__(self, q, map_file, map_prj, max_zoom, tile_size, tile_fmt):
self.q = q
self.m = mapnik.Map(tile_size, tile_size)
self.m.aspect_fix_mode = mapnik.aspect_fix_mode.RESPECT
# Load style XML
mapnik.load_map(self.m, map_file, True)
# Obtain <Map> projection
self.prj = mapnik.Projection(self.m.srs)
self.tr = mapnik.ProjTransform(map_prj, self.prj)
# Projects between tile pixel co-ordinates and LatLong (EPSG:4326)
self.tile_proj = GoogleProjection(max_zoom + 1, tile_size)
self.tile_size = tile_size
self.tile_fmt = tile_fmt
def render_tile(self, tile_uri, x, y, z):
# Calculate pixel positions of bottom-left & top-right
p0 = (x * self.tile_size, (y + 1) * self.tile_size)
p1 = ((x + 1) * self.tile_size, y * self.tile_size)
# Convert to LatLong (EPSG:4326)
l0 = self.tile_proj.from_pixel_to_ll(p0, z)
l1 = self.tile_proj.from_pixel_to_ll(p1, z)
# Bounding box for the tile
bbox = mapnik.Box2d(l0[0], l0[1], l1[0], l1[1])
# Convert to map projection (e.g. mercator co-ordinates EPSG:3857)
bbox = self.tr.forward(bbox)
self.m.zoom_to_box(bbox)
if self.m.buffer_size < 128:
self.m.buffer_size = 128
# Render image with default AGG renderer
im = mapnik.Image(self.tile_size, self.tile_size)
mapnik.render(self.m, im)
if self.tile_fmt == "webp":
im.save(tile_uri, "webp:lossless=1:quality=100:image_hint=3")
elif self.tile_fmt == "jpg":
im.save(tile_uri, "jpeg100")
else:
im.save(tile_uri, self.tile_fmt + ":z=9:s=rle")
def loop(self):
while True:
# Fetch a tile from the queue and render it
r = self.q.get()
if r is None:
self.q.task_done()
break
(name, tile_uri, x, y, z) = r
exists = ""
if os.path.isfile(tile_uri):
exists = "exists"
else:
self.render_tile(tile_uri, x, y, z)
empty = ""
if logger.isEnabledFor(logging.DEBUG):
data = os.stat(tile_uri)[6]
if data in (103, 126, 222):
empty = "empty"
logger.debug(self.m.scale_denominator())
logger.debug("(%s : %s, %s, %s, %s, %s)", name, z, x, y, exists, empty)
self.q.task_done()
def render_tiles(
map_file,
map_prj,
bbox,
min_zoom,
max_zoom,
threads,
name,
tile_size,
tile_fmt,
tile_dir,
tile_ext,
):
logger.info(
"render_tiles(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s %s)",
map_file,
map_prj,
bbox,
min_zoom,
max_zoom,
threads,
name,
tile_size,
tile_fmt,
tile_dir,
tile_ext,
)
# Launch rendering threads
queue = Queue(-1)
renderers = {}
for i in range(threads):
renderer = RenderThread(queue, map_file, map_prj, max_zoom, tile_size, tile_fmt)
render_thread = threading.Thread(target=renderer.loop)
render_thread.start()
logger.info("Started render thread %s", render_thread.getName())
renderers[i] = render_thread
if not os.path.isdir(tile_dir):
os.mkdir(tile_dir)
gprj = GoogleProjection(max_zoom + 1, tile_size)
ll0 = (bbox[0], bbox[3])
ll1 = (bbox[2], bbox[1])
for z in range(min_zoom, max_zoom + 1):
px0 = gprj.from_ll_to_pixel(ll0, z)
px1 = gprj.from_ll_to_pixel(ll1, z)
# Check if we have directories in place
str_z = "%s" % z
tile_sizef = float(tile_size)
if not os.path.isdir(os.path.join(tile_dir, str_z)):
os.mkdir(os.path.join(tile_dir, str_z))
for x in range(int(px0[0] / tile_sizef), int(px1[0] / tile_sizef) + 1):
# Validate x co-ordinate
if (x < 0) or (x >= 2 ** z):
continue
# Check if we have directories in place
str_x = "%s" % x
if not os.path.isdir(os.path.join(tile_dir, str_z, str_x)):
os.mkdir(os.path.join(tile_dir, str_z, str_x))
for y in range(int(px0[1] / tile_sizef), int(px1[1] / tile_sizef) + 1):
# Validate y co-ordinate
if (y < 0) or (y >= 2 ** z):
continue
# Submit tile to be rendered into the queue
str_y = "%s" % y
tile_uri = os.path.join(tile_dir, str_z, str_x, str_y + "." + tile_ext)
t = (name, tile_uri, x, y, z)
try:
queue.put(t)
except KeyboardInterrupt:
raise SystemExit("Ctrl-C detected, exiting...")
# Signal render threads to exit by sending empty request to queue
for _ in range(threads):
queue.put(None)
# Wait for pending rendering jobs to complete
queue.join()
for i in range(threads):
renderers[i].join()
if __name__ == "__main__":
parser = ArgumentParser(
usage="%(prog)s [options] input output 1..18 1..18",
allow_abbrev=False,
)
# Positional arguments
parser.add_argument(
"input",
help="mapnik XML file",
)
parser.add_argument(
"output",
help="a MBTiles file",
default=None,
)
parser.add_argument(
"min",
help="minimum zoom level to render",
type=int,
metavar="1..18",
choices=range(1, 18),
default="1",
)
parser.add_argument(
"max",
help="maximum zoom level to render",
type=int,
metavar="1..18",
choices=range(1, 18),
default="18",
)
# Optional arguments
parser.add_argument(
"--bbox",
help="bounding box that will be rendered",
nargs=4,
type=float,
metavar="f",
)
parser.add_argument(
"--threads",
help="# of rendering threads to spawn",
type=int,
default=8,
)
parser.add_argument(
"--name",
help="name for each renderer",
default="unknown",
)
parser.add_argument(
"--size",
help="resolution of the tile image",
type=int,
metavar="SIZE",
choices=[1024, 512, 256],
default=512,
)
# MBUtil arguments
parser.add_argument(
"--format",
help="format of the image tiles",
dest="format",
type=str,
metavar="FORMAT",
choices=["jpg", "png", "png8", "png24", "png32", "png256", "webp"],
default="png",
)
parser.add_argument(
"--scheme",
help="tiling scheme of the tiles",
dest="scheme",
type=str,
metavar="SCHEME",
choices=["xyz", "tms"],
default="tms",
)
parser.add_argument(
"--no_compression",
help="disable MBTiles compression",
dest="compression",
action="store_false",
default=True,
)
parser.add_argument(
"--verbose",
dest="silent",
action="store_false",
default=True,
)
args = parser.parse_args()
if not args.silent:
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)
base_dir = os.path.dirname(args.input)
tiles_dir = os.path.join(base_dir, "tiles")
tiles_ext = "png" if args.format.startswith("png") else args.format
mbtiles_path = args.output
prj = mapnik.Projection(MAPNIK_LONGLAT_PROJ)
if os.path.exists(tiles_dir) and os.path.isdir(tiles_dir):
shutil.rmtree(tiles_dir)
if not args.bbox:
args.bbox = [-180, -MERC_MAX_LATITUDE, 180, MERC_MAX_LATITUDE]
else:
args.bbox[0] = min(max(-180.0, args.bbox[0]), 180.0)
args.bbox[1] = min(max(-MERC_MAX_LATITUDE, args.bbox[1]), MERC_MAX_LATITUDE)
args.bbox[2] = min(max(-180.0, args.bbox[2]), 180.0)
args.bbox[3] = min(max(-MERC_MAX_LATITUDE, args.bbox[3]), MERC_MAX_LATITUDE)
render_tiles(
args.input,
prj,
args.bbox,
args.min,
args.max,
args.threads,
args.name,
args.size,
args.format,
tiles_dir,
tiles_ext,
)
# Import `tiles` directory into a `MBTiles` file
if mbtiles_path:
if os.path.isfile(mbtiles_path):
# `MBTiles` file must not already exist
sys.stderr.write("Importing tiles into already-existing MBTiles is not yet supported\n")
sys.exit(1)
metadata = {
"name": os.path.splitext(os.path.basename(mbtiles_path))[0],
"format": tiles_ext,
"bounds": f"{str(args.bbox[0])}, {str(args.bbox[1])}, {str(args.bbox[2])}, {str(args.bbox[3])}",
"minzoom": str(args.min),
"maxzoom": str(args.max),
"attribution": "",
"description": "",
"type": "",
"version": "",
}
metadata = {k: v for k, v in metadata.items() if v != ""}
with open(os.path.join(tiles_dir, "metadata.json"), "w") as outfile:
json.dump(metadata, outfile, sort_keys=True, indent=4, separators=(",", ": "))
args.format = tiles_ext
disk_to_mbtiles(tiles_dir, mbtiles_path, **args.__dict__)