This repository has been archived by the owner on Sep 26, 2024. It is now read-only.
forked from mapbox/osm-adiff-parser-saxjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
93 lines (82 loc) · 2.74 KB
/
index.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
var R = require('ramda');
var sax = require('sax');
// Returns elements grouped by changeset ID.
function AugmentedDiffParser (xmlData, changesetsFilter, callback) {
var xmlParser = sax.parser(true, { lowercase: true });
var currentAction = '';
var currentElement = {};
var oldElement = {};
var currentMember = {};
var currentMode = '';
var changesetMap = {};
function isElement (symbol) {
return (symbol === 'node' || symbol === 'way' || symbol === 'relation');
}
function endTag (symbol) {
if (symbol === 'action') {
var changeset = currentElement.changeset;
if (changesetsFilter && changesetsFilter.length) {
if (changesetsFilter.indexOf(changeset) !== -1) {
if (changesetMap[changeset]) {
changesetMap[changeset].push(currentElement);
} else {
changesetMap[changeset] = [currentElement];
}
}
} else {
if (changesetMap[changeset]) {
changesetMap[changeset].push(currentElement);
} else {
changesetMap[changeset] = [currentElement];
}
}
}
if (symbol === 'osm') {
callback(null, changesetMap);
}
}
function startTag (node) {
var symbol = node.name;
var attrs = node.attributes;
if (symbol === 'action') {
currentAction = attrs.type;
}
if (symbol === 'new' || symbol === 'old') {
currentMode = symbol;
}
if (isElement(symbol)) {
if (currentMode === 'new' && (currentAction === 'modify' ||
currentAction === 'delete')) {
oldElement = R.clone(currentElement);
currentElement = attrs;
currentElement.old = oldElement;
} else {
currentElement = attrs;
}
currentElement.action = currentAction;
currentElement.type = symbol;
currentElement.tags = {};
if (symbol === 'way') {currentElement.nodes = []; }
if (symbol === 'relation') {currentElement.members = []; currentMember = {};}
}
if (symbol === 'tag' && currentElement) {
currentElement.tags[attrs.k] = attrs.v;
}
if (symbol === 'nd' && currentElement && currentElement.type === 'way') {
currentElement.nodes.push(attrs);
}
if (symbol === 'nd' && currentElement && currentElement.type === 'relation') {
currentMember.nodes.push(attrs);
}
if (symbol === 'member' && currentElement && currentElement.type === 'relation') {
currentMember = R.clone(attrs);
currentMember.nodes = [];
currentElement.members.push(currentMember);
}
}
xmlParser.onopentag = startTag;
xmlParser.onclosetag = endTag;
xmlParser.onerror = function(err) { callback(err, null); };
xmlParser.write(xmlData);
}
module.exports = AugmentedDiffParser;