[gnome-maps/wip/jonasdn/service: 1/2] wip: read service file in mapSource.js



commit cfabeb7b3891010f81890d60946dee6b3ba43d12
Author: Jonas Danielsson <jonas threetimestwo org>
Date:   Fri Aug 12 21:27:31 2016 +0200

    wip: read service file in mapSource.js

 src/mapSource.js |  101 ++++++++++++++++++++++++++++++++++++-----------------
 1 files changed, 68 insertions(+), 33 deletions(-)
---
diff --git a/src/mapSource.js b/src/mapSource.js
index 3a3ac50..25e9526 100644
--- a/src/mapSource.js
+++ b/src/mapSource.js
@@ -17,57 +17,80 @@
  * Author: Jonas Danielsson <jonas threetimestwo org>
  */
 
+const ByteArray = imports.byteArray;
 const Champlain = imports.gi.Champlain;
+const GdkPixbuf = imports.gi.GdkPixbuf;
+const Gio = imports.gi.Gio;
+const GLib = imports.gi.GLib;
 const Gtk = imports.gi.Gtk;
 const Lang = imports.lang;
+const Soup = imports.gi.Soup;
 
 const Utils = imports.utils;
 
-/*
- * These URIs are used by the libchamplain network tile source.
- * The #X#, #Y#, #Z# coords will get replaced with actual tile numbers.
- */
-const _AERIAL_URI = "https://gis.gnome.org/tiles/satellite/v1/#Z#/#X#/#Y#";;
-const _STREET_URI = "https://gis.gnome.org/tiles/street/v1/#Z#/#X#/#Y#";;
+let _tileService = null;
+let _attributionData = null;
 
-/* unique names are needed for file caching */
-const _AERIAL_NAME = "mapbox-satellite-v1";
-const _STREET_NAME = "mapbox-street-v1";
-
-const _TILE_SIZE = 256;
-const _MIN_ZOOM = 0;
-const _MAX_ZOOM = 19;
+const _TILE_SERVICE_URL = 'http://threetimestwo.org/service.json';
 
 const _FILE_CACHE_SIZE_LIMIT = (10 * 1024 * 1024); /* 10Mb */
 const _MEMORY_CACHE_SIZE_LIMIT = 100; /* number of tiles */
 
-function _createTileSource(uri, name) {
-    return new Champlain.NetworkTileSource(
-        { id: name,
-          name: name,
-          license: null,
-          license_uri: null,
-          min_zoom_level: _MIN_ZOOM,
-          max_zoom_level: _MAX_ZOOM,
-          tile_size: _TILE_SIZE,
-          projection: Champlain.MapProjection.MERCATOR,
-          renderer: new Champlain.ImageRenderer(),
-          uri_format: uri
-        });
-}
-
 const AttributionLogo = new Lang.Class({
     Name: 'AttributionLogo',
     Extends: Gtk.Image,
     Template: 'resource:///org/gnome/Maps/ui/attribution-logo.ui',
 
     _init: function() {
-        this.parent();
+         this.parent();
+
+        /*
+        let byteArray = ByteArray.fromString(GLib.base64_decode(_attributionData));
+        this.pixbuf = GdkPixbuf.Pixbuf.new_from_inline(byteArray, false);
+         */
     }
 });
 
-function _createCachedSource(uri, name) {
-    let tileSource = _createTileSource(uri, name);
+function _getTileService() {
+    let user_agent = 'gnome-maps/' + pkg.version;
+    let session = new Soup.Session({ user_agent : user_agent });
+    let msg = Soup.Message.new('GET', _TILE_SERVICE_URL);
+    try {
+        let stream = Gio.DataInputStream.new(session.send(msg, null));
+
+        let lines = "";
+        while(true) {
+            let [line, _] = stream.read_line_utf8(null);
+            if (line === null)
+                break;
+            lines += line;
+        }
+        return JSON.parse(lines);
+    } catch(e) {
+        Utils.debug(e);
+        return null;
+    }
+}
+
+function _createTileSource(source) {
+    let tileSource = new Champlain.NetworkTileSource({
+        id: source.id,
+        name: source.name,
+        license: source.license,
+        license_uri: source.license_uri,
+        min_zoom_level: source.min_zoom_level,
+        max_zoom_level: source.max_zoom_level,
+        tile_size: source.tile_size,
+        projection: Champlain.MapProjection.MERCATOR,
+        renderer: new Champlain.ImageRenderer(),
+        uri_format: source.uri
+    });
+    tileSource.max_conns = source.max_connections;
+    return tileSource;
+}
+
+function _createCachedSource(source) {
+    let tileSource = _createTileSource(source);
 
     let fileCache = new Champlain.FileCache({
         size_limit: _FILE_CACHE_SIZE_LIMIT,
@@ -98,9 +121,21 @@ function _createCachedSource(uri, name) {
 }
 
 function createAerialSource() {
-    return _createCachedSource(_AERIAL_URI, _AERIAL_NAME);
+    _tileService = _getTileService();
+    if (_tileService) {
+        _attributionData = _tileService.aerial.attribution_logo;
+        return _createCachedSource(_tileService.aerial);
+    } else {
+        return null;
+    }
 };
 
 function createStreetSource() {
-    return _createCachedSource(_STREET_URI, _STREET_NAME);
+    _tileService = _getTileService();
+    if (_tileService) {
+        _attributionData = _tileService.street.attribution_logo;
+        return _createCachedSource(_tileService.street);
+    } else {
+        return null;
+    }
 };


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