/
Sandwich.py
295 lines (221 loc) · 9.32 KB
/
Sandwich.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
""" Layered, composite rendering for TileStache.
The Sandwich Provider supplies a Photoshop-like rendering pipeline, making it
possible to use the output of other configured tile layers as layers or masks
to create a combined output. Sandwich is modeled on Lars Ahlzen's TopOSM.
The external "Blit" library is required by Sandwich, and can be installed
via Pip, easy_install, or directly from Github:
https://github.com/migurski/Blit
The "stack" configuration parameter describes a layer or stack of layers that
can be combined to create output. A simple stack that merely outputs a single
color orange tile looks like this:
{"color" "#ff9900"}
Other layers in the current TileStache configuration can be reference by name,
as in this example stack that simply echoes another layer:
{"src": "layer-name"}
Layers can be limited to appear at certain zoom levels, given either as a range
or as a single number:
{"src": "layer-name", "zoom": "12"}
{"src": "layer-name", "zoom": "12-18"}
Layers can also be used as masks, as in this example that uses one layer
to mask another layer:
{"mask": "layer-name", "src": "other-layer"}
Many combinations of "src", "mask", and "color" can be used together, but it's
an error to provide all three.
Layers can be combined through the use of opacity and blend modes. Opacity is
specified as a value from 0.0-1.0, and blend mode is specified as a string.
This example layer is blended using the "hard light" mode at 50% opacity:
{"src": "hillshading", "mode": "hard light", "opacity": 0.5}
Currently-supported blend modes include "screen", "add", "multiply", "subtract",
"linear light", and "hard light".
Layers can also be affected by adjustments. Adjustments are specified as an
array of names and parameters. This example layer has been slightly darkened
using the "curves" adjustment, moving the input value of 181 (light gray)
to 50% gray while leaving black and white alone:
{"src": "hillshading", "adjustments": [ ["curves", [0, 181, 255]] ]}
Available adjustments:
"threshold" - Blit.adjustments.threshold()
"curves" - Blit.adjustments.curves()
"curves2" - Blit.adjustments.curves2()
See detailed information about adjustments in Blit documentation:
https://github.com/migurski/Blit#readme
Finally, the stacking feature allows layers to combined in more complex ways.
This example stack combines a background color and foreground layer:
[
{"color": "#ff9900"},
{"src": "layer-name"}
]
A complete example configuration might look like this:
{
"cache":
{
"name": "Test"
},
"layers":
{
"base":
{
"provider": {"name": "mapnik", "mapfile": "mapnik-base.xml"}
},
"halos":
{
"provider": {"name": "mapnik", "mapfile": "mapnik-halos.xml"},
"metatile": {"buffer": 128}
},
"outlines":
{
"provider": {"name": "mapnik", "mapfile": "mapnik-outlines.xml"},
"metatile": {"buffer": 16}
},
"streets":
{
"provider": {"name": "mapnik", "mapfile": "mapnik-streets.xml"},
"metatile": {"buffer": 128}
},
"sandwiches":
{
"provider":
{
"name": "Sandwich",
"stack":
[
{"src": "base"},
{"src": "outlines", "mask": "halos"},
{"src": "streets"}
]
}
}
}
}
"""
from re import search
from StringIO import StringIO
from . import Core
import Image
import Blit
blend_modes = {
'screen': Blit.blends.screen,
'add': Blit.blends.add,
'multiply': Blit.blends.multiply,
'subtract': Blit.blends.subtract,
'linear light': Blit.blends.linear_light,
'hard light': Blit.blends.hard_light
}
adjustment_names = {
'threshold': Blit.adjustments.threshold,
'curves': Blit.adjustments.curves,
'curves2': Blit.adjustments.curves2
}
class Provider:
""" Sandwich Provider.
Stack argument is a list of layer dictionaries described in module docs.
"""
def __init__(self, layer, stack):
self.config = layer.config
self.stack = stack
def renderTile(self, width, height, srs, coord):
rendered = draw_stack(self.stack, coord, self.config, dict())
if rendered.size() == (width, height):
return rendered.image()
else:
return rendered.image().resize((width, height))
def draw_stack(stack, coord, config, tiles):
""" Render this image stack.
Given a stack of sandwich layers, a coordinate, and configuration
object, return an output image with the results of all the layers
in this stack pasted on in turn.
Final argument is a dictionary used to temporarily cache results
of layers retrieved from layer_bitmap(), to speed things up in case
of repeatedly-used identical images.
"""
# start with an empty base
rendered = Blit.Color(0, 0, 0, 0x10)
for layer in stack:
if 'zoom' in layer and not in_zoom(coord, layer['zoom']):
continue
#
# Prepare pixels from elsewhere.
#
source_name, mask_name, color_name = [layer.get(k, None) for k in ('src', 'mask', 'color')]
if source_name and color_name and mask_name:
raise Core.KnownUnknown("You can't specify src, color and mask together in a Sandwich Layer: %s, %s, %s" % (repr(source_name), repr(color_name), repr(mask_name)))
if source_name and source_name not in tiles:
tiles[source_name] = layer_bitmap(config.layers[source_name], coord)
if mask_name and mask_name not in tiles:
tiles[mask_name] = layer_bitmap(config.layers[mask_name], coord)
#
# Build up the foreground layer.
#
if source_name and color_name:
# color first, then layer
foreground = make_color(color_name).blend(tiles[source_name])
elif source_name:
foreground = tiles[source_name]
elif color_name:
foreground = make_color(color_name)
elif mask_name:
raise Core.KnownUnknown("You have to provide more than just a mask to Sandwich Layer: %s" % repr(mask_name))
else:
raise Core.KnownUnknown("You have to provide at least some combination of src, color and mask to Sandwich Layer")
#
# Do the final composition with adjustments and blend modes.
#
for (name, args) in layer.get('adjustments', []):
adjustfunc = adjustment_names.get(name)(*args)
foreground = foreground.adjust(adjustfunc)
opacity = float(layer.get('opacity', 1.0))
blendfunc = blend_modes.get(layer.get('mode', None), None)
if mask_name:
rendered = rendered.blend(foreground, tiles[mask_name], opacity, blendfunc)
else:
rendered = rendered.blend(foreground, None, opacity, blendfunc)
return rendered
def layer_bitmap(layer, coord):
""" Return Blit.Bitmap representation of tile from a given layer.
Uses TileStache.getTile(), so caches are read and written as normal.
"""
from . import getTile
mime, body = getTile(layer, coord, 'png')
image = Image.open(StringIO(body)).convert('RGBA')
return Blit.Bitmap(image)
def in_zoom(coord, range):
""" Return True if the coordinate zoom is within the textual range.
Range might look like "1-10" or just "5".
"""
zooms = search("^(\d+)-(\d+)$|^(\d+)$", range)
if not zooms:
raise Core.KnownUnknown("Bad zoom range in a Sandwich Layer: %s" % repr(range))
min_zoom, max_zoom, at_zoom = zooms.groups()
if min_zoom is not None and max_zoom is not None:
min_zoom, max_zoom = int(min_zoom), int(max_zoom)
elif at_zoom is not None:
min_zoom, max_zoom = int(at_zoom), int(at_zoom)
else:
min_zoom, max_zoom = 0, float('inf')
return min_zoom <= coord.zoom and coord.zoom <= max_zoom
def make_color(color):
""" Convert colors expressed as HTML-style RGB(A) strings to Blit.Color.
Examples:
white: "#ffffff", "#fff", "#ffff", "#ffffffff"
black: "#000000", "#000", "#000f", "#000000ff"
null: "#0000", "#00000000"
orange: "#f90", "#ff9900", "#ff9900ff"
transparent orange: "#f908", "#ff990088"
"""
if type(color) not in (str, unicode):
raise Core.KnownUnknown('Color must be a string: %s' % repr(color))
if color[0] != '#':
raise Core.KnownUnknown('Color must start with hash: "%s"' % color)
if len(color) not in (4, 5, 7, 9):
raise Core.KnownUnknown('Color must have three, four, six or eight hex chars: "%s"' % color)
if len(color) == 4:
color = ''.join([color[i] for i in (0, 1, 1, 2, 2, 3, 3)])
elif len(color) == 5:
color = ''.join([color[i] for i in (0, 1, 1, 2, 2, 3, 3, 4, 4)])
try:
r = int(color[1:3], 16)
g = int(color[3:5], 16)
b = int(color[5:7], 16)
a = len(color) == 7 and 0xFF or int(color[7:9], 16)
except ValueError:
raise Core.KnownUnknown('Color must be made up of valid hex chars: "%s"' % color)
return Blit.Color(r, g, b, a)