-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimport.js
94 lines (86 loc) · 3.03 KB
/
import.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
91
92
93
94
var clone = require('clone');
var jsonfile = require('jsonfile');
var path = require('path');
jsonfile.spaces = 4 //format json output
//Generate lines and stations schematic json from geojson data
station = {
"title": "Clarendon\nArms",
"address": "35-36 Clarendon St, Cambridge CB1 1JX",
"phone": "01223 778272",
"position": {
"lat": 52.20465,
"lon": 0.127679
}
};
line = {
"name": "Town",
"label": "Town",
"color": "#ffd300",
"shiftCoords": [0, 0],
"nodes":[]
};
node = {
"coords": [0, 0]
}
var file = path.join(process.cwd(),'data','stations.geojson')
geojson = jsonfile.readFileSync(file);
var schematic = {
"stations" : {},
"lines": []
}
//iterate trough ordered points, grouped by line
geojson.features.sort((p1,p2) => {
//order by line
if(p1.properties.line < p2.properties.line) return -1;
if(p1.properties.line > p2.properties.line) return 1;
//order by order in the same line
return p1.properties.order - p2.properties.order;
}).map(point => {
//console.log(point.properties)
count = 0;
//get line
l = schematic.lines.find(l => l.name == point.properties.line);
l = (point.properties.line == "River")? schematic.river : l;
if(!l) { //add line if not exists
console.log("Line " + point.properties.line + " found!");
l = clone(line);
l.name = point.properties.line;
l.label = point.properties.label || point.properties.line;
l.color = point.properties.color;
//special case: River
if(l.name == "River") {
schematic.river = l;
} else { //normal line
schematic.lines.push(l);
}
}
//add node
n = clone(node);
n.coords[0] = point.geometry.coordinates[0];
n.coords[1] = point.geometry.coordinates[1];
if (point.properties.name) n.name = point.properties.name;
if (point.properties.labelPos) n.labelPos = point.properties.labelPos ;
if (point.properties.marker) n.marker = point.properties.marker;
if (point.properties.shCoordX && point.properties.shCoordY) {
n.shiftCoords = [0, 0];
n.shiftCoords[0] = point.properties.shCoordX;
n.shiftCoords[1] = point.properties.shCoordY;
}
if (point.properties.canonical) n.canonical = point.properties.canonical;
if (point.properties.dir) n.dir = point.properties.dir;
l.nodes.push(n);
if(point.properties.name) { //station node
st = schematic.stations[point.properties.name]
if(!st) { //add station if not exists
console.log("Station " + point.properties.name + " found!");
st = clone(station);
st.title = point.properties.title || point.properties.name;
st.position.lat = point.geometry.coordinates[0];
st.position.lon = point.geometry.coordinates[1];
schematic.stations[point.properties.name] = st;
}
}
});
jsonfile.writeFile(path.join(process.cwd(),'www', 'stations.json'), schematic, function (err) {
if (err) console.error(err);
});