ソースを参照

Return cluster data via API

jmacura 5 年 前
コミット
e3be0d7caf
2 ファイル変更51 行追加0 行削除
  1. 22 0
      index.js
  2. 29 0
      nuts-data.js

+ 22 - 0
index.js

@@ -8,6 +8,7 @@ const app = express();
 
 const _datasetsFilePath = 'data/datasets.csv';
 const _dataFilePath = 'data/data.csv';
+const _clustersFilePath = 'data/clustering/out_file.csv';
 var _datasets = undefined;
 var _ruralData = undefined;
 
@@ -144,6 +145,27 @@ app.get('/runR', (req, res, next) => {
     );
 });
 
+app.get('/clusters', (req, res, next) => {
+    //console.log(console);
+    console.log('calling R...')
+    R('./r/selected_data.r').call(
+        function(err, data) {
+            console.log('R done');
+            if (err) {
+                console.log(err.toString('utf8'));
+                data = { result: err.toString('utf8') };
+            }
+            else {
+                console.log(data);
+                nutsData.loadClusters(_clustersFilePath, function(clusterData) {
+                    data = clusterData;
+                    helpers.formatResponse({ response: data }, req, res);
+                });
+            }
+        }
+    );
+});
+
 // start the service on the port xxxx
 app.listen(3000, () => console.log('Rural attractivity WS listening on port 3000...'));
 

+ 29 - 0
nuts-data.js

@@ -75,6 +75,35 @@ module.exports.loadRuralData = function (filePath, datasets, dataLoadedCallback)
         });
 }
 
+/* Reads the out_file.csv created by R script and saves it into an object
+*/
+module.exports.loadClusters = function (filePath, dataLoadedCallback) {
+    console.log('Reading clustering data file processing started.');
+    let clusters = [];
+
+    let columns = undefined;
+
+    fs.createReadStream(filePath)
+        .pipe(csv())
+        .on('data', (row) => {
+            if (!columns) {
+                columns = row;
+            }
+            else {
+                let item = {};
+                for (let i = 0; i < columns.length; i++) {
+                    const colName = columns[i].length > 0 ? columns[i].toLowerCase() : 'nuts_id';
+                    item[colName] = row[i];
+                }
+                clusters.push(item);
+            }
+        })
+        .on('end', () => {
+            //console.log('Rural data file processing finished.');
+            dataLoadedCallback(clusters);
+        });
+}
+
 module.exports.getFactorIndex = function (region, factor) {
     //console.log('getFactorIndex');
     //console.log('region: ' + region.nuts);