summaryrefslogtreecommitdiff
path: root/scripts/extractPoiTypesFromID.js
blob: 32585b170aacebc6013482a40828b97cbffa8b9f (plain)
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
#!/usr/bin/env -S gjs -m

/* -*- Mode: JS2; indent-tabs-mode: nil; js2-basic-offset: 4 -*- */
/* vim: set et ts=4 sw=4: */
/*
 * Copyright (c) 2016 Marcus Lundblad.
 *
 * GNOME Maps is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.
 *
 * GNOME Maps is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with GNOME Maps; if not, see <http://www.gnu.org/licenses/>.
 *
 * Author: Marcus Lundblad <ml@update.uu.se>
 */

/*
 * Script to generate a simplified JSON mapping file for POI types from the
 * presets definitions from the iD Web-based OpenStreetMap editor
 * 
 * Usage: ./extractPoiTypesFromID.js <path to iD checkout> > osm-types.json
 *
 */

import Gio from 'gi://Gio';

const PRESETS_PATH = 'data/presets';
const LOCALES_PATH = 'dist/translations';
const PRESET_TYPES = [ 'aeroway',
                       'amenity',
                       'leisure',
                       'office',
                       'place',
                       'shop',
                       'tourism' ];

const OUTPUT = {};

let decoder = new TextDecoder('utf-8');

function parseJson(dirPath, fileName) {
    let file = Gio.File.new_for_path(dirPath + '/' + fileName);
    let [status, buffer] = file.load_contents(null);
    let {tags, name} = JSON.parse(decoder.decode(buffer));

    for (let key in tags) {
        let value = tags[key];

        OUTPUT[key + '/' + value] = {'title': {'C': name}};
    }
}

function processType(type, basePath) {
    let dirPath = [basePath, PRESETS_PATH, type].join('/');
    let dir = Gio.File.new_for_path(dirPath);
    let enumerator =
        dir.enumerate_children('*',
                               Gio.FileQueryInfoFlags.NOFOLLOW_SYMLINKS, null);

    while (true) {
        let file = enumerator.next_file(null);

        if (file === null)
            break;

        if (file.get_name().endsWith('.json'))
            parseJson(dirPath, file.get_name());
    }
}

function processTypes(basePath) {
    PRESET_TYPES.forEach(function(type) {
        processType(type, basePath);
    });
}

function processLocale(dirPath, fileName) {
    let file = Gio.File.new_for_path(dirPath + '/' + fileName);
    let [status, buffer] = file.load_contents(null);
    let object = JSON.parse(decoder.decode(buffer));
    let lang = fileName.substring(0, fileName.indexOf('.json'));

    for (let type in OUTPUT) {
        let name;

        try {
            name = object[lang].presets.presets[type].name;
        } catch (ex) {
            continue;
        }

        OUTPUT[type].title[lang] = name;
    }
}

function processLocales(basePath) {
    let dirPath = basePath + '/' + LOCALES_PATH;
    let dir = Gio.File.new_for_path(dirPath);
    let enumerator =
        dir.enumerate_children('*.json',
                               Gio.FileQueryInfoFlags.NOFOLLOW_SYMLINKS, null);

    while (true) {
        let file = enumerator.next_file(null);

        if (file === null)
            break;

        if (file.get_name().endsWith('.json'))
            processLocale(dirPath, file.get_name());
    }
}

function outputJson() {
    print(JSON.stringify(OUTPUT, null, 2));
}

function main(args) {
    let path = args[0];

    processTypes(path);
    processLocales(path);

    outputJson();
}

main(ARGV);