summaryrefslogtreecommitdiff
path: root/src/mapSource.js
blob: d7b82c456998a634a0e4cbdf26659dbde3daea26 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/* -*- Mode: JS2; indent-tabs-mode: nil; js2-basic-offset: 4 -*- */
/* vim: set et ts=4 sw=4: */
/*
 * 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: Jonas Danielsson <jonas@threetimestwo.org>
 */

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;

let _tileService = null;
let _attributionImage = null;

const _TILE_SERVICE_URL = 'https://gis.gnome.org/services/v1/service.json';
const _DEFAULT_SERVICE_FILE = 'maps-service.json';

const _FILE_CACHE_SIZE_LIMIT = (10 * 1024 * 1024); /* 10Mb */
const _MEMORY_CACHE_SIZE_LIMIT = 100; /* number of tiles */

const AttributionLogo = new Lang.Class({
    Name: 'AttributionLogo',
    Extends: Gtk.Bin,
    _init: function() {
        this.parent({ margin_end: 6,
                      margin_bottom: 25,
                      halign: Gtk.Align.END,
                      valign: Gtk.Align.END });
        this.add(_attributionImage);
    }
});

function _updateAttributionImage(source) {
    if (!_attributionImage)
        _attributionImage = new Gtk.Image();

    if (!source.attribution_logo || source.attribution_logo === "") {
        _attributionImage.visible = false;
        return;
    }

    let data = GLib.base64_decode(source.attribution_logo);
    let stream = Gio.MemoryInputStream.new_from_bytes(GLib.Bytes.new(data));
    _attributionImage.pixbuf = GdkPixbuf.Pixbuf.new_from_stream(stream, null);
}

function _createDefaultService() {
    let filename = GLib.build_filenamev([pkg.pkgdatadir,
                                         _DEFAULT_SERVICE_FILE]);
    let data = Utils.readFile(filename);
    _tileService = JSON.parse(data).tiles;
    return _tileService;
}

function _getTileService() {
    if (_tileService)
        return _tileService;

    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;
        }
        _tileService = JSON.parse(lines).tiles;
        return _tileService;
    } catch(e) {
        Utils.debug(e);
        return _createDefaultService();
    }
}

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,
        renderer: new Champlain.ImageRenderer(),
        uri_format: source.uri_format
    });
    tileSource.max_conns = source.max_connections;
    return tileSource;
}

function _createCachedSource(source) {
    let tileSource = _createTileSource(source);
    _updateAttributionImage(source);

    let fileCache = new Champlain.FileCache({
        size_limit: _FILE_CACHE_SIZE_LIMIT,
        renderer: new Champlain.ImageRenderer()
    });

    let memoryCache = new Champlain.MemoryCache({
        size_limit: _MEMORY_CACHE_SIZE_LIMIT,
        renderer: new Champlain.ImageRenderer()
    });

    let errorSource = new Champlain.NullTileSource({
        renderer: new Champlain.ImageRenderer()
    });

    /*
     * When the a source in the chain fails to load a given tile
     * the next one in the chain tries instead. Until we get to the error
     * source.
     */
    let sourceChain = new Champlain.MapSourceChain();
    sourceChain.push(errorSource);
    sourceChain.push(tileSource);
    sourceChain.push(fileCache);
    sourceChain.push(memoryCache);

    return sourceChain;
}

function createAerialSource() {
    return _createCachedSource(_getTileService().aerial);
}

function createStreetSource() {
    return _createCachedSource(_getTileService().street);
}