You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

207 lines
8.3 KiB

  1. Categories can be created as YAML files. This is much simpler as JSON files, because you don't have to add all these quotes, you can use multi-line strings and allows adding comments.
  2. A simple example ([Source](https://www.openstreetbrowser.org/dev/OpenStreetBrowser/examples/src/branch/master/example1.yaml)). It queries nodes, ways and relations with amenity=restaurant from OpenStreetMap (via Overpass API), starting from zoom level 15. `nwr` is short for `(node[amenity=restaurant];way[amenity=restaurant];relation[amenity=restaurant];)`. Please note, that only a subset of OverpassQL is available (see [overpass-frontend](https://github.com/plepe/overpass-frontend) for details).
  3. ```yaml
  4. # From zoom level 15 on, load all node, ways and relations with amenity=restaurant.
  5. query:
  6. 15: nwr[amenity=restaurant]
  7. ```
  8. Another example, showing fountains from z15 and (additionally) drinking_water from z17. ([Source](https://www.openstreetbrowser.org/dev/OpenStreetBrowser/examples/src/branch/master/example2.yaml)):
  9. This is the first examples which uses [TwigJS](https://github.com/twigjs/twig.js) for programming logic. TwigJS is a port of the [Twig template language](https://twig.symfony.com/doc/3.x/templates.html).
  10. Here, we are using Unicode characters as icons. Alternatively, OpenStreetBrowser includes a [few icon sets](./Icons.md) which you can use.
  11. ```yaml
  12. query:
  13. # query as single line string:
  14. 15: nwr[amenity=fountain]
  15. # query as multi line string:
  16. 17: |
  17. (
  18. nwr[amenity=fountain];
  19. nwr[amenity=drinking_water];
  20. )
  21. feature:
  22. # In the description, '{{ ... }}' is a TwigJS template. In this case it will
  23. # translate either the tag 'amenity=fountain' or 'amenity=drinking_water'
  24. # into a localized string:
  25. description: |
  26. {{ tagTrans('amenity', tags.amenity) }}
  27. # '{% ... %} is a code block in Twig, it can be used for setting variables,
  28. # if and for statements. This places different icons in the markers:
  29. markerSign: |
  30. {% if tags.amenity == 'fountain' %}
  31. {% elseif tags.amenity == 'drinking_water' %}
  32. 🚰
  33. {% endif %}
  34. ```
  35. Improving on the example above, we add a `const` block. The values of this block are available throughout the code ([Source](https://www.openstreetbrowser.org/dev/OpenStreetBrowser/examples/src/branch/master/example3.yaml)):
  36. ```yaml
  37. # Adding a category name (in English: "Example 3")
  38. name:
  39. en: Example 3
  40. query:
  41. 15: nwr[amenity=fountain]
  42. # This query uses a regular expression to match either fountain or drinking_water:
  43. 17: nwr[amenity~"^(fountain|drinking_water)$"]
  44. feature:
  45. description: |
  46. {{ tagTrans('amenity', tags.amenity) }}
  47. # Here, the correct icon for display is read from the 'const' structure
  48. markerSign: |
  49. {{ const[tags.amenity].icon }}
  50. # We can use different markers depending on the type of item
  51. markerSymbol: |
  52. {{ markerPointer({ fillColor: const[tags.amenity].color }) }}
  53. # This is for the marker in the listing in the sidebar
  54. listMarkerSymbol: |
  55. {{ markerCircle({ fillColor: const[tags.amenity].color }) }}
  56. const:
  57. fountain:
  58. icon: ⛲
  59. color: '#0000ff' # need to quote, because YAML would treat the color as comment
  60. drinking_water:
  61. icon: 🚰
  62. color: '#007fff'
  63. ```
  64. Improving on the example above, we add a `info` block to show a map key. ([Source](https://www.openstreetbrowser.org/dev/OpenStreetBrowser/examples/src/branch/master/example4.yaml)):
  65. ```yaml
  66. query:
  67. 15: nwr[amenity=fountain]
  68. 17: nwr[amenity~"^(fountain|drinking_water)$"]
  69. feature:
  70. description: |
  71. {{ tagTrans('amenity', tags.amenity) }}
  72. # Here, the correct icon for display is read from the 'const' structure
  73. markerSign: |
  74. {{ const[tags.amenity].icon }}
  75. markerSymbol: |
  76. {{ markerPointer({ fillColor: const[tags.amenity].color }) }}
  77. listMarkerSymbol: |
  78. {{ markerCircle({ fillColor: const[tags.amenity].color }) }}
  79. info: |
  80. # We create a table which shows icon in the left column and description in the
  81. # right. Due to the 'if' statement in the for loop the map key changes due to
  82. # the current zoom level (`map.zoom`):
  83. <table>
  84. {% for value, data in const if data.zoom <= map.zoom %}
  85. <tr>
  86. <td>{{ markerCircle({ fillColor: data.color }) }}<div class='sign'>{{ data.icon }}</div></td>
  87. <td>{{ tagTrans('amenity', value) }}</td>
  88. </tr>
  89. {% endfor %}
  90. </table>
  91. const:
  92. fountain:
  93. icon: ⛲
  94. color: '#0000ff'
  95. zoom: 15
  96. drinking_water:
  97. icon: 🚰
  98. color: '#007fff'
  99. zoom: 17
  100. ```
  101. Back to the restaurants, we will display the cuisine(s) of the restaurants and even add a filter. In OpenStreetMap, cuisine is tag which can take several values, separated by `;`, e.g. `pizza;burger`. Detailed documentation about filters can be found [here](./Filter.md). ([Source](https://www.openstreetbrowser.org/dev/OpenStreetBrowser/examples/src/branch/master/example5.yaml)):
  102. ```yaml
  103. name:
  104. en: Example 5
  105. query:
  106. 15: nwr[amenity=restaurant]
  107. feature:
  108. description: |
  109. {{ tagTrans('amenity', tags.amenity) }}
  110. # Details are written to the right side of the popup / the box in the list.
  111. # tagTransList is a function, which splits the value by ';' and translates
  112. # each value individually. They are joined as enumeration.
  113. details: |
  114. {{ tagTransList('cuisine', tags.cuisine) }}
  115. # Body is shown in the popup and the details in the sidebar. An easy way to
  116. # show all tags is using the TwigJS 'yaml' filter, which produces YAML.
  117. # Alternatively, you could use 'json_pp' (JSON pretty print).
  118. body: |
  119. <pre>{{ tags|yaml }}</pre>
  120. filter:
  121. cuisine:
  122. name: "{{ keyTrans('cuisine') }}"
  123. type: select
  124. key: cuisine
  125. op: has # query semicolon-separated lists
  126. values: |
  127. {% for value in const.cuisine %}
  128. <option value='{{ value }}'>{{ tagTrans('cuisine', value) }}</option>
  129. {% endfor %}
  130. <option value='-' query='nwr[!cuisine]' weight='1'>{{ trans('empty value') }}</option>
  131. <option value='*' query='nwr[cuisine]' weight='1'>Any value</option>
  132. # The option will be ordered by text content. Set 'weight' option to override order.
  133. # Also, the last two options set an explicit OverpassQL query.
  134. const:
  135. cuisine: ["pizza", "burger", "kebab"]
  136. ```
  137. Roads, with different color depending on its priority ([Source](https://www.openstreetbrowser.org/dev/OpenStreetBrowser/examples/src/branch/master/roads1.yaml)):
  138. ```yaml
  139. name:
  140. en: Roads 1 # English name of the category
  141. query:
  142. 9: way[highway~"^(motorway|trunk)$"];
  143. 11: way[highway~"^(motorway|trunk|primary)$"];
  144. 13: way[highway~"^(motorway|trunk|primary|secondary|tertiary)$"];
  145. 15: way[highway~"^(motorway|trunk|primary|secondary|tertiary|road|residential)$"];
  146. feature:
  147. description: |
  148. {{ tagTrans('highway', tags.highway) }}
  149. markerSymbol: # empty, to hide the marker
  150. listMarkerSymbol: line # show a line which is generated from the style
  151. style:
  152. width: 4
  153. color: |
  154. {% if tags.highway == 'motorway' %}#ff0000
  155. {% elseif tags.highway == 'trunk' %}#ff3f00
  156. {% elseif tags.highway == 'primary' %}#ff7f00
  157. {% else %}#ffff00{% endif %}
  158. ```
  159. We rewrite the above example to use `const` for coloring. Also, we are adding a casing, to improve visibility of the roads on the map, and a label. ([Source](https://www.openstreetbrowser.org/dev/OpenStreetBrowser/examples/src/branch/master/roads1.yaml)):
  160. ```yaml
  161. name:
  162. en: Roads 2 # English name of the category
  163. query:
  164. 9: way[highway~"^(motorway|trunk)$"];
  165. 11: way[highway~"^(motorway|trunk|primary)$"];
  166. 13: way[highway~"^(motorway|trunk|primary|secondary|tertiary)$"];
  167. 15: way[highway~"^(motorway|trunk|primary|secondary|tertiary|road|residential)$"];
  168. feature:
  169. description: |
  170. {{ tagTrans('highway', tags.highway) }}
  171. markerSymbol: # empty, to hide the marker
  172. listMarkerSymbol: line # show a line which is generated from the style
  173. style:casing:
  174. width: 8
  175. color: '#000000'
  176. pane: casing # use the predefined 'casing' pane, so that this line is below the 'style'
  177. style:
  178. width: 4
  179. color: |
  180. {{ (const[tags.highway]|default(const.default)).color }}
  181. text: |
  182. {{ tags.name }}
  183. textOffset: -8
  184. const:
  185. motorway:
  186. color: '#ff0000'
  187. trunk:
  188. color: '#ff3f00'
  189. primary:
  190. color: '#ff7f00'
  191. default:
  192. color: '#ffff00'
  193. ```
  194. All scripts of a feature are processed in the order of their appearance. As they all use the same scope, Twig variables (set via `{% set varname = 'value' %}`) are available in all sub-sequent scripts.