adjuster.service.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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.init();
  18. }
  19. apply() {
  20. const f = () => {
  21. this.$http({
  22. method: 'get',
  23. url: this.serviceBaseUrl + 'clusters',
  24. /*data: {
  25. factors: this.factors.map((f) => {
  26. return {
  27. factor: f.factor,
  28. weight: f.weight,
  29. datasets: f.datasets
  30. .filter((ds) => ds.included)
  31. .map((ds) => ds.name),
  32. };
  33. }),
  34. },*/
  35. }).then((response) => {
  36. const clusterData = response.data.response;
  37. /*let max = 0;
  38. this.clusters.forEach((a) => {
  39. if (a.aggregate > max) {
  40. max = a.aggregate;
  41. }
  42. });
  43. const normalizer = 1 / max;
  44. this.attractivity.forEach((a) => {
  45. a.aggregate *= normalizer;
  46. });
  47. this.attractivity.forEach((a) => {
  48. this.nutsCodeRecordRelations[a.code] = a;
  49. });*/
  50. nuts.nuts3Source.forEachFeature((feature) => {
  51. // Pair each feature with its clustering data
  52. const featureData = clusterData.find(
  53. (item) => item['nuts_id'] === feature.get('NUTS_ID')
  54. );
  55. Object.keys(featureData).forEach(function (key, index) {
  56. if (key !== 'nuts_id') {
  57. feature.set(key, featureData[key]);
  58. }
  59. });
  60. });
  61. const clusters = [];
  62. for (const region of clusterData) {
  63. if (!clusters.includes(region['km25.cluster'])) {
  64. clusters.push(region['km25.cluster']);
  65. }
  66. }
  67. this.clusters = clusters;
  68. this.$rootScope.$broadcast('clusters_loaded');
  69. });
  70. };
  71. this.HsUtilsService.debounce(f, 300)();
  72. }
  73. init() {
  74. this.$http({
  75. url: this.serviceBaseUrl + 'datasets',
  76. }).then((response) => {
  77. this.factors = response.data.map((dataset) => {
  78. return {name: dataset.Factor, weight: 1, datasets: []};
  79. });
  80. this.factors = this.HsUtilsService.removeDuplicates(this.factors, 'name');
  81. this.factors.forEach((factor) => {
  82. factor.datasets = response.data
  83. .filter((ds) => ds.Factor === factor.name)
  84. .map((ds) => {
  85. return {
  86. name: ds.Name,
  87. included: true,
  88. };
  89. });
  90. });
  91. this.apply();
  92. });
  93. }
  94. }