-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathPBFParser.js
60 lines (50 loc) · 1.55 KB
/
PBFParser.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
self.OSM = self.OSM || {};
var pbfParser = require('osm-read');
OSM.PBFParser = {
getNodes: function(buffer) {
var nodes = {};
var ways = [];
pbfParser.parse({
buffer: buffer,
endDocument: function(){},
bounds: function(bounds){},
node: function(node){
node.type = "node";
node.used = false;
nodes[node.id] = node;
},
way: function(way){
var len, incomplete = false;
way.type = "way";
way.nodes = new Array(way.nodeRefs.length),
len = way.nodes.length;
for (var j = 0; j < len; j++) {
var node = nodes[way.nodeRefs[j]];
if (!node) {
incomplete = true;
break;
}
way.nodes[j] = node;
node.used = true;
}
delete way.nodeRefs;
//discard incomplete ways
if (!incomplete) {
ways.push(way);
}
},
error: function(msg){
console.log('error: ' + msg);
throw msg;
}
});
nodes.ways = ways;
return nodes;
},
getWays: function(buffer, nodes) {
// quick hack to avoid two-pass read: ways passed from getNodes
var ways = nodes.ways;
delete nodes.ways;
return ways;
}
};