[gnome-maps/wip/mlundblad/osm-add-location: 4/7] osmEdit: WIP: Add a module for handling POI type mapping to OSM tagging
- From: Marcus Lundblad <mlundblad src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-maps/wip/mlundblad/osm-add-location: 4/7] osmEdit: WIP: Add a module for handling POI type mapping to OSM tagging
- Date: Tue, 12 Jan 2016 22:33:21 +0000 (UTC)
commit a460ed864d17a58b8d6df4c0fd8b9ca48e59823e
Author: Marcus Lundblad <ml update uu se>
Date: Wed Dec 30 00:24:00 2015 +0100
osmEdit: WIP: Add a module for handling POI type mapping to OSM tagging
src/org.gnome.Maps.src.gresource.xml | 3 +
src/osmTypeLookup.js | 145 ++++++++++++++++++++++++++++++++++
2 files changed, 148 insertions(+), 0 deletions(-)
---
diff --git a/src/org.gnome.Maps.src.gresource.xml b/src/org.gnome.Maps.src.gresource.xml
index 36f4c07..676ea96 100644
--- a/src/org.gnome.Maps.src.gresource.xml
+++ b/src/org.gnome.Maps.src.gresource.xml
@@ -34,6 +34,9 @@
<file>osmConnection.js</file>
<file>osmEdit.js</file>
<file>osmEditDialog.js</file>
+ <file>osmTypeSearchEntry.js</file>
+ <file>osmTypeLookup.js</file>
+ <file>osmTypeSearchPopover.js</file>
<file>osmUtils.js</file>
<file>overpass.js</file>
<file>place.js</file>
diff --git a/src/osmTypeLookup.js b/src/osmTypeLookup.js
new file mode 100644
index 0000000..60e0051
--- /dev/null
+++ b/src/osmTypeLookup.js
@@ -0,0 +1,145 @@
+/* -*- Mode: JS2; indent-tabs-mode: nil; js2-basic-offset: 4 -*- */
+/* vim: set et ts=4 sw=4: */
+/*
+ * Copyright (c) 2015 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>
+ */
+
+const GLib = imports.gi.GLib;
+const Lang = imports.lang;
+
+const Utils = imports.utils;
+
+const _RECENT_TYPES_STORE_FILE = 'maps-recent-types.json';
+const _NUM_RECENT_TYPES = 10;
+
+/* Lists the OSM tags we base our notion of location types on */
+const OSM_TYPE_TAGS = ['amenity', 'leasure', 'office', 'place', 'shop' ];
+
+const OSM_TYPE_MAP = [
+ /* amenities */
+ {key: 'amenity', value: 'pub', title: _("Pub")},
+ {key: 'amenity', value: 'bar', title: _("Bar")},
+ {key: 'amenity', value: 'atm', title: _("ATM")},
+ {key: 'amenity', value: 'restaurant', title: _("Restaurant")},
+ {key: 'amenity', value: 'school', title: _("School")},
+ {key: 'amenity', value: 'kindergarden', title: _("Kindergarden")},
+ {key: 'amenity', value: 'pharmacy', title: _("Pharmacy")},
+
+ /* shops */
+ {key: 'shop', value: 'supermarket', title: _("Supermarket")}
+];
+
+/* Sort function comparing two type values accoring to the locale-specific
+ comparison of the type title */
+function _sortType(t1, t2) {
+ return t1.title.toLocaleLowerCase().localeCompare(t2.title.toLocaleLowerCase());
+}
+
+function findMatches(prefix, maxMatches) {
+ let numMatches = 0;
+ let prefixLength = prefix.length;
+ let normalized = prefix.toLocaleLowerCase();
+ let matches = [];
+
+ for (let i = 0; i < OSM_TYPE_MAP.length && numMatches < maxMatches; i++) {
+ let item = OSM_TYPE_MAP[i];
+
+ if (item.title.substring(0, prefixLength).toLocaleLowerCase() === normalized) {
+ numMatches++;
+ matches.push(item);
+ }
+ }
+
+ return matches.sort(_sortType);
+}
+
+/* return the title of a type with a given key/value if it is known by us */
+function lookupType(key, value) {
+ for (let i = 0; i < OSM_TYPE_MAP.length; i++) {
+ let row = OSM_TYPE_MAP[i];
+
+ if (row.value === value && row.key === key)
+ return row.title;
+ }
+
+ return null;
+}
+
+const RecentTypesStore = new Lang.Class({
+ Name: 'RecentTypesStore',
+ Extends: GLib.Object,
+
+ _init: function() {
+ this.parent();
+ this._filename = GLib.build_filenamev([GLib.get_user_data_dir(),
+ _RECENT_TYPES_STORE_FILE]);
+ this._load();
+ },
+
+ get recentTypes() {
+ return this._recentTypes;
+ },
+
+ _load: function() {
+ if (!GLib.file_test(this._filename, GLib.FileTest.EXISTS)) {
+ this._recentTypes = [];
+ return;
+ }
+
+ let buffer = Utils.readFile(this._filename);
+ if (buffer === null) {
+ this._recentTypes = [];
+ return;
+ }
+
+ this._recentTypes = JSON.parse(buffer);
+ },
+
+ _save: function() {
+ let buffer = JSON.stringify(this._recentTypes);
+ if (!Utils.writeFile(this._filename, buffer))
+ log('Failed to write recent types file!');
+ },
+
+ /* push a type key/value as the most recently used type */
+ pushType: function(key, value) {
+ /* find out if the type is already stored */
+ let pos = -1;
+ for (let i = 0; i < this._recentTypes.length; i++) {
+ if (this._recentTypes[i].key === key &&
+ this._recentTypes[i].value === value) {
+ pos = i;
+ break;
+ }
+ }
+
+ /* remove the type if it was already found in the list */
+ if (pos != -1)
+ this._recentTypes.splice(pos, 1);
+
+ this._recentTypes.unshift({key: key, value: value});
+
+ /* prune the list */
+ this._recentTypes.splice(_NUM_RECENT_TYPES);
+
+ this._save();
+ }
+});
+
+const recentTypesStore = new RecentTypesStore();
+
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]