forked from anvaka/extract-osm-roads
-
Notifications
You must be signed in to change notification settings - Fork 0
/
1.save-roads-graph.js
156 lines (123 loc) · 3.58 KB
/
1.save-roads-graph.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
var JSONStream = require('JSONStream');
var es = require('event-stream');
var fs = require('fs')
var path = require('path');
var createProjector = require('./lib/createProjector.js');
var graph = require('ngraph.graph')();
var nodes = new Map();
var BBox = require('./lib/bbox.js');
var lonLatBbox = new BBox();
var latLonToNodeId = new Map();
var inFileName = process.argv[2];
let fileName = path.basename(inFileName, '.json')
var outFileName = path.join(__dirname, 'data', fileName);
var parser = JSONStream.parse('elements.*');
fs.createReadStream(inFileName)
.pipe(parser)
.pipe(es.mapSync(callback))
.on('end', done);
function callback(el) {
if (el.type === 'node') processOSMNode(el);
else if (el.type === 'way') processOSMWay(el)
}
function done() {
saveResults();
}
function processOSMWay(way) {
var nodes = way.nodes;
if (!nodes) {
console.log('no nodes', way)
return;
}
for (var i = 1; i < nodes.length; ++i) {
let from = nodes[i];
let to = nodes[i - 1];
graph.addLink(from, to);
}
}
function processOSMError() {
console.log('error');
}
function processOSMNode(node) {
lonLatBbox.addPoint(node.lon, node.lat);
nodes.set(node.id, {
lon: node.lon,
lat: node.lat
});
}
function id(x, y) { return x + ';' + y; }
function saveResults() {
console.log('Graph loaded. Processing...');
var xyBBox = new BBox();
let project = createProjector(lonLatBbox);
var nodesToDelete = new Set();
graph.forEachNode(node => {
let data = nodes.get(node.id);
if (!data) throw new Error('missing data for ' + node.id);
var nodeData = project(data.lon, data.lat);
let xyID = id(nodeData.x, nodeData.y);
let prevNode = latLonToNodeId.get(xyID)
if (prevNode) {
xyID = id(nodeData.x, nodeData.y);
prevNode = latLonToNodeId.get(xyID);
console.log('!Marking for deletion', node.id);
nodesToDelete.add(node.id);
} else {
latLonToNodeId.set(xyID, node);
}
node.data = nodeData;
xyBBox.addPoint(node.data.x, node.data.y);
});
nodesToDelete.forEach(nodeId => {
console.log('removing', nodeId);
graph.removeNode(nodeId);
});
moveCoordinatesToZero();
writeGraph(outFileName, graph);
console.log(xyBBox);
function moveCoordinatesToZero() {
let dx = xyBBox.cx
let dy = xyBBox.cy;
let movedBbox = new BBox();
graph.forEachNode(node => {
node.data.x = Math.round(node.data.x - dx);
node.data.y = Math.round(node.data.y - dy);
movedBbox.addPoint(node.data.x, node.data.y);
});
console.log('moved bbox', movedBbox);
}
}
function writeGraph(fileName, graph) {
let nodeIdMap = new Map();
saveNodes(fileName + '.co.bin', graph, nodeIdMap);
saveLinks(fileName + '.gr.bin', graph, nodeIdMap);
}
function saveNodes(fileName, graph, nodeIdMap) {
var nodeCount = graph.getNodesCount();
var buf = new Buffer(nodeCount * 4 * 2);
var idx = 0;
graph.forEachNode(p => {
nodeIdMap.set(p.id, 1 + idx / 8);
buf.writeInt32LE(p.data.x, idx);
idx += 4;
buf.writeInt32LE(p.data.y, idx);
idx += 4;
});
fs.writeFileSync(fileName , buf);
console.log('Nodes saved to ', fileName);
}
function saveLinks(fileName, graph, nodeIdMap) {
var buf = new Buffer(graph.getLinksCount() * 4 * 2);
var idx = 0;
graph.forEachLink(l => {
let fromId = nodeIdMap.get(l.fromId);
let toId = nodeIdMap.get(l.toId);
if (!fromId || !toId) throw new Error('missing id')
buf.writeInt32LE(fromId, idx);
idx += 4;
buf.writeInt32LE(toId, idx);
idx += 4;
})
fs.writeFileSync(fileName, buf);
console.log('links saved to ', fileName);
}