-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcovid_geojson_converter.js
50 lines (45 loc) · 1.27 KB
/
covid_geojson_converter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
const csv = require('csv-parser');
const fs = require('fs');
const myArgs = process.argv.slice(2);
let results = {
"type": "FeatureCollection",
"features": []
};
let headers = [];
fs.createReadStream(myArgs[0])
.pipe(csv())
.on('headers', (headersRead) => {
headers = headersRead;
})
.on('data', (row) => {
let result = [];
for (const [index, [, value]] of Object.entries(Object.entries(row))) {
result.push(value);
}
for (let i = 4; i < result.length; i++) {
const feature = {
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [parseFloat(result[3]), parseFloat(result[2])]
},
"properties": {
"province": result[0],
"country": result[1],
"date": headers[i],
"confirmed": parseInt(result[i])
}
};
results["features"].push(feature);
}
})
.on('end', () => {
console.log('CSV file successfully processed');
//console.log(results);
const resultString = JSON.stringify(results);
const fs = require('fs');
fs.writeFile(myArgs[1] + '.geojson', resultString, 'utf8', (err) => {
if (err) throw err;
console.log('complete');
});
});