-
Notifications
You must be signed in to change notification settings - Fork 44
/
generate_shields.py
executable file
·169 lines (138 loc) · 5.99 KB
/
generate_shields.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
#!/usr/bin/env python
"""
Script to generate highway and bike routes shields as SVG files in
symbols/shields.
Taken from
https://github.com/gravitystorm/openstreetmap-carto/blob/86196d204a65d6c115c387eecb7a37a1837c4350/scripts/generate_shields.py
Licensed under CC0 license.
"""
# Generate highway shields as SVG files in symbols/shields.
from __future__ import print_function
import copy
import itertools
import lxml.etree
import math
import os
import yaml
SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
def load_settings():
"""Read the settings from YAML."""
with open(os.path.join(SCRIPT_DIR, '../road-shields.yml'), 'r') as fh:
return yaml.load(fh, Loader=yaml.FullLoader)
def main():
settings = load_settings()
namespace = 'http:https://www.w3.org/2000/svg'
svgns = '{' + namespace + '}'
svgnsmap = {None: namespace}
config = {}
config['base'] = {}
config['base']['min_width'] = 1
config['base']['min_height'] = 1
config['base']['max_width'] = 11
config['base']['max_height'] = 4
config['base']['additional_sizes'] = ['base', 'z16', 'z18']
# font_height and font_width are determined by trial and error
config['base']['rounded_corners'] = 2
config['base']['font_height'] = 12.1
config['base']['font_width'] = 6.2
config['base']['padding_x'] = 4
config['base']['padding_y'] = 2
config['base']['stroke_width'] = 1
# Fall back colours used if no colours are defined in road-colours.yaml for a road type.
config['base']['fill'] = '#f1f1f1'
config['base']['stroke_fill'] = '#c6c6c6'
config['global'] = {}
config['global']['types'] = settings.get('shield', {}).keys()
config['global']['output_dir'] = '../symbols/shields/' # specified relative to the script location
# changes for different size versions
config['z16'] = {}
config['z18'] = {}
config['z16']['font_width'] = 6.1
config['z16']['font_height'] = 14.1
config['z18']['font_width'] = 6.9
config['z18']['font_height'] = 15.1
output_dir = os.path.join(SCRIPT_DIR,
os.path.dirname(config['global']['output_dir']))
if not os.path.exists(output_dir):
os.makedirs(output_dir)
for shield_type in config['global']['types']:
# merge base config and specific styles
vars = copy.deepcopy(config['base'])
from_settings = settings.get('shield', {}).get(shield_type, {})
for option, value in from_settings.items():
vars[option] = value
for height, width in itertools.product(
range(vars['min_height'], vars['max_height'] + 1),
range(vars['min_width'], vars['max_width'] + 1)
):
for shield_size in vars['additional_sizes']:
specific_vars = copy.deepcopy(vars)
if shield_size != 'base':
for option in config.get(shield_size, []):
specific_vars[option] = config[shield_size][option]
for option in vars.get(shield_size, []):
specific_vars[option] = vars[shield_size][option]
shield_width = specific_vars.get(
'shield_width',
(2 * specific_vars['padding_x']
+ math.ceil(specific_vars['font_width'] * width))
)
shield_height = specific_vars.get(
'shield_height',
(2 * specific_vars['padding_y']
+ math.ceil(specific_vars['font_height'] * height))
)
svg = lxml.etree.Element('svg', nsmap=svgnsmap)
svg.set('width', '100%')
svg.set('height', '100%')
svg.set(
'viewBox',
'0 0 %d %d' %
(
shield_width + specific_vars['stroke_width'],
shield_height + specific_vars['stroke_width']
)
)
offset_x = 0
offset_y = 0
if specific_vars['stroke_width'] > 0:
offset_x = specific_vars['stroke_width'] / 2.0
offset_y = specific_vars['stroke_width'] / 2.0
shield = lxml.etree.Element(svgns + 'rect')
shield.set('x', str(offset_x))
shield.set('y', str(offset_y))
shield.set('width', str(shield_width))
shield.set('height', str(shield_height))
if specific_vars['rounded_corners'] > 0:
shield.set('rx', str(specific_vars['rounded_corners']))
shield.set('ry', str(specific_vars['rounded_corners']))
shield.set('id', 'shield')
stroke = ''
if specific_vars['stroke_width'] > 0:
stroke = 'stroke:' + specific_vars['stroke_fill'] + ';stroke-width:' + str(specific_vars['stroke_width']) + ';'
shield.set('style', 'fill:' + specific_vars['fill'] + ';' + stroke)
svg.append(shield)
filename = shield_type + '_' + str(width) + 'x' + str(height)
if shield_size != 'base':
filename = filename + '_' + shield_size
filename = filename + '.svg'
# save file
try:
path = os.path.join(
output_dir,
filename
)
with open(path, 'w') as shieldfile:
shieldfile.write(
lxml.etree.tostring(
svg,
encoding='utf-8',
xml_declaration=True,
pretty_print=True
).decode('utf-8')
)
except IOError:
print('Could not save file %s.' % filename)
continue
if __name__ == "__main__":
main()