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

iTowns / itowns / 10902507646

17 Sep 2024 11:56AM UTC coverage: 86.931% (-0.03%) from 86.964%
10902507646

push

github

Desplandis
release v2.44.2

2791 of 3694 branches covered (75.55%)

Branch coverage included in aggregate %.

24241 of 27402 relevant lines covered (88.46%)

1027.5 hits per line

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

98.2
/src/Source/TMSSource.js
1
import Source from 'Source/Source';
1✔
2
import URLBuilder from 'Provider/URLBuilder';
1✔
3
import Extent, { globalExtentTMS } from 'Core/Geographic/Extent';
1✔
4
import CRS from 'Core/Geographic/Crs';
1✔
5

1✔
6
const extent = new Extent(CRS.tms_4326, 0, 0, 0);
1✔
7

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

19✔
80
        super(source);
19✔
81

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

19✔
86
        this.isTMSSource = true;
19✔
87

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

19✔
93
        this.zoom = source.zoom;
19✔
94

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

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

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

1✔
118
    urlFromExtent(extent) {
1✔
119
        return URLBuilder.xyz(extent, this);
8✔
120
    }
8✔
121

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

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

1✔
149
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