1 | #!/usr/bin/env python
|
---|
2 | # generate tiles
|
---|
3 |
|
---|
4 | """Generate gheat tiles.
|
---|
5 | """
|
---|
6 | import math
|
---|
7 | import os
|
---|
8 | import sys
|
---|
9 | from os.path import join
|
---|
10 |
|
---|
11 | import aspen
|
---|
12 | root = aspen.find_root()
|
---|
13 | aspen.configure(['--root', root])
|
---|
14 |
|
---|
15 | from gmerc import ll2px
|
---|
16 | from gheat import pil_ as backend
|
---|
17 |
|
---|
18 |
|
---|
19 | color_schemes = dict() # this is used below
|
---|
20 | _color_schemes_dir = os.path.join(aspen.paths.__, 'etc', 'color-schemes')
|
---|
21 | for fname in os.listdir(_color_schemes_dir):
|
---|
22 | if not fname.endswith('.png'):
|
---|
23 | continue
|
---|
24 | name = os.path.splitext(fname)[0]
|
---|
25 | fspath = os.path.join(_color_schemes_dir, fname)
|
---|
26 | color_schemes[name] = backend.ColorScheme(name, fspath)
|
---|
27 |
|
---|
28 |
|
---|
29 | def load_dots(backend):
|
---|
30 | """Given a backend module, return a mapping of zoom level to Dot object.
|
---|
31 | """
|
---|
32 | return dict([(zoom, backend.Dot(zoom)) for zoom in range(16)])
|
---|
33 | dots = load_dots(backend) # factored for easier use from scripts
|
---|
34 |
|
---|
35 |
|
---|
36 | for zoom in [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]: # generate for zoomlevels
|
---|
37 | width, height = ll2px(-90, 180, zoom)
|
---|
38 | numcols = int(math.ceil(width / 256.0))
|
---|
39 | numrows = int(math.ceil(height / 256.0))
|
---|
40 | cs_name = 'classic'
|
---|
41 | color_scheme = color_schemes[cs_name]
|
---|
42 | for x in range(numcols):
|
---|
43 | for y in range(numrows):
|
---|
44 | fspath = join( aspen.paths.root
|
---|
45 | , cs_name
|
---|
46 | , str(zoom)
|
---|
47 | , "%d,%d" % (x, y)
|
---|
48 | ) + '.png'
|
---|
49 | tile = backend.Tile(color_scheme, dots, zoom, x, y, fspath)
|
---|
50 | sys.stdout.write('tile %s\n' % fspath); sys.stdout.flush()
|
---|
51 | if tile.is_empty():
|
---|
52 | sys.stdout.flush()
|
---|
53 | elif tile.is_stale():
|
---|
54 | sys.stdout.write('rebuilding tile %s\n' % fspath)
|
---|
55 | sys.stdout.flush()
|
---|
56 | tile.rebuild()
|
---|
57 | tile.save()
|
---|
58 | else:
|
---|
59 | sys.stdout.write('skipping cached tile %s\n' % fspath)
|
---|
60 | sys.stdout.flush()
|
---|