adjuster.service.ts 4.2 KB

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