forked from osmlab/name-suggestion-index
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build_nameSuggestions.js
142 lines (114 loc) · 4.39 KB
/
build_nameSuggestions.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
const colors = require('colors/safe');
const fs = require('fs');
const package = require('./package.json');
const shell = require('shelljs');
const stringify = require('json-stringify-pretty-compact');
const xmlbuilder = require('xmlbuilder');
const canonical = require('./config/canonical.json');
let out = {};
buildAll();
function buildAll() {
console.log('building data');
console.time(colors.green('data built'));
// Start clean
shell.rm('-f', ['dist/name-suggestions.*', 'dist/taginfo.json']);
buildJSON();
buildXML();
buildTaginfo();
console.timeEnd(colors.green('data built'));
}
function buildJSON() {
Object.keys(canonical).forEach(k => {
const obj = canonical[k];
const wd = obj.tags['brand:wikidata'];
if (!wd || !/^Q\d+$/.test(wd)) return; // wikidata tag missing or looks wrong..
const parts = k.split('|', 2);
const tag = parts[0].split('/', 2);
const key = tag[0];
const value = tag[1];
const name = parts[1].replace('~', ' ');
if (!out[key]) {
out[key] = {};
}
if (!out[key][value]) {
out[key][value] = {};
}
out[key][value][name] = {
count: obj.count,
tags: obj.tags
};
});
// Save individual data files
fs.writeFileSync('dist/name-suggestions.json', stringify(out));
fs.writeFileSync('dist/name-suggestions.min.json', JSON.stringify(out));
}
function buildXML() {
let presets = xmlbuilder
.create('presets', { version: '1.0', encoding: 'UTF-8' })
.att('xmlns', 'http://josm.openstreetmap.de/tagging-preset-1.0')
.att('author', 'Name Suggestion Index')
.att('shortdescription', 'Name Suggestion Index')
.att('description', package.description)
.att('link', 'https://github.com/' + package.repository)
.att('version', package.version);
let topgroup = presets
.ele('group')
.att('name', 'Name Suggestion Index');
// Create JOSM presets using the key and value structure from the json
// to organize the presets into JOSM preset groups.
for (let key in out) {
let keygroup = topgroup.ele('group').att('name', key);
for (let value in out[key]) {
let valuegroup = keygroup.ele('group').att('name', value);
for (let name in out[key][value]) {
let item = valuegroup
.ele('item')
.att('name', name)
.att('type', 'node,closedway,multipolygon');
let tags = out[key][value][name].tags;
for (let k in tags) {
item.ele('key').att('key', k).att('value', tags[k]);
}
}
}
}
let xmlstring = presets.end({ pretty: true })
fs.writeFileSync('dist/name-suggestions.presets.xml', xmlstring);
}
function buildTaginfo() {
var taginfo = {
'data_format': 1,
'data_url': 'https://raw.githubusercontent.com/osmlab/name-suggestion-index/master/dist/taginfo.json',
'project': {
'name': 'name-suggestion-index',
'description': 'Canonical common brand names for OpenStreetMap',
'project_url': 'https://github.com/osmlab/name-suggestion-index',
'doc_url': 'https://github.com/osmlab/name-suggestion-index/blob/master/README.md',
'icon_url': 'https://raw.githubusercontent.com/mapbox/maki/master/icons/fast-food-15.svg?sanitize=true',
'contact_name': 'Bryan Housel',
'contact_email': '[email protected]'
}
};
let items = {};
for (let key in out) {
for (let value in out[key]) {
for (let name in out[key][value]) {
let tags = out[key][value][name].tags;
for (let k in tags) {
let v = tags[k];
// skip value for most tags this project uses
if (/name|brand|operator/.test(k)) {
v = '*';
}
let id = k + '/' + v;
items[id] = { key: k }
if (v !== '*') {
items[id].value = v;
}
}
}
}
}
taginfo.tags = Object.keys(items).sort().map(k => items[k]);
fs.writeFileSync('dist/taginfo.json', stringify(taginfo, { maxLength: 100 }));
}