-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.js
251 lines (224 loc) · 7.33 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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
var reqwest = require('reqwest'),
qs = require('qs'),
through = require('through');
var osmStream = (function osmMinutely() {
var s = {};
// presets
var baseUrl = 'https://overpass-api.de/',
minuteStatePath = 'api/augmented_diff_status',
changePath = 'api/augmented_diff?';
function minuteStateUrl() {
return baseUrl + minuteStatePath;
}
function changeUrl(id, bbox) {
return baseUrl + changePath + qs.stringify({
id: id, info: 'no', bbox: bbox || '-180,-90,180,90'
});
}
function requestState(cb) {
reqwest({
url: minuteStateUrl(),
crossOrigin: true,
type: 'text',
success: function(res) {
cb(null, parseInt(res.response, 10));
}
});
}
function requestChangeset(state, cb, bbox) {
reqwest({
url: changeUrl(state, bbox),
crossOrigin: true,
type: 'xml',
success: function(res) {
cb(null, res);
},
error: function (err) {
cb(err);
}
});
}
function parseTags(x) {
var tgs = x.getElementsByTagName('tag');
var tags = {};
for (var j = 0; j < tgs.length; j++) {
tags[tgs[j].getAttribute("k")] = tgs[j].getAttribute("v");
}
return tags;
}
function parseNodeBase(x) {
if (!x) return undefined;
return {
type: x.tagName,
id: +x.getAttribute('id'),
version: +x.getAttribute('version'),
timestamp: x.getAttribute('timestamp'),
changeset: +x.getAttribute('changeset'),
uid: +x.getAttribute('uid'),
user: x.getAttribute('user'),
visible: x.getAttribute('visible') !== 'false',
tags: parseTags(x)
};
}
function parseBounds(x) {
var bounds = get(x, ['bounds']);
return [
+bounds.getAttribute('maxlat'),
+bounds.getAttribute('maxlon'),
+bounds.getAttribute('minlat'),
+bounds.getAttribute('minlon')
];
}
function parseMembers(x) {
var mbrs = x.getElementsByTagName('members');
var members = [];
for (var i = 0; i < mbrs.length; i++) {
var mbr = {
type: mbrs[i].getAttribute('type'),
ref: +mbrs[i].getAttribute('ref'),
role: mbrs[i].getAttribute('role')
};
members.push(mbr);
}
return members;
}
function parseLinestring(x) {
var nds = x.getElementsByTagName('nd');
var nodes = [];
for (var i = 0; i < nds.length; i++) {
nodes.push([
+nds[i].getAttribute('lat'),
+nds[i].getAttribute('lon')
]);
}
return nodes;
}
function parseNode(x) {
if (!x) return undefined;
var o = parseNodeBase(x);
if (o.type === 'node') {
o.lat = +x.getAttribute('lat');
o.lon = +x.getAttribute('lon');
} else if (o.type === 'way') {
o.bounds = parseBounds(x);
var nodes = parseLinestring(x);
if (nodes.length > 0) {
o.linestring = nodes;
}
} else if (o.type === 'relation') {
o.bounds = parseBounds(x);
o.members = parseMembers(x);
}
return o;
}
function get(x, y) {
if (!x) return undefined;
for (var i = 0; i < y.length; i++) {
var o = x.getElementsByTagName(y[i])[0];
if (o) return o;
}
}
function run(id, cb, bbox) {
requestChangeset(id, function(err, xml) {
if (err || !xml) return cb('Error');
if (!xml.getElementsByTagName) return cb('No items');
var actions = xml.getElementsByTagName('action'), a;
var items = [];
for (var i = 0; i < actions.length; i++) {
var o = {};
a = actions[i];
o.type = a.getAttribute('type');
if (o.type == 'create') {
o.neu = parseNode(get(a, ['node', 'way']));
} else if (o.type == 'modify') {
o.old = parseNode(get(get(a, ['old']), ['node', 'way']));
o.neu = parseNode(get(get(a, ['new']), ['node', 'way']));
} else if (o.type == 'delete') {
o.old = parseNode(get(get(a, ['old']), ['node', 'way']));
o.neu = parseNodeBase(get(get(a, ['new']), ['node', 'way']));
}
if (o.old || o.neu) {
items.push(o);
}
}
cb(null, items);
}, bbox);
}
s.once = function(cb, bbox) {
requestState(function(err, state) {
var stream = through(function write(err, data) {
cb(null, data);
});
run(state, stream.write, bbox);
});
};
s.run = function(cb, duration, dir, bbox, maxRetries) {
dir = dir || 1;
duration = duration || 60 * 1000;
var tries = 0;
var cancel = false;
function setCancel() { cancel = true; }
requestState(function(err, state) {
var stream = through(
function write(data) {
this.queue(data);
},
function end() {
cancel = true;
this.queue(null);
});
function write(items) {
for (var i = 0; i < items.length; i++) {
stream.write(items[i]);
}
}
cb(null, stream);
function iterate() {
run(state, function(err, items) {
if (!err) {
write(items);
}
if (!err || ((maxRetries || maxRetries === 0) && tries >= maxRetries)) {
tries = 0;
state += dir;
}
else {
tries++;
}
if (!cancel) setTimeout(iterate, duration);
}, bbox);
}
iterate();
});
return { cancel: setCancel };
};
s.runFn = function(cb, duration, dir, bbox, maxRetries) {
dir = dir || 1;
duration = duration || 60 * 1000;
var tries = 0;
function setCancel() { cancel = true; }
var cancel = false;
requestState(function(err, state) {
function write(items) { cb(null, items); }
function iterate() {
run(state, function(err, items) {
if (!err) {
write(items);
}
if (!err || ((maxRetries || maxRetries === 0) && tries >= maxRetries)) {
tries = 0;
state += dir;
}
else {
tries++;
}
if (!cancel) setTimeout(iterate, duration);
}, bbox);
}
iterate();
});
return { cancel: setCancel };
};
return s;
})();
module.exports = osmStream;