zkv1000 / Nasal / maps / tiles.nas /
Newer Older
112 lines | 4.754kb
separates maps code
Sébastien MARQUE authored on 2017-05-11
1
var MapTiles = {
2
# displays maps background from web tiles
3
# code from http://wiki.flightgear.org/Canvas_Snippets#A_simple_tile_map
4
    new : func (device, group) {
5
        var m = { parents: [MapTiles] };
6
        m.device = device;
7
        m.display = m.device.display.display;
8
        m.tile_size = 256;
9
        m.maps_base = getprop("/sim/fg-home") ~ '/cache/maps';
add template option for tile...
Sébastien MARQUE authored on 2017-05-11
10
        m.makeUrl = string.compileTemplate(data['tiles-template']);
add options for online tiles
Sébastien MARQUE authored on 2017-05-14
11
        m.makePath = string.compileTemplate(m.maps_base ~ '/{server}/{type}/{z}/{x}/{y}.{format}');
separates maps code
Sébastien MARQUE authored on 2017-05-11
12
        m.num_tiles = [
13
            math.ceil( m.device.data.mapsize[0] / m.tile_size ) + 1,
14
            math.ceil( m.device.data.mapsize[1] / m.tile_size ) + 1
15
        ];
16
        m.center_tile_offset = [
17
            (m.num_tiles[0] - 1) / 2,
18
            (m.num_tiles[1] - 1) / 2
19
        ];
20
        m.visibility = m.device.role == 'MFD';
21
        m.group = group.createChild('group', 'tiles')
22
            .setTranslation(
23
                    m.device.role == 'MFD' ? (m.device.data.mapview[0] - m.device.data.mapsize[0] + m.device.data.mapclip.left)/2 : -520,
24
                    m.device.role == 'MFD' ? -250 : -45)
25
            .setVisible(m.visibility);
26
        m.tiles = setsize([], m.num_tiles[0]);
27
        m.last_tile = [-1,-1];
28
        m.last_type = data['tiles-type'];
29
        m.initialize_grid();
improve the selection of dis...
Sébastien MARQUE authored on 2017-12-30
30
        if (m.device.role == 'PFD')
31
            m.device.softkeys.colored.INSETTERRAIN = 1;
32
        if (m.device.role == 'MFD')
33
            m.device.softkeys.colored.MAPTERRAIN = 1;
separates maps code
Sébastien MARQUE authored on 2017-05-11
34
        return m;
35
    },
36

            
37
    setVisible : func (v) {
38
        if (v != me.visibility) {
39
            me.visibility = v;
40
            me.group.setVisible(v);
41
        }
42
    },
43

            
44
# initialize the map by setting up a grid of raster images
45
    initialize_grid : func {
46
        for(var x = 0; x < me.num_tiles[0]; x += 1) {
47
            me.tiles[x] = setsize([], me.num_tiles[1]);
48
            for(var y = 0; y < me.num_tiles[1]; y += 1)
49
                me.tiles[x][y] = me.group.createChild('image', 'tile ' ~ x ~ ',' ~ y);
50
        }
51
    },
52

            
53
# this is the callback that will be regularly called by the timer to update the map
54
    update : func {
55
         if (! me.visibility)
56
             return;
57

            
58
        var n = math.pow(2, me.device.data.zoom);
59
        var offset = [
60
            n * ((data.lon + 180) / 360) - me.center_tile_offset[0],
61
            (1 - math.ln(math.tan(data.lat * math.pi/180) + 1 / math.cos(data.lat * math.pi/180)) / math.pi) / 2 * n - me.center_tile_offset[1]
62
        ];
63
        var tile_index = [int(offset[0]), int(offset[1])];
64

            
65
        var ox = tile_index[0] - offset[0];
66
        var oy = tile_index[1] - offset[1];
67

            
68
        for (var x = 0; x < me.num_tiles[0]; x += 1)
69
            for(var y = 0; y < me.num_tiles[1]; y += 1)
70
                me.tiles[x][y]
71
                    .setTranslation(
72
                        int((ox + x) * me.tile_size + 0.5),
73
                        int((oy + y) * me.tile_size + 0.5));
74

            
75
        if (tile_index[0] != me.last_tile[0]
76
         or tile_index[1] != me.last_tile[1]
77
         or data['tiles-type'] != me.last_type) {
78
            for(var x = 0; x < me.num_tiles[0]; x += 1)
79
                for(var y = 0; y < me.num_tiles[1]; y += 1) {
80
                    var pos = {
81
                        z: me.device.data.zoom,
82
                        x: int(offset[0] + x),
83
                        y: int(offset[1] + y),
84
                        type: data['tiles-type'],
85
                        server : data['tiles-server'],
add options for online tiles
Sébastien MARQUE authored on 2017-05-14
86
                        format: data['tiles-format'],
separates maps code
Sébastien MARQUE authored on 2017-05-11
87
                        apikey: data['tiles-apikey'],
88
                    };
89

            
90
                    (func {
91
                        var img_path = me.makePath(pos);
92
                        printlog('debug', 'img_path: ', img_path);
93
                        var tile = me.tiles[x][y];
94

            
95
                        if (io.stat(img_path) == nil) { # image not found, save in $FG_HOME
96
                            var img_url = me.makeUrl(pos);
97
                            printlog('debug', 'requesting ' ~ img_url);
98
                            http.save(img_url, img_path)
99
                                .done(func {printlog('info', 'received image ' ~ img_path); tile.set("src", img_path);})
100
                                .fail(func (r) printlog('warn', 'Failed to get image ' ~ img_path ~ ' ' ~ r.status ~ ': ' ~ r.reason));
101
                        }
102
                        else { # cached image found, reusing
103
                            printlog('debug', 'loading ' ~ img_path);
104
                            tile.set("src", img_path);
105
                        }
106
                    })();
107
                }
108
            me.last_tile = tile_index;
109
            me.last_type = data['tiles-type'];
110
        }
111
    },
112
};