app.service.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import hsv2rgb from 'hsv2rgb';
  2. import {Feature} from 'ol';
  3. import {Fill, Stroke, Style} from 'ol/style';
  4. import {Injectable} from '@angular/core';
  5. import {TopoJSON} from 'ol/format';
  6. import {Vector as VectorLayer} from 'ol/layer';
  7. import {Vector as VectorSource} from 'ol/source';
  8. import {HsConfig} from 'hslayers-ng/config.service';
  9. import {HsEventBusService} from 'hslayers-ng/components/core/event-bus.service';
  10. import {HsLanguageService} from 'hslayers-ng/components/language/language.service';
  11. import {HsLayerManagerService} from 'hslayers-ng/components/layermanager';
  12. import {HsLayoutService} from 'hslayers-ng/components/layout/layout.service';
  13. import {HsPanelContainerService} from 'hslayers-ng/components/layout/panels/panel-container.service';
  14. import {HsSidebarService} from 'hslayers-ng/components/sidebar/sidebar.service';
  15. import {AdjusterComponent} from './adjuster/adjuster.component';
  16. import {AdjusterEventService} from './adjuster/adjuster-event.service';
  17. import {AdjusterLegendService} from './adjuster/adjuster-legend.service';
  18. import {AdjusterService} from './adjuster/adjuster.service';
  19. import {krajeLayer, obceIndexLayer, okresyLayer} from './app.config';
  20. @Injectable({providedIn: 'root'})
  21. export class AppService {
  22. constructor(
  23. public adjusterService: AdjusterService,
  24. public adjusterEventService: AdjusterEventService,
  25. public adjusterLegendService: AdjusterLegendService,
  26. public hsConfig: HsConfig,
  27. public hsEventBus: HsEventBusService,
  28. public hsLanguageService: HsLanguageService,
  29. public hsLayerManagerService: HsLayerManagerService,
  30. public hsLayoutService: HsLayoutService,
  31. public hsPanelContainerService: HsPanelContainerService,
  32. public hsSidebarService: HsSidebarService
  33. ) {
  34. this.adjusterEventService.loaded.subscribe(({success}) => {
  35. if (success) {
  36. this.adjusterLegendService.refreshColorPalette(
  37. this.adjusterService.numberOfClusters
  38. );
  39. }
  40. });
  41. this.hsEventBus.layoutLoads.subscribe(() => {
  42. this.init();
  43. });
  44. this.prepareLayers();
  45. }
  46. init(): void {
  47. this.hsLanguageService.setLanguage('cs');
  48. this.hsSidebarService.buttons.push({
  49. panel: 'adjuster',
  50. module: 'pra.adjuster',
  51. order: 0,
  52. title: () =>
  53. this.hsLanguageService.getTranslation('ADJUSTER.adjustFactors'),
  54. description: 'Adjust factors for computation',
  55. icon: 'icon-analytics-piechart',
  56. });
  57. this.hsPanelContainerService.create(AdjusterComponent, {});
  58. this.hsLayoutService.setDefaultPanel('adjuster');
  59. }
  60. /**
  61. * Create separate layer fo each clustering method and add it to default_layers
  62. */
  63. prepareLayers(): void {
  64. for (const method of this.adjusterService.methods) {
  65. method.layer = new VectorLayer({
  66. source: new VectorSource({
  67. format: new TopoJSON({dataProjection: 'EPSG:5514'}),
  68. url: require('./data/obce_cr_20210310_5p_5514.topojson').default,
  69. overlaps: false,
  70. }),
  71. editor: {editable: false},
  72. visible: true,
  73. style: this.generateStyle(method.codename),
  74. title: `Obce ČR: ${method.name.replaceAll(/\((.+?)\)/g, '')} clusters`,
  75. attributions: ['CC-BY ČÚZK, 2021'],
  76. popUp: {
  77. attributes: [
  78. {
  79. attribute: method.codename,
  80. label: 'ID shluku',
  81. },
  82. ],
  83. },
  84. });
  85. this.hsConfig.default_layers.push(method.layer);
  86. }
  87. // obceIndexLayer, okresyLayer and krajeLayer must be pushed in this order
  88. // so they will display in correct order
  89. this.hsConfig.default_layers.push(obceIndexLayer);
  90. obceIndexLayer.on('featuresloadend', this.adjusterService.init());
  91. obceIndexLayer.getSource().legend_categories = this.adjusterLegendService.createIndexLegend();
  92. this.hsConfig.default_layers.push(okresyLayer);
  93. this.hsConfig.default_layers.push(krajeLayer);
  94. }
  95. /**
  96. * @description Function factory for generating style functions based on different clustering methods
  97. * @param {string} method selected method
  98. * @returns {function} style function
  99. */
  100. private generateStyle(method: string) {
  101. return (feature: Feature): Style => {
  102. if (isNaN(feature.get(method))) {
  103. return new Style({
  104. fill: new Fill({
  105. color: '#FFF',
  106. }),
  107. stroke: new Stroke({
  108. color: '#3399CC',
  109. width: 0.25,
  110. }),
  111. });
  112. } else {
  113. return new Style({
  114. fill: new Fill({
  115. color: this.adjusterLegendService.colorPalette[
  116. feature.get(method) - 1
  117. ],
  118. }),
  119. stroke: new Stroke({
  120. color: '#FFF',
  121. width: 0.25,
  122. }),
  123. });
  124. }
  125. };
  126. }
  127. }