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

geosolutions-it / MapStore2 / 15022192473

14 May 2025 01:37PM UTC coverage: 76.899% (-0.09%) from 76.993%
15022192473

Pull #10515

github

web-flow
Merge 76310647b into d76ffd67a
Pull Request #10515: #10514 - FeatureEditor filter by geometric area

30971 of 48268 branches covered (64.16%)

27 of 42 new or added lines in 6 files covered. (64.29%)

532 existing lines in 55 files now uncovered.

38582 of 50172 relevant lines covered (76.9%)

35.92 hits per line

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

80.28
/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 assign from 'object-assign';
18
import { isEqual, isObject, isArray, slice, get, head} from 'lodash';
19
import urlParser from 'url';
20
import { isVectorFormat } from '../../../../utils/VectorTileUtils';
21
import { getCredentials } from '../../../../utils/SecurityUtils';
22

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

36
function WMTSProxy(proxy) {
UNCOV
37
    this.proxy = proxy;
×
38
}
39

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

47

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

53
function NoProxy() {
54
}
55

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

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

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

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

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

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

156
const createLayer = options => {
1✔
157
    let layer;
158
    const cesiumOptions = wmtsToCesiumOptions(options);
22✔
159
    layer = new Cesium.WebMapTileServiceImageryProvider(cesiumOptions);
22✔
160
    const orig = layer.requestImage;
22✔
161
    layer.requestImage = (x, y, level) => cesiumOptions.isValid(x, y, level) ? orig.bind(layer)( x, y, level) : new Promise( () => undefined);
22✔
162
    layer.updateParams = (params) => {
22✔
UNCOV
163
        const newOptions = assign({}, options, {
×
164
            params: assign({}, options.params || {}, params)
×
165
        });
UNCOV
166
        return createLayer(newOptions);
×
167
    };
168
    return layer;
22✔
169
};
170

171
const updateLayer = (layer, newOptions, oldOptions) => {
1✔
172
    if (newOptions.securityToken !== oldOptions.securityToken
17✔
173
    || oldOptions.format !== newOptions.format
174
    || !isEqual(oldOptions.security, newOptions.security)
175
    || oldOptions.credits !== newOptions.credits || newOptions.forceProxy !== oldOptions.forceProxy) {
176
        return createLayer(newOptions);
15✔
177
    }
178
    return null;
2✔
179
};
180

181
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