zkv1000 / Nasal / maps / tiles.nas /
Newer Older
118 lines | 4.882kb
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

            
poweroff improved
Sébastien MARQUE authored on 2020-05-16
37
    off: func {
38
        me.group.setVisible(0);
39
        me.group.removeAllChildren();
40
        me.group.del();
41
    },
42

            
separates maps code
Sébastien MARQUE authored on 2017-05-11
43
    setVisible : func (v) {
44
        if (v != me.visibility) {
45
            me.visibility = v;
46
            me.group.setVisible(v);
47
        }
48
    },
49

            
50
# initialize the map by setting up a grid of raster images
51
    initialize_grid : func {
52
        for(var x = 0; x < me.num_tiles[0]; x += 1) {
53
            me.tiles[x] = setsize([], me.num_tiles[1]);
54
            for(var y = 0; y < me.num_tiles[1]; y += 1)
55
                me.tiles[x][y] = me.group.createChild('image', 'tile ' ~ x ~ ',' ~ y);
56
        }
57
    },
58

            
59
# this is the callback that will be regularly called by the timer to update the map
60
    update : func {
61
         if (! me.visibility)
62
             return;
63

            
64
        var n = math.pow(2, me.device.data.zoom);
65
        var offset = [
66
            n * ((data.lon + 180) / 360) - me.center_tile_offset[0],
67
            (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]
68
        ];
69
        var tile_index = [int(offset[0]), int(offset[1])];
70

            
71
        var ox = tile_index[0] - offset[0];
72
        var oy = tile_index[1] - offset[1];
73

            
74
        for (var x = 0; x < me.num_tiles[0]; x += 1)
75
            for(var y = 0; y < me.num_tiles[1]; y += 1)
76
                me.tiles[x][y]
77
                    .setTranslation(
78
                        int((ox + x) * me.tile_size + 0.5),
79
                        int((oy + y) * me.tile_size + 0.5));
80

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

            
96
                    (func {
97
                        var img_path = me.makePath(pos);
printlog deprecated + consta...
Sébastien MARQUE authored on 2020-04-24
98
                        logprint(LOG_DEBUG, 'img_path: ', img_path);
separates maps code
Sébastien MARQUE authored on 2017-05-11
99
                        var tile = me.tiles[x][y];
100

            
101
                        if (io.stat(img_path) == nil) { # image not found, save in $FG_HOME
102
                            var img_url = me.makeUrl(pos);
printlog deprecated + consta...
Sébastien MARQUE authored on 2020-04-24
103
                            logprint(LOG_DEBUG, 'requesting ' ~ img_url);
separates maps code
Sébastien MARQUE authored on 2017-05-11
104
                            http.save(img_url, img_path)
printlog deprecated + consta...
Sébastien MARQUE authored on 2020-04-24
105
                                .done(func {logprint(LOG_INFO, 'received image ' ~ img_path); tile.set("src", img_path);})
106
                                .fail(func (r) logprint(LOG_WARN, 'Failed to get image ' ~ img_path ~ ' ' ~ r.status ~ ': ' ~ r.reason));
separates maps code
Sébastien MARQUE authored on 2017-05-11
107
                        }
108
                        else { # cached image found, reusing
printlog deprecated + consta...
Sébastien MARQUE authored on 2020-04-24
109
                            logprint(LOG_DEBUG, 'loading ' ~ img_path);
separates maps code
Sébastien MARQUE authored on 2017-05-11
110
                            tile.set("src", img_path);
111
                        }
112
                    })();
113
                }
114
            me.last_tile = tile_index;
115
            me.last_type = data['tiles-type'];
116
        }
117
    },
118
};