-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathtaginfo.js
382 lines (311 loc) · 10.6 KB
/
taginfo.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
import _debounce from 'lodash-es/debounce';
import { json as d3_json } from 'd3-fetch';
import { utilObjectOmit, utilQsString } from '../util';
import { localizer } from '../core/localizer';
import { allowUpperCaseTagValues } from '../osm/tags';
import { taginfoApiUrl } from '../../config/id.js';
var apibase = taginfoApiUrl;
var _inflight = {};
var _popularKeys = {};
var _taginfoCache = {};
var tag_sorts = {
point: 'count_nodes',
vertex: 'count_nodes',
area: 'count_ways',
line: 'count_ways'
};
var tag_sort_members = {
point: 'count_node_members',
vertex: 'count_node_members',
area: 'count_way_members',
line: 'count_way_members',
relation: 'count_relation_members'
};
var tag_filters = {
point: 'nodes',
vertex: 'nodes',
area: 'ways',
line: 'ways'
};
var tag_members_fractions = {
point: 'count_node_members_fraction',
vertex: 'count_node_members_fraction',
area: 'count_way_members_fraction',
line: 'count_way_members_fraction',
relation: 'count_relation_members_fraction'
};
function sets(params, n, o) {
if (params.geometry && o[params.geometry]) {
params[n] = o[params.geometry];
}
return params;
}
function setFilter(params) {
return sets(params, 'filter', tag_filters);
}
function setSort(params) {
return sets(params, 'sortname', tag_sorts);
}
function setSortMembers(params) {
return sets(params, 'sortname', tag_sort_members);
}
function clean(params) {
return utilObjectOmit(params, ['geometry', 'debounce']);
}
function filterKeys(type) {
var count_type = type ? 'count_' + type : 'count_all';
return function(d) {
return Number(d[count_type]) > 2500 || d.in_wiki;
};
}
function filterMultikeys(prefix) {
return function(d) {
// d.key begins with prefix, and d.key contains no additional ':'s
var re = new RegExp('^' + prefix + '(.*)$', 'i');
var matches = d.key.match(re) || [];
return (matches.length === 2 && matches[1].indexOf(':') === -1);
};
}
function filterValues(allowUpperCase) {
return function(d) {
if (d.value.match(/[;,]/) !== null) return false; // exclude some punctuation
if (!allowUpperCase && d.value.match(/[A-Z*]/) !== null) return false; // exclude uppercase letters
return d.count > 100 || d.in_wiki; // exclude rare undocumented tags
};
}
function filterRoles(geometry) {
return function(d) {
if (d.role === '') return false; // exclude empty role
if (d.role.match(/[A-Z*;,]/) !== null) return false; // exclude uppercase letters and some punctuation
return Number(d[tag_members_fractions[geometry]]) > 0.0;
};
}
function valKey(d) {
return {
value: d.key,
title: d.key
};
}
function valKeyDescription(d) {
var obj = {
value: d.value,
title: d.description || d.value
};
return obj;
}
function roleKey(d) {
return {
value: d.role,
title: d.role
};
}
// sort keys with ':' lower than keys without ':'
function sortKeys(a, b) {
return (a.key.indexOf(':') === -1 && b.key.indexOf(':') !== -1) ? -1
: (a.key.indexOf(':') !== -1 && b.key.indexOf(':') === -1) ? 1
: 0;
}
var debouncedRequest = _debounce(request, 300, { leading: false });
function request(url, params, exactMatch, callback, loaded) {
if (_inflight[url]) return;
if (checkCache(url, params, exactMatch, callback)) return;
var controller = new AbortController();
_inflight[url] = controller;
d3_json(url, { signal: controller.signal })
.then(function(result) {
delete _inflight[url];
if (loaded) loaded(null, result);
})
.catch(function(err) {
delete _inflight[url];
if (err.name === 'AbortError') return;
if (loaded) loaded(err.message);
});
}
function checkCache(url, params, exactMatch, callback) {
var rp = params.rp || 25;
var testQuery = params.query || '';
var testUrl = url;
do {
var hit = _taginfoCache[testUrl];
// exact match, or shorter match yielding fewer than max results (rp)
if (hit && (url === testUrl || hit.length < rp)) {
callback(null, hit);
return true;
}
// don't try to shorten the query
if (exactMatch || !testQuery.length) return false;
// do shorten the query to see if we already have a cached result
// that has returned fewer than max results (rp)
testQuery = testQuery.slice(0, -1);
testUrl = url.replace(/&query=(.*?)&/, '&query=' + testQuery + '&');
} while (testQuery.length >= 0);
return false;
}
export default {
init: function() {
_inflight = {};
_taginfoCache = {};
_popularKeys = {
// manually exclude some keys – #5377, #7485
postal_code: true,
full_name: true,
loc_name: true,
reg_name: true,
short_name: true,
sorting_name: true,
artist_name: true,
nat_name: true,
long_name: true,
via: true,
'bridge:name': true
};
// Fetch popular keys. We'll exclude these from `values`
// lookups because they stress taginfo, and they aren't likely
// to yield meaningful autocomplete results.. see #3955
var params = {
rp: 100,
sortname: 'values_all',
sortorder: 'desc',
page: 1,
debounce: false,
lang: localizer.languageCode()
};
this.keys(params, function(err, data) {
if (err) return;
data.forEach(function(d) {
if (d.value === 'opening_hours') return; // exception
_popularKeys[d.value] = true;
});
});
},
reset: function() {
Object.values(_inflight).forEach(function(controller) { controller.abort(); });
_inflight = {};
},
keys: function(params, callback) {
var doRequest = params.debounce ? debouncedRequest : request;
params = clean(setSort(params));
params = Object.assign({
rp: 10,
sortname: 'count_all',
sortorder: 'desc',
page: 1,
lang: localizer.languageCode()
}, params);
var url = apibase + 'keys/all?' + utilQsString(params);
doRequest(url, params, false, callback, function(err, d) {
if (err) {
callback(err);
} else {
var f = filterKeys(params.filter);
var result = d.data.filter(f).sort(sortKeys).map(valKey);
_taginfoCache[url] = result;
callback(null, result);
}
});
},
multikeys: function(params, callback) {
var doRequest = params.debounce ? debouncedRequest : request;
params = clean(setSort(params));
params = Object.assign({
rp: 25,
sortname: 'count_all',
sortorder: 'desc',
page: 1,
lang: localizer.languageCode()
}, params);
var prefix = params.query;
var url = apibase + 'keys/all?' + utilQsString(params);
doRequest(url, params, true, callback, function(err, d) {
if (err) {
callback(err);
} else {
var f = filterMultikeys(prefix);
var result = d.data.filter(f).map(valKey);
_taginfoCache[url] = result;
callback(null, result);
}
});
},
values: function(params, callback) {
// Exclude popular keys from values lookups.. see #3955
var key = params.key;
if (key && _popularKeys[key]) {
callback(null, []);
return;
}
var doRequest = params.debounce ? debouncedRequest : request;
params = clean(setSort(setFilter(params)));
params = Object.assign({
rp: 25,
sortname: 'count_all',
sortorder: 'desc',
page: 1,
lang: localizer.languageCode()
}, params);
var url = apibase + 'key/values?' + utilQsString(params);
doRequest(url, params, false, callback, function(err, d) {
if (err) {
callback(err);
} else {
// In most cases we prefer taginfo value results with lowercase letters.
// A few OSM keys expect values to contain uppercase values (see #3377).
// This is not an exhaustive list (e.g. `name` also has uppercase values)
// but these are the fields where taginfo value lookup is most useful.
var allowUpperCase = allowUpperCaseTagValues.test(params.key);
var f = filterValues(allowUpperCase);
var result = d.data.filter(f).map(valKeyDescription);
_taginfoCache[url] = result;
callback(null, result);
}
});
},
roles: function(params, callback) {
var doRequest = params.debounce ? debouncedRequest : request;
var geometry = params.geometry;
params = clean(setSortMembers(params));
params = Object.assign({
rp: 25,
sortname: 'count_all_members',
sortorder: 'desc',
page: 1,
lang: localizer.languageCode()
}, params);
var url = apibase + 'relation/roles?' + utilQsString(params);
doRequest(url, params, true, callback, function(err, d) {
if (err) {
callback(err);
} else {
var f = filterRoles(geometry);
var result = d.data.filter(f).map(roleKey);
_taginfoCache[url] = result;
callback(null, result);
}
});
},
docs: function(params, callback) {
var doRequest = params.debounce ? debouncedRequest : request;
params = clean(setSort(params));
var path = 'key/wiki_pages?';
if (params.value) {
path = 'tag/wiki_pages?';
} else if (params.rtype) {
path = 'relation/wiki_pages?';
}
var url = apibase + path + utilQsString(params);
doRequest(url, params, true, callback, function(err, d) {
if (err) {
callback(err);
} else {
_taginfoCache[url] = d.data;
callback(null, d.data);
}
});
},
apibase: function(_) {
if (!arguments.length) return apibase;
apibase = _;
return this;
}
};