Skip to content

Instantly share code, notes, and snippets.

@Phyks
Created February 24, 2019 17:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Phyks/6cf2e030f5d7442f36ae8070e2207357 to your computer and use it in GitHub Desktop.
Save Phyks/6cf2e030f5d7442f36ae8070e2207357 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import yaml
CYCLEWAYS_OUTLINES = [
'lane',
'opposite_lane',
'track',
'opposite_track',
'share_busway',
'opposite_share_busway',
]
SIDES = ['left', 'right']
def get_width_variable(zoom, highway):
return '@rdz%s_%s' % (zoom, highway)
def get_outline_variable(zoom, highway):
return '@rdz%s_%s_outline' % (zoom, highway)
with open('roads.yaml', 'r') as fh:
params = yaml.load(fh).get('params', [])
out = []
for zoom_data in params:
zoom = zoom_data['zoom']
# Generate width variables
out.append('/* Z%s widths */' % zoom)
for highway, width in zoom_data['widths'].items():
out.append('%s: %s;' % (get_width_variable(zoom, highway), width))
out.append('/* Z%s outlines */' % zoom)
# Generate outlines variables
for highway, outline in zoom_data['outlines'].items():
out.append('%s: %s;' % (get_outline_variable(zoom, highway), outline))
out.append('')
out.append('')
# Generate outline code
# Line to draw both borders (left and right)
out.append(
"#roads_high::outline[zoom>=11],\n"
"#tunnel::outline[zoom>=11],\n"
"#bridge::outline[zoom>=11] {"
)
out.append("""
line-cap: round;
line-join: round;
line-color: @standard-case;
[type='motorway'],
[type='motorway_link'] {
line-color: @motorway-case;
}
[type='trunk'],
[type='trunk_link'] {
line-color: @trunk-case;
}
[type='primary'],
[type='primary_link'] {
line-color: @primary-case;
}
[type='secondary'],
[type='secondary_link'] {
line-color: @secondary-case;
}
[cycleway='lane'],
[cycleway='opposite_lane'] {
line-color: @cycle_lane_case;
line-dasharray: 6,3;
line-cap: butt;
}
[cycleway='track'],
[cycleway='opposite_track'] {
line-color: @cycle_track_case;
line-cap: butt;
}
[cycleway='share_busway'],
[cycleway='opposite_share_busway'] {
line-color: @cycle_busway_case;
line-dasharray: 6,10;
line-cap: butt;
}
[type='steps']{
line-color: @steps-case;
[ramp_cycle='yes'],[ramp_wheelchair='yes'],[ramp_stroller='yes']{
line-color: fadeout(@cycle_lane_case, 50%);
}
}
[type='railway'] {
line-color: fadeout(@land,50%);
}
""")
for zoom_data in params:
zoom = zoom_data['zoom']
out.append(' [zoom=%s] {' % zoom)
for highway, width in zoom_data['widths'].items():
width_variable = get_width_variable(zoom, highway)
outline_variable = get_outline_variable(zoom, highway)
out.append(" [type='%s'] {" % highway)
out.append(" line-width: %s + (2 * %s);" % (width_variable,
outline_variable))
out.append(' }')
out.append(' }')
out.append('}')
# Bridge and tunnel specific outline
out.append('''
bridge::outline[zoom>=11] {
line-cap: butt;
line-color: @standard-case * 0.8;
[type='motorway'],
[type='motorway_link'] {
line-color: @motorway-case * 0.8;
}
[type='trunk'],
[type='trunk_link'] {
line-color: @trunk-case * 0.8;
}
[type='primary'],
[type='primary_link'] {
line-color: @primary-case * 0.8;
}
[type='secondary'],
[type='secondary_link'] {
line-color: @secondary-case * 0.8;
}
[type='railway'] {
line-color: @secondary-case * 0.8;
}
}
#tunnel::outline[zoom>=11] {
line-cap: butt;
line-dasharray: 3,3;
}
''')
for side in ['left', 'right']:
# Generate reset left/right outline
# This eventually overload one side border for cycleways. First, hide the
# casing on this side, then show cycleway.
for cycleway in CYCLEWAYS_OUTLINES:
out.append(
"#roads_high::reset_outline_%s[zoom>=11][cycleway_%s='%s'],"
% (side, side, cycleway)
)
out.append(
"#tunnel::reset_outline_%s[zoom>=11][cycleway_%s='%s'],"
% (side, side, cycleway)
)
out.append(
"#bridge::reset_outline_%s[zoom>=11][cycleway_%s='%s'],"
% (side, side, cycleway)
)
out.append('''{
/* -- colors & styles -- */
line-color: @standard-fill;
[type='motorway'],
[type='motorway_link'] {
line-color: @motorway-fill;
}
[type='trunk'],
[type='trunk_link'] {
line-color: @trunk-fill;
}
[type='primary'],
[type='primary_link'] {
line-color: @primary-fill;
}
[type='secondary'],
[type='secondary_link'] {
line-color: @secondary-fill;
}
[type='path'],
[type='pedestrian'],
[type='bridleway'],
[type='footway'] {
line-color: @pedestrian-fill;
}
[type='cycleway'],
[type='path'][bicycle='yes'],
[type='path'][bicycle='designated'],
[type='footway'][bicycle='yes'],
[type='footway'][bicycle='designated'],
[type='bridleway'][bicycle='yes'],
[type='bridleway'][bicycle='designated'],
[type='pedestrian'][bicycle='yes'],
[type='pedestrian'][bicycle='designated'] {
line-color: @cycle-fill;
}
[type='steps'] {
line-color: @steps-fill;
}
''')
for zoom_data in params:
zoom = zoom_data['zoom']
out.append(' [zoom=%s] {' % zoom)
for highway, width in zoom_data['widths'].items():
width_variable = get_width_variable(zoom, highway)
outline_variable = get_outline_variable(zoom, highway)
out.append(" [type='%s'] {" % highway)
out.append(" line-width: %s + %s;" % (width_variable,
outline_variable))
out.append(" line-offset: %s;" % outline_variable)
out.append(' }')
out.append(' }')
out.append('}')
out.append('')
for cycleway in CYCLEWAYS_OUTLINES:
out.append(
"#tunnel::reset_outline_%s[zoom>=11][cycleway_right='%s']"
% (side, cycleway)
)
out.append('''{
[type='motorway'],
[type='motorway_link'] {
line-color: lighten(@motorway-fill, 10%);
}
[type='trunk'],
[type='trunk_link'] {
line-color: lighten(@trunk-fill, 10%);
}
[type='primary'],
[type='primary_link'] {
line-color: lighten(@primary-fill, 10%);
}
[type='secondary'],
[type='secondary_link'] {
line-color: lighten(@secondary-fill, 10%);
}
}''')
out.append('')
# Generate left/right outline
for cycleway in CYCLEWAYS_OUTLINES:
out.append(
"#roads_high::outline_%s[zoom>=11][cycleway_%s='%s'],"
% (side, side, cycleway)
)
out.append(
"#tunnel::outline_%s[zoom>=11][cycleway_%s='%s'],"
% (side, side, cycleway)
)
out.append(
"#bridge::outline_%s[zoom>=11][cycleway_%s='%s'],"
% (side, side, cycleway)
)
out.append('''{
/* -- colors & styles -- */
[cycleway_right='lane'],
[cycleway_right='opposite_lane'] {
line-color: @cycle_lane_case;
line-dasharray: 6,3;
line-cap: butt;
}
[cycleway_right='track'],
[cycleway_right='opposite_track'] {
line-color: @cycle_track_case;
line-cap: butt;
}
[cycleway_right='share_busway'],
[cycleway_right='opposite_share_busway'] {
line-color: @cycle_busway_case;
line-dasharray: 6,10;
line-cap: butt;
}''')
for zoom_data in params:
zoom = zoom_data['zoom']
out.append(' [zoom=%s] {' % zoom)
for highway, width in zoom_data['widths'].items():
width_variable = get_width_variable(zoom, highway)
outline_variable = get_outline_variable(zoom, highway)
out.append(" [type='%s'] {" % highway)
out.append(" line-width: %s + %s;" % (width_variable,
outline_variable))
out.append(" line-offset: %s;" % outline_variable)
out.append(' }')
out.append(' }')
out.append('}')
out.append('')
# Generate inline fill
out.append("#roads_high[zoom>=11],")
out.append("#tunnel::inline[zoom>=11],")
out.append("#bridge::inline[zoom>=11] {")
out.append('''/* -- colors & styles -- */
line-color: @standard-fill;
[type='motorway'],
[type='motorway_link'] {
line-color: @motorway-fill;
}
[type='trunk'],
[type='trunk_link'] {
line-color: @trunk-fill;
}
[type='primary'],
[type='primary_link'] {
line-color: @primary-fill;
}
[type='secondary'],
[type='secondary_link'] {
line-color: @secondary-fill;
}
[type='railway'] {
line-color: @rail-line;
line-dasharray: 1,1;
[type='subway'] { line-opacity: 0.67; }
[zoom >= 16] { line-dasharray: 1, 2; }
}
[type='cycleway'] {
line-color: @cycle-fill;
}
[type='pedestrian'],
[type='bridleway'],
[type='footway'],
[type='path'] {
line-color: @pedestrian-fill;
}
[type='steps'] {
line-color: @steps-fill;
}
[type='service'],
[type='track'],
[type='tertiary'],
[type='living_street'],
[type='road'],
[type='unclassified'],
[type='residential'],
[type='tertiary_link'],
[type='secondary_link'],
[type='primary_link'],
[type='trunk_link'],
[type='motorway_link'],
[type='primary'],
[type='secondary'],
[type='motorway'],
[type='trunk'] {
line-cap: round;
line-join: round;
}
[type='cycleway'],
[type='pedestrian'],
[type='bridleway'],
[type='footway'],
[type='path'] {
line-join: round;
}
''')
for zoom_data in params:
zoom = zoom_data['zoom']
out.append(' [zoom=%s] {' % zoom)
for highway, width in zoom_data['widths'].items():
width_variable = get_width_variable(zoom, highway)
out.append(" [type='%s'] {" % highway)
out.append(" line-width: %s;" % (width_variable))
out.append(' }')
out.append(' }')
out.append('}')
# Tunnel-specific overload
out.append('''#tunnel::inline[zoom>=11] {
line-cap: butt;
[type='motorway'],
[type='motorway_link'] {
line-color: lighten(@motorway-fill, 10%);
}
[type='trunk'],
[type='trunk_link'] {
line-color: lighten(@trunk-fill, 10%);
}
[type='primary'],
[type='primary_link'] {
line-color: lighten(@primary-fill, 10%);
}
[type='secondary'],
[type='secondary_link'] {
line-color: lighten(@secondary-fill, 10%);
}
}
''')
print('\n'.join(out))
# Road width variables that are used in road & bridge styles.
#
# Roads are drawn in two steps. First, a line if the width of the road + the
# two borders is drawn and then a line of the width of the road is drawn on
# top, to make a road with borders. Here, the width of the ways is the width
# of the fill of the road and the border width is the width of a single
# border, on one side (first line is drawn with a width of way with + 2 *
# border_width).
params:
- zoom: 11
widths:
motorway: 1.6
trunk: 1.6
primary: 0.8
secondary: 0.8
motorway_link: 0
trunk_link: 0
primary_link: 0
secondary_link: 0
tertiary: 0
tertiary_link: 0
residential: 0
unclassified: 0
road: 0
living_street: 0
service: 0
track: 0
pedestrian: 0
bridleway: 0
path: 0
footway: 0
steps: '@rdz11_pedestrian'
cycle: 0.5
railway: 0.2
outlines:
primary: 0.8
secondary: 0.8
motorway_link: 0.15
trunk_link: 0.15
primary_link: 0.15
secondary_link: 0.15
tertiary: 0.15
tertiary_link: 0.15
residential: 0.15
unclassified: 0.15
road: 0.15
living_street: 0.15
service: 0
track: 0
pedestrian: 0
bridleway: 0
path: 0
footway: 0
steps: 0
cycle: 0
railway: 0
- zoom: 12
widths:
motorway: 2.5
trunk: 2.5
primary: 1.2
secondary: 1.2
motorway_link: 0.5
trunk_link: 0.5
primary_link: 0.5
secondary_link: 0.5
tertiary: 0.5
tertiary_link: 0.5
residential: 0.5
unclassified: 0.5
road: 0.5
living_street: 0.5
service: 0
track: 0
pedestrian: 0
bridleway: 0
path: 0
footway: 0
steps: pedestrian
cycle: 1
railway: 0.4
outlines:
motorway: 1
trunk: 1
primary: 1
secondary: 1
motorway_link: 0.25
trunk_link: 0.25
primary_link: 0.25
secondary_link: 0.25
tertiary: 0.25
tertiary_link: 0.25
residential: 0.25
unclassified: 0.25
road: 0.25
living_street: 0.25
service: 0
track: 0
pedestrian: 0
bridleway: 0
path: 0
footway: 0
steps: 0
cycle: 0
railway: 0
- zoom: 13
widths:
motorway: 3
trunk: 3
primary: 1.5
secondary: 1.5
motorway_link: 0.6
trunk_link: 0.6
primary_link: 0.6
secondary_link: 0.6
tertiary: 1
tertiary_link: 0.6
residential: 0.6
unclassified: 0.6
road: 0.6
living_street: 0.6
service: 0.3
track: 0.2
pedestrian: 0.20
bridleway: 0.20
path: 0.20
footway: 0.20
steps: '@rdz13_pedestrian'
cycle: 1
railway: 0.8
outlines:
motorway: 1
trunk: 1
primary: 1
secondary: 1
motorway_link: 0.6
trunk_link: 0.6
primary_link: 0.6
secondary_link: 0.6
tertiary: 0.6
tertiary_link: 0.6
residential: 0.6
unclassified: 0.6
road: 0.6
living_street: 0.6
tertiary: 1
service: 0
track: 0
pedestrian: 0
bridleway: 0
path: 0
footway: 0
steps: 0
cycle: 0
railway: 0
- zoom: 14
widths:
motorway: 4
trunk: 4
primary: 2.8
secondary: 2.8
motorway_link: 1
trunk_link: 1
primary_link: 1
secondary_link: 1
tertiary: 1
tertiary_link: 1
residential: 1
unclassified: 1
road: 1
living_street: 1
service: 0.33
track: 0.5
pedestrian: 0.25
bridleway: 0.25
path: 0.25
footway: 0.25
steps: '@rdz14_pedestrian'
cycle: 2
railway: 1.0
outlines:
motorway: 1
trunk: 1
primary: 1
secondary: 1
motorway_link: 1
trunk_link: 1
primary_link: 1
secondary_link: 1
tertiary: 1
tertiary_link: 1
residential: 1
unclassified: 1
road: 1
living_street: 1
service: 0
track: 0
pedestrian: 0
bridleway: 0
path: 0
footway: 0
steps: 0
cycle: 0
railway: 0
- zoom: 15
widths:
motorway: 6
trunk: 6
primary: 4
secondary: 4
motorway_link: 1.5
trunk_link: 1.5
primary_link: 1.5
secondary_link: 1.5
tertiary: 1.5
tertiary_link: 1.5
residential: 1.5
unclassified: 1.5
road: 1.5
living_street: 1.5
service: 0.5
track: 0.5
pedestrian: 0.5
bridleway: 0.5
path: 0.5
footway: 0.5
steps: '@rdz15_pedestrian'
cycle: 2
railway: 1.5
outlines:
motorway: 1.25
trunk: 1.25
primary: 1
secondary: 1
motorway_link: 1
trunk_link: 1
primary_link: 1
secondary_link: 1
tertiary: 1
tertiary_link: 1
residential: 1
unclassified: 1
road: 1
living_street: 1
service: 1
track: 1
pedestrian: 0
bridleway: 0
path: 0
footway: 0
steps: 1
cycle: 0
railway: 1
- zoom: 16
widths:
motorway: 10
trunk: 10
primary: 8
secondary: 8
motorway_link: 4
trunk_link: 4
primary_link: 4
secondary_link: 4
tertiary: 4
tertiary_link: 4
residential: 4
unclassified: 4
road: 4
living_street: 4
service: 1.33
track: 1.33
pedestrian: 0.75
bridleway: 0.75
path: 0.75
footway: 0.75
steps: '@rdz16_pedestrian'
cycle: 2
railway: 2
outlines:
motorway: 1.25
trunk: 1.25
primary: 1.25
secondary: 1.25
motorway_link: 1
trunk_link: 1
primary_link: 1
secondary_link: 1
tertiary: 1
tertiary_link: 1
residential: 1
unclassified: 1
road: 1
living_street: 1
service: 1
track: 1
pedestrian: 0
bridleway: 0
path: 0
footway: 0
steps: 1
cycle: 0
railway: 1
- zoom: 17
widths:
motorway: 17
trunk: 17
primary: 15
secondary: 15
motorway_link: 10
trunk_link: 10
primary_link: 10
secondary_link: 10
tertiary: 10
tertiary_link: 10
residential: 10
unclassified: 10
road: 10
living_street: 10
service: 3.3
track: 3.3
pedestrian: 1
bridleway: 1
path: 1
footway: 1
steps: '@rdz17_pedestrian'
cycle: 3
railway: 3
outlines:
motorway: 1.5
trunk: 1.5
primary: 1.25
secondary: 1.25
motorway_link: 1
trunk_link: 1
primary_link: 1
secondary_link: 1
tertiary: 1
tertiary_link: 1
residential: 1
unclassified: 1
road: 1
living_street: 1
service: 1
track: 1
pedestrian: 0
bridleway: 0
path: 0
footway: 0
steps: 1
cycle: 0
railway: 2
- zoom: 18
widths:
motorway: 23
trunk: 23
primary: 20
secondary: 20
motorway_link: 14
trunk_link: 14
primary_link: 14
secondary_link: 14
tertiary: 14
tertiary_link: 14
residential: 14
unclassified: 14
road: 14
living_street: 14
service: 7
track: 7
pedestrian: 1.5
bridleway: 1.5
path: 1.5
footway: 1.5
steps: 2
cycle: 4
railway: 4
outlines:
motorway: 2
trunk: 2
primary: 2
secondary: 2
motorway_link: 1.75
trunk_link: 1.75
primary_link: 1.75
secondary_link: 1.75
tertiary: 1.75
tertiary_link: 1.75
residential: 1.75
unclassified: 1.75
road: 1.75
living_street: 1.75
service: 1.75
track: 1.75
pedestrian: 0
bridleway: 0
path: 0
footway: 0
steps: 1
cycle: 0
railway: 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment