-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge_styles.py
executable file
·98 lines (80 loc) · 2.81 KB
/
merge_styles.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
#!/usr/bin/python
import json
import sys
from copy import deepcopy
from itertools import chain
OPACITY_DICT = {
# all of these need lists of props just to support
# the symbol edge case where we set two opacity props
'background': ['background-opacity'],
'fill': ['fill-opacity'],
'line': ['line-opacity'],
'symbol': ['icon-opacity', 'text-opacity'],
'raster': ['raster-opacity'],
'circle': ['circle-opacity']
}
OPACITY_PROPS = list(chain(*OPACITY_DICT.values()))
def process_layer(alllayers, prefix, layer, default=False):
layer['id'] = '{}-{}'.format(prefix, layer['id'])
if 'ref' in layer:
refname = layer['ref']
layer['ref'] = '{}-{}'.format(prefix, layer['ref'])
layer['metadata'] = {
'mapbox:group': prefix
}
for op in OPACITY_PROPS:
if op in layer['paint']:
layer['metadata']['orig-{}'.format(op)] = layer['paint'][op]
if not default:
# set 0 opacity on all non-default layers
if 'type' in layer:
ltype = layer['type']
else:
ref = layer['ref']
# we don't know if ref layer has been changed already or not
try:
reflayer = list(filter(lambda l: l['id'] == ref, alllayers))[0]
except:
reflayer = list(filter(lambda l: l['id'] == refname, alllayers))[0]
ltype = reflayer['type']
for op in OPACITY_DICT[ltype]:
layer['paint'][op] = 0
return layer
def merge_styles(styles):
with open(styles[0], 'r') as base:
jb = json.load(base)
jb_name = base.name.split('.')[0].split('/')[-1]
merged = deepcopy(jb)
merged['layers'] = []
for layer in jb['layers']:
l = process_layer(jb['layers'], jb_name, layer, default=True)
merged['layers'].append(l)
merged['metadata'] = {
'mapbox:groups': {
jb_name: {
'name': jb_name,
'collapsed': False
}
}
}
for stylename in styles[1:]:
with open(stylename, 'r') as f:
j = json.load(f)
j_name = f.name.split('.')[0].split('/')[-1]
for layer in j['layers']:
l = process_layer(j['layers'], j_name, layer)
merged['layers'].append(l)
for name, source in j['sources'].items():
if not name in merged['sources']:
merged['sources'][name] = source
merged['metadata']['mapbox:groups'][j_name] = {
'name': j_name,
'collapsed': False
}
with open('merged.json', 'w') as f:
json.dump(merged, f, indent=4)
if __name__ == '__main__':
if len(sys.argv) < 3:
print('Usage: merge_styles.py <base_style> <merge_style1> [merge_style2 [...merge_styleN]]')
exit(-1)
merge_styles(sys.argv[1:])