-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmapbox.py
476 lines (376 loc) · 18.7 KB
/
mapbox.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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
import math, requests, json, re, io, colorsys, sys, os
import mapbox_vector_tile
tile_url = 'https://api.mapbox.com/v4/{}/{}/{}/{}.mvt'
style_url = 'https://api.mapbox.com/styles/v1/{}'
properties = {}
sprite_cache = {}
class MapBoxError(Exception):
pass
def check_token_valid(token):
response = requests.get(style_url.format(''), params = {'access_token': token})
if response.status_code == 401:
return False
else:
return True
def resource_path(relative_path):
base_path = getattr(sys, '_MEIPASS', os.path.dirname(os.path.abspath(__file__)))
return os.path.join(base_path, relative_path)
def deg2num(lat_deg, lon_deg, zoom):
lat_rad = math.radians(lat_deg)
n = 1 << zoom
xtile = int((lon_deg + 180.0) / 360.0 * n)
ytile = int((1.0 - math.asinh(math.tan(lat_rad)) / math.pi) / 2.0 * n)
return xtile, ytile
def num2deg(tile_x, tile_y, zoom):
n = 1 << zoom
lon_deg = tile_x / n * 360.0 - 180.0
lat_rad = math.atan(math.sinh(math.pi * (1 - 2 * tile_y / n)))
lat_deg = math.degrees(lat_rad)
return lat_deg, lon_deg
def css_style(style):
style_str = ''
for key, value in style.items():
style_str += '{}:{};'.format(key, value)
return style_str
def rgb_to_hex(rgb):
return '#{:02x}{:02x}{:02x}'.format(int(rgb[0] * 255), int(rgb[1] * 255), int(rgb[2] * 255))
def color_to_rgb(color):
rx_hsl = re.compile(r'hsl\(\s*(\d+),\s*(\d+)%,\s*(\d+)%\s*\)')
rx_hsla = re.compile(r'hsla\(\s*(\d+),\s*(\d+)%,\s*(\d+)%\s*,\s*[0-9.]+\)')
rx_rgb = re.compile(r'rgb\(\s*(\d+),\s*(\d+),\s*(\d+)\s*\)')
rx_hex = re.compile(r'#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$')
hsl_match = rx_hsl.match(color)
if hsl_match:
return colorsys.hls_to_rgb(int(hsl_match[1])/360, int(hsl_match[3])/100, int(hsl_match[2])/100)
hsla_match = rx_hsla.match(color)
if hsla_match:
return colorsys.hls_to_rgb(int(hsla_match[1])/360, int(hsla_match[3])/100, int(hsla_match[2])/100)
hex_match = rx_hex.match(color)
if hex_match:
if len(color) == 4:
return (int(color[1], 16)/15, int(color[2], 16)/15, int(color[3], 16)/15)
else:
return (int(color[1:3], 16)/255, int(color[3:5], 16)/255, int(color[5:7], 16)/255)
rgb_match = rx_rgb.match(color)
if rgb_match:
return (int(rgb_match[1]) / 255, int(rgb_match[2]) / 255, int(rgb_match[3]) / 255)
raise ValueError('Unknown Color: "{}"'.format(color))
def color_to_hex(color):
return rgb_to_hex(color_to_rgb(color))
def interpolate_color(expression, method, input_value, feature):
if len(expression) % 2 != 0:
raise ValueError()
color_list = []
value_list = []
for i in range(0, len(expression), 2):
color_list.append(expression[i+1])
value_list.append(expression[i])
rgbs = []
result = None
for color in color_list:
rgbs.append(color_to_rgb(color))
for i in range(len(value_list)):
if input_value < value_list[i]:
return rgb_to_hex(rgbs[i])
return rgb_to_hex(rgbs[-1])
# todo: implement color interpolation
def interpolate(expression, method, input_value, feature):
if len(expression) % 2 != 0:
raise ValueError()
value = get_value(input_value, feature)
if value < expression[0]:
return get_value(expression[1], feature)
if value >= expression[-2]:
return get_value(expression[-1], feature)
result_type = get_value(expression[-1], feature)
if isinstance(result_type, int) or isinstance(result_type, float):
for i in range(2, len(expression), 2):
if value < expression[i]:
right_value = get_value(expression[i+1], feature)
left_value = get_value(expression[i-1], feature)
return ((value - expression[i-2]) / (expression[i] - expression[i-2])) * (right_value - left_value) + left_value
else:
return interpolate_color(expression, method, value, feature)
# todo: implement exponentional interpolation
def get_value(expression, feature):
if isinstance(expression, list):
op = expression[0]
values = expression[1:]
if not isinstance(op, str):
return expression
if op == '!':
return not get_value(values[0], feature)
elif op == '==':
return get_value(values[0], feature) == get_value(values[1], feature)
elif op == '!=':
return get_value(values[0], feature) != get_value(values[1], feature)
elif op == '>':
return get_value(values[0], feature) > get_value(values[1], feature)
elif op == '<':
return get_value(values[0], feature) < get_value(values[1], feature)
elif op == '>=':
return get_value(values[0], feature) >= get_value(values[1], feature)
elif op == '<=':
return get_value(values[0], feature) <= get_value(values[1], feature)
elif op == '+':
return get_value(values[0], feature) + get_value(values[1], feature)
elif op == '-':
return get_value(values[0], feature) - get_value(values[1], feature)
elif op == '*':
return get_value(values[0], feature) * get_value(values[1], feature)
elif op == '/':
return get_value(values[0], feature) / get_value(values[1], feature)
elif op == 'sqrt':
return math.sqrt(get_value(values[0], feature))
elif op == 'zoom':
return properties['zoom']
elif op == 'all':
for value in values:
if not get_value(value, feature):
return False
return True
elif op == 'any':
for value in values:
if get_value(value):
return True
return False
elif op == 'at':
if len(values) != 2:
raise ValueError()
if not isinstance(values[2], list):
raise TypeError()
return get_value(values[2][get_value(values[1], feature)], feature)
elif op == 'get':
if values[0] in feature['properties']:
return feature['properties'][values[0]]
else:
return 0
elif op == 'has':
if len(values) == 1:
return bool(values[0] in feature['properties'])
else:
raise ValueError()
elif op == 'literal':
return values[0]
elif op == 'to-number':
return int(get_value(values[0], feature))
elif op == 'to-string':
return str(get_value(values[0], feature))
elif op == 'match':
label = get_value(values[0], feature)
for i in range(1, len(values) - 1):
if isinstance(values[i], list):
if label in values[i]:
return get_value(values[i+1], feature)
else:
if label == values[i]:
return get_value(values[i+1], feature)
return get_value(values[-1], feature)
elif op == 'case':
for i in range(0, len(values) - 1, 2):
if get_value(values[i], feature):
return get_value(values[i+1], feature)
return get_value(values[-1], feature)
elif op == 'coalesce':
for i in range(0, len(values)):
value = get_value(values[i], feature)
if value:
return value
return get_value(values[-1], feature)
elif op == 'step':
label = get_value(values[0], feature)
for i in range(2, len(values) - 1, 2):
if values[i] > label:
return get_value(values[i-1], feature)
return get_value(values[-1], feature)
elif op == 'interpolate':
return interpolate(values[2:], values[0], values[1], feature)
elif op == 'geometry-type':
geometry_type = feature['geometry']['type']
if geometry_type == 'MultiPolygon':
return 'Polygon'
elif geometry_type == 'MultiLineString':
return 'LineString'
else:
return geometry_type
else:
raise ValueError('Unknown Expression: "{}"'.format(op))
else:
return expression
def get_color(color_style, feature = None):
if isinstance(color_style, str):
return color_to_hex(color_style)
else:
return color_to_hex(get_value(color_style, feature))
def draw_geometry(f, feature, style):
style_str = css_style(style)
if feature['geometry']['type'] == 'Polygon':
for coords in feature['geometry']['coordinates']:
point_str = ''
for point in coords:
point_str += '{},{} '.format(point[0], point[1])
f.write('<polygon points="{}" style="{}" />\n'.format(point_str, style_str))
elif feature['geometry']['type'] == 'MultiPolygon':
f.write('<g>\n')
for polygons in feature['geometry']['coordinates']:
for coords in polygons:
point_str = ''
for point in coords:
point_str += '{},{} '.format(point[0], point[1])
f.write('<polygon points="{}" style="{}" />\n'.format(point_str, style_str))
f.write('</g>\n')
elif feature['geometry']['type'] == 'LineString':
point_str = ''
for point in feature['geometry']['coordinates']:
point_str += '{},{} '.format(point[0], point[1])
f.write('<polyline points="{}" style="{}" />\n'.format(point_str, style_str))
elif feature['geometry']['type'] == 'MultiLineString':
f.write('<g>\n')
for polylines in feature['geometry']['coordinates']:
point_str = ''
for point in polylines:
point_str += '{},{} '.format(point[0], point[1])
f.write('<polyline points="{}" style="{}" />\n'.format(point_str, style_str))
f.write('</g>\n')
def draw_symbol(f, feature, layout, paint):
if feature['geometry']['type'] == 'Point':
coord = feature['geometry']['coordinates']
icon_image = None
if 'icon-image' in layout:
icon_image = layout['icon-image']
if icon_image:
sprite = load_sprite(get_value(icon_image, feature))
size = 1
if 'icon-size' in layout:
size = get_value(layout['icon-size'], feature)
size *= 8
x = coord[0] - (sprite['size'][0] / 2) * size
y = coord[1] + (sprite['size'][1] / 2) * size
f.write('<g transform="translate({0}, {1}) scale({2}, -{2})">'.format(x, y, size))
f.write(sprite['image'])
f.write('</g>\n')
if 'text-field' in layout:
text = get_value(layout['text-field'], feature)
text_style = {'fill': '#111111', 'stroke': 'none', 'text-anchor': 'middle', 'font-size': 15, 'text-align': 'center'}
if 'text-font' in layout:
text_style['font-family'] = layout['text-font'][0]
if 'text-size' in layout:
text_style['font-size'] = get_value(layout['text-size'], feature) * 8
text_style['stroke-width'] = text_style['font-size'] / 4
if 'text-color' in paint:
text_style['fill'] = get_color(paint['text-color'], feature)
if 'text-halo-color' in paint:
text_style['stroke'] = get_color(paint['text-halo-color'], feature)
x = coord[0]
y = coord[1]
if 'text-offset' in layout:
text_offset = get_value(layout['text-offset'], feature)
x += text_offset[0] * text_style['font-size']
y -= text_offset[1] * text_style['font-size']
if text_style['stroke'] != 'none':
f.write('<text x="0" y="0" transform="translate({}, {}) scale(1, -1)" style="{}">{}</text>\n'.format(x, y, css_style(text_style), text))
text_style['stroke'] = 'none'
f.write('<text x="0" y="0" transform="translate({}, {}) scale(1, -1)" style="{}">{}</text>\n'.format(x, y, css_style(text_style), text))
def load_sprite(sprite_id):
if sprite_id in sprite_cache:
return sprite_cache[sprite_id]
sprite_path = resource_path('styles/{}.svg'.format(sprite_id))
with open(sprite_path, mode='r', encoding='utf-8') as f:
svg_text = f.read()
match = re.match(r'<svg (.*?)>(.*)</svg>', svg_text)
if match:
w = re.search(r'width="([0-9]+)"', match[1])
h = re.search(r'height="([0-9]+)"', match[1])
sprite_cache[sprite_id] = {'image': match[2], 'size': (int(w[1]), int(h[1]))}
return sprite_cache[sprite_id]
else:
raise ValueError()
def load_tile(style_id, token, x, y, zoom, draw_full_svg = True, clip_mask = True, fp = None):
properties['x'] = x
properties['y'] = y
properties['zoom'] = zoom
# Load styles
style_response = requests.get(style_url.format(style_id), params = {'access_token': token})
styles = style_response.json()
if style_response.status_code != 200:
if 'message' in styles:
raise MapBoxError(styles['message'])
# Get tileset sources
if re.match(r'mapbox://', styles['sources']['composite']['url']) and styles['sources']['composite']['type'] == 'vector':
sources = styles['sources']['composite']['url'][9:]
else:
raise ValueError()
# Load tilesets
tile_response = requests.get(tile_url.format(sources, zoom, x, y), params = {'access_token': token})
tile = mapbox_vector_tile.decode(tile_response.content)
if fp == None:
f = io.StringIO()
else:
f = fp
if draw_full_svg:
f.write('<?xml version="1.0" encoding="UTF-8" standalone="no"?>\n')
f.write('<svg width="4096" height="4096" viewBox="0 0 4096 4096" xmlns="http://www.w3.org/2000/svg" xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape" xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"><style></style>\n')
f.write('<sodipodi:namedview id="namedview1" pagecolor="#ffffff" bordercolor="#cccccc" borderopacity="1" inkscape:deskcolor="#e5e5e5"/>\n')
if clip_mask:
f.write('<defs><clipPath id="map-clip-mask"><rect x="0" y="0" width="4112" height="4112" /></clipPath></defs>\n')
f.write('<g id="map" transform="scale(1, -1) translate(0, -4096)" clip-path="url(#map-clip-mask)">')
else:
f.write('<g id="map" transform="scale(1, -1) translate(0, -4096)">')
for layer in styles['layers']:
if 'minzoom' in layer:
if layer['minzoom'] > properties['zoom']:
continue
if layer['type'] == 'background':
if 'background-color' in layer['paint']:
fill = get_color(layer['paint']['background-color'])
f.write('<g id="{0}"><rect x="0" y="0" width="4096" height="4096" fill="{1}" stroke="{1}" stroke-width="32" /></g>'.format(layer['id'], fill))
else:
if not layer['source-layer'] in tile:
continue
f.write('<g id="{}">'.format(layer['id']))
source_layer = tile[layer['source-layer']]
for feature in source_layer['features']:
draw_filter = True
if 'filter' in layer:
draw_filter = get_value(layer['filter'], feature)
if draw_filter:
if layer['type'] == 'fill':
feature_style = {'fill': '#000000', 'opacity': 1}
if 'paint' in layer:
if 'fill-color' in layer['paint']:
feature_style['fill'] = get_color(layer['paint']['fill-color'], feature)
if 'opacity' in layer['paint']:
feature_style['opacity'] = get_value(layer['paint']['opacity'], feature)
draw_geometry(f, feature, feature_style)
elif layer['type'] == 'line':
feature_style = {'fill': 'none', 'stroke': '#000000', 'stroke-width': 1, 'stroke-opacity': 1}
if 'paint' in layer:
if 'line-color' in layer['paint']:
feature_style['stroke'] = get_color(layer['paint']['line-color'], feature)
if 'line-width' in layer['paint']:
feature_style['stroke-width'] = get_value(layer['paint']['line-width'], feature) * 8
if 'line-opacity' in layer['paint']:
feature_style['stroke-opacity'] = get_value(layer['paint']['line-opacity'], feature)
if 'line-dasharray' in layer['paint']:
if not isinstance(layer['paint']['line-dasharray'], list):
raise TypeError()
dasharray_str = ''
for dash in layer['paint']['line-dasharray']:
dasharray_str += '{} '.format(dash)
feature_style['stroke-dasharray'] = dasharray_str
if 'layout' in layer:
if 'line-cap' in layer['layout']:
feature_style['stroke-linecap'] = get_value(layer['layout']['line-cap'], feature)
if 'line-join' in layer['layout']:
feature_style['stroke-linejoin'] = get_value(layer['layout']['line-join'], feature)
draw_geometry(f, feature, feature_style)
elif layer['type'] == 'symbol':
draw_symbol(f, feature, layer['layout'], layer['paint'])
f.write('</g>')
f.write('</g>')
if draw_full_svg:
f.write('</svg>')
if fp == None:
result = f.getvalue()
f.close()
return result