@@ -5,6 +5,29 @@ import mapboxgl from 'mapbox-gl';
5
5
import { FetchRequest } from '@supermap/iclient-common/util/FetchRequest' ;
6
6
import { Util } from '../../../core/Util' ;
7
7
8
+ const LAEYR_TYPE_LEGEND_TYPE = {
9
+ circle : 'POINT' ,
10
+ symbol : 'POINT' ,
11
+ line : 'LINE' ,
12
+ fill : 'FILL' ,
13
+ [ 'fill-extrusion' ] : 'FILLEXTRUSION'
14
+ } ;
15
+
16
+ const LegendPointStyleKey = [ 'symbolsContent' , 'size' , 'color' , 'opacity' ] ;
17
+
18
+ const LegendLineStyleKey = [ 'width' , 'color' , 'opacity' , 'lineDasharray' , 'symbolsContent' ] ;
19
+
20
+ const LegendFillStyleKey = [ 'color' , 'opacity' , 'antialias' , 'outlineColor' , 'symbolsContent' ] ;
21
+
22
+ const LegendFILLEXTRUSIONStyleKey = [ 'color' , 'opacity' , 'symbolsContent' ] ;
23
+
24
+ export const LEGEND_STYLE_KEYS = {
25
+ POINT : LegendPointStyleKey ,
26
+ LINE : LegendLineStyleKey ,
27
+ FILL : LegendFillStyleKey ,
28
+ FILLEXTRUSION : LegendFILLEXTRUSIONStyleKey
29
+ } ;
30
+
8
31
export class WebMap extends mapboxgl . Evented {
9
32
constructor ( mapId , options ) {
10
33
super ( ) ;
@@ -15,7 +38,10 @@ export class WebMap extends mapboxgl.Evented {
15
38
this . mapResourceUrl = Util . transformUrl ( Object . assign ( { url : `${ this . server } web/maps/${ mapId } ` } , this . options ) ) ;
16
39
this . _layersOfV3 = [ ] ;
17
40
this . _layerIdMapList = { } ;
41
+ this . _legendList = [ ] ;
18
42
this . _mapResourceInfo = { } ;
43
+ this . _sprite = '' ;
44
+ this . _spriteDatas = { } ;
19
45
}
20
46
21
47
/**
@@ -67,6 +93,8 @@ export class WebMap extends mapboxgl.Evented {
67
93
this . map = new mapboxgl . Map ( mapOptions ) ;
68
94
this . fire ( 'mapinitialized' , { map : this . map } ) ;
69
95
this . map . on ( 'load' , ( ) => {
96
+ sprite && this . _getSpriteDatas ( sprite ) ;
97
+ this . _sprite = sprite ;
70
98
this . _initLayers ( ) ;
71
99
} ) ;
72
100
}
@@ -98,26 +126,18 @@ export class WebMap extends mapboxgl.Evented {
98
126
* @description 创建地图相关资源。
99
127
*/
100
128
_createMapRelatedInfo ( ) {
101
- const { glyphs, sprite } = this . _mapInfo ;
129
+ const { glyphs } = this . _mapInfo ;
102
130
for ( let key in glyphs ) {
103
131
this . map . style . addGlyphs ( key , glyphs [ key ] ) ;
104
132
}
105
- // if (typeof sprite === 'object') {
106
- // for (let key in sprite) {
107
- // this.map.style.addSprite(key, sprite[key]);
108
- // }
109
- // } else {
110
- // this.map.style.sprite = sprite;
111
- // this.map.setStyle(this.map.style);
112
- // }
113
133
}
114
134
115
135
/**
116
136
* @private
117
137
* @function WebMap.prototype._getMapRelatedInfo
118
138
* @description 获取地图关联信息的 JSON 信息。
119
139
*/
120
- _getMapRelatedInfo ( ) {
140
+ _getMapRelatedInfo ( ) {
121
141
return FetchRequest . get ( this . mapResourceUrl , null , { withCredentials : this . withCredentials } )
122
142
. then ( ( response ) => {
123
143
return response . json ( ) ;
@@ -212,34 +232,77 @@ export class WebMap extends mapboxgl.Evented {
212
232
* @description emit 图层加载成功事件。
213
233
*/
214
234
_sendMapToUser ( ) {
215
- const overlayLayers = this . _generateV2LayersStructure ( ) ;
235
+ const overlayLayers = this . _generateLayers ( ) ;
216
236
this . fire ( 'addlayerssucceeded' , { map : this . map , mapparams : this . mapParams , layers : overlayLayers } ) ;
217
237
}
218
238
239
+ _getLayerInfosFromCatalogs ( catalogs ) {
240
+ let results = [ ] ;
241
+ for ( let i = 0 ; i < catalogs . length ; i ++ ) {
242
+ const { catalogType, children, visible } = catalogs [ i ] ;
243
+ if ( catalogType === 'layer' && visible ) {
244
+ results . push ( catalogs [ i ] ) ;
245
+ }
246
+ if ( catalogType === 'group' && children && children . lnegth ) {
247
+ const result = this . _getLayerInfosFromCatalogs ( children ) ;
248
+ results = results . concat ( result ) ;
249
+ }
250
+ }
251
+ return results ;
252
+ }
253
+
254
+ getMapInfo ( ) {
255
+ return this . _mapInfo ;
256
+ }
257
+
258
+ getLegendInfo ( ) {
259
+ return this . _legendList ;
260
+ }
261
+
219
262
/**
220
263
* @private
221
264
* @function WebMap.prototype._generateV2LayersStructure
222
265
* @description emit 图层加载成功事件。
223
266
* @param {Array<Object> } layers - 图层信息。
224
267
*/
225
- _generateV2LayersStructure ( ) {
226
- const originLayers = this . _mapResourceInfo . catalogs . filter ( item => item . visible ) ;
268
+ _generateLayers ( ) {
269
+ const originLayers = this . _getLayerInfosFromCatalogs ( this . _mapResourceInfo . catalogs ) ;
270
+ let _this = this ;
227
271
const layers = originLayers . map ( layer => {
228
- const { themeSetting = { } , title } = layer ;
272
+ const { visualization , title } = layer ;
229
273
const realLayerId = this . _layerIdMapList [ layer . id ] ;
230
- let layerType = themeSetting . type ;
231
- if ( ! layerType ) {
232
- const matchLayer = this . _mapInfo . layers . find ( item => item . id === layer . id ) ;
233
- layerType = this . _mapInfo . sources [ matchLayer . source ] . type ;
234
- if ( layerType === 'raster' ) {
235
- layerType = 'TIle' ;
236
- }
237
- }
238
- const overlayLayers = {
274
+ let dataId = '' ;
275
+ let layerType = _this . _mapInfo . layers . find ( function ( item ) {
276
+ return item . id === layer . id ;
277
+ } ) . type ;
278
+ this . _createLegendInfo ( layer , layerType ) ;
279
+ let type = layerType ;
280
+ layerType = layerType === 'raster' ? 'raster' : 'vector' ;
281
+ var dataType = '' ;
282
+ _this . _mapResourceInfo . datas . forEach ( ( data ) => {
283
+ data . datasets . forEach ( ( dataset ) => {
284
+ if ( dataset . msDatasetId === layer . msDatasetId ) {
285
+ dataType = data . sourceType ;
286
+ dataId = dataset . datasetId ;
287
+ }
288
+ } ) ;
289
+ } ) ;
290
+ var overlayLayers = {
291
+ dataSource : {
292
+ serverId : dataId ,
293
+ type : dataType
294
+ } ,
239
295
layerID : realLayerId ,
240
- name : title ,
241
- layerType : layerType ,
242
- themeSetting
296
+ layerType,
297
+ type,
298
+ name : title
299
+ } ;
300
+ if ( visualization ) {
301
+ if ( visualization . renderer [ 0 ] && visualization . renderer [ 0 ] . color && visualization . renderer [ 0 ] . color . field ) {
302
+ overlayLayers . themeSetting = {
303
+ themeField : visualization . renderer [ 0 ] . color . field
304
+ }
305
+ }
243
306
}
244
307
return overlayLayers ;
245
308
} )
@@ -263,4 +326,73 @@ export class WebMap extends mapboxgl.Evented {
263
326
const fontFamilys = fonts . join ( ',' ) ;
264
327
return fontFamilys ;
265
328
}
329
+
330
+ /**
331
+ * @private
332
+ * @function WebMap.prototype._getSpriteDatas
333
+ * @description 获取雪碧图信息。
334
+ */
335
+ _getSpriteDatas ( spriteUrl ) {
336
+ return FetchRequest . get ( spriteUrl , null , { withCredentials : this . withCredentials } )
337
+ . then ( ( response ) => {
338
+ return response . json ( ) ;
339
+ } ) . then ( ( res ) => {
340
+ this . _spriteDatas = res ;
341
+ } ) ;
342
+ }
343
+
344
+ _createLegendInfo ( layer , layerType ) {
345
+ const legendType = LAEYR_TYPE_LEGEND_TYPE [ layerType ] ;
346
+ const keys = LEGEND_STYLE_KEYS [ legendType ] ;
347
+ const styleList = layer . visualization . renderer [ 0 ] ;
348
+ const simpleStyle = this . _getLegendSimpleStyle ( layerType , styleList ) ;
349
+
350
+ keys . forEach ( ( keyName ) => {
351
+ if ( ! simpleStyle [ keyName ] ) {
352
+ const legendItemInfo = {
353
+ themeField : styleList [ keyName ] . field [ 0 ] ,
354
+ styleGroup : [ ] ,
355
+ layerId : layer . id
356
+ } ;
357
+ if ( keyName === 'color' ) {
358
+ let symbolId = simpleStyle [ 'symbolsContent' ] . value . symbol ;
359
+ if ( symbolId ) {
360
+ let symbolInfo = this . _spriteDatas [ symbolId ] ;
361
+ styleList [ keyName ] . values . forEach ( ( info ) => {
362
+ let groupItemInfo = {
363
+ value : info . key ,
364
+ style : {
365
+ backgroundColor : info . value
366
+ }
367
+ }
368
+ if ( symbolInfo ) {
369
+ const { width, height, x, y, pixelRatio } = symbolInfo ;
370
+ groupItemInfo . style . width = `${ width } px` ;
371
+ groupItemInfo . style . height = `${ height } px` ;
372
+ groupItemInfo . style . backgroundPosition = `-${ x * pixelRatio } px -${ y * pixelRatio } px` ;
373
+ groupItemInfo . style . backgroundImage = `url(${ this . _sprite } .png)` ;
374
+ }
375
+ if ( legendType === 'FILL' ) {
376
+ groupItemInfo . style . width = '20px' ;
377
+ groupItemInfo . style . height = '20px' ;
378
+ }
379
+ legendItemInfo . styleGroup . push ( groupItemInfo ) ;
380
+ } ) ;
381
+ }
382
+ }
383
+ this . _legendList . push ( legendItemInfo ) ;
384
+ }
385
+ } ) ;
386
+ }
387
+
388
+ _getLegendSimpleStyle ( layerType , styleSetting ) {
389
+ const simpleStyle = { } ;
390
+ const legendType = LAEYR_TYPE_LEGEND_TYPE [ layerType ] ;
391
+ const keys = LEGEND_STYLE_KEYS [ legendType ] ;
392
+ const simpleKeys = keys . filter ( k => styleSetting [ k ] && styleSetting [ k ] . type === 'simple' ) ;
393
+ simpleKeys . forEach ( k => {
394
+ simpleStyle [ k ] = styleSetting [ k ] && styleSetting [ k ] . value ;
395
+ } ) ;
396
+ return simpleStyle ;
397
+ }
266
398
}
0 commit comments