-
Notifications
You must be signed in to change notification settings - Fork 416
/
Copy pathWMSUtils.js
172 lines (155 loc) · 6.09 KB
/
WMSUtils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
* Copyright 2017, GeoSolutions Sas.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
import * as Cesium from 'cesium';
import { isArray } from 'lodash';
import { addAuthenticationToSLD, getAuthenticationHeaders } from "../SecurityUtils";
import { getProxyUrl } from "../ProxyUtils";
import ConfigUtils from "../ConfigUtils";
import { creditsToAttribution, getAuthenticationParam, getURLs, getWMSVendorParams } from "../LayersUtils";
import { isVectorFormat } from '../VectorTileUtils';
import { optionsToVendorParams } from '../VendorParamsUtils';
import { randomInt } from '../RandomUtils';
function getQueryString(parameters) {
return Object.keys(parameters).map((key) => key + '=' + encodeURIComponent(parameters[key])).join('&');
}
const PARAM_OPTIONS = ["layers", "styles", "style", "format", "transparent", "version", "tiled", "opacity", "zindex", "srs", "singletile", "_v_", "filterobj" ];
function splitUrl(originalUrl) {
let url = originalUrl;
let queryString = "";
if (originalUrl.indexOf('?') !== -1) {
url = originalUrl.substring(0, originalUrl.indexOf('?') + 1);
if (originalUrl.indexOf('%') !== -1) {
url = decodeURIComponent(url);
}
queryString = originalUrl.substring(originalUrl.indexOf('?') + 1);
}
return {url, queryString};
}
function WMSProxy(proxy) {
this.proxy = proxy;
}
WMSProxy.prototype.getURL = function(resource) {
let {url, queryString} = splitUrl(resource);
return getProxyUrl() + encodeURIComponent(url + queryString);
};
function NoProxy() {}
NoProxy.prototype.getURL = function(resource) {
const { url, queryString } = splitUrl(resource);
return url + queryString;
};
// Check and apply proxy to source url
export const getProxy = (options) => {
let proxyUrl = ConfigUtils.getProxyUrl({});
let proxy;
if (proxyUrl) {
proxy = options.noCors || options.forceProxy;
}
return proxy ? new WMSProxy(proxyUrl) : new NoProxy();
};
/**
* Generate cesium BIL option for BILTerrainProvider from wms layer option
* @param {object} options
* @returns {object} converted BIL options
*/
export const wmsToCesiumOptionsBIL = (options) => {
let url = options.url;
const headers = getAuthenticationHeaders(url, options.securityToken);
const params = getAuthenticationParam(options);
// MapStore only supports "image/bil" format for WMS provider
return {
url,
headers,
proxy: getProxy(options),
littleEndian: options.littleEndian || options.littleendian || false,
layerName: options.name,
version: options.version,
crs: options.crs, // Support only CRS:84 | EPSG:4326 | EPSG:3857 | OSGEO:41001
sampleTerrainZoomLevel: options.sampleTerrainZoomLevel,
heightMapWidth: options.heightMapWidth,
heightMapHeight: options.heightMapHeight,
waterMask: options.waterMask,
offset: options.offset,
highest: options.highest,
lowest: options.lowest,
params
};
};
export function wmsToCesiumOptions(options) {
var opacity = options.opacity !== undefined ? options.opacity : 1;
const params = optionsToVendorParams(options);
const cr = options.credits;
const credit = cr ? new Cesium.Credit(creditsToAttribution(cr)) : options.attribution;
// NOTE: can we use opacity to manage visibility?
const urls = getURLs(isArray(options.url) ? options.url : [options.url]);
const headers = getAuthenticationHeaders(urls[0], options.securityToken);
return {
url: new Cesium.Resource({
url: "{s}",
headers,
proxy: getProxy(options)
}),
// #7516 this helps Cesium to use CORS requests in a proper way, even when headers are not
// present in the Resource
tileDiscardPolicy: options.tileDiscardPolicy === "none" ?
undefined :
(options.tileDiscardPolicy ?? new Cesium.NeverTileDiscardPolicy()),
credit,
subdomains: urls,
layers: options.name,
enablePickFeatures: false,
parameters: {
styles: options.style || "",
format: isVectorFormat(options.format) && 'image/png' || options.format || 'image/png',
transparent: options.transparent !== undefined ? options.transparent : true,
opacity: opacity,
version: options.version || "1.1.1",
tiled: options.tiled !== undefined ? options.tiled : true,
width: options.tileSize || 256,
height: options.tileSize || 256,
...(options._v_ ? {_v_: options._v_} : {}),
...(params || {}),
...getAuthenticationParam(options)
}
};
}
export function wmsToCesiumOptionsSingleTile(options) {
const opacity = options.opacity !== undefined ? options.opacity : 1;
const params = optionsToVendorParams(options);
const parameters = {
styles: options.style || "",
format: isVectorFormat(options.format) && 'image/png' || options.format || 'image/png',
transparent: options.transparent !== undefined ? options.transparent : true,
opacity: opacity,
...getWMSVendorParams(options),
layers: options.name,
width: options.size || 2000,
height: options.size || 2000,
bbox: "-180.0,-90,180.0,90",
srs: "EPSG:4326",
...(params || {}),
...getAuthenticationParam(options),
...(options._v_ ? {_v_: options._v_} : {})
};
const url = (isArray(options.url) ? options.url[Math.round(randomInt(options.url.length - 1))] : options.url) + '?service=WMS&version=1.1.0&request=GetMap&'
+ getQueryString(addAuthenticationToSLD(parameters, options));
const headers = getAuthenticationHeaders(url, options.securityToken);
return {
url: new Cesium.Resource({
url,
headers,
proxy: getProxy(options)
})
};
}
export default {
PARAM_OPTIONS,
wmsToCesiumOptionsBIL,
wmsToCesiumOptions,
wmsToCesiumOptionsSingleTile,
getProxy
};