Skip to content

Commit 3d4d0a3

Browse files
committed
【feature】处理可感知图层,对接图例部分单值; review by qiw
1 parent 46faafd commit 3d4d0a3

File tree

1 file changed

+158
-26
lines changed

1 file changed

+158
-26
lines changed

src/mapboxgl/mapping/webmap/v3/WebMap.js

Lines changed: 158 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,29 @@ import mapboxgl from 'mapbox-gl';
55
import { FetchRequest } from '@supermap/iclient-common/util/FetchRequest';
66
import { Util } from '../../../core/Util';
77

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+
831
export class WebMap extends mapboxgl.Evented {
932
constructor(mapId, options) {
1033
super();
@@ -15,7 +38,10 @@ export class WebMap extends mapboxgl.Evented {
1538
this.mapResourceUrl = Util.transformUrl(Object.assign({ url: `${this.server}web/maps/${mapId}` }, this.options));
1639
this._layersOfV3 = [];
1740
this._layerIdMapList = {};
41+
this._legendList = [];
1842
this._mapResourceInfo = {};
43+
this._sprite = '';
44+
this._spriteDatas = {};
1945
}
2046

2147
/**
@@ -67,6 +93,8 @@ export class WebMap extends mapboxgl.Evented {
6793
this.map = new mapboxgl.Map(mapOptions);
6894
this.fire('mapinitialized', { map: this.map });
6995
this.map.on('load', () => {
96+
sprite && this._getSpriteDatas(sprite);
97+
this._sprite = sprite;
7098
this._initLayers();
7199
});
72100
}
@@ -98,26 +126,18 @@ export class WebMap extends mapboxgl.Evented {
98126
* @description 创建地图相关资源。
99127
*/
100128
_createMapRelatedInfo() {
101-
const { glyphs, sprite } = this._mapInfo;
129+
const { glyphs } = this._mapInfo;
102130
for (let key in glyphs) {
103131
this.map.style.addGlyphs(key, glyphs[key]);
104132
}
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-
// }
113133
}
114134

115135
/**
116136
* @private
117137
* @function WebMap.prototype._getMapRelatedInfo
118138
* @description 获取地图关联信息的 JSON 信息。
119139
*/
120-
_getMapRelatedInfo() {
140+
_getMapRelatedInfo() {
121141
return FetchRequest.get(this.mapResourceUrl, null, { withCredentials: this.withCredentials })
122142
.then((response) => {
123143
return response.json();
@@ -212,34 +232,77 @@ export class WebMap extends mapboxgl.Evented {
212232
* @description emit 图层加载成功事件。
213233
*/
214234
_sendMapToUser() {
215-
const overlayLayers = this._generateV2LayersStructure();
235+
const overlayLayers = this._generateLayers();
216236
this.fire('addlayerssucceeded', { map: this.map, mapparams: this.mapParams, layers: overlayLayers });
217237
}
218238

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+
219262
/**
220263
* @private
221264
* @function WebMap.prototype._generateV2LayersStructure
222265
* @description emit 图层加载成功事件。
223266
* @param {Array<Object>} layers - 图层信息。
224267
*/
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;
227271
const layers = originLayers.map(layer => {
228-
const { themeSetting = {}, title } = layer;
272+
const { visualization, title } = layer;
229273
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+
},
239295
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+
}
243306
}
244307
return overlayLayers;
245308
})
@@ -263,4 +326,73 @@ export class WebMap extends mapboxgl.Evented {
263326
const fontFamilys = fonts.join(',');
264327
return fontFamilys;
265328
}
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+
}
266398
}

0 commit comments

Comments
 (0)