forked from osmlab/name-suggestion-index
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build_all_names.js
74 lines (58 loc) · 1.96 KB
/
build_all_names.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
#!/usr/bin/env node
// This script will process a planet file and extract frequently occuring names.
// It produces a file containing all the top names and tags: `dist/names_all.json`
//
// `names_all.json` contains a dictionary object in the format:
// "key/value|name": count
// "amenity/cafe|Starbucks": 159
//
// Please see README.md for more info
const colors = require('colors/safe');
const fs = require('fs');
const osmium = require('osmium');
const shell = require('shelljs');
const sort = require('./lib/sort');
const stringify = require('json-stringify-pretty-compact');
if (process.argv.length < 3) {
console.log('');
console.log('Usage: node build_all_names <planet.osm>');
console.log('');
process.exit(1);
}
const checkKeys = ['amenity', 'shop', 'leisure', 'tourism'];
const THRESHOLD = process.argv[3] || 50;
let counts = {};
build();
function build() {
console.log('building names_all.json');
console.time(colors.green('data built'));
// Start clean
shell.rm('-f', ['dist/names_all.json']);
let handler = new osmium.Handler();
handler.options({ tagged_nodes_only: true });
handler.on('node', countTags);
handler.on('way', countTags);
handler.on('relation', countTags);
let reader = new osmium.Reader(process.argv[2]);
osmium.apply(reader, handler);
// filter
let filtered = {};
for (let key in counts) {
if (counts[key] > THRESHOLD) {
filtered[key] = counts[key];
}
}
fs.writeFileSync('dist/names_all.json', stringify(sort(filtered)));
console.timeEnd(colors.green('data built'));
}
function countTags(entity) {
let name = entity.tags('name'); // fast name check
if (!name) return;
let tags = entity.tags();
for (let i = 0; i < checkKeys.length; i++) {
let key = checkKeys[i];
if (!tags[key]) continue;
let fullName = key + '/' + tags[key] + '|' + name;
counts[fullName] = (counts[fullName] || 0) + 1;
}
}