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



commit b64e2bf03c5e315da58f486fe782f7fcc040dc1b
Author: Marcus Lundblad <ml update uu se>
Date:   Thu Jan 21 23:10:56 2016 +0100

    osmEdit: Add GJS script to extract POI definitions from the iD OSM editor

 Makefile.am                      |    2 +-
 configure.ac                     |    1 +
 scripts/Makefile.am              |    9 +++
 scripts/README                   |    7 ++
 scripts/extractPoiTypesFromID.js |  142 ++++++++++++++++++++++++++++++++++++++
 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 b7998d2..93f2aa0 100644
--- a/configure.ac
+++ b/configure.ac
@@ -74,5 +74,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..9e9cf69
--- /dev/null
+++ b/scripts/README
@@ -0,0 +1,7 @@
+Run the extractPoiTypesFromID.js script against a checkout of iD (https://github.com/openstreetmap/iD)
+
+$ ./extractPoiTypesFromID.js <path to iD checkout> > osm-types.json
+
+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..82fe4d4
--- /dev/null
+++ b/scripts/extractPoiTypesFromID.js
@@ -0,0 +1,142 @@
+#!/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 object = JSON.parse(buffer);
+    let tags = object.tags;
+    let name = object.name;
+
+    for (let key in tags) {
+        let value = tags[key];
+        let title = {};
+
+        title['C'] = name;
+
+        OUTPUT[key + '/' + value] = {'title': title};
+    }
+}
+
+function processType(type, basePath) {
+    let dirPath = basePath + '/' + PRESETS_PATH + '/' + type;
+    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'))
+            continue;
+
+        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 presets = object['presets'];
+
+        if (presets) {
+            let innerPresets = presets['presets'];
+
+            if (innerPresets) {
+                let translation = innerPresets[type];
+
+                if (translation) {
+                    let name = translation.name;
+
+                    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'))
+            continue;
+
+        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]