app.component.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. import VectorLayer from 'ol/layer/Vector';
  2. import hsv2rgb from 'hsv2rgb';
  3. import nuts from './nuts.js';
  4. import {Fill, Stroke, Style} from 'ol/style';
  5. export const HsComponent = {
  6. template: (HsCore) => {
  7. 'ngInject';
  8. return HsCore.hslayersNgTemplate;
  9. },
  10. bindings: {},
  11. controller: function (
  12. $scope,
  13. $compile,
  14. $element,
  15. gettext,
  16. HsCore,
  17. HsLayoutService,
  18. HsSidebarService,
  19. PraAdjusterService
  20. ) {
  21. 'ngInject';
  22. const vm = this;
  23. vm.exists = HsCore.exists;
  24. vm.panelVisible = HsLayoutService.panelVisible;
  25. vm.PraAdjusterService = PraAdjusterService;
  26. HsLayoutService.fullScreenMap($element, HsCore);
  27. HsLayoutService.sidebarRight = false;
  28. // HsLayoutService.sidebarToggleable = false;
  29. // HsCore.singleDatasources = true;
  30. //HsLayoutService.sidebarButtons = true;
  31. HsSidebarService.buttons.push({
  32. panel: 'adjuster',
  33. module: 'pra.adjuster',
  34. order: '6',
  35. title: 'Adjust factors',
  36. description: '',
  37. icon: 'icon-analytics-piechart',
  38. });
  39. $scope.$on('scope_loaded', function (event, args) {
  40. if (args === 'Sidebar') {
  41. const el = angular.element(
  42. '<pra-adjuster hs.draggable ng-if="vm.exists(\'pra.adjuster\')" ng-show="vm.panelVisible(\'adjuster\', this)"></pra-adjuster>'
  43. )[0];
  44. HsLayoutService.panelListElement.appendChild(el);
  45. $compile(el)($scope);
  46. HsLayoutService.setDefaultPanel('adjuster');
  47. const loader = angular.element(
  48. '<pra-adjuster-loader class="loader-splash" ng-if="vm.PraAdjusterService.isClusteringInProcess()"></pra-adjuster-loader>'
  49. )[0];
  50. HsLayoutService.contentWrapper
  51. .querySelector('.hs-page-content')
  52. .appendChild(loader);
  53. $compile(loader)(event.targetScope);
  54. }
  55. });
  56. $scope.$on('clusters_loaded', function (event, args) {
  57. randomColorPalette = generateRandomColorPalette(
  58. PraAdjusterService.clusters.length
  59. );
  60. });
  61. },
  62. controllerAs: 'vm',
  63. };
  64. let randomColorPalette = [];
  65. const nuts2style = new Style({
  66. stroke: new Stroke({
  67. color: '#FFFFFF',
  68. width: 0.5,
  69. }),
  70. });
  71. // TODO: 'method' must be obtained from AdjusterService
  72. const method = 'haclust';
  73. const nuts3style = function (feature) {
  74. if (isNaN(feature.get(method))) {
  75. return [
  76. new Style({
  77. fill: new Fill({
  78. color: '#FFF',
  79. }),
  80. stroke: new Stroke({
  81. color: '#3399CC',
  82. width: 0.25,
  83. }),
  84. }),
  85. ];
  86. } else {
  87. return [
  88. new Style({
  89. fill: new Fill({
  90. color: randomColorPalette[feature.get(method) - 1],
  91. }),
  92. }),
  93. ];
  94. }
  95. };
  96. export const nuts2Layer = new VectorLayer({
  97. source: nuts.nuts2Source,
  98. editor: {editable: false},
  99. visible: true,
  100. style: nuts2style,
  101. title: 'NUTS2 regions',
  102. });
  103. export const nuts3Layer = new VectorLayer({
  104. source: nuts.nuts3Source,
  105. editor: {editable: false},
  106. visible: true,
  107. style: nuts3style,
  108. title: 'NUTS3 regions',
  109. });
  110. nuts3Layer.set('popUp', {
  111. attributes: [
  112. {attribute: 'CNTR_CODE', label: 'Country'},
  113. {attribute: 'NUTS_NAME', label: 'Name'},
  114. {attribute: method, label: 'Cluster ID'},
  115. ],
  116. });
  117. /**
  118. * https://martin.ankerl.com/2009/12/09/how-to-create-random-colors-programmatically/
  119. * @param {number} colorCount Number of colors to randomly generate
  120. * @returns {Array<Array<Number>>} Array of RGB colors
  121. */
  122. function generateRandomColorPalette(colorCount) {
  123. const palette = [];
  124. const goldenRatioConjugate = 0.618033988749895;
  125. for (let i = 0; i < colorCount; i++) {
  126. let h = Math.random();
  127. h += goldenRatioConjugate;
  128. h %= 1;
  129. h *= 360;
  130. palette.push(hsv2rgb(h, 0.5, 0.95));
  131. }
  132. return palette;
  133. //return `rgba(${r}, ${g}, ${b}, 0.7)`;
  134. }