# -------------------------------------------------------------------
# This is the default mkgmap style file for converting OSM ways
# into Garmin lines.
# Any line prefaced with # is a comment and will not be applied.
# Each line is a rule, of the generalised format:
#  OSM_tag(s)_to_match  [Garmin_type_code resolution]
# See http://wiki.openstreetmap.org/wiki/Mkgmap/help/style_rules
# and http://wiki.openstreetmap.org/wiki/Mkgmap/help/Custom_styles
# for more information.

addr:housenumber=* {set mkgmap:execute_finalize_rules=true}

abandoned=yes {deletealltags}  # old, depreciated, ambiguous, method of handling abandoned

# do these as lines regardless of being closed unless explicity marked as area. continue in case also a highway
aeroway=runway & area!=yes {name '${ref}'} [0x27 resolution 20 continue]
(aeroway=taxiway | aeroway=taxilane) & area!=yes {name '${ref}'} [0x27 resolution 24 continue]

# Assign the street name for house number search
highway=* & name=* {set mkgmap:street='${name}'}

# Mark highways with the toll flag
highway=* & (toll=yes | toll=true) {set mkgmap:toll=yes}

# Hide proposed ways
highway=proposed | highway=proposal | highway=planned | highway~'.*proposed.*' {delete highway; delete junction}
# Hide removed ways
highway=razed | highway=dismantled | highway=disused | highway=demolished {delete highway; delete junction}
# Hide abandoned ways. Abandoned highways have some evidence of their former existence but are no longer used. These
# abandoned highways could be useful in topographical maps.
# https://wiki.openstreetmap.org/wiki/Key:abandoned:
(abandoned:highway=* & (highway!=* | highway=yes)) | highway=abandoned {delete highway; delete junction}
# Hide other non-existent ways
highway=unbuilt | highway=neverbuilt | highway=rejected | highway~'x-.*' {delete highway; delete junction}
# Remove highway tag from ways which are not suitable for routing
highway=traffic_signals | highway=junction | highway=island | highway=centre_line | highway=traffic_island | highway=stopline |
    highway=bus_stop | highway=bus_guideway | highway=emergency_access_point | highway=ohm:military:Trench
    {delete highway}
highway=via_ferrata {delete highway}  # this shouldn't show as routable on default map: path only for specialists
highway=piste | highway=ski {delete highway}
highway=no | highway=none {delete highway}

# Disable dead-end-checks for unaccessible oneways
highway=* & oneway=yes & (access=private | access=no) {add mkgmap:dead-end-check=false}
# Validation-like checks (uncomment to enable)
#highway=motorway_link & oneway!=yes & oneway!=no {echo "motorway_link lacks oneway"}
highway=motorway | highway=motorway_link {add oneway=yes; add mkgmap:numbers=false}

# start of rules for process-exits and process-destination options
# which may add info to a part of these highway=*_link roads:
# motorway_link, trunk_link, primary_link, secondary_link, tertiary_link
# build destination hint
mkgmap:dest_hint=* {
    set tmp:dest_hint='${destination:ref|subst: =>} ${mkgmap:dest_hint|subst:;=> |subst:/=> }' |
                      '${ref|subst: =>} ${mkgmap:dest_hint|subst:;=> |subst:/=> }' |
                      '${mkgmap:dest_hint|subst:;=> |subst:/=> }';
    }
# build exit hint
mkgmap:exit_hint=true {
    set tmp:exit_hint='Exit ${mkgmap:exit_hint_ref} ${mkgmap:exit_hint_name}' |
                      'Exit ${mkgmap:exit_hint_ref} ${mkgmap:exit_hint_exit_to}' |
                      'Exit ${mkgmap:exit_hint_exit_to}' |
                      'Exit ${mkgmap:exit_hint_name}' |
                      'Exit ${mkgmap:exit_hint_ref}';
    }
# use destination hint and/or exit hint to build name
mkgmap:exit_hint=true | mkgmap:dest_hint=* {name '${tmp:exit_hint} ${tmp:dest_hint}' | '${tmp:dest_hint}' | '${tmp:exit_hint}'}
# end of rules for process-exits and process-destination options

# Flag paved roads
highway=* & (surface=asphalt | surface=paved | surface=sett |
    surface=concrete | surface=concrete:lanes | surface=concrete:plates |
    surface=paving_stones | surface=cobblestone |
    surface=cobblestone:flattened | surface=metal | surface=wood | surface=chipseal)
    {set mkgmap:unpaved=0}
highway=* & tracktype=grade1 & surface!=* {set mkgmap:unpaved=0}

# Flag unpaved roads.
highway=* & mkgmap:unpaved!=0 & (
    surface=* |
    mtb:scale=* |
    tracktype~'grade[2-6]')
    {add mkgmap:unpaved=1}
highway=* & (
    mtb:scale~'[2-6].' |
    sac_scale~'.*(mountain|alpine)_hiking' |
    sport=via_ferrata)
    {set mkgmap:unpaved=1}
(highway=bridleway | highway=path | highway=track) & mkgmap:unpaved!=0 {add mkgmap:unpaved=1}
(highway=unsurfaced | highway=via_ferrata) {set mkgmap:unpaved=1}

highway=* & mkgmap:unpaved!=1 & smoothness~'.*(bad|horrible|impassable)' {add mkgmap:road-speed=-2}

# Good ways without relation
highway=* & mkgmap:fast_road!=* & (int_ref=* | network=e-road | network=AH | network=TAH | network=US:I | network=US:US)
    {add mkgmap:fast_road=yes}
# Not best for car routing
highway=* & (access=hov | hov=designated | oneway=reversible)
    {set mkgmap:road-class=-2; set mkgmap:road-speed=-2; delete mkgmap:fast_road}

# convert some specific to generic with restrictions (platform is in a bus station)
highway=steps | highway=corridor | highway=stepping_stones | highway=elevator | highway=escalator | highway=platform
    {set highway=footway; add bicycle=no}
# virtual seems to be a way across a pedestrian area
highway=crossing | highway=virtual
    {set highway=path}  # following logic may change

# Convert generic path to most specific
highway=path & (designation=permissive_bridleway | designation=public_bridleway)
    {set highway=bridleway; add foot=yes}
highway=path & (designation=permissive_footpath | designation=public_footpath)
    {set highway=footway; add foot=designated}
highway=path & snowplowing!=no & (bicycle=designated | bicycle=permissive | bicycle=official | cycleway=lane)
    {set highway=cycleway; add foot=yes}
highway=path & foot=designated
    {set highway=footway}

leisure=track & area!=yes {name '${name} (${sport})' | '${sport}'} [0x30 resolution 22]
man_made=pier | man_made=piste:halfpipe {add highway=footway; name '${ref} ${name}' | '${ref}' | '${name}'}

man_made=breakwater & is_closed()=false & mkgmap:mp_created!=true [0x17 resolution 22 default_name 'Breakwater']

# Roundabouts
junction=roundabout & (highway=trunk | highway=trunk_link) [0x0c road_class=4 road_speed=2 resolution 24 continue]
junction=roundabout & (highway=trunk | highway=trunk_link) [0x10801 resolution 18]

junction=roundabout & (highway=primary | highway=primary_link) & mkgmap:fast_road=yes [0x0c road_class=4 road_speed=2 resolution 24 continue]
junction=roundabout & (highway=primary | highway=primary_link) & mkgmap:fast_road=yes [0x10802 resolution 19]

junction=roundabout & (highway=primary | highway=primary_link) [0x0c road_class=3 road_speed=2 resolution 24 continue]
junction=roundabout & (highway=primary | highway=primary_link) [0x10802 resolution 19]

junction=roundabout & (highway=secondary | highway=secondary_link) & mkgmap:fast_road=yes [0x0c road_class=3 road_speed=2 resolution 24 continue]
junction=roundabout & (highway=secondary | highway=secondary_link) & mkgmap:fast_road=yes [0x10803 resolution 20]

junction=roundabout & (highway=secondary | highway=secondary_link) [0x0c road_class=2 road_speed=2 resolution 24 continue]
junction=roundabout & (highway=secondary | highway=secondary_link) [0x10803 resolution 20]

junction=roundabout & (highway=tertiary | highway=tertiary_link) [0x0c road_class=1 road_speed=1 resolution 24 continue]
junction=roundabout & (highway=tertiary | highway=tertiary_link) [0x10804 resolution 21]

junction=roundabout & (highway=unclassified | highway=minor) [0x0c road_class=1 road_speed=1 resolution 21]
junction=roundabout & highway=* [0x0c road_class=0 road_speed=1 resolution 22]

# Ways that may or may not be useable

# Treat ways under construction almost as highway=path, see also extra rule in inc/access
highway=construction {add mkgmap:dead-end-check=false} [0x16 road_class=0 road_speed=0 resolution 23]

# Lower the road speed of ways under some construction.
highway=* & construction=* & maxspeed!=* {add mkgmap:road-speed='-1'}
# Lower the road speed of disused ways.
highway=* & disused=yes & maxspeed!=* {add mkgmap:road-speed='-2'}

# Ways sorted roughly by descending order of class
highway=motorway & mkgmap:fast_road=yes [0x01 road_class=4 road_speed=7 resolution 14]
highway=motorway [0x01 road_class=4 road_speed=7 resolution 15]
highway=motorway_link & (mkgmap:exit_hint=true | mkgmap:dest_hint=*) [0x0b road_class=4 road_speed=2 resolution 20]
highway=motorway_link [0x09 road_class=4 road_speed=2 resolution 20]

highway=trunk & mkgmap:fast_road=yes [0x02 road_class=4 road_speed=5 resolution 15]
highway=trunk [0x02 road_class=4 road_speed=5 resolution 18]
highway=trunk_link & (mkgmap:exit_hint=true | mkgmap:dest_hint=*) [0x0b road_class=4 road_speed=2 resolution 20]
highway=trunk_link [0x09 road_class=4 road_speed=2 resolution 20]
highway=* & motorroad=yes [0x02 road_class=4 road_speed=4 resolution 18]
highway=primary & mkgmap:fast_road=yes [0x03 road_class=4 road_speed=4 resolution 17]
highway=primary [0x03 road_class=3 road_speed=4 resolution 19]
highway=primary_link & (mkgmap:exit_hint=true | mkgmap:dest_hint=*) & mkgmap:fast_road=yes [0x0b road_class=4 road_speed=1 resolution 21]
highway=primary_link & (mkgmap:exit_hint=true | mkgmap:dest_hint=*) [0x0b road_class=3 road_speed=1 resolution 21]
highway=primary_link & mkgmap:fast_road=yes [0x08 road_class=4 road_speed=1 resolution 21]
highway=primary_link [0x08 road_class=3 road_speed=1 resolution 21]
highway=secondary & mkgmap:fast_road=yes [0x04 road_class=3 road_speed=3 resolution 18]
highway=secondary [0x04 road_class=2 road_speed=3 resolution 20]
highway=secondary_link & mkgmap:fast_road=yes [0x08 road_class=3 road_speed=1 resolution 22]
highway=secondary_link [0x08 road_class=2 road_speed=1 resolution 22]
highway=tertiary [0x05 road_class=1 road_speed=3 resolution 20]
highway=tertiary_link [0x08 road_class=1 road_speed=1 resolution 22]
highway=minor [0x06 road_class=1 road_speed=3 resolution 21]
highway=unclassified [0x06 road_class=0 road_speed=3 resolution 21]

highway=yes {set highway=footway}  # fix common bad tagging - little bits of path to join other routable ways

# Some countries allow, others disallow bicycling on pedestrian streets. See inc/access_country

# highway=pedestrian could be closed polygon to represent a 'square', add route regardless and continue for polygon processing
highway=pedestrian {set tmp:stopMopUp=yes} [0x16 road_class=0 road_speed=0 resolution 22 continue with_actions]

highway=living_street [0x06 road_class=0 road_speed=1 resolution 22]
highway=residential [0x06 road_class=0 road_speed=2 resolution 22]
highway=bridleway [0x07 road_class=0 road_speed=0 resolution 23]
highway=byway [0x16 road_class=0 road_speed=0 resolution 23]

highway=driveway {set highway=service; add service=driveway}  # fix common bad tagging
highway=access  {set highway=service}  # fix common bad tagging
highway=access_ramp {set highway=footway}  # proposed

highway=service & service=parking_aisle [0x07 road_class=0 road_speed=1 resolution 24]
highway=service & (service=alley | service=driveway) [0x07 road_class=0 road_speed=0 resolution 23]
highway=service [0x07 road_class=0 road_speed=2 resolution 22]

highway=cycleway [0x11 road_class=0 road_speed=1 resolution 23]

# highway=footway is often an area as well, continue for polygon processing
highway=footway {set tmp:stopMopUp=yes} [0x16 road_class=0 road_speed=0 resolution 23 continue with_actions]

highway=trail {set highway=path; add bicycle=no}  # fix common bad tagging

highway=path [0x16 road_class=0 road_speed=0 resolution 23]
highway=track [0x0a road_class=0 road_speed=1 resolution 22]
highway=unsurfaced [0x0a road_class=0 road_speed=1 resolution 22]
highway=road {add mkgmap:dead-end-check=false} [0x06 road_class=0 road_speed=1 resolution 22]

# handle some rarer bits of road. Do just in case joining paths or bits of one-way roads
highway=turning_loop | highway=turning_circle | highway=layby | highway=escape | highway=emergency_bay
    [0x07 road_class=0 road_speed=0 resolution 24]

# These are too specialized to have routing, so shouldn't drop into the mop-up, but, maybe needing TYP, can be shown on the map
highway=raceway | highway=gallop {add name='${highway}'} [0x30 resolution 23]

# Don't have routable for either of these:
# most 'rest_area' don't connect to the main road
# 'services' is normally a big area that covers the whole complex
highway=rest_area | highway=services {set tmp:stopMopUp=yes}

# Mop up any unrecognised highway types
#highway=* & tmp:stopMopUp!=yes {echotags 'mop-up highway'}  # mostly actual road names
# It is considered best not to process unrecognised highway types
#highway=* & area!=yes & tmp:stopMopUp!=yes [0x07 road_class=0 road_speed=0 resolution 23]

# Showing the coastline adds almost nothing visuals of the map, so it's disabled:
#natural=coastline [0x15 resolution 12]
power=line [0x29 resolution 21]

railway=platform & tmp:stopMopUp!=yes [0x16 road_class=0 road_speed=0 resolution 23]
# Railway lines, note that many devices display type 0x14 only at resolution 24
(railway=rail | railway=tram | railway=disused | railway=subway | railway=narrow_gauge | railway=light_rail | railway=preserved) & !(tunnel=yes)
    [0x14 resolution 22]

(man_made=cable | (man_made=* & man_made~'.*pipe.*')) & area!=yes & tunnel!=yes & location!=underground
    {name '${name} (${operator})' | '${name}' | '${operator}'} [0x28 resolution 23]

# The following boundary styles are after the highway rules because ways
# are frequently tagged with both and we want the highway to take priority.
boundary=administrative {name '${mkgmap:boundary_name}'}
boundary=administrative & admin_level<3 [0x1e resolution 12]
boundary=administrative & admin_level<5 [0x1d resolution 19]
boundary=administrative & admin_level<7 [0x1c resolution 21]
boundary=administrative & admin_level<9 [0x1c resolution 22]
boundary=administrative [0x1c resolution 22]
boundary=national [0x1e resolution 17]
boundary=political [0x1c resolution 19]

barrier=wall | barrier=fence | barrier=hedge | barrier=ditch {add name='${barrier|subst:"_=> "}'} [0x17 resolution 24]

route=ferry & (toll=no | toll=false) {set mkgmap:toll=no}
route=ferry {set mkgmap:numbers=false; set mkgmap:ferry=1; add mkgmap:toll=yes}
route=ferry & (motorcar=no | motor_vehicle=no) [0x1b road_class=0 road_speed=0 resolution 23]
route=ferry [0x1a road_class=3 road_speed=0 resolution 19]

(waterway=river | waterway=stream | waterway=rapids | waterway=waterfall) {add mkgmap:has-direction=true}
(waterway=river | waterway=canal) & intermittent=yes [0x26 resolution 20]
(waterway=stream | waterway=drain) & intermittent=yes [0x26 resolution 22]

waterway=canal [0x1f resolution 21]
waterway=river [0x1f resolution 18]
waterway=rapids|waterway=waterfall [0x1f resolution 22]
waterway=stream | waterway=drain [0x18 resolution 22]

# mop up any remaining water lines
waterway=* & waterway!=no & (area=no | (is_closed()=false & mkgmap:mp_created!=true))
    {add name='${waterway|subst:"_=> "}'} [0x26 resolution 24]

include 'inc/contour_lines';

<finalize>
# The finalizer section is executed for each element when a rule with an element type matches

include 'inc/address';
# calculate the road speed based on maxspeed tag
include 'inc/roadspeed';
# calculate the access rules
include 'inc/access';
                                   
#limit artificial cycleways to resolution 24
mkgmap:synthesised=yes & mkgmap:bicycle=yes {set mkgmap:highest-resolution-only=true}

# don't add house numbers to unnamed or artifical bicycle ways
mkgmap:bicycle=yes & (mkgmap:foot=no & mkgmap:car=no & mkgmap:street!=* | mkgmap:synthesised=yes) {set mkgmap:numbers=false}

# Display highway shield for mayor roads if they have a ref and make them searchable by their name
(highway=motorway | highway=trunk)     & ref=* {name '${ref|highway-symbol:hbox}'; addlabel '${name}'}
highway=primary                        & ref=* {name '${ref|highway-symbol:box}' ; addlabel '${name}'}
(highway=secondary | highway=tertiary) & ref=* {name '${ref|highway-symbol:oval}'; addlabel '${name}'}

name=* {name '${name}'}
highway=* & ref=* & highway!=motorway & highway!=trunk & highway!=primary & highway!=secondary & highway!=tertiary
    {addlabel '${ref}'}

highway=* & int_ref=* {addlabel '${int_ref}'}
highway=* & nat_ref=* {addlabel '${nat_ref}'}
highway=* & reg_ref=* {addlabel '${reg_ref}'}
