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

iTowns / itowns / 11292497093

11 Oct 2024 12:39PM UTC coverage: 86.935% (-0.001%) from 86.936%
11292497093

Pull #2436

github

web-flow
Merge 54c424351 into cfb9d0f51
Pull Request #2436: refacto: migrate Crs to typescript

2804 of 3720 branches covered (75.38%)

Branch coverage included in aggregate %.

182 of 187 new or added lines in 20 files covered. (97.33%)

69 existing lines in 11 files now uncovered.

24377 of 27546 relevant lines covered (88.5%)

1022.73 hits per line

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

97.63
/src/Source/TMSSource.js
1
import Source from 'Source/Source';
1✔
2
import URLBuilder from 'Provider/URLBuilder';
1✔
3
import Extent from 'Core/Geographic/Extent';
1✔
4
import Tile from 'Core/Tile/Tile';
1✔
5
import { globalExtentTMS } from 'Core/Tile/TileGrid';
1✔
6
import CRS from 'Core/Geographic/Crs';
1✔
7

1✔
8
const _tile = new Tile(CRS.tms_4326, 0, 0, 0);
1✔
9

1✔
10
/**
1✔
11
 * An object defining the source of resources to get from a
1✔
12
 * [TMS](https://wiki.osgeo.org/wiki/Tile_Map_Service_Specification) server.
1✔
13
 * It inherits from {@link Source}.
1✔
14
 *
1✔
15
 * @extends Source
1✔
16
 *
1✔
17
 * @property {boolean} isTMSSource - Used to checkout whether this source is a
1✔
18
 * TMSSource. Default is true. You should not change this, as it is used
1✔
19
 * internally for optimisation.
1✔
20
 * @property {boolean} isInverted - The isInverted property is to be set to the
1✔
21
 * correct value, true or false (default being false) if the computation of the
1✔
22
 * coordinates needs to be inverted to match the same scheme as OSM, Google Maps
1✔
23
 * or other system. See [this link](
1✔
24
 * https://alastaira.wordpress.com/2011/07/06/converting-tms-tile-coordinates-to-googlebingosm-tile-coordinates/)
1✔
25
 * for more information.
1✔
26
 * @property {Object} tileMatrixSetLimits - it describes the available tile for this layer
1✔
27
 * @property {Object} extentSetlimits - these are the extents of the set of identical zoom tiles.
1✔
28
 * @property {Object} zoom - Object containing the minimum and maximum values of
1✔
29
 * the level, to zoom in the source.
1✔
30
 * @property {number} zoom.min - The minimum level of the source. Default value
1✔
31
 * is 0.
1✔
32
 * @property {number} zoom.max - The maximum level of the source. Default value
1✔
33
 * is 20.
1✔
34
 * @property {function} tileMatrixCallback - a method that create a TileMatrix
1✔
35
 * identifier from the zoom level. For example, if set to `(zoomLevel) => 'EPSG:4326:' + zoomLevel`,
1✔
36
 * the TileMatrix that will be fetched at zoom level 5 will be the one with identifier `EPSG:4326:5`.
1✔
37
 * By default, the method returns the input zoom level.
1✔
38
 *
1✔
39
 * @example <caption><b>Source from OpenStreetMap server :</b></caption>
1✔
40
 * // Create the source
1✔
41
 * const tmsSource = new itowns.TMSSource({
1✔
42
 *     format: 'image/png',
1✔
43
 *     url: 'http://osm.io/styles/${z}/${x}/${y}.png',
1✔
44
 *     attribution: {
1✔
45
 *         name: 'OpenStreetMap',
1✔
46
 *         url: 'http://www.openstreetmap.org/',
1✔
47
 *     },
1✔
48
 *     crs: 'EPSG:3857',
1✔
49
 * });
1✔
50
 *
1✔
51
 * // Create the layer
1✔
52
 * const colorLayer = new itowns.ColorLayer('OPENSM', {
1✔
53
 *     source: tmsSource,
1✔
54
 * });
1✔
55
 *
1✔
56
 * // Add the layer
1✔
57
 * view.addLayer(colorLayer);
1✔
58
 *
1✔
59
 * @example <caption><b>Source from Mapbox server :</b></caption>
1✔
60
 * // Create the source
1✔
61
 * const orthoSource = new itowns.TMSSource({
1✔
62
 *     url: 'https://api.mapbox.com/v4/mapbox.satellite/${z}/${x}/${y}.jpg?access_token=' + accessToken,
1✔
63
 *     crs: 'EPSG:3857',
1✔
64
 * };
1✔
65
 *
1✔
66
 * // Create the layer
1✔
67
 * const imageryLayer = new itowns.ColorLayer("Ortho", {
1✔
68
 *     source: orthoSource,
1✔
69
 * };
1✔
70
 *
1✔
71
 * // Add the layer to the view
1✔
72
 * view.addLayer(imageryLayer);
1✔
73
 */
1✔
74
class TMSSource extends Source {
1✔
75
    /**
1✔
76
     * @param {Object} source - An object that can contain all properties of a
1✔
77
     * TMSSource and {@link Source}. Only `url` is mandatory.
1✔
78
     */
1✔
79
    constructor(source) {
1✔
80
        source.format = source.format || 'image/png';
19✔
81

19✔
82
        super(source);
19✔
83

19✔
84
        if (!source.crs) {
19!
85
            throw new Error('New TMSSource/WMTSSource: crs is required');
×
UNCOV
86
        }
×
87

19✔
88
        this.isTMSSource = true;
19✔
89

19✔
90
        if (!source.extent) {
19✔
91
            // default to the global extent
17✔
92
            this.extent = globalExtentTMS.get(source.crs);
17✔
93
        }
17✔
94

19✔
95
        this.zoom = source.zoom;
19✔
96

19✔
97
        this.isInverted = source.isInverted || false;
19✔
98
        this.crs = CRS.formatToTms(source.crs);
19✔
99
        this.tileMatrixSetLimits = source.tileMatrixSetLimits;
19✔
100
        this.extentSetlimits = {};
19✔
101
        this.tileMatrixCallback = source.tileMatrixCallback || (zoomLevel => zoomLevel);
19✔
102

19✔
103
        if (!this.zoom) {
19✔
104
            if (this.tileMatrixSetLimits) {
16✔
105
                const arrayLimits = Object.keys(this.tileMatrixSetLimits);
3✔
106
                const size = arrayLimits.length;
3✔
107
                const maxZoom = Number(arrayLimits[size - 1]);
3✔
108
                const minZoom = maxZoom - size + 1;
3✔
109

3✔
110
                this.zoom = {
3✔
111
                    min: minZoom,
3✔
112
                    max: maxZoom,
3✔
113
                };
3✔
114
            } else {
16✔
115
                this.zoom = { min: 0, max: Infinity };
13✔
116
            }
13✔
117
        }
16✔
118
    }
19✔
119

1✔
120
    urlFromExtent(tile) {
1✔
121
        return URLBuilder.xyz(tile, this);
8✔
122
    }
8✔
123

1✔
124
    onLayerAdded(options) {
1✔
125
        super.onLayerAdded(options);
7✔
126
        // Build extents of the set of identical zoom tiles.
7✔
127
        const parent = options.out.parent;
7✔
128
        // The extents crs is chosen to facilitate in raster tile process.
7✔
129
        const crs = parent ? parent.extent.crs : options.out.crs;
7✔
130
        if (this.tileMatrixSetLimits && !this.extentSetlimits[crs]) {
7✔
131
            this.extentSetlimits[crs] = {};
2✔
132
            _tile.crs = this.crs;
2✔
133
            for (let i = this.zoom.max; i >= this.zoom.min; i--) {
2✔
134
                const tmsl = this.tileMatrixSetLimits[i];
7✔
135
                const { west, north } = _tile.set(i, tmsl.minTileRow, tmsl.minTileCol).toExtent(crs);
7✔
136
                const { east, south } = _tile.set(i, tmsl.maxTileRow, tmsl.maxTileCol).toExtent(crs);
7✔
137
                this.extentSetlimits[crs][i] = new Extent(crs, west, east, south, north);
7✔
138
            }
7✔
139
        }
2✔
140
    }
7✔
141

1✔
142
    extentInsideLimit(extent, zoom) {
1✔
143
        // This layer provides data starting at level = layer.source.zoom.min
11✔
144
        // (the zoom.max property is used when building the url to make
11✔
145
        //  sure we don't use invalid levels)
11✔
146
        return zoom >= this.zoom.min && zoom <= this.zoom.max &&
11✔
147
                (this.extentSetlimits[extent.crs] == undefined || this.extentSetlimits[extent.crs][zoom].intersectsExtent(extent));
11!
148
    }
11✔
149
}
1✔
150

1✔
151
export default TMSSource;
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

© 2026 Coveralls, Inc