-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert.js
45 lines (38 loc) · 843 Bytes
/
convert.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
tsv = require('tsv');
fs = require('fs');
function fixWktGeom(data) {
var ret = [];
if (data.match(/^POLYGON\(\((.*)\)\)/)) {
var vals = RegExp.$1.split(/\,/);
vals.forEach(function(v) {
var geoPos = v.split(/\s+/);
ret.push([parseFloat(geoPos[1]), parseFloat(geoPos[0])]);
});
}
return ret;
}
function convertData(tsvData) {
var json = [];
var data = tsv.parse(tsvData);
//console.log(data);
if (data) {
data.forEach(function(v) {
if (v.wkt_geom != "") {
json.push({
KOMM: v.KOMM,
wkt_geo: fixWktGeom(v.wkt_geom),
NAVN: v.NAVN,
FRI: v.ANSLAG_FRI
})
}
});
}
return json;
}
fs.readFile("frieintekter.tsv", 'utf-8', function(err, data) {
if (err) {
return console.log(err);
}
var json = convertData(data);
console.log("data = " + JSON.stringify(json, null, " "));
});