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

geosolutions-it / MapStore2 / 14534587011

18 Apr 2025 11:41AM UTC coverage: 76.977% (-0.02%) from 76.993%
14534587011

Pull #11037

github

web-flow
Merge f22d700f6 into 48d6a1a15
Pull Request #11037: Remove object assign pollyfills

30792 of 47937 branches covered (64.23%)

446 of 556 new or added lines in 94 files covered. (80.22%)

8 existing lines in 4 files now uncovered.

38277 of 49725 relevant lines covered (76.98%)

36.07 hits per line

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

82.35
/web/client/components/map/cesium/plugins/WMTSLayer.js
1
/**
2
 * Copyright 2015, GeoSolutions Sas.
3
 * All rights reserved.
4
 *
5
 * This source code is licensed under the BSD-style license found in the
6
 * LICENSE file in the root directory of this source tree.
7
 */
8

9
import Layers from '../../../../utils/cesium/Layers';
10
import * as Cesium from 'cesium';
11
import ConfigUtils from '../../../../utils/ConfigUtils';
12
import {
13
    getProxyUrl
14
} from '../../../../utils/ProxyUtils';
15
import * as WMTSUtils from '../../../../utils/WMTSUtils';
16
import { creditsToAttribution, getAuthenticationParam, getURLs } from '../../../../utils/LayersUtils';
17
import { isObject, isArray, slice, get, head} from 'lodash';
18
import urlParser from 'url';
19
import { isVectorFormat } from '../../../../utils/VectorTileUtils';
20

21
function splitUrl(originalUrl) {
22
    let url = originalUrl;
24✔
23
    let queryString = "";
24✔
24
    if (originalUrl.indexOf('?') !== -1) {
24✔
25
        url = originalUrl.substring(0, originalUrl.indexOf('?') + 1);
14✔
26
        if (originalUrl.indexOf('%') !== -1) {
14!
27
            url = decodeURIComponent(url);
14✔
28
        }
29
        queryString = originalUrl.substring(originalUrl.indexOf('?') + 1);
14✔
30
    }
31
    return {url, queryString};
24✔
32
}
33

34
function WMTSProxy(proxy) {
35
    this.proxy = proxy;
×
36
}
37

38
const isValidTile = (tileMatrixSet) => (x, y, level) =>
22✔
39
    tileMatrixSet && tileMatrixSet[level] && !tileMatrixSet[level].ranges ||
7✔
40
    (x <= parseInt(get(tileMatrixSet[level], "ranges.cols.max"), 10) &&
41
    x >= parseInt(get(tileMatrixSet[level], "ranges.cols.min"), 10) &&
42
    y <= parseInt(get(tileMatrixSet[level], "ranges.rows.max"), 10) &&
43
    y >= parseInt(get(tileMatrixSet[level], "ranges.rows.min"), 10));
44

45

46
WMTSProxy.prototype.getURL = function(resource) {
1✔
47
    let {url, queryString} = splitUrl(resource);
×
48
    return getProxyUrl() + encodeURIComponent(url + queryString);
×
49
};
50

51
function NoProxy() {
52
}
53

54
NoProxy.prototype.getURL = function(resource) {
1✔
55
    let {url, queryString} = splitUrl(resource);
24✔
56
    return url + queryString;
24✔
57
};
58
function getMatrixIds(matrix = [], setId) {
×
59
    return ((isObject(matrix) && matrix[setId]) || isArray(matrix) && matrix || []).map((el) => el.identifier);
42!
60
}
61

62
function getDefaultMatrixId(options) {
63
    let matrixIds = new Array(30);
×
64
    for (let z = 0; z < 30; ++z) {
×
65
        // generate matrixIds arrays for this WMTS
66
        matrixIds[z] = options.tileMatrixPrefix + z;
×
67
    }
68
    return matrixIds;
×
69
}
70

71
const limitMatrix = (matrix, len) => {
1✔
72
    if (matrix.length > len) {
42!
73
        return slice(matrix, 0, len);
×
74
    }
75
    if (matrix.length < len) {
42!
76
        return matrix.concat(new Array(len - matrix.length));
×
77
    }
78
    return matrix;
42✔
79
};
80

81
const getTilingSchema = (srs) => {
1✔
82
    if (srs.indexOf("EPSG:4326") >= 0) {
22✔
83
        return new Cesium.GeographicTilingScheme();
2✔
84
    } else if (srs.indexOf("EPSG:3857") >= 0) {
20!
85
        return new Cesium.WebMercatorTilingScheme();
20✔
86
    }
87
    return null;
×
88
};
89

90
const getMatrixOptions = (options, srs) => {
1✔
91
    const tileMatrixSet = WMTSUtils.getTileMatrixSet(options.tileMatrixSet, srs, options.allowedSRS, options.matrixIds);
42✔
92
    const matrixIds = limitMatrix(options.matrixIds && getMatrixIds(options.matrixIds, tileMatrixSet) || getDefaultMatrixId(options));
42!
93
    return {tileMatrixSet, matrixIds};
42✔
94
};
95

96
function wmtsToCesiumOptions(_options) {
97
    const options = WMTSUtils.parseTileMatrixSetOption(_options);
22✔
98
    let srs = 'EPSG:4326';
22✔
99
    let { tileMatrixSet: tileMatrixSetID, matrixIds} = getMatrixOptions(options, srs);
22✔
100
    if (matrixIds.length === 0) {
22✔
101
        srs = 'EPSG:3857';
20✔
102
        const matrixOptions = getMatrixOptions(options, srs);
20✔
103
        tileMatrixSetID = matrixOptions.tileMatrixSet;
20✔
104
        matrixIds = matrixOptions.matrixIds;
20✔
105
    }
106
    // NOTE: can we use opacity to manage visibility?
107
    // var opacity = options.opacity !== undefined ? options.opacity : 1;
108
    let proxyUrl = ConfigUtils.getProxyUrl({});
22✔
109
    let proxy;
110
    if (proxyUrl) {
22!
111
        proxy = options.forceProxy;
22✔
112
    }
113
    const isValid = isValidTile(options.matrixIds && options.matrixIds[tileMatrixSetID]);
22✔
114
    const queryParametersString = urlParser.format({ query: {...getAuthenticationParam(options)}});
22✔
115
    const cr = options.credits;
22✔
116
    const credit = cr ? new Cesium.Credit(creditsToAttribution(cr)) : '';
22✔
117
    return Object.assign({
22✔
118
        // TODO: multi-domain support, if use {s} switches to RESTFul mode
119
        url: new Cesium.Resource({
120
            url: head(getURLs(isArray(options.url) ? options.url : [options.url], queryParametersString)),
22!
121
            proxy: proxy && new WMTSProxy(proxy) || new NoProxy()
44!
122
        }),
123
        // set image format to png if vector to avoid errors while switching between map type
124
        format: isVectorFormat(options.format) && 'image/png' || options.format || 'image/png',
44!
125
        isValid,
126
        // tileDiscardPolicy: {
127
        //    isReady: () => true,
128
        //    shouldDiscardImage: ({x, y, level}) => !isValid(x, y, level)
129
        // }, // not supported yet
130
        credit,
131
        layer: options.name,
132
        style: options.style || "",
44✔
133
        tileMatrixLabels: matrixIds,
134
        tilingScheme: getTilingSchema(srs, options.matrixIds[tileMatrixSetID]),
135
        enablePickFeatures: false,
136
        tileWidth: options.tileWidth || options.tileSize || 256,
66✔
137
        tileHeight: options.tileHeight || options.tileSize || 256,
66✔
138
        tileMatrixSetID: tileMatrixSetID,
139
        maximumLevel: 30,
140
        parameters: {...getAuthenticationParam(options)}
141
    });
142
}
143

144
const createLayer = options => {
1✔
145
    let layer;
146
    const cesiumOptions = wmtsToCesiumOptions(options);
22✔
147
    layer = new Cesium.WebMapTileServiceImageryProvider(cesiumOptions);
22✔
148
    const orig = layer.requestImage;
22✔
149
    layer.requestImage = (x, y, level) => cesiumOptions.isValid(x, y, level) ? orig.bind(layer)( x, y, level) : new Promise( () => undefined);
22✔
150
    layer.updateParams = (params) => {
22✔
NEW
151
        const newOptions = Object.assign({}, options, {
×
152
            params: Object.assign({}, options.params || {}, params)
×
153
        });
154
        return createLayer(newOptions);
×
155
    };
156
    return layer;
22✔
157
};
158

159
const updateLayer = (layer, newOptions, oldOptions) => {
1✔
160
    if (newOptions.securityToken !== oldOptions.securityToken
17✔
161
    || oldOptions.format !== newOptions.format
162
    || oldOptions.credits !== newOptions.credits || newOptions.forceProxy !== oldOptions.forceProxy) {
163
        return createLayer(newOptions);
15✔
164
    }
165
    return null;
2✔
166
};
167

168
Layers.registerType('wmts', {create: createLayer, update: updateLayer});
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