|
0 |
#!/usr/bin/env gjs
|
|
1 |
|
|
2 |
/* -*- Mode: JS2; indent-tabs-mode: nil; js2-basic-offset: 4 -*- */
|
|
3 |
/* vim: set et ts=4 sw=4: */
|
|
4 |
/*
|
|
5 |
* Copyright (c) 2016 Marcus Lundblad.
|
|
6 |
*
|
|
7 |
* GNOME Maps is free software; you can redistribute it and/or modify
|
|
8 |
* it under the terms of the GNU General Public License as published by the
|
|
9 |
* Free Software Foundation; either version 2 of the License, or (at your
|
|
10 |
* option) any later version.
|
|
11 |
*
|
|
12 |
* GNOME Maps is distributed in the hope that it will be useful, but
|
|
13 |
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
14 |
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
15 |
* for more details.
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License along
|
|
18 |
* with GNOME Maps; if not, see <http://www.gnu.org/licenses/>.
|
|
19 |
*
|
|
20 |
* Author: Marcus Lundblad <ml@update.uu.se>
|
|
21 |
*/
|
|
22 |
|
|
23 |
/*
|
|
24 |
* Script to generate a simplified JSON mapping file for POI types from the
|
|
25 |
* presets definitions from the iD Web-based OpenStreetMap editor
|
|
26 |
*
|
|
27 |
* Usage: ./extractPoiTypesFromID.js <path to iD checkout> > osm-types.json
|
|
28 |
*
|
|
29 |
*/
|
|
30 |
|
|
31 |
const Gio = imports.gi.Gio;
|
|
32 |
|
|
33 |
const PRESETS_PATH = 'data/presets/presets';
|
|
34 |
const LOCALES_PATH = 'dist/locales';
|
|
35 |
const PRESET_TYPES = [ 'amenity',
|
|
36 |
'leisure',
|
|
37 |
'office',
|
|
38 |
'place',
|
|
39 |
'shop',
|
|
40 |
'tourism' ];
|
|
41 |
|
|
42 |
const OUTPUT = {};
|
|
43 |
|
|
44 |
function parseJson(dirPath, fileName) {
|
|
45 |
let file = Gio.File.new_for_path(dirPath + '/' + fileName);
|
|
46 |
let [status, buffer] = file.load_contents(null);
|
|
47 |
let {tags, name} = JSON.parse(buffer);
|
|
48 |
|
|
49 |
for (let key in tags) {
|
|
50 |
let value = tags[key];
|
|
51 |
|
|
52 |
OUTPUT[key + '/' + value] = {'title': {'C': name}};
|
|
53 |
}
|
|
54 |
}
|
|
55 |
|
|
56 |
function processType(type, basePath) {
|
|
57 |
let dirPath = [basePath, PRESETS_PATH, type].join('/');
|
|
58 |
let dir = Gio.File.new_for_path(dirPath);
|
|
59 |
let enumerator =
|
|
60 |
dir.enumerate_children('*',
|
|
61 |
Gio.FileQueryInfoFlags.NOFOLLOW_SYMLINKS, null);
|
|
62 |
|
|
63 |
while (true) {
|
|
64 |
let file = enumerator.next_file(null);
|
|
65 |
|
|
66 |
if (file === null)
|
|
67 |
break;
|
|
68 |
|
|
69 |
if (file.get_name().endsWith('.json'))
|
|
70 |
parseJson(dirPath, file.get_name());
|
|
71 |
}
|
|
72 |
}
|
|
73 |
|
|
74 |
function processTypes(basePath) {
|
|
75 |
PRESET_TYPES.forEach(function(type) {
|
|
76 |
processType(type, basePath);
|
|
77 |
});
|
|
78 |
}
|
|
79 |
|
|
80 |
function processLocale(dirPath, fileName) {
|
|
81 |
let file = Gio.File.new_for_path(dirPath + '/' + fileName);
|
|
82 |
let [status, buffer] = file.load_contents(null);
|
|
83 |
let object = JSON.parse(buffer);
|
|
84 |
let lang = fileName.substring(0, fileName.indexOf('.json'));
|
|
85 |
|
|
86 |
for (let type in OUTPUT) {
|
|
87 |
let name;
|
|
88 |
|
|
89 |
try {
|
|
90 |
name = object.presets.presets[type].name;
|
|
91 |
} catch (ex) {
|
|
92 |
continue;
|
|
93 |
}
|
|
94 |
|
|
95 |
OUTPUT[type].title[lang] = name;
|
|
96 |
}
|
|
97 |
}
|
|
98 |
|
|
99 |
function processLocales(basePath) {
|
|
100 |
let dirPath = basePath + '/' + LOCALES_PATH;
|
|
101 |
let dir = Gio.File.new_for_path(dirPath);
|
|
102 |
let enumerator =
|
|
103 |
dir.enumerate_children('*.json',
|
|
104 |
Gio.FileQueryInfoFlags.NOFOLLOW_SYMLINKS, null);
|
|
105 |
|
|
106 |
while (true) {
|
|
107 |
let file = enumerator.next_file(null);
|
|
108 |
|
|
109 |
if (file === null)
|
|
110 |
break;
|
|
111 |
|
|
112 |
if (file.get_name().endsWith('.json'))
|
|
113 |
processLocale(dirPath, file.get_name());
|
|
114 |
}
|
|
115 |
}
|
|
116 |
|
|
117 |
function outputJson() {
|
|
118 |
print(JSON.stringify(OUTPUT));
|
|
119 |
}
|
|
120 |
|
|
121 |
function main(args) {
|
|
122 |
let path = args[0];
|
|
123 |
|
|
124 |
processTypes(path);
|
|
125 |
processLocales(path);
|
|
126 |
|
|
127 |
outputJson();
|
|
128 |
}
|
|
129 |
|
|
130 |
main(ARGV);
|