adjuster.service.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. //import attractivity from '../Attractivity.json';
  2. import nuts from 'nuts';
  3. export default ['Core', 'hs.utils.service', '$rootScope', '$http',
  4. function (Core, utils, $rootScope, $http) {
  5. var me = {
  6. factors: [
  7. {
  8. name: 'Natural',
  9. column: 'N_index',
  10. weight: 1
  11. },
  12. {
  13. name: 'Social & Human',
  14. column: 'S_index',
  15. weight: 1
  16. },
  17. {
  18. name: 'Anthropic',
  19. column: 'A_index',
  20. weight: 1
  21. },
  22. {
  23. name: 'Economical',
  24. column: 'E_index',
  25. weight: 1
  26. },
  27. {
  28. name: 'Cultural',
  29. column: 'C_index',
  30. weight: 1
  31. },
  32. {
  33. name: 'Institutional',
  34. column: 'I_index',
  35. weight: 1
  36. }
  37. ],
  38. apply() {
  39. utils.debounce(function() {
  40. $http({
  41. method: 'post',
  42. url: 'https://publish.lesprojekt.cz/nodejs/scores',
  43. data: {
  44. "factors": me.factors.map(f => {
  45. return {
  46. factor: f.factor,
  47. weight: f.weight,
  48. datasets: f.datasets.filter(ds => ds.included).map(ds => ds.name)
  49. }
  50. })
  51. }
  52. }).
  53. then(response => {
  54. me.attractivity = response.data;
  55. let max = 0;
  56. me.attractivity.forEach(a => {
  57. if (a.aggregate > max) max = a.aggregate;
  58. });
  59. let normalizer = 1 / max;
  60. me.attractivity.forEach(a => {
  61. a.aggregate *= normalizer;
  62. })
  63. me.attractivity.forEach(a => {
  64. me.nutsCodeRecordRelations[a.code] = a;
  65. })
  66. nuts.nuts3Source.forEachFeature(feature => {
  67. feature.set('total', me.nutsCodeRecordRelations[feature.get('NUTS_ID')].aggregate);
  68. feature.set('totalForHumans', (me.nutsCodeRecordRelations[feature.get('NUTS_ID')].aggregate * 100).toFixed(2));
  69. me.factors.forEach(factor => {
  70. feature.set(factor.factor, (me.nutsCodeRecordRelations[feature.get('NUTS_ID')][factor.factor] * 100).toFixed(2))
  71. })
  72. })
  73. })
  74. /*
  75. */}, 300)()
  76. }
  77. };
  78. me.nutsCodeRecordRelations = {};
  79. $http({ url: 'https://publish.lesprojekt.cz/nodejs/datasets' }).
  80. then(response => {
  81. me.factors = response.data.map(dataset => { return { factor: dataset.Factor, weight: 1, datasets: [] } });
  82. me.factors = utils.removeDuplicates(me.factors, 'factor');
  83. me.factors.forEach(factor => {
  84. factor.datasets = response.data
  85. .filter(ds => ds.Factor == factor.factor)
  86. .map(ds => {
  87. return {
  88. name: ds.Name,
  89. included: true
  90. }
  91. })
  92. })
  93. me.apply();
  94. })
  95. return me;
  96. }
  97. ]