adjuster.service.ts 5.0 KB

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