-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpleiades-histograph.js
executable file
·90 lines (70 loc) · 2.39 KB
/
pleiades-histograph.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/local/bin/node
/*
* Conversion script for militieregisters data set
* Converts input ilvb csv to two separate ndjson files for vertices and edges
*/
// dependencies
var argv = require('optimist')
.usage('Transforms pleiades data set into Histograph input format.\nUsage: $0')
.demand('f')
.alias('f', 'file')
.describe('f', 'Load a file')
.argv,
fs = require('fs'),
parse = require('csv-parse'),
path = require('path');
// files
var pitFileNameOut = path.join(path.dirname(path.resolve(argv.file)), 'pleiades.pits.ndjson'),
relationFileNameOut = path.join(path.dirname(path.resolve(argv.file)), 'pleiades.relations.ndjson');
try {
fs.openSync(pitFileNameOut, 'r');
fs.unlinkSync(pitFileNameOut);
} catch (e) {}
try {
fs.openSync(relationFileNameOut, 'r');
fs.unlinkSync(relationFileNameOut);
} catch (e) {}
// objects
var usedURIs = [],
edges = [];
parse(fs.readFileSync(argv.file, {encoding: 'utf8'}), {delimiter: ',', escape: '\\'}, function(err, data) {
if (err) {
console.log(err);
} else {
console.log("Parsing vertices...");
data.shift(); // Skip CSV header
data.forEach(function(obj){
var internaluri = obj[1];
if (usedURIs.indexOf(internaluri) === -1) {
var vertex = {
id: internaluri,
name: obj[4],
type: (obj[11] === "river" || obj[11] === "lake") ? "hg:Water" : "hg:Place",
geometry: {'type': 'Point', 'coordinates': [ parseFloat(obj[10]), parseFloat(obj[9]) ] },
data: {
currentPlaceName: obj[5],
source: obj[12]
},
"hasBeginning": parseInt(obj[8]) + "-01-01",
"hasEnd": parseInt(obj[7]) + "-01-01",
uri: obj[2]
}
fs.appendFileSync(pitFileNameOut, JSON.stringify(vertex, null, 0) + "\n");
// EDGES
if (obj[6] != "0" && obj[6] !== "" && obj[6] !== "NULL") {
var geonamesURI = obj[6];
var edge = {
from: internaluri,
to: geonamesURI,
label: "hg:sameHgConcept"
};
edges.push(edge);
fs.appendFileSync(relationFileNameOut, JSON.stringify(edge, null, 0) + "\n");
}
usedURIs.push(internaluri);
}
});
console.log(usedURIs.length + " vertices parsed.");
console.log(edges.length + " edges parsed.");
}
});