@supermapgis/iclient-ol 11.3.0 → 12.0.0-r

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (83) hide show
  1. package/control/ChangeTileVersion.js +1 -1
  2. package/control/Logo.js +1 -1
  3. package/control/ScaleLine.js +2 -3
  4. package/control/index.js +1 -1
  5. package/core/MapExtend.js +1 -1
  6. package/core/StyleUtils.js +3 -3
  7. package/core/Util.js +2 -2
  8. package/core/index.js +1 -1
  9. package/css/index.js +1 -1
  10. package/dist/iclient-openlayers-es6.js +24377 -15489
  11. package/dist/iclient-openlayers-es6.min.js +3 -3
  12. package/dist/iclient-openlayers.css +2 -2
  13. package/dist/iclient-openlayers.js +135888 -123771
  14. package/dist/iclient-openlayers.min.css +2 -2
  15. package/dist/iclient-openlayers.min.js +3 -3
  16. package/index.js +1 -1
  17. package/mapping/BaiduMap.js +1 -2
  18. package/mapping/ImageSuperMapRest.js +68 -12
  19. package/mapping/ImageTileSuperMapRest.js +1 -1
  20. package/mapping/SuperMapCloud.js +1 -1
  21. package/mapping/Tianditu.js +1 -1
  22. package/mapping/TileSuperMapRest.js +1 -1
  23. package/mapping/WebMap.js +297 -125
  24. package/mapping/index.js +1 -1
  25. package/namespace.js +1 -1
  26. package/overlay/DataFlow.js +1 -1
  27. package/overlay/FGB.js +69 -4
  28. package/overlay/Graph.js +1 -1
  29. package/overlay/GraphMap.js +1 -1
  30. package/overlay/Graphic.js +1 -1
  31. package/overlay/HeatMap.js +1 -1
  32. package/overlay/Label.js +1 -1
  33. package/overlay/Mapv.js +1 -1
  34. package/overlay/Range.js +1 -1
  35. package/overlay/RankSymbol.js +1 -1
  36. package/overlay/Turf.js +1 -1
  37. package/overlay/Unique.js +1 -1
  38. package/overlay/VectorTileSuperMapRest.js +120 -87
  39. package/overlay/graphic/CanvasRenderer.js +1 -1
  40. package/overlay/graphic/CloverShape.js +1 -1
  41. package/overlay/graphic/Graphic.js +1 -1
  42. package/overlay/graphic/HitCloverShape.js +1 -1
  43. package/overlay/graphic/WebGLRenderer.js +1 -1
  44. package/overlay/graphic/index.js +1 -1
  45. package/overlay/index.js +1 -1
  46. package/overlay/mapv/MapvCanvasLayer.js +1 -1
  47. package/overlay/mapv/MapvLayer.js +1 -1
  48. package/overlay/mapv/index.js +1 -1
  49. package/overlay/theme/GeoFeature.js +1 -1
  50. package/overlay/theme/Theme.js +1 -1
  51. package/overlay/theme/ThemeFeature.js +1 -1
  52. package/overlay/theme/index.js +1 -1
  53. package/overlay/vectortile/DeafultCanvasStyle.js +1 -1
  54. package/overlay/vectortile/MapboxStyles.js +428 -389
  55. package/overlay/vectortile/StyleMap.js +1 -1
  56. package/overlay/vectortile/VectorTileStyles.js +1 -1
  57. package/overlay/vectortile/index.js +1 -1
  58. package/overlay/vectortile/olExtends.js +1 -1
  59. package/package.json +6 -6
  60. package/services/AddressMatchService.js +1 -1
  61. package/services/ChartService.js +1 -1
  62. package/services/DataFlowService.js +1 -1
  63. package/services/DatasetService.js +1 -1
  64. package/services/DatasourceService.js +1 -1
  65. package/services/FeatureService.js +1 -1
  66. package/services/FieldService.js +1 -1
  67. package/services/GridCellInfosService.js +1 -1
  68. package/services/ImageCollectionService.js +1 -1
  69. package/services/ImageService.js +1 -1
  70. package/services/KnowledgeGraphService.js +1 -1
  71. package/services/LayerInfoService.js +1 -1
  72. package/services/MapService.js +1 -1
  73. package/services/MeasureService.js +1 -1
  74. package/services/NetworkAnalyst3DService.js +1 -1
  75. package/services/NetworkAnalystService.js +1 -1
  76. package/services/ProcessingService.js +1 -1
  77. package/services/QueryService.js +1 -1
  78. package/services/ServiceBase.js +1 -1
  79. package/services/SpatialAnalystService.js +1 -1
  80. package/services/ThemeService.js +1 -1
  81. package/services/TrafficTransferAnalystService.js +1 -1
  82. package/services/WebPrintingJobService.js +1 -1
  83. package/services/index.js +1 -1
@@ -1,395 +1,434 @@
1
- /* Copyright© 2000 - 2024 SuperMap Software Co.Ltd. All rights reserved.
1
+ /* Copyright© 2000 - 2025 SuperMap Software Co.Ltd. All rights reserved.
2
2
  * This program are made available under the terms of the Apache License, Version 2.0
3
3
  * which accompanies this distribution and is available at http://www.apache.org/licenses/LICENSE-2.0.html.*/
4
- import { FetchRequest } from '@supermapgis/iclient-common/util/FetchRequest';
5
- import { SecurityManager } from '@supermapgis/iclient-common/security/SecurityManager';
6
- import { Util as CommonUtil} from '@supermapgis/iclient-common/commontypes/Util';
7
- import { olExtends } from './olExtends';
8
- import remove from 'lodash.remove';
9
- import Observable from 'ol/Observable';
10
- import Style from 'ol/style/Style';
11
- import CircleStyle from 'ol/style/Circle';
12
- import FillStyle from 'ol/style/Fill';
13
- import StrokeStyle from 'ol/style/Stroke';
14
- import Text from 'ol/style/Text';
15
- /**
16
- * @class MapboxStyles
17
- * @classdesc Mapbox 矢量瓦片风格。
18
- * <div style="padding: 20px;border: 1px solid #eee;border-left-width: 5px;border-radius: 3px;border-left-color: #ce4844;">
19
- * <p style="color: #ce4844">Notice</p>
20
- * <p style="font-size: 13px">该功能依赖 <a href='https://github.com/boundlessgeo/ol-mapbox-style'>ol-mapbox-style</a> 插件,请确认引入该插件。</p>
21
- * <a src="https://iclient.supermap.io/web/libs/openlayers/plugins/ol-mapbox-style/2.11.2-6/olms.js">https://iclient.supermap.io/web/libs/openlayers/plugins/ol-mapbox-style/2.11.2-6/olms.js</a>
22
- * </div>
23
- * @modulecategory Overlay
24
- * @category Visualization VectorTile
25
- * @param {Object} options - 参数。
26
- * @param {(string|undefined)} [options.url] - SuperMap iServer 地图服务地址,例如'http://localhost:8090/iserver/services/map-mvt-test/rest/maps/test',与 options.style 互斥,优先级低于 options.style。
27
- * @param {(Object|string|undefined)} [options.style] - Mapbox Style JSON 对象或获取 Mapbox Style JSON 对象的 URL。与 options.url 互斥,优先级高于 options.url。
28
- * @param {Array.<number>} [options.resolutions] - 地图分辨率数组,用于映射 zoom 值。通常情況与地图的 {@link ol.View} 的分辨率一致。</br>
29
- * 默认值为:[78271.51696402048,39135.75848201024, 19567.87924100512,9783.93962050256,4891.96981025128,2445.98490512564, 1222.99245256282,611.49622628141,305.748113140705,152.8740565703525, 76.43702828517625,38.21851414258813,19.109257071294063,9.554628535647032, 4.777314267823516,2.388657133911758,1.194328566955879,0.5971642834779395, 0.29858214173896974,0.14929107086948487,0.07464553543474244]。
30
- * @param {(string|Array.<string>|undefined)} [options.source] - Mapbox Style 'source'的 key 值或者 'layer' 的 ID 数组。
31
- * 当配置 'source' 的 key 值时,source 为该值的 layer 会被加载;
32
- * 当配置为 'layer' 的 ID 数组时,指定的 layer 会被加载,注意被指定的 layer 需要有相同的 source。
33
- * 当不配置时,默认为 Mapbox Style JSON 的 `sources` 对象中的第一个。
34
- * @param {ol.Map} [options.map] - Openlayers 地图对象,仅用于面填充样式,若没有面填充样式可不填。
35
- * @param {ol.StyleFunction} [options.selectedStyle] -选中样式 Function。
36
- * @param {boolean} [options.withCredentials] - 请求是否携带 cookie。
37
- * @param {Object} [options.headers] - 请求头。
38
- * @example
39
- * var mbStyle = new MapboxStyles({
40
- url: url,
41
- source: 'California',
42
- resolutions: [78271.51696402048,39135.75848201024, 19567.87924100512,9783.93962050256,4891.96981025128,2445.98490512564]
43
- })
44
- mbStyle.on('styleLoaded', function () {
45
- var vectorLayer = new ol.layer.VectorTile({
46
- //设置避让参数
47
- declutter: true,
48
- source: new ol.source.VectorTileSuperMapRest({
49
- url: url,
50
- format: new ol.format.MVT({
51
- featureClass: ol.Feature
52
- }),
53
- tileType: 'ScaleXY'
54
- }),
55
- style: mbStyle.featureStyleFuntion
56
- });
57
- map.addLayer(vectorLayer);
58
- })
59
- * @usage
60
- */
61
- export class MapboxStyles extends Observable {
62
- constructor(options) {
63
- super();
64
- options = options || {};
65
- this.spriteRegEx = /^(.*)(\?.*)$/;
66
- this.defaultFont = ['DIN Offc Pro Medium', 'Arial Unicode MS Regular'];
67
- this.map = options.map;
68
- this.source = options.source;
69
- this.styleTarget =
70
- options.style ||
71
- CommonUtil.urlAppend(
72
- CommonUtil.urlPathAppend(options.url, 'tileFeature/vectorstyles'),
73
- 'type=MapBox_GL&styleonly=true'
74
- );
75
- this.resolutions = options.resolutions;
76
- this.withCredentials = options.withCredentials;
77
- this.headers = options.headers;
78
- this.selectedObjects = [];
79
- this.selectedStyle =
80
- options.selectedStyle ||
81
- function() {
82
- return new Style({
83
- fill: new FillStyle({
84
- color: 'rgba(255, 0, 0, 1)'
85
- }),
86
- stroke: new StrokeStyle({
87
- color: 'rgba(255, 0, 0, 1)',
88
- width: 10
89
- }),
90
- text: new Text({
91
- font: 'normal 400 11.19px "Microsoft YaHei"',
92
- placement: 'point',
93
- fill: new FillStyle({
94
- color: 'blue'
95
- })
96
- }),
97
- image: new CircleStyle({
98
- radius: 5,
99
- fill: new FillStyle({
100
- color: 'blue'
101
- })
102
- })
103
- });
104
- };
105
- this.layersBySourceLayer = {};
106
- olExtends(this.map);
107
- this._loadStyle(this.styleTarget);
108
- }
109
- /**
110
- * @function MapboxStyles.prototype.getStyleFunction
111
- * @description 获取 ol.StyleFunction。
112
- * @returns {ol.StyleFunction} 返回 ol.StyleFunction。
113
- */
114
- getStyleFunction() {
115
- return this.featureStyleFuntion;
116
- }
117
- /**
118
- * @function MapboxStyles.prototype.getStylesBySourceLayer
119
- * @description 根据图层名称获取样式。
120
- * @param {string} sourceLayer - 数据图层名称。
121
- */
122
- getStylesBySourceLayer(sourceLayer) {
123
- if (this.layersBySourceLayer[sourceLayer]) {
124
- return this.layersBySourceLayer[sourceLayer];
125
- }
126
- const layers = [];
127
- for (let index = 0; index < this._mbStyle.layers.length; index++) {
128
- const layer = this._mbStyle.layers[index];
129
- if (layer['source-layer'] !== sourceLayer) {
130
- continue;
131
- }
132
- layers.push(layer);
133
- }
134
- this.layersBySourceLayer[sourceLayer] = layers;
135
- return layers;
136
- }
137
- /**
138
- * @function MapboxStyles.prototype.setSelectedId
139
- * @description 设置选中要素,该要素将会用 `selectedStyle` 样式绘制。调用该方法后需要调用 {@link ol.layer.VectorTile} 的 `changed`,才能生效。
140
- * @param {number} selectedId - 要素 ID。
141
- * @param {string} sourceLayer - 要素所在图层名称。
142
- */
143
- setSelectedId(selectedId, sourceLayer) {
144
- this.selectedObjects = [];
145
- this.selectedObjects.push({
146
- id: selectedId,
147
- sourceLayer: sourceLayer
148
- });
149
- }
150
- /**
151
- * @typedef {Object} MapboxStyles.selectedObject
152
- * @description 要选择的要素对象。
153
- * @property {number} selectedId - 要素 ID。
154
- * @property {string} sourceLayer - 要素所在图层名称。
155
- */
156
-
157
- /**
158
- * @function MapboxStyles.prototype.setSelectedObjects
159
- * @version 10.0.0
160
- * @description 设置选中要素或要素数组,该要素将会用 `selectedStyle` 样式绘制。调用该方法后需要调用 {@link ol.layer.VectorTile} 的 `changed`,才能生效。
161
- * @param {MapboxStyles.selectedObject|Array.<MapboxStyles.selectedObject>} addSelectedObjects - 选择的要素或要素数组。
162
- */
163
- setSelectedObjects(selectedObjects) {
164
- if (!Array.isArray(selectedObjects)) {
165
- selectedObjects = [selectedObjects];
166
- }
167
- this.selectedObjects = [];
168
- this.selectedObjects = selectedObjects;
169
- }
170
- /**
171
- * @function MapboxStyles.prototype.addSelectedObjects
172
- * @version 10.0.0
173
- * @description 增加选中的要素或要素数组,该要素将会用 `selectedStyle` 样式绘制。调用该方法后需要调用 {@link ol.layer.VectorTile} 的 `changed` ,才能生效。
174
- * @param {MapboxStyles.selectedObject|Array.<MapboxStyles.selectedObject>} addSelectedObjects - 选择的要素或要素数组。
175
- */
176
- addSelectedObjects(selectedObjects) {
177
- if (!Array.isArray(selectedObjects)) {
178
- selectedObjects = [selectedObjects];
179
- }
180
- this.selectedObjects.push(...selectedObjects);
181
- }
182
- /**
183
- * @function MapboxStyles.prototype.removeSelectedObjects
184
- * @version 10.0.0
185
- * @description 清空选中状态。调用该方法后需要调用 {@link ol.layer.VectorTile} 的 `changed` ,才能生效。
186
- */
187
- removeSelectedObjects(selectedObjects) {
188
- if (!Array.isArray(selectedObjects)) {
189
- selectedObjects = [selectedObjects];
190
- }
191
- selectedObjects.forEach(element => {
192
- remove(this.selectedObjects, obj => {
193
- return element.id === obj.id && element.sourceLayer === obj.sourceLayer;
194
- });
195
- });
196
- }
197
- /**
198
- * @function MapboxStyles.prototype.clearSelectedObjects
199
- * @version 10.0.0
200
- * @description 清空选中状态。调用该方法后需要调用 {@link ol.layer.VectorTile} 的 `changed`,才能生效。
201
- */
202
- clearSelectedObjects() {
203
- this.selectedObjects = [];
204
- }
205
- /**
206
- * @function MapboxStyles.prototype.updateStyles
207
- * @description 更新图层样式。
208
- * @param {Object} layerStyles - 图层样式或图层样式数组。
209
- */
210
- updateStyles(layerStyles) {
211
- if (Object.prototype.toString.call(layerStyles) !== '[object Array]') {
212
- layerStyles = [layerStyles];
213
- }
214
- const layerObj = {};
215
- layerStyles.forEach(layerStyle => {
216
- layerObj[layerStyle.id] = layerStyle;
217
- });
218
- let count = 0;
219
- for (const key in this._mbStyle.layers) {
220
- const oldLayerStyle = this._mbStyle.layers[key];
221
- if (count >= layerStyles.length) {
222
- break;
223
- }
224
- if (!layerObj[oldLayerStyle.id]) {
225
- continue;
226
- }
227
- const newLayerStyle = JSON.parse(JSON.stringify(layerObj[oldLayerStyle.id]));
228
- if (newLayerStyle.paint) {
229
- newLayerStyle.paint = Object.assign({}, oldLayerStyle.paint, newLayerStyle.paint);
230
- }
231
- if (newLayerStyle.layout) {
232
- newLayerStyle.layout = Object.assign({}, oldLayerStyle.layout, newLayerStyle.layout);
233
- }
234
- Object.assign(oldLayerStyle, newLayerStyle);
235
- count++;
236
- }
237
- this._createStyleFunction();
238
- }
239
- /**
240
- * @function MapboxStyles.prototype.setStyle
241
- * @version 9.1.1
242
- * @description 设置 Mapbox style 对象。
243
- * @param {Object} style - Mapbox style 对象。
244
- */
245
- setStyle(style) {
246
- this.layersBySourceLayer = {};
247
- this._loadStyle(style);
248
- }
249
- _loadStyle(style) {
250
- if (Object.prototype.toString.call(style) == '[object Object]') {
251
- this._mbStyle = style;
252
- setTimeout(() => {
253
- this._resolve();
254
- }, 0);
255
- } else {
256
- var url = SecurityManager.appendCredential(style);
257
- FetchRequest.get(url, null, { withCredentials: this.withCredentials, headers: this.headers })
258
- .then(response => response.json())
259
- .then(mbStyle => {
260
- this._mbStyle = mbStyle;
261
- this._resolve();
262
- });
263
- }
264
- }
265
- _resolve() {
266
- if (!this.source) {
267
- this.source = Object.keys(this._mbStyle.sources)[0];
268
- }
269
- if (this._mbStyle.sprite) {
270
- const spriteScale = window.devicePixelRatio >= 1.5 ? 0.5 : 1;
271
- const sizeFactor = spriteScale == 0.5 ? '@2x' : '';
272
- //兼容一下iServer 等iServer修改
273
- this._mbStyle.sprite = this._mbStyle.sprite.replace('@2x', '');
274
- const spriteUrl = this._toSpriteUrl(this._mbStyle.sprite, this.path, sizeFactor + '.json');
275
- FetchRequest.get(SecurityManager.appendCredential(spriteUrl), null, {
276
- withCredentials: this.withCredentials,
277
- headers: this.headers
278
- })
279
- .then((response) => response.json())
280
- .then((spritesJson) => {
281
- this._spriteData = spritesJson;
282
- this._spriteImageUrl = SecurityManager.appendCredential(
283
- this._toSpriteUrl(this._mbStyle.sprite, this.path, sizeFactor + '.png')
284
- );
285
- this._spriteImage = null;
286
-
287
- var xhr = new XMLHttpRequest();
288
- xhr.responseType = 'blob';
289
- xhr.addEventListener('loadend',(e) => {
290
- var data = e.target.response;
291
- if (data !== undefined) {
292
- const img = new Image();
293
- img.src = URL.createObjectURL(data);
294
- this._spriteImage = img;
295
- } else {
296
- this._spriteImage = null;
4
+ import { FetchRequest } from '@supermapgis/iclient-common/util/FetchRequest';
5
+ import { SecurityManager } from '@supermapgis/iclient-common/security/SecurityManager';
6
+ import { Util as CommonUtil } from '@supermapgis/iclient-common/commontypes/Util';
7
+ import { olExtends } from './olExtends';
8
+ import remove from 'lodash.remove';
9
+ import Observable from 'ol/Observable';
10
+ import Style from 'ol/style/Style';
11
+ import CircleStyle from 'ol/style/Circle';
12
+ import FillStyle from 'ol/style/Fill';
13
+ import StrokeStyle from 'ol/style/Stroke';
14
+ import Text from 'ol/style/Text';
15
+ /**
16
+ * @class MapboxStyles
17
+ * @classdesc Mapbox 矢量瓦片风格。
18
+ * <div style="padding: 20px;border: 1px solid #eee;border-left-width: 5px;border-radius: 3px;border-left-color: #ce4844;">
19
+ * <p style="color: #ce4844">Notice</p>
20
+ * <p style="font-size: 13px">该功能依赖 <a href='https://github.com/boundlessgeo/ol-mapbox-style'>ol-mapbox-style</a> 插件,请确认引入该插件。</p>
21
+ * <a src="https://iclient.supermap.io/web/libs/openlayers/plugins/ol-mapbox-style/2.11.2-6/olms.js">https://iclient.supermap.io/web/libs/openlayers/plugins/ol-mapbox-style/2.11.2-6/olms.js</a>
22
+ * </div>
23
+ * @modulecategory Overlay
24
+ * @category Visualization VectorTile
25
+ * @param {Object} options - 参数。
26
+ * @param {(string|undefined)} [options.url] - SuperMap iServer 地图服务地址,例如'http://localhost:8090/iserver/services/map-mvt-test/rest/maps/test',与 options.style 互斥,优先级低于 options.style。
27
+ * @param {(Object|string|undefined)} [options.style] - Mapbox Style JSON 对象或获取 Mapbox Style JSON 对象的 URL。与 options.url 互斥,优先级高于 options.url。
28
+ * @param {string} [options.baseUrl] - 当传入 style 对象且 style 中包含了相对路径时,需要传入 baseUrl 来拼接资源路径。
29
+ * @param {Array.<number>} [options.resolutions] - 地图分辨率数组,用于映射 zoom 值。通常情況与地图的 {@link ol.View} 的分辨率一致。</br>
30
+ * 默认值为:[78271.51696402048,39135.75848201024, 19567.87924100512,9783.93962050256,4891.96981025128,2445.98490512564, 1222.99245256282,611.49622628141,305.748113140705,152.8740565703525, 76.43702828517625,38.21851414258813,19.109257071294063,9.554628535647032, 4.777314267823516,2.388657133911758,1.194328566955879,0.5971642834779395, 0.29858214173896974,0.14929107086948487,0.07464553543474244]。
31
+ * @param {(string|Array.<string>|undefined)} [options.source] - Mapbox Style 'source'的 key 值或者 'layer' 的 ID 数组。
32
+ * 当配置 'source' 的 key 值时,source 为该值的 layer 会被加载;
33
+ * 当配置为 'layer' 的 ID 数组时,指定的 layer 会被加载,注意被指定的 layer 需要有相同的 source。
34
+ * 当不配置时,默认为 Mapbox Style JSON 的 `sources` 对象中的第一个。
35
+ * @param {ol.Map} [options.map] - Openlayers 地图对象,仅用于面填充样式,若没有面填充样式可不填。
36
+ * @param {ol.StyleFunction} [options.selectedStyle] -选中样式 Function。
37
+ * @param {boolean} [options.withCredentials] - 请求是否携带 cookie。
38
+ * @param {Object} [options.headers] - 请求头。
39
+ * @example
40
+ * var mbStyle = new MapboxStyles({
41
+ url: url,
42
+ source: 'California',
43
+ resolutions: [78271.51696402048,39135.75848201024, 19567.87924100512,9783.93962050256,4891.96981025128,2445.98490512564]
44
+ })
45
+ mbStyle.on('styleLoaded', function () {
46
+ var vectorLayer = new ol.layer.VectorTile({
47
+ //设置避让参数
48
+ declutter: true,
49
+ source: new ol.source.VectorTileSuperMapRest({
50
+ url: url,
51
+ format: new ol.format.MVT({
52
+ featureClass: ol.Feature
53
+ }),
54
+ tileType: 'ScaleXY'
55
+ }),
56
+ style: mbStyle.featureStyleFuntion
57
+ });
58
+ map.addLayer(vectorLayer);
59
+ })
60
+ * @usage
61
+ */
62
+ export class MapboxStyles extends Observable {
63
+ constructor(options) {
64
+ super();
65
+ options = options || {};
66
+ this.spriteRegEx = /^(.*)(\?.*)$/;
67
+ this.defaultFont = ['DIN Offc Pro Medium', 'Arial Unicode MS Regular'];
68
+ this.map = options.map;
69
+ this.source = options.source;
70
+ this.styleTarget =
71
+ options.style ||
72
+ CommonUtil.urlAppend(
73
+ CommonUtil.urlPathAppend(options.url, 'tileFeature/vectorstyles'),
74
+ 'type=MapBox_GL&styleonly=true'
75
+ );
76
+ this.resolutions = options.resolutions;
77
+ this.withCredentials = options.withCredentials;
78
+ this.headers = options.headers;
79
+ this.selectedObjects = [];
80
+ this.selectedStyle =
81
+ options.selectedStyle ||
82
+ function() {
83
+ return new Style({
84
+ fill: new FillStyle({
85
+ color: 'rgba(255, 0, 0, 1)'
86
+ }),
87
+ stroke: new StrokeStyle({
88
+ color: 'rgba(255, 0, 0, 1)',
89
+ width: 10
90
+ }),
91
+ text: new Text({
92
+ font: 'normal 400 11.19px "Microsoft YaHei"',
93
+ placement: 'point',
94
+ fill: new FillStyle({
95
+ color: 'blue'
96
+ })
97
+ }),
98
+ image: new CircleStyle({
99
+ radius: 5,
100
+ fill: new FillStyle({
101
+ color: 'blue'
102
+ })
103
+ })
104
+ });
105
+ };
106
+ this.layersBySourceLayer = {};
107
+ this.baseUrl = options.baseUrl;
108
+ olExtends(this.map);
109
+ this._loadStyle(this.styleTarget);
110
+ }
111
+ /**
112
+ * @function MapboxStyles.prototype.getStyleFunction
113
+ * @description 获取 ol.StyleFunction。
114
+ * @returns {ol.StyleFunction} 返回 ol.StyleFunction。
115
+ */
116
+ getStyleFunction() {
117
+ return this.featureStyleFuntion;
118
+ }
119
+ /**
120
+ * @function MapboxStyles.prototype.getStylesBySourceLayer
121
+ * @description 根据图层名称获取样式。
122
+ * @param {string} sourceLayer - 数据图层名称。
123
+ */
124
+ getStylesBySourceLayer(sourceLayer) {
125
+ if (this.layersBySourceLayer[sourceLayer]) {
126
+ return this.layersBySourceLayer[sourceLayer];
127
+ }
128
+ const layers = [];
129
+ for (let index = 0; index < this._mbStyle.layers.length; index++) {
130
+ const layer = this._mbStyle.layers[index];
131
+ if (layer['source-layer'] !== sourceLayer) {
132
+ continue;
133
+ }
134
+ layers.push(layer);
135
+ }
136
+ this.layersBySourceLayer[sourceLayer] = layers;
137
+ return layers;
138
+ }
139
+ /**
140
+ * @function MapboxStyles.prototype.setSelectedId
141
+ * @description 设置选中要素,该要素将会用 `selectedStyle` 样式绘制。调用该方法后需要调用 {@link ol.layer.VectorTile} 的 `changed`,才能生效。
142
+ * @param {number} selectedId - 要素 ID。
143
+ * @param {string} sourceLayer - 要素所在图层名称。
144
+ */
145
+ setSelectedId(selectedId, sourceLayer) {
146
+ this.selectedObjects = [];
147
+ this.selectedObjects.push({
148
+ id: selectedId,
149
+ sourceLayer: sourceLayer
150
+ });
151
+ }
152
+ /**
153
+ * @typedef {Object} MapboxStyles.selectedObject
154
+ * @description 要选择的要素对象。
155
+ * @property {number} selectedId - 要素 ID。
156
+ * @property {string} sourceLayer - 要素所在图层名称。
157
+ */
158
+
159
+ /**
160
+ * @function MapboxStyles.prototype.setSelectedObjects
161
+ * @version 10.0.0
162
+ * @description 设置选中要素或要素数组,该要素将会用 `selectedStyle` 样式绘制。调用该方法后需要调用 {@link ol.layer.VectorTile} 的 `changed`,才能生效。
163
+ * @param {MapboxStyles.selectedObject|Array.<MapboxStyles.selectedObject>} addSelectedObjects - 选择的要素或要素数组。
164
+ */
165
+ setSelectedObjects(selectedObjects) {
166
+ if (!Array.isArray(selectedObjects)) {
167
+ selectedObjects = [selectedObjects];
168
+ }
169
+ this.selectedObjects = [];
170
+ this.selectedObjects = selectedObjects;
171
+ }
172
+ /**
173
+ * @function MapboxStyles.prototype.addSelectedObjects
174
+ * @version 10.0.0
175
+ * @description 增加选中的要素或要素数组,该要素将会用 `selectedStyle` 样式绘制。调用该方法后需要调用 {@link ol.layer.VectorTile} 的 `changed` ,才能生效。
176
+ * @param {MapboxStyles.selectedObject|Array.<MapboxStyles.selectedObject>} addSelectedObjects - 选择的要素或要素数组。
177
+ */
178
+ addSelectedObjects(selectedObjects) {
179
+ if (!Array.isArray(selectedObjects)) {
180
+ selectedObjects = [selectedObjects];
181
+ }
182
+ this.selectedObjects.push(...selectedObjects);
183
+ }
184
+ /**
185
+ * @function MapboxStyles.prototype.removeSelectedObjects
186
+ * @version 10.0.0
187
+ * @description 清空选中状态。调用该方法后需要调用 {@link ol.layer.VectorTile} 的 `changed` ,才能生效。
188
+ */
189
+ removeSelectedObjects(selectedObjects) {
190
+ if (!Array.isArray(selectedObjects)) {
191
+ selectedObjects = [selectedObjects];
192
+ }
193
+ selectedObjects.forEach(element => {
194
+ remove(this.selectedObjects, obj => {
195
+ return element.id === obj.id && element.sourceLayer === obj.sourceLayer;
196
+ });
197
+ });
198
+ }
199
+ /**
200
+ * @function MapboxStyles.prototype.clearSelectedObjects
201
+ * @version 10.0.0
202
+ * @description 清空选中状态。调用该方法后需要调用 {@link ol.layer.VectorTile} 的 `changed`,才能生效。
203
+ */
204
+ clearSelectedObjects() {
205
+ this.selectedObjects = [];
206
+ }
207
+ /**
208
+ * @function MapboxStyles.prototype.updateStyles
209
+ * @description 更新图层样式。
210
+ * @param {Object} layerStyles - 图层样式或图层样式数组。
211
+ */
212
+ updateStyles(layerStyles) {
213
+ if (Object.prototype.toString.call(layerStyles) !== '[object Array]') {
214
+ layerStyles = [layerStyles];
215
+ }
216
+ const layerObj = {};
217
+ layerStyles.forEach(layerStyle => {
218
+ layerObj[layerStyle.id] = layerStyle;
219
+ });
220
+ let count = 0;
221
+ for (const key in this._mbStyle.layers) {
222
+ const oldLayerStyle = this._mbStyle.layers[key];
223
+ if (count >= layerStyles.length) {
224
+ break;
225
+ }
226
+ if (!layerObj[oldLayerStyle.id]) {
227
+ continue;
228
+ }
229
+ const newLayerStyle = JSON.parse(JSON.stringify(layerObj[oldLayerStyle.id]));
230
+ if (newLayerStyle.paint) {
231
+ newLayerStyle.paint = Object.assign({}, oldLayerStyle.paint, newLayerStyle.paint);
232
+ }
233
+ if (newLayerStyle.layout) {
234
+ newLayerStyle.layout = Object.assign({}, oldLayerStyle.layout, newLayerStyle.layout);
235
+ }
236
+ Object.assign(oldLayerStyle, newLayerStyle);
237
+ count++;
238
+ }
239
+ this._createStyleFunction();
240
+ }
241
+ /**
242
+ * @function MapboxStyles.prototype.setStyle
243
+ * @version 9.1.1
244
+ * @description 设置 Mapbox style 对象。
245
+ * @param {Object} style - Mapbox style 对象。
246
+ */
247
+ setStyle(style) {
248
+ this.layersBySourceLayer = {};
249
+ this._loadStyle(style);
250
+ }
251
+
252
+ _loadStyle(style) {
253
+ if (Object.prototype.toString.call(style) == '[object Object]') {
254
+ this._handleRelativeUrl(style, this.baseUrl);
255
+ this._mbStyle = style;
256
+ setTimeout(() => {
257
+ this._resolve();
258
+ }, 0);
259
+ } else {
260
+ var url = SecurityManager.appendCredential(style);
261
+ FetchRequest.get(url, null, { withCredentials: this.withCredentials, headers: this.headers })
262
+ .then(response => response.json())
263
+ .then(mbStyle => {
264
+ this._handleRelativeUrl(mbStyle, url);
265
+ this._mbStyle = mbStyle;
266
+ this._resolve();
267
+ });
268
+ }
269
+ }
270
+ _resolve() {
271
+ if (!this.source) {
272
+ this.source = Object.keys(this._mbStyle.sources)[0];
273
+ }
274
+ if (this._mbStyle.sprite) {
275
+ const spriteScale = window.devicePixelRatio >= 1.5 ? 0.5 : 1;
276
+ const sizeFactor = spriteScale == 0.5 ? '@2x' : '';
277
+ //兼容一下iServer 等iServer修改
278
+ this._mbStyle.sprite = this._mbStyle.sprite.replace('@2x', '');
279
+ const spriteUrl = this._toSpriteUrl(this._mbStyle.sprite, this.path, sizeFactor + '.json');
280
+ FetchRequest.get(SecurityManager.appendCredential(spriteUrl), null, {
281
+ withCredentials: this.withCredentials,
282
+ headers: this.headers
283
+ })
284
+ .then((response) => response.json())
285
+ .then((spritesJson) => {
286
+ this._spriteData = spritesJson;
287
+ this._spriteImageUrl = SecurityManager.appendCredential(
288
+ this._toSpriteUrl(this._mbStyle.sprite, this.path, sizeFactor + '.png')
289
+ );
290
+ this._spriteImage = null;
291
+
292
+ var xhr = new XMLHttpRequest();
293
+ xhr.responseType = 'blob';
294
+ xhr.addEventListener('loadend',(e) => {
295
+ var data = e.target.response;
296
+ if (data !== undefined) {
297
+ const img = new Image();
298
+ img.src = URL.createObjectURL(data);
299
+ this._spriteImage = img;
300
+ } else {
301
+ this._spriteImage = null;
302
+ }
303
+ this._initStyleFunction();
304
+ });
305
+ xhr.addEventListener('error', () => {
306
+ this._spriteImage = null;
307
+ this._initStyleFunction();
308
+ });
309
+ xhr.open('GET', this._spriteImageUrl);
310
+ for (const key in this.headers) {
311
+ if (this.headers.hasOwnProperty(key)) {
312
+ xhr.setRequestHeader(key, this.headers[key]);
313
+ }
314
+ }
315
+ xhr.withCredentials = this.withCredentials;
316
+ xhr.send();
317
+ })
318
+ .catch((err) => {
319
+ console.log(err);
320
+ this._spriteImage = null;
321
+ this._initStyleFunction();
322
+ });
323
+ } else {
324
+ this._initStyleFunction();
325
+ }
326
+ }
327
+ _initStyleFunction() {
328
+ if (!this.resolutions && this._mbStyle.metadata && this._mbStyle.metadata.indexbounds) {
329
+ const indexbounds = this._mbStyle.metadata.indexbounds;
330
+ var max = Math.max(indexbounds[2] - indexbounds[0], indexbounds[3] - indexbounds[1]);
331
+ const defaultResolutions = [];
332
+ for (let index = 0; index < 30; index++) {
333
+ defaultResolutions.push(max / 512 / Math.pow(2, index));
334
+ }
335
+ this.resolutions = defaultResolutions;
336
+ }
337
+ this._createStyleFunction();
338
+ /**
339
+ * @event MapboxStyles#styleloaded
340
+ * @description 样式加载成功后触发。
341
+ */
342
+ this.dispatchEvent('styleloaded');
343
+ }
344
+ _createStyleFunction() {
345
+ if (this.map) {
346
+ window.olms.applyBackground(this.map, this._mbStyle);
347
+ }
348
+ this.featureStyleFuntion = this._getStyleFunction();
349
+ }
350
+ _getStyleFunction() {
351
+ this.fun = window.olms.stylefunction(
352
+ { setStyle: function() {}, set: function() {}, changed: function() {} },
353
+ this._mbStyle,
354
+ this.source,
355
+ this.resolutions,
356
+ this._spriteData,
357
+ '',
358
+ this._spriteImage
359
+ );
360
+ return (feature, resolution) => {
361
+ const style = this.fun(feature, resolution);
362
+ if (
363
+ this.selectedObjects.length > 0 &&
364
+ this.selectedObjects.find(element => {
365
+ return element.id === feature.getId() && element.sourceLayer === feature.get('layer');
366
+ })
367
+ ) {
368
+ const styleIndex = style && style[0] ? style[0].getZIndex() : 99999;
369
+ let selectStyles = this.selectedStyle(feature, resolution);
370
+ if (!Array.isArray(selectStyles)) {
371
+ selectStyles = [selectStyles];
372
+ }
373
+ for (let index = 0; index < selectStyles.length; index++) {
374
+ const selectStyle = selectStyles[index];
375
+ if (feature.getGeometry().getType() === 'Point' && style[0].getText() && selectStyle.getText()) {
376
+ selectStyle.setFill(null);
377
+ selectStyle.setStroke(null);
378
+ selectStyle.setImage();
379
+ selectStyle.getText().setText(style[0].getText().getText());
380
+ }
381
+ selectStyle.setZIndex(styleIndex);
382
+ }
383
+ return selectStyles;
384
+ }
385
+ return style;
386
+ };
387
+ }
388
+ _withPath(url, path) {
389
+ if (path && url.indexOf('http') != 0) {
390
+ url = path + url;
391
+ }
392
+ return url;
393
+ }
394
+
395
+ _toSpriteUrl(url, path, extension) {
396
+ url = this._withPath(url, path);
397
+ const parts = url.match(this.spriteRegEx);
398
+ return parts ? parts[1] + extension + (parts.length > 2 ? parts[2] : '') : url + extension;
399
+ }
400
+
401
+ _handleRelativeUrl(styles, url) {
402
+ if (!url) {
403
+ return styles;
404
+ }
405
+ const baseUrl = url.split('?')[0];
406
+ const paramUrl = url.split('?')[1] || '';
407
+ Object.keys(styles).forEach((fieldName) => {
408
+ if (fieldName === 'sources') {
409
+ Object.keys(styles[fieldName]).forEach((sourceName) => {
410
+ this._handleRelativeUrl(styles[fieldName][sourceName], url);
411
+ })
412
+ }
413
+ if (fieldName === 'sprite' || fieldName === 'glyphs' || fieldName === 'url') {
414
+ if (styles[fieldName] && typeof styles[fieldName] === 'string' && !CommonUtil.isAbsoluteURL(styles[fieldName])) {
415
+ styles[fieldName] = CommonUtil.relative2absolute(styles[fieldName], baseUrl);
416
+ }
417
+ if (paramUrl && !styles[fieldName].includes(paramUrl)) {
418
+ styles[fieldName] = styles[fieldName] + (paramUrl ? (styles[fieldName].includes('?') ? '&' + paramUrl : '?' + paramUrl) : '');
419
+ }
420
+ }
421
+ if (fieldName === 'tiles' && Array.isArray(styles[fieldName])) {
422
+ styles[fieldName].forEach((tile) => {
423
+ if (!CommonUtil.isAbsoluteURL(tile)) {
424
+ tile = CommonUtil.relative2absolute(tile, baseUrl);
297
425
  }
298
- this._initStyleFunction();
299
- });
300
- xhr.addEventListener('error', () => {
301
- this._spriteImage = null;
302
- this._initStyleFunction();
303
- });
304
- xhr.open('GET', this._spriteImageUrl);
305
- for (const key in this.headers) {
306
- if (this.headers.hasOwnProperty(key)) {
307
- xhr.setRequestHeader(key, this.headers[key]);
426
+ if (paramUrl && !tile.includes(paramUrl)) {
427
+ tile = tile + (paramUrl ? (styles[fieldName].includes('?') ? '&' + paramUrl : '?' + paramUrl) : '');
308
428
  }
309
- }
310
- xhr.withCredentials = this.withCredentials;
311
- xhr.send();
312
429
  })
313
- .catch((err) => {
314
- console.log(err);
315
- this._spriteImage = null;
316
- this._initStyleFunction();
317
- });
318
- } else {
319
- this._initStyleFunction();
320
- }
321
- }
322
- _initStyleFunction() {
323
- if (!this.resolutions && this._mbStyle.metadata && this._mbStyle.metadata.indexbounds) {
324
- const indexbounds = this._mbStyle.metadata.indexbounds;
325
- var max = Math.max(indexbounds[2] - indexbounds[0], indexbounds[3] - indexbounds[1]);
326
- const defaultResolutions = [];
327
- for (let index = 0; index < 30; index++) {
328
- defaultResolutions.push(max / 512 / Math.pow(2, index));
329
- }
330
- this.resolutions = defaultResolutions;
331
- }
332
- this._createStyleFunction();
333
- /**
334
- * @event MapboxStyles#styleloaded
335
- * @description 样式加载成功后触发。
336
- */
337
- this.dispatchEvent('styleloaded');
338
- }
339
- _createStyleFunction() {
340
- if (this.map) {
341
- window.olms.applyBackground(this.map, this._mbStyle);
342
- }
343
- this.featureStyleFuntion = this._getStyleFunction();
344
- }
345
- _getStyleFunction() {
346
- this.fun = window.olms.stylefunction(
347
- { setStyle: function() {}, set: function() {}, changed: function() {} },
348
- this._mbStyle,
349
- this.source,
350
- this.resolutions,
351
- this._spriteData,
352
- '',
353
- this._spriteImage
354
- );
355
- return (feature, resolution) => {
356
- const style = this.fun(feature, resolution);
357
- if (
358
- this.selectedObjects.length > 0 &&
359
- this.selectedObjects.find(element => {
360
- return element.id === feature.getId() && element.sourceLayer === feature.get('layer');
361
- })
362
- ) {
363
- const styleIndex = style && style[0] ? style[0].getZIndex() : 99999;
364
- let selectStyles = this.selectedStyle(feature, resolution);
365
- if (!Array.isArray(selectStyles)) {
366
- selectStyles = [selectStyles];
367
- }
368
- for (let index = 0; index < selectStyles.length; index++) {
369
- const selectStyle = selectStyles[index];
370
- if (feature.getGeometry().getType() === 'Point' && style[0].getText() && selectStyle.getText()) {
371
- selectStyle.setFill(null);
372
- selectStyle.setStroke(null);
373
- selectStyle.setImage();
374
- selectStyle.getText().setText(style[0].getText().getText());
375
- }
376
- selectStyle.setZIndex(styleIndex);
377
- }
378
- return selectStyles;
379
- }
380
- return style;
381
- };
382
- }
383
- _withPath(url, path) {
384
- if (path && url.indexOf('http') != 0) {
385
- url = path + url;
386
- }
387
- return url;
388
- }
389
-
390
- _toSpriteUrl(url, path, extension) {
391
- url = this._withPath(url, path);
392
- const parts = url.match(this.spriteRegEx);
393
- return parts ? parts[1] + extension + (parts.length > 2 ? parts[2] : '') : url + extension;
430
+ }
431
+ })
394
432
  }
395
- }
433
+ }
434
+