[gnome-maps/wip/osm-edit: 2/47] osmApi: Add first draft of OSM JS API.



commit 2113369543b94505b756e360d2aa230ffff4d7f8
Author: Marcus Lundblad <ml update uu se>
Date:   Mon Jan 19 22:09:58 2015 +0100

    osmApi: Add first draft of OSM JS API.

 src/org.gnome.Maps.src.gresource.xml |    6 +++
 src/osmChangeset.js                  |   53 +++++++++++++++++++++++++++
 src/osmConnection.js                 |   66 ++++++++++++++++++++++++++++++++++
 src/osmNode.js                       |   66 ++++++++++++++++++++++++++++++++++
 src/osmObject.js                     |   66 ++++++++++++++++++++++++++++++++++
 src/osmRelation.js                   |   62 +++++++++++++++++++++++++++++++
 src/osmWay.js                        |   39 ++++++++++++++++++++
 7 files changed, 358 insertions(+), 0 deletions(-)
---
diff --git a/src/org.gnome.Maps.src.gresource.xml b/src/org.gnome.Maps.src.gresource.xml
index 1e6cdb9..a33d19b 100644
--- a/src/org.gnome.Maps.src.gresource.xml
+++ b/src/org.gnome.Maps.src.gresource.xml
@@ -26,6 +26,12 @@
     <file>mapWalker.js</file>
     <file>notification.js</file>
     <file>notificationManager.js</file>
+    <file>osmChangeset.js</file>
+    <file>osmConnection.js</file>
+    <file>osmNode.js</file>
+    <file>osmObject.js</file>
+    <file>osmRelation.js</file>
+    <file>osmWay.js</file>
     <file>overpass.js</file>
     <file>place.js</file>
     <file>placeBubble.js</file>
diff --git a/src/osmChangeset.js b/src/osmChangeset.js
new file mode 100644
index 0000000..761fdc7
--- /dev/null
+++ b/src/osmChangeset.js
@@ -0,0 +1,53 @@
+/* -*- 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, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * Author: Marcus Lundblad <ml update uu se>
+ */
+
+const Lang = imports.lang;
+const OSMObject = imports.osmObject;
+const OSMTag = imports.osmTag;
+
+const OSMChangeset = new Lang.Class({
+    Name: 'OSMChangeset',
+    Extends: OSMObject.OSMObject,
+
+    _init: function(comment) {
+       // add default tags
+       this.setTag(new OSMTag.OSMTag({key: 'created_by',
+                                      value: 'gnome-maps'})); // TODO: add real version number
+       this.setTag(new OSMTag.OSMTag({key: 'comment',
+                                      value: comment}));
+
+       // undefined changeset ID until the changeset has been opened
+       this._id = undefined;
+    },
+
+    open: function(callback) {
+       // TODO: call the OSM REST API to open the changeset, parse response,
+       // call callback, etc.
+
+    },
+
+    close: function(callback) {
+       // TODO: call the OSM REST API to close the changeset, parse response,
+       // call callback, etc.
+
+    }
+})
diff --git a/src/osmConnection.js b/src/osmConnection.js
new file mode 100644
index 0000000..989917a
--- /dev/null
+++ b/src/osmConnection.js
@@ -0,0 +1,66 @@
+/* -*- 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, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * Author: Marcus Lundblad <ml update uu se>
+ */
+
+const Lang = imports.lang;
+const Soup = imports.gi.Soup;
+
+const BASE_URL = 'http://api.openstreetmap.org/api';
+const API_VERSION = '0.6';
+
+const OSMConnection = new Lang.Class({
+    Name: 'OSMConnection',
+
+    _init: function(params) {
+       this._session = new Soup.Session();
+    },
+
+    getOSMObject: function(type, id, callback) {
+       let url = this._getQueryUrl(type, id);
+       let uri = new Soup.URI(url);
+       let request = new Soup.Message({ method: 'GET',
+                                        uri: uri });
+
+       print('calling URL: ' + url);
+       
+       this._session.queue_message(request, (function(obj, message) {
+           print('got response status: ' + message.status_code);
+
+           if (message.status_code !== Soup.KnownStatusCode.OK) {
+                callback(false, message.status_code, null);
+                return;
+            }
+
+           print('got response: ' + message.response_body.data);
+           
+            callback(true,
+                     message.status_code,
+                    message.response_body.data);
+        }));
+
+       print('after');
+    },
+    
+    _getQueryUrl: function(type, id) {
+       return BASE_URL + '/' + API_VERSION + '/' + type + '/' + id;
+    }
+})
+
diff --git a/src/osmNode.js b/src/osmNode.js
new file mode 100644
index 0000000..02ec8ca
--- /dev/null
+++ b/src/osmNode.js
@@ -0,0 +1,66 @@
+/* -*- 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, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * Author: Marcus Lundblad <ml update uu se>
+ */
+
+const Lang = imports.lang;
+const OSMObject = imports.osmObject;
+const OSMConnection = imports.osmConnection;
+
+const OSMNode = new Lang.Class({
+    Name: 'OSMNode',
+    Extends: OSMObject.OSMObject,
+    
+    _init: function(params) {
+       this._lat = params.lat;
+       this._lon = params.lon;
+       this.parent(params);
+    },
+
+    get lat() {
+       return this._lat;
+    },
+
+    get lon() {
+       return this._lon;
+    },
+
+    toString: function() {
+       let s = 'node changeset: ' + this._changeset + ' lat: ' + this._lat +
+           ' lon: ' + this._lon;
+
+       for (var v in this._tags) {
+           s += '\n';
+           s += '\t';
+           s += 'tag: k=' + v + ', v=' + this.getTag(v);
+       }
+       
+       return s;
+    }
+});
+
+OSMNode.download = function(conn, id, callback) {
+    conn.getOSMObject('node', id, callback);
+
+    // TODO: should parse the returned XML into an OSMNode object,
+    // call the callback with that
+}
+
+
diff --git a/src/osmObject.js b/src/osmObject.js
new file mode 100644
index 0000000..e02cc99
--- /dev/null
+++ b/src/osmObject.js
@@ -0,0 +1,66 @@
+/* -*- 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, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * Author: Marcus Lundblad <ml update uu se>
+ */
+
+const Lang = imports.lang;
+
+const OSMObject = new Lang.Class({
+    Name: 'OSMObject',
+    Abstract: true,
+
+    _init: function(params) {
+       this._changeset = params.changeset;
+
+       if (params.id)
+           this._id = params.id;
+       else
+           this._id = undefined;
+       
+       if (params.tags)
+           this._tags = params.tags;
+       else
+           this._tags = {}
+    },
+    
+    get id() {
+       return this._id;
+    },
+
+    set id(val) {
+       this._id = val;
+    },
+
+    get changeset() {
+       return this._changeset;
+    },
+
+    setTag: function(key, value) {
+       this._tags[key] = value;
+    },    
+    
+    getTag: function(key) {
+       return this._tags[key];
+    },
+
+    deleteTag: function(key) {
+       delete this._tags[key];
+    }
+});
diff --git a/src/osmRelation.js b/src/osmRelation.js
new file mode 100644
index 0000000..ba62e0f
--- /dev/null
+++ b/src/osmRelation.js
@@ -0,0 +1,62 @@
+/* -*- 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, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * Author: Marcus Lundblad <ml update uu se>
+ */
+
+const Lang = imports.lang;
+const OSMObject = imports.osmObject;
+
+const OSMRelation = new Lang.Class({
+    Name: 'OSMRelation',
+    Extends: OSMObject.OSMObject,
+
+    const Member = new Lang.Class({
+       Name: 'Element',
+
+       _init: function(params) {
+           this._type = params.type;
+           this._role = params.role;
+           this._ref = params.ref;
+       },
+
+       get type() {
+           return this._type;
+       },
+
+       get role() {
+           return this._role;
+       },
+
+       get ref() {
+           return this._ref;
+       }
+    }),
+
+    _init: function(params) {
+       this._members = params.members;
+       
+       this.parent(params);
+    },
+
+    get members() {
+       return this._members;
+    }
+    
+});
diff --git a/src/osmWay.js b/src/osmWay.js
new file mode 100644
index 0000000..04ac482
--- /dev/null
+++ b/src/osmWay.js
@@ -0,0 +1,39 @@
+/* -*- 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, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * Author: Marcus Lundblad <ml update uu se>
+ */
+
+const Lang = imports.lang;
+const OSMObject = imports.osmObject;
+
+const OSMWay = new Lang.Class({
+    Name: 'OSMWay',
+    Extends: OSMObject.OSMObject,
+
+    _init: function(params) {
+       this._nodeRefs = params.nodeRefs;
+
+       this.parent(params);
+    },
+
+    get nodeRefs() {
+       return this._nodeRefs;
+    }
+})


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