forked from osmlab/name-suggestion-index
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build_dist.js
160 lines (125 loc) · 5.08 KB
/
build_dist.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
const colors = require('colors/safe');
const fs = require('fs');
const dissolved = require('./dist/dissolved.json');
const namesKeep = require('./dist/names_keep.json');
const packageJSON = require('./package.json');
const shell = require('shelljs');
const stringify = require('json-stringify-pretty-compact');
const xmlbuilder = require('xmlbuilder');
const fileTree = require('./lib/file_tree.js');
const toParts = require('./lib/to_parts.js');
let brands = fileTree.read('brands');
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']);
// copy some project config files into `/dist`
shell.cp('-f', 'config/filters.json', 'dist/filters.json');
shell.cp('-f', 'config/match_groups.json', 'dist/match_groups.json');
// write `brands.json` as a single file
fs.writeFileSync('dist/brands.json', stringify({ brands: brands }));
buildJSON();
buildXML();
buildTaginfo();
console.timeEnd(colors.green('data built'));
}
function buildJSON() {
Object.keys(brands).forEach(kvnd => {
const obj = brands[kvnd];
const wd = obj.tags['brand:wikidata'];
if (!wd || !/^Q\d+$/.test(wd)) return; // wikidata tag missing or looks wrong..
if (dissolved[kvnd]) {
return; // brand does not exist anymore, so it should not be in the index
}
const parts = toParts(kvnd);
const k = parts.k;
const v = parts.v;
const n = parts.n + (parts.d !== undefined ? ' ' + parts.d : "");
if (!out[k]) out[k] = {};
if (!out[k][v]) out[k][v] = {};
out[k][v][n] = {};
if (namesKeep[kvnd]) {
out[k][v][n].count = namesKeep[kvnd];
}
if (obj.countryCodes) {
out[k][v][n].countryCodes = obj.countryCodes;
}
out[k][v][n].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', packageJSON.description)
.att('link', 'https://github.com/' + packageJSON.repository)
.att('version', packageJSON.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 }));
}