[gnome-maps/wip/mlundblad/osm-add-location: 5/9] osmEdit: Add GJS script to extract POI definitions from the iD OSM editor



commit bdad1db1561758970b45db0664cd8a1ceaae34d0
Author: Marcus Lundblad <ml update uu se>
Date:   Sun Jan 31 11:48:46 2016 +0100

    osmEdit: Add GJS script to extract POI definitions from the iD OSM editor
    
    This creates an updated definition of POI types (with translations)
    from a checkout of the iD editor's code. This script could be run
    (and the output copied to data/osm-types.json) if POI presets have
    been added and/or updated or if new or updated translations are
    available in a new version of iD.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=761327

 Makefile.am                      |    2 +-
 configure.ac                     |    1 +
 scripts/Makefile.am              |    9 +++
 scripts/README                   |   18 +++++
 scripts/extractPoiTypesFromID.js |  131 ++++++++++++++++++++++++++++++++++++++
 5 files changed, 160 insertions(+), 1 deletions(-)
---
diff --git a/Makefile.am b/Makefile.am
index 8f53c8f..c5dd3b6 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -1,3 +1,3 @@
 ACLOCAL_AMFLAGS = -I m4 ${ACLOCAL_FLAGS}
 
-SUBDIRS = lib src data po
+SUBDIRS = lib src data po scripts
diff --git a/configure.ac b/configure.ac
index b8cab4d..f0ca3cc 100644
--- a/configure.ac
+++ b/configure.ac
@@ -75,5 +75,6 @@ AC_CONFIG_FILES([
     data/Makefile
     data/icons/Makefile
     po/Makefile.in
+    scripts/Makefile
 ])
 AC_OUTPUT
diff --git a/scripts/Makefile.am b/scripts/Makefile.am
new file mode 100644
index 0000000..fb623d1
--- /dev/null
+++ b/scripts/Makefile.am
@@ -0,0 +1,9 @@
+noinst_DATA = \
+       extractPoiTypesFromID.js\
+       README
+
+EXTRA_DIST = $(noinst_DATA)
+
+-include $(top_srcdir)/git.mk
+
+
diff --git a/scripts/README b/scripts/README
new file mode 100644
index 0000000..2be7d78
--- /dev/null
+++ b/scripts/README
@@ -0,0 +1,18 @@
+SCRIPTS
+=======
+
+extractPoiTypesFromID.js
+------------------------
+
+Extracts the POI types (presets) that the iD editor uses to JSON for us to consume.
+
+Run the extractPoiTypesFromID.js script against a checkout of iD (https://github.com/openstreetmap/iD)
+
+$ ./extractPoiTypesFromID.js <path to iD checkout> > osm-types.json
+
+This script would be run by upstream before making a release of gnome-maps if an updated version
+of the iD editor is available with updated OSM types preset and/or new or updated translations
+of those.
+Check the resulting .json file (i.e. check size and possibly diff against the current version).
+Copy the result to data/osm-types.json
+
diff --git a/scripts/extractPoiTypesFromID.js b/scripts/extractPoiTypesFromID.js
new file mode 100755
index 0000000..30d5279
--- /dev/null
+++ b/scripts/extractPoiTypesFromID.js
@@ -0,0 +1,131 @@
+#!/usr/bin/env gjs
+
+/* -*- 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
+ *
+ */
+
+const Gio = imports.gi.Gio;
+
+const PRESETS_PATH = 'data/presets/presets';
+const LOCALES_PATH = 'dist/locales';
+const PRESET_TYPES = [ 'amenity',
+                       'leisure',
+                       'office',
+                       'place',
+                       'shop',
+                       'tourism' ];
+
+const OUTPUT = {};
+
+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(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(buffer);
+    let lang = fileName.substring(0, fileName.indexOf('.json'));
+
+    for (let type in OUTPUT) {
+        let name;
+
+        try {
+            name = object.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));
+}
+
+function main(args) {
+    let path = args[0];
+
+    processTypes(path);
+    processLocales(path);
+
+    outputJson();
+}
+
+main(ARGV);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]