adjuster.service.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // import attractivity from '../Attractivity.json';
  2. import nuts from '../nuts.js';
  3. import {factors} from './factors.js';
  4. export class AdjusterService {
  5. constructor(HsCore, HsUtilsService, $rootScope, $http, $location) {
  6. 'ngInject';
  7. this.HsCore = HsCore;
  8. this.HsUtilsService = HsUtilsService;
  9. this.$rootScope = $rootScope;
  10. this.$http = $http;
  11. this.serviceBaseUrl =
  12. $location.host() === 'localhost'
  13. ? 'https://jmacura.ml/ws/'
  14. : 'https://publish.lesprojekt.cz/nodejs/';
  15. this.factors = factors;
  16. this.clusters = [];
  17. this._clusteringInProcess = true;
  18. this.init();
  19. }
  20. /**
  21. * Sends a request to polirural-attractiveness-service
  22. * and applies the returned values
  23. */
  24. apply() {
  25. const f = () => {
  26. this._clusteringInProcess = true;
  27. this.$http({
  28. method: 'get',
  29. url: this.serviceBaseUrl + 'clusters',
  30. /*data: {
  31. factors: this.factors.map((f) => {
  32. return {
  33. factor: f.factor,
  34. weight: f.weight,
  35. datasets: f.datasets
  36. .filter((ds) => ds.included)
  37. .map((ds) => ds.name),
  38. };
  39. }),
  40. },*/
  41. }).then((response) => {
  42. const clusterData = response.data.response;
  43. /*let max = 0;
  44. this.clusters.forEach((a) => {
  45. if (a.aggregate > max) {
  46. max = a.aggregate;
  47. }
  48. });
  49. const normalizer = 1 / max;
  50. this.attractivity.forEach((a) => {
  51. a.aggregate *= normalizer;
  52. });
  53. this.attractivity.forEach((a) => {
  54. this.nutsCodeRecordRelations[a.code] = a;
  55. });*/
  56. nuts.nuts3Source.forEachFeature((feature) => {
  57. // Pair each feature with its clustering data
  58. const featureData = clusterData.find(
  59. (item) => item['nuts_id'] === feature.get('NUTS_ID')
  60. );
  61. Object.keys(featureData).forEach(function (key, index) {
  62. if (key !== 'nuts_id') {
  63. feature.set(key, featureData[key]);
  64. }
  65. });
  66. });
  67. const clusters = [];
  68. for (const region of clusterData) {
  69. if (!clusters.includes(region['km25.cluster'])) {
  70. clusters.push(region['km25.cluster']);
  71. }
  72. }
  73. this.clusters = clusters;
  74. this._clusteringInProcess = false;
  75. this.$rootScope.$broadcast('clusters_loaded');
  76. });
  77. };
  78. this.HsUtilsService.debounce(f, 300)();
  79. }
  80. init() {
  81. this.$http({
  82. url: this.serviceBaseUrl + 'datasets',
  83. }).then((response) => {
  84. this.factors = response.data.map((dataset) => {
  85. return {name: dataset.Factor, weight: 1, datasets: []};
  86. });
  87. this.factors = this.HsUtilsService.removeDuplicates(this.factors, 'name');
  88. this.factors.forEach((factor) => {
  89. factor.datasets = response.data
  90. .filter((ds) => ds.Factor === factor.name)
  91. .map((ds) => {
  92. return {
  93. name: ds.Name,
  94. included: true,
  95. };
  96. });
  97. });
  98. this.apply();
  99. });
  100. }
  101. /**
  102. * @returns {boolean} true if clustering is in process, false otherwise
  103. */
  104. isClusteringInProcess() {
  105. return this._clusteringInProcess;
  106. }
  107. }