adjuster.service.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import {HttpClient} from '@angular/common/http';
  2. import {Injectable} from '@angular/core';
  3. import {HsUtilsService} from 'hslayers-ng/components/utils/utils.service';
  4. // import attractivity from '../Attractivity.json';
  5. import nuts from '../nuts';
  6. @Injectable({providedIn: 'root'})
  7. export class AdjusterService {
  8. nutsCodeRecordRelations = {};
  9. factors: any = [
  10. {
  11. name: 'Natural',
  12. column: 'N_index',
  13. weight: 1,
  14. },
  15. {
  16. name: 'Social & Human',
  17. column: 'S_index',
  18. weight: 1,
  19. },
  20. {
  21. name: 'Anthropic',
  22. column: 'A_index',
  23. weight: 1,
  24. },
  25. {
  26. name: 'Economical',
  27. column: 'E_index',
  28. weight: 1,
  29. },
  30. {
  31. name: 'Cultural',
  32. column: 'C_index',
  33. weight: 1,
  34. },
  35. {
  36. name: 'Institutional',
  37. column: 'I_index',
  38. weight: 1,
  39. },
  40. ];
  41. constructor(public HsUtilsService: HsUtilsService, public $http: HttpClient) {
  42. this.$http
  43. .get('https://publish.lesprojekt.cz/nodejs/datasets')
  44. .toPromise()
  45. .then((response: any) => {
  46. this.factors = response.data.map((dataset) => {
  47. return {factor: dataset.Factor, weight: 1, datasets: []};
  48. });
  49. this.factors = HsUtilsService.removeDuplicates(this.factors, 'factor');
  50. this.factors.forEach((factor) => {
  51. factor.datasets = response.data
  52. .filter((ds) => ds.Factor === factor.factor)
  53. .map((ds) => {
  54. return {
  55. name: ds.Name,
  56. included: true,
  57. };
  58. });
  59. });
  60. this.apply();
  61. });
  62. }
  63. apply() {
  64. this.HsUtilsService.debounce(
  65. function () {
  66. this.$http
  67. .post('https://publish.lesprojekt.cz/nodejs/scores', {
  68. factors: this.factors.map((f) => {
  69. return {
  70. factor: f.factor,
  71. weight: f.weight,
  72. datasets: f.datasets
  73. .filter((ds) => ds.included)
  74. .map((ds) => ds.name),
  75. };
  76. }),
  77. })
  78. .toPromise()
  79. .then((response: any) => {
  80. this.attractivity = response.data;
  81. let max = 0;
  82. this.attractivity.forEach((a) => {
  83. if (a.aggregate > max) {
  84. max = a.aggregate;
  85. }
  86. });
  87. const normalizer = 1 / max;
  88. this.attractivity.forEach((a) => {
  89. a.aggregate *= normalizer;
  90. });
  91. this.attractivity.forEach((a) => {
  92. this.nutsCodeRecordRelations[a.code] = a;
  93. });
  94. nuts.nuts3Source.forEachFeature((feature) => {
  95. feature.set(
  96. 'total',
  97. this.nutsCodeRecordRelations[feature.get('NUTS_ID')].aggregate
  98. );
  99. feature.set(
  100. 'totalForHumans',
  101. (
  102. this.nutsCodeRecordRelations[feature.get('NUTS_ID')]
  103. .aggregate * 100
  104. ).toFixed(2)
  105. );
  106. this.factors.forEach((factor) => {
  107. feature.set(
  108. factor.factor,
  109. (
  110. this.nutsCodeRecordRelations[feature.get('NUTS_ID')][
  111. factor.factor
  112. ] * 100
  113. ).toFixed(2)
  114. );
  115. });
  116. });
  117. });
  118. },
  119. 300,
  120. false,
  121. this
  122. )();
  123. }
  124. }