• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

iTowns / itowns / 6640358485

25 Oct 2023 12:28PM UTC coverage: 76.601% (+0.3%) from 76.291%
6640358485

push

github

ftoromanoff
refactor(Style): homogenize gestion fill.pattern between all existing

4002 of 5946 branches covered (0.0%)

Branch coverage included in aggregate %.

155 of 155 new or added lines in 6 files covered. (100.0%)

7878 of 9563 relevant lines covered (82.38%)

1600.43 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

84.68
/src/Source/VectorTilesSource.js
1
import { featureFilter } from '@mapbox/mapbox-gl-style-spec';
1✔
2
import Style from 'Core/Style';
1✔
3
import TMSSource from 'Source/TMSSource';
1✔
4
import Fetcher from 'Provider/Fetcher';
1✔
5
import urlParser from 'Parser/MapBoxUrlParser';
41✔
6

7
function toTMSUrl(url) {
8
    return url.replace(/\{/g, '${');
2✔
9
}
10

11
/**
12
 * @classdesc
13
 * VectorTilesSource are object containing informations on how to fetch vector
14
 * tiles resources.
15
 *
16
 * @property {function} filter - function to filter vector tiles layers, the
17
 * parameter function is a layer.
18
 * @property {boolean} [symbolToCircle=false] - If true, all symbols from a tile
19
 * will be considered as circle, and render as circles.
20
 */
21
class VectorTilesSource extends TMSSource {
2✔
22
    /**
23
     * @param {Object} source - An object that can contain all properties of a
24
     * VectorTilesSource and {@link Source}.
25
     * @param {string|Object} source.style - The URL of the JSON style, of the
26
     * JSON style directly.
27
     * @param {string} [source.sprite] - The base URL to load informations about
28
     * the sprite of the style. If this is set, it overrides the `sprite` value
29
     * of the `source.style`. A style's sprite property supplies a URL template
30
     * for loading small images.
31
     * ```js
32
     * {
33
     *      sprite: 'http//:xxxxx/maps/sprites/'
34
     * }
35
     * ```
36
     * A valid sprite source must supply two types of files:
37
     * * An index file, which is a JSON document containing a description of each image contained in the sprite.
38
     * * Image files, which are PNG images containing the sprite data.
39
     *
40
     * For more specification : [the Mapbox sprite Specification](https://docs.mapbox.com/mapbox-gl-js/style-spec/sprite/)
41
     *
42
     * @param {string} [source.url] - The base URL to load the tiles. If no url
43
     * is specified, it reads it from the loaded style. Read [the Mapbox Style
44
     * Specification](https://docs.mapbox.com/mapbox-gl-js/style-spec/sources/)
45
     * for more informations.
46
     * @param {string} [source.accessToken] - Mapbox access token
47
     * @constructor
48
     */
49
    constructor(source) {
7✔
50
        source.format = 'application/x-protobuf;type=mapbox-vector';
7✔
51
        source.crs = 'EPSG:3857';
7✔
52
        source.isInverted = true;
7✔
53
        source.url = source.url || '.';
7✔
54
        super(source);
7✔
55
        const ffilter = source.filter || (() => true);
8✔
56
        this.layers = {};
7✔
57
        this.styles = {};
7✔
58
        let promise;
59
        this.isVectorTileSource = true;
7✔
60

61
        this.accessToken = source.accessToken;
7✔
62

63
        if (source.style) {
7✔
64
            if (typeof source.style == 'string') {
6✔
65
                const styleUrl = urlParser.normalizeStyleURL(source.style, this.accessToken);
1✔
66
                promise = Fetcher.json(styleUrl, this.networkOptions);
1✔
67
            } else {
68
                promise = Promise.resolve(source.style);
5✔
69
            }
70
        } else {
71
            throw new Error('New VectorTilesSource: style is required');
1✔
72
        }
73

74
        this.whenReady = promise.then((style) => {
6✔
75
            this.jsonStyle = style;
6✔
76
            const baseurl = source.sprite || style.sprite;
6✔
77
            if (baseurl) {
6✔
78
                const spriteUrl = urlParser.normalizeSpriteURL(baseurl, '', '.json', this.accessToken);
1✔
79
                return Fetcher.json(spriteUrl, this.networkOptions).then((sprites) => {
1✔
80
                    this.sprites = sprites;
1✔
81
                    const imgUrl = urlParser.normalizeSpriteURL(baseurl, '', '.png', this.accessToken);
1✔
82
                    this.sprites.source = imgUrl;
1✔
83
                    return style;
1✔
84
                });
85
            }
86

87
            return style;
5✔
88
        }).then((style) => {
89
            const s = Object.keys(style.sources)[0];
6✔
90
            const os = style.sources[s];
6✔
91

92
            style.layers.forEach((layer, order) => {
6✔
93
                layer.sourceUid = this.uid;
10✔
94
                if (layer.type === 'background') {
10✔
95
                    this.backgroundLayer = layer;
2✔
96
                } else if (ffilter(layer)) {
8!
97
                    const style = new Style().setFromVectorTileLayer(layer, this.sprites, order, this.symbolToCircle);
8✔
98
                    style.zoom.min = layer.minzoom || 0;
8✔
99
                    style.zoom.max = layer.maxzoom || 24;
8✔
100
                    this.styles[layer.id] = style;
8✔
101

102
                    if (!this.layers[layer['source-layer']]) {
8✔
103
                        this.layers[layer['source-layer']] = [];
4✔
104
                    }
105
                    this.layers[layer['source-layer']].push({
8✔
106
                        id: layer.id,
107
                        order,
108
                        filterExpression: featureFilter(layer.filter),
109
                        zoom: {
110
                            min: layer.minzoom || 0,
13✔
111
                            max: layer.maxzoom || 24,
15✔
112
                        },
113
                    });
114
                }
115
            });
116

117
            if (this.url == '.') {
6✔
118
                if (os.url) {
2✔
119
                    const urlSource = urlParser.normalizeSourceURL(os.url, this.accessToken);
1✔
120
                    return Fetcher.json(urlSource, this.networkOptions).then((tileJSON) => {
1✔
121
                        if (tileJSON.tiles[0]) {
1!
122
                            this.url = toTMSUrl(tileJSON.tiles[0]);
1✔
123
                        }
124
                    });
125
                } else if (os.tiles[0]) {
1!
126
                    this.url = toTMSUrl(os.tiles[0]);
1✔
127
                }
128
            }
129
        });
6✔
130
    }
1✔
131

132
    onLayerAdded(options) {
×
133
        super.onLayerAdded(options);
×
134
        if (options.out.style) {
×
135
            if (options.out.isFeatureGeometryLayer && options.out.accurate) {
×
136
                console.warn('With VectorTilesSource and FeatureGeometryLayer, the accurate option is always false');
×
137
                options.out.accurate = false;
×
138
            }
139
            const keys = Object.keys(this.styles);
×
140

141
            keys.forEach((k) => { this.styles[k].parent = options.out.style; });
×
142
        }
143
    }
1✔
144
}
145

146
export default VectorTilesSource;
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc