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.

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