nuts-data.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. const fs = require('fs');
  2. const csv = require('csv-parse');
  3. /* Helper method to load the datasets from CSV and store it in server object */
  4. module.exports.loadDatasets = function(filePath, dataLoadedCallback) {
  5. //console.log('Datasets structure loading.');
  6. var datasets = [];
  7. let columns = undefined;
  8. fs.createReadStream(filePath)
  9. .pipe(csv({ separator: ';' }))
  10. .on('data', (row) => {
  11. if (!columns) {
  12. columns = row;
  13. }
  14. else {
  15. let ds = {};
  16. for (let i = 0; i < columns.length; i++) {
  17. ds[columns[i]] = row[i];
  18. }
  19. datasets.push(ds);
  20. }
  21. })
  22. .on('end', () => {
  23. //console.log('Datasets structure loaded.');
  24. dataLoadedCallback(datasets);
  25. });
  26. }
  27. /* Load all the attractivness data from CSV into a server object.
  28. The data don't need to be loaded for each request then. */
  29. module.exports.loadRuralData = function (filePath, datasets, dataLoadedCallback) {
  30. console.log('Reading rural data file processing started.');
  31. var ruralData = [];
  32. let columns = undefined;
  33. fs.createReadStream(filePath)
  34. .pipe(csv())
  35. .on('data', (row) => {
  36. if (!columns) {
  37. columns = row;
  38. }
  39. else {
  40. let item = {};
  41. for (let i = 0; i < columns.length; i++) {
  42. let colName = columns[i].toLowerCase();
  43. if (colName == "nuts_id") // ID of the NUTS region
  44. item.nuts = row[i];
  45. else if (colName == "datasets") // empty datasets count
  46. item.availableDS = datasets.length - row[i];
  47. else if (colName == "quality")
  48. item.quality = row[i];
  49. else {
  50. let factor = getDataSetFactor(datasets, colName);
  51. if (factor) {
  52. if (!item[factor])
  53. item[factor] = {};
  54. //item[factor].push({ dataset: columns[i], value: row[i] });
  55. item[factor][columns[i]] = Number(row[i]);
  56. }
  57. }
  58. }
  59. ruralData.push(item);
  60. }
  61. })
  62. .on('end', () => {
  63. //console.log('Rural data file processing finished.');
  64. dataLoadedCallback(ruralData);
  65. });
  66. }
  67. /* Reads the out_file.csv created by R script and saves it into an object
  68. */
  69. module.exports.loadClusters = function (filePath, dataLoadedCallback) {
  70. console.log('Reading clustering data file processing started.');
  71. let clusters = [];
  72. let columns = undefined;
  73. fs.createReadStream(filePath)
  74. .pipe(csv())
  75. .on('data', (row) => {
  76. if (!columns) {
  77. columns = row;
  78. }
  79. else {
  80. let item = {};
  81. for (let i = 0; i < columns.length; i++) {
  82. const colName = columns[i].length > 0 ? columns[i].toLowerCase() : 'nuts_id';
  83. item[colName] = row[i];
  84. }
  85. clusters.push(item);
  86. }
  87. })
  88. .on('end', () => {
  89. //console.log('Rural data file processing finished.');
  90. dataLoadedCallback(clusters);
  91. });
  92. }
  93. module.exports.getFactorIndex = function (region, factor) {
  94. //console.log('getFactorIndex');
  95. //console.log('region: ' + region.nuts);
  96. //console.log('factor: ' + JSON.stringify(factor, null, 4));
  97. sumValue = 0;
  98. count = 0;
  99. factor.datasets.forEach(ds => {
  100. //console.log('factor: ' + factor.factor);
  101. let value = region[factor.factor][ds];
  102. if (value) {
  103. sumValue += value;
  104. count++;
  105. }
  106. });
  107. return { index: sumValue / count, sumValue: sumValue, sumWeight: count * factor.weight };
  108. }
  109. function getDataSetFactor(datasets, colName) {
  110. for (let i = 0; i < datasets.length; i++) {
  111. if (datasets[i].Name.toLowerCase() == colName)
  112. return datasets[i].Factor;
  113. }
  114. return undefined;
  115. }