adjuster.service.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. import {HttpClient} from '@angular/common/http';
  2. import {Injectable} from '@angular/core';
  3. import {Subject} from 'rxjs';
  4. import {HsDialogContainerService} from 'hslayers-ng/components/layout/dialogs/dialog-container.service';
  5. import {HsUtilsService} from 'hslayers-ng/components/utils/utils.service';
  6. // import attractivity from '../Attractivity.json';
  7. import {AdjusterEventService} from './adjuster-event.service';
  8. import {AdjusterLoaderComponent} from './adjuster-loader.component';
  9. import {nuts} from '../nuts';
  10. //import {factors} from './factors.js';
  11. @Injectable({providedIn: 'root'})
  12. export class AdjusterService {
  13. serviceBaseUrl: string;
  14. factors;
  15. clusters;
  16. method: string;
  17. _clusteringInProcess: boolean;
  18. constructor(
  19. private adjusterEventService: AdjusterEventService,
  20. private hsDialogContainerService: HsDialogContainerService,
  21. private hsUtilsService: HsUtilsService,
  22. private httpClient: HttpClient
  23. ) {
  24. this.serviceBaseUrl =
  25. window.location.hostname === 'localhost'
  26. ? 'https://jmacura.ml/ws/' // 'http://localhost:3000/'
  27. : 'https://publish.lesprojekt.cz/nodejs/';
  28. this.factors = [];
  29. this.clusters = [];
  30. this.method = 'haclustwd2';
  31. this._clusteringInProcess = true;
  32. this.init();
  33. }
  34. /**
  35. * Sends a request to polirural-attractiveness-service
  36. * and applies the returned values
  37. */
  38. apply(): void {
  39. this.hsDialogContainerService.create(AdjusterLoaderComponent, {});
  40. const f = () => {
  41. this._clusteringInProcess = true;
  42. this.httpClient
  43. .post(this.serviceBaseUrl + 'clusters', {
  44. factors: this.factors.map((f) => {
  45. return {
  46. factor: f.name,
  47. weight: f.weight,
  48. datasets: f.datasets
  49. .filter((ds) => ds.included)
  50. .map((ds) => ds.name),
  51. };
  52. }),
  53. })
  54. .toPromise()
  55. .then((data: any) => {
  56. const clusterData = data.response;
  57. /*let max = 0;
  58. this.clusters.forEach((a) => {
  59. if (a.aggregate > max) {
  60. max = a.aggregate;
  61. }
  62. });
  63. const normalizer = 1 / max;
  64. this.attractivity.forEach((a) => {
  65. a.aggregate *= normalizer;
  66. });
  67. this.attractivity.forEach((a) => {
  68. this.nutsCodeRecordRelations[a.code] = a;
  69. });*/
  70. nuts.nuts3Source.forEachFeature((feature) => {
  71. // Pair each feature with its clustering data
  72. const featureData = clusterData.find(
  73. (item) => item['nuts_id'] === feature.get('NUTS_ID')
  74. );
  75. if (!featureData) {
  76. console.error(`No data for feature ${feature.get('NUTS_ID')}`);
  77. console.log(feature);
  78. return;
  79. }
  80. Object.keys(featureData).forEach(function (key, index) {
  81. if (key !== 'nuts_id') {
  82. feature.set(key, featureData[key]);
  83. }
  84. });
  85. });
  86. const clusters = [];
  87. for (const region of clusterData) {
  88. if (!clusters.includes(region[this.method])) {
  89. clusters.push(region[this.method]);
  90. }
  91. }
  92. this.clusters = clusters;
  93. this._clusteringInProcess = false;
  94. this.adjusterEventService.clustersLoaded.next();
  95. })
  96. .catch((error) => {
  97. console.warn(`Error obtaining data from ${this.serviceBaseUrl}.`);
  98. console.log(error);
  99. this._clusteringInProcess = false;
  100. });
  101. };
  102. this.hsUtilsService.debounce(f, 300, false, this)();
  103. }
  104. init(): void {
  105. this.httpClient
  106. .get(this.serviceBaseUrl + 'datasets')
  107. .toPromise()
  108. .then((data: any) => {
  109. this.factors = data.map((dataset) => {
  110. return {name: dataset.Factor, weight: 1, datasets: []};
  111. });
  112. this.factors = this.hsUtilsService.removeDuplicates(
  113. this.factors,
  114. 'name'
  115. );
  116. this.factors.forEach((factor) => {
  117. factor.datasets = data
  118. .filter((ds) => ds.Factor === factor.name)
  119. .map((ds) => {
  120. return {
  121. name: ds.Name,
  122. desc: ds.Description,
  123. included: true,
  124. };
  125. });
  126. });
  127. this.apply();
  128. })
  129. .catch((error) => {
  130. console.warn(`Web service at ${this.serviceBaseUrl} unavailable!`);
  131. console.log(error);
  132. this._clusteringInProcess = false;
  133. });
  134. }
  135. /**
  136. * @returns {boolean} true if clustering is in process, false otherwise
  137. */
  138. isClusteringInProcess(): boolean {
  139. return this._clusteringInProcess;
  140. }
  141. }