app.component.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import * as hsv2rgb from 'hsv2rgb';
  2. import {Fill, Stroke, Style} from 'ol/style';
  3. import {Vector as VectorLayer} from 'ol/layer';
  4. import {nuts} from './nuts';
  5. export const HsComponent = {
  6. template: (HsCore) => {
  7. 'ngInject';
  8. return HsCore.hslayersNgTemplate;
  9. },
  10. bindings: {},
  11. controllerAs: 'vm',
  12. controller: class HsComponent {
  13. exists;
  14. panelVisible;
  15. PraAdjusterService;
  16. constructor(
  17. gettext,
  18. HsCore,
  19. HsLayoutService,
  20. HsSidebarService,
  21. AdjusterService,
  22. AppService
  23. ) {
  24. 'ngInject';
  25. const vm = this;
  26. //vm.exists = HsCore.exists;
  27. //vm.panelVisible = HsLayoutService.panelVisible;
  28. //vm.PraAdjusterService = AdjusterService;
  29. HsLayoutService.sidebarRight = false;
  30. console.log(HsLayoutService.sidebarRight);
  31. AdjusterService.clustersLoaded.subscribe(() => {
  32. colorPalette = generateRandomColorPalette(
  33. AdjusterService.clusters.length
  34. );
  35. });
  36. }
  37. },
  38. };
  39. // https://colorbrewer2.org/?type=qualitative&scheme=Paired&n=12
  40. let colorPalette = [
  41. '#a6cee3',
  42. '#1f78b4',
  43. '#b2df8a',
  44. '#33a02c',
  45. '#fb9a99',
  46. '#e31a1c',
  47. '#fdbf6f',
  48. '#ff7f00',
  49. '#cab2d6',
  50. '#6a3d9a',
  51. '#ffff99',
  52. '#b15928',
  53. ];
  54. const nuts2style = new Style({
  55. stroke: new Stroke({
  56. color: '#000000',
  57. width: 0.5,
  58. }),
  59. });
  60. // TODO: 'method' must be obtained from AdjusterService
  61. const method = 'haclustwd2';
  62. const serviceUrl = 'https://jmacura.ml/ws/georeport/';
  63. const nuts3style = function (feature) {
  64. if (isNaN(feature.get(method))) {
  65. return [
  66. new Style({
  67. fill: new Fill({
  68. color: '#FFF',
  69. }),
  70. stroke: new Stroke({
  71. color: '#3399CC',
  72. width: 0.25,
  73. }),
  74. }),
  75. ];
  76. } else {
  77. return [
  78. new Style({
  79. fill: new Fill({
  80. color: colorPalette[feature.get(method) - 1],
  81. }),
  82. stroke: new Stroke({
  83. color: '#FFF',
  84. width: 0.25,
  85. }),
  86. }),
  87. ];
  88. }
  89. };
  90. export const nuts2Layer = new VectorLayer({
  91. source: nuts.nuts2Source,
  92. editor: {editable: false},
  93. visible: false,
  94. style: nuts2style,
  95. title: 'NUTS2 regions',
  96. });
  97. export const nuts3Layer = new VectorLayer({
  98. source: nuts.nuts3Source,
  99. editor: {editable: false},
  100. visible: true,
  101. style: nuts3style,
  102. title: 'NUTS3 regions',
  103. });
  104. nuts3Layer.set('popUp', {
  105. attributes: [
  106. {attribute: 'CNTR_CODE', label: 'Country'},
  107. {attribute: 'NUTS_NAME', label: 'Name'},
  108. {attribute: method, label: 'Cluster ID'},
  109. {
  110. attribute: 'NUTS_ID',
  111. label: 'Detailed report',
  112. displayFunction: (x) => {
  113. return `<a href="${serviceUrl}${x}" target="_blank">in a new page</a>.`;
  114. },
  115. },
  116. ],
  117. });
  118. /**
  119. * https://martin.ankerl.com/2009/12/09/how-to-create-random-colors-programmatically/
  120. * @private
  121. * @param {number} colorCount Number of colors to randomly generate
  122. * @returns {Array<Array<Number>>} Array of RGB colors
  123. */
  124. function generateRandomColorPalette(colorCount) {
  125. const palette = colorPalette;
  126. const goldenRatioConjugate = 0.618033988749895;
  127. let i = palette.length;
  128. while (i < colorCount) {
  129. let h = Math.random();
  130. h += goldenRatioConjugate;
  131. h %= 1;
  132. h *= 360;
  133. palette.push(hsv2rgb(h, 0.5, 0.95));
  134. i++;
  135. }
  136. return palette;
  137. //return `rgba(${r}, ${g}, ${b}, 0.7)`;
  138. }