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.

139 lines
3.6 KiB

  1. var OverpassLayer = require('overpass-layer')
  2. var parseLength = require('overpass-layer/src/parseLength')
  3. function cssStyle (style) {
  4. let ret = ''
  5. if ('color' in style) {
  6. ret += 'stroke: ' + style.color + ';'
  7. }
  8. ret += 'stroke-width: ' + parseLength('width' in style ? style.width : '3', global.map.getMetersPerPixel()) + ';'
  9. if ('dashArray' in style) {
  10. ret += 'stroke-dasharray: ' + style.dashArray + ';'
  11. }
  12. if ('dashArray' in style) {
  13. ret += 'stroke-dasharray: ' + style.dashArray + ';'
  14. }
  15. if ('dashOffset' in style) {
  16. ret += 'stroke-dashoffset: ' + style.dashOffset + ';'
  17. }
  18. if ('fillColor' in style) {
  19. ret += 'fill: ' + style.fillColor + ';'
  20. } else if ('color' in style) {
  21. ret += 'fill: ' + style.color + ';'
  22. } else {
  23. ret += 'fill: #3388ff;'
  24. }
  25. if ('fillOpacity' in style) {
  26. ret += 'fill-opacity: ' + style.fillOpacity + ';'
  27. } else {
  28. ret += 'fill-opacity: 0.2;'
  29. }
  30. return ret
  31. }
  32. function markerLine (data) {
  33. let styles = parseOptions(data)
  34. let ret = '<svg anchorX="13" anchorY="8" width="25" height="15">'
  35. styles.forEach(style => {
  36. let y = 8.0 + parseLength('offset' in style ? style.offset : 0, global.map.getMetersPerPixel())
  37. ret += '<line x1="0" y1="' + y + '" x2="25" y2="' + y + '" style="' + cssStyle(style) + '"/>'
  38. })
  39. ret += '</svg>'
  40. return OverpassLayer.twig.filters.raw(ret)
  41. }
  42. function markerPolygon (data) {
  43. let ret = '<svg anchorX="13" anchorY="8" width="25" height="25">'
  44. let styles = parseOptions(data)
  45. styles.forEach(style => {
  46. ret += '<rect x="3" y="3" width="18" height="18" style="' + cssStyle(style) + '"/>'
  47. })
  48. ret += '</svg>'
  49. return OverpassLayer.twig.filters.raw(ret)
  50. }
  51. function markerCircle (data) {
  52. let styles = parseOptions(data)
  53. let c = styles
  54. .map(style => (style.size || style.radius || 12) + (style.width / 2))
  55. .sort()[0]
  56. let ret = '<svg anchorX="' + (c + 0.5) + ' anchorY="' + (c + 0.5) + '" width="' + (c * 2) + '" height="' + (c * 2) + '">'
  57. styles.forEach(style => {
  58. ret += '<circle cx="' + c + '" cy="' + c + '" r="' + (style.radius || 12) + '" style="' + cssStyle(style) + '"/>'
  59. })
  60. ret += '</svg>'
  61. return OverpassLayer.twig.filters.raw(ret)
  62. }
  63. function markerPointer (data) {
  64. let ret = '<svg anchorX="13" anchorY="45" width="25" height="45" signAnchorX="0" signAnchorY="-31">'
  65. let styles = parseOptions(data)
  66. styles.forEach(style => {
  67. ret += '<path d="M0.5,12.5 A 12,12 0 0 1 24.5,12.5 C 24.5,23 13,30 12.5,44.5 C 12,30 0.5,23 0.5,12.5" style="' + cssStyle(style) + '"/>'
  68. })
  69. ret += '</svg>'
  70. return OverpassLayer.twig.filters.raw(ret)
  71. }
  72. function parseOptions (data) {
  73. if (!data || !('style' in data) && !('styles' in data)) {
  74. let ret = [
  75. { fillColor: '#f2756a', color: '#000000', width: 1, radius: 12, fillOpacity: 1 },
  76. ]
  77. if (data && data.color) {
  78. ret[0].fillColor = data.color
  79. ret[0].fillOpacity = 0.2
  80. }
  81. if (data) for (let k in data) {
  82. ret[0][k] = data[k]
  83. }
  84. return ret
  85. }
  86. if (!('styles' in data)) {
  87. data = {
  88. style: data,
  89. styles: [ 'default' ]
  90. }
  91. }
  92. if (typeof data.styles === 'string') {
  93. data.styles = data.styles.split(/,/g)
  94. }
  95. return data.styles.map(k => (k === 'default' ? data.style : data['style:' + k]) || {})
  96. }
  97. OverpassLayer.twig.extendFunction('markerLine', markerLine)
  98. OverpassLayer.twig.extendFunction('markerCircle', markerCircle)
  99. OverpassLayer.twig.extendFunction('markerPointer', markerPointer)
  100. OverpassLayer.twig.extendFunction('markerPolygon', markerPolygon)
  101. module.exports = {
  102. line: markerLine,
  103. circle: markerCircle,
  104. pointer: markerPointer,
  105. polygon: markerPolygon
  106. }