Skip to content

Commit f0de46e

Browse files
committed
【feature】webmap v2优化;抽取 mapboxgl maplibregl 公共代码
1 parent 572d4b8 commit f0de46e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

72 files changed

+23597
-6604
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,11 @@
139139
"@antv/g6": "^4.8.14",
140140
"@mapbox/mapbox-gl-style-spec": "^14.3.0",
141141
"@mapbox/vector-tile": "1.3.1",
142+
"@maplibre/maplibre-gl-style-spec": "^20.3.0",
142143
"@supermapgis/iclient-common": "file:src/common",
143144
"@supermapgis/tile-decryptor": "^1.0.0",
144145
"@turf/center": "^6.5.0",
146+
"@turf/meta": "^6.5.0",
145147
"@turf/turf": "6.5.0",
146148
"canvg": "3.0.10",
147149
"echarts": "5.5.0",

src/common/index.common.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -750,3 +750,4 @@ export { CartoCSS, ThemeStyle };
750750
export { ElasticSearch };
751751
export { Lang };
752752
export { KnowledgeGraph } from './overlay/KnowledgeGraph';
753+
export * from './mapping';

src/common/mapping/MapBase.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
export function createMapClassExtending(SuperClass = class {}) {
2+
return class MapBase extends SuperClass {
3+
constructor() {
4+
super();
5+
this._sourceListModel = null;
6+
this._legendList = [];
7+
}
8+
9+
initializeMap() {
10+
throw new Error('initializeMap is not implemented');
11+
}
12+
13+
clean() {
14+
throw new Error('clean is not implemented');
15+
}
16+
17+
getLayerCatalog() {
18+
return (this._sourceListModel && this._sourceListModel.getSourceList()) || [];
19+
}
20+
21+
getAppreciableLayers() {
22+
return (this._sourceListModel && this._sourceListModel.getLayers()) || [];
23+
}
24+
25+
getLegendInfo() {
26+
return this._legendList;
27+
}
28+
29+
getSelfAppreciableLayers(appreciableLayers) {
30+
return (this._sourceListModel && this._sourceListModel.getSelfLayers(appreciableLayers)) || [];
31+
}
32+
33+
echartsLayerResize() {}
34+
35+
updateOverlayLayer() {}
36+
37+
copyLayer() {}
38+
};
39+
}

src/common/mapping/MapStyle.js

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
import { WebMapService } from './WebMapService';
2+
import { SourceListModel } from './utils/SourceListModelV2';
3+
4+
export function createMapStyleExtending(SuperClass, { MapManager, mapRepo }) {
5+
return class MapStyle extends SuperClass {
6+
constructor(id, options = {}, mapOptions = {}) {
7+
super();
8+
this.options = options;
9+
this.mapOptions = mapOptions;
10+
this.webMapService = new WebMapService(id, options);
11+
this._layerIdRenameMapList = [];
12+
this._appendLayers = false;
13+
}
14+
15+
initializeMap(_, map) {
16+
if (map) {
17+
this._appendLayers = true;
18+
this.map = map;
19+
this._addLayersToMap();
20+
return;
21+
}
22+
this.mapOptions.container = this.options.target;
23+
if (typeof this.mapOptions.crs === 'object' && this.mapOptions.crs.epsgCode) {
24+
this.mapOptions.crs = new mapRepo.CRS(
25+
this.mapOptions.crs.epsgCode,
26+
this.mapOptions.crs.WKT,
27+
this.mapOptions.crs.extent,
28+
this.mapOptions.crs.unit
29+
);
30+
}
31+
if (!this.mapOptions.transformRequest) {
32+
this.mapOptions.transformRequest = (url, resourceType) => {
33+
let proxy = '';
34+
if (typeof this.options.proxy === 'string') {
35+
let proxyType = 'data';
36+
if (resourceType === 'Tile') {
37+
proxyType = 'image';
38+
}
39+
proxy = this.webMapService.handleProxy(proxyType);
40+
}
41+
return {
42+
url: proxy ? `${proxy}${encodeURIComponent(url)}` : url,
43+
credentials: this.webMapService.handleWithCredentials(proxy, url, this.options.withCredentials || false)
44+
? 'include'
45+
: undefined
46+
};
47+
};
48+
}
49+
this.mapOptions.center = this.mapOptions.center || [0, 0];
50+
this.mapOptions.zoom = this.mapOptions.zoom || 0;
51+
let fadeDuration = 0;
52+
if (Object.prototype.hasOwnProperty.call(this.mapOptions, 'fadeDuration')) {
53+
fadeDuration = this.mapOptions.fadeDuration;
54+
}
55+
this.map = new MapManager({ ...this.mapOptions, fadeDuration });
56+
this.fire('mapinitialized', { map: this.map });
57+
this.map.on('load', () => {
58+
this._sendMapToUser();
59+
});
60+
}
61+
62+
clean(removeMap = true) {
63+
if (this.map) {
64+
removeMap && this.map.remove();
65+
this.map = null;
66+
this._sourceListModel = null;
67+
}
68+
}
69+
70+
_addLayersToMap() {
71+
const { sources, layers, layerIdMapList } = this._setUniqueId(this.mapOptions.style);
72+
layers.forEach((layer) => {
73+
layer.source && !this.map.getSource(layer.source) && this.map.addSource(layer.source, sources[layer.source]);
74+
this.map.addLayer(layer);
75+
});
76+
Object.assign(this.mapOptions.style, { layers, sources });
77+
this._layerIdRenameMapList = layerIdMapList;
78+
this._sendMapToUser();
79+
}
80+
81+
_setUniqueId(style) {
82+
const layersToMap = JSON.parse(JSON.stringify(style.layers));
83+
const nextSources = {};
84+
const layerIdToChange = [];
85+
const timestamp = `_${+new Date()}`;
86+
for (const sourceId in style.sources) {
87+
let nextSourceId = sourceId;
88+
if (this.map.getSource(sourceId)) {
89+
nextSourceId = sourceId + timestamp;
90+
}
91+
nextSources[nextSourceId] = style.sources[sourceId];
92+
for (const layer of layersToMap) {
93+
if (layer.source === sourceId) {
94+
layer.source = nextSourceId;
95+
}
96+
}
97+
}
98+
for (const layer of layersToMap) {
99+
const originId = layer.id;
100+
if (this.map.getLayer(layer.id)) {
101+
const layerId = layer.id + timestamp;
102+
layer.id = layerId;
103+
}
104+
layerIdToChange.push({ originId: originId, renderId: layer.id });
105+
}
106+
return {
107+
sources: nextSources,
108+
layers: layersToMap,
109+
layerIdMapList: layerIdToChange
110+
};
111+
}
112+
113+
_generateAppreciableLayers() {
114+
return this.mapOptions.style.layers.reduce((layers, layer) => {
115+
const id = layer['source-layer'] || layer.source || layer.id;
116+
const matchLayer = layers.find(item => item.id === id);
117+
if (matchLayer) {
118+
matchLayer.renderLayers.push(layer.id);
119+
} else {
120+
const matchRenameLayer = this._layerIdRenameMapList.find((item) => item.renderId === layer.id);
121+
layers.push({
122+
...layer,
123+
id,
124+
name: matchRenameLayer && matchRenameLayer.originId,
125+
renderLayers: [layer.id]
126+
});
127+
}
128+
return layers;
129+
}, []);
130+
}
131+
132+
_sendMapToUser() {
133+
const layersFromStyle = this._generateAppreciableLayers();
134+
this._sourceListModel = new SourceListModel({
135+
map: this.map,
136+
layers: layersFromStyle,
137+
appendLayers: this._appendLayers
138+
});
139+
this.fire('addlayerssucceeded', {
140+
map: this.map,
141+
mapparams: { title: this.mapOptions.name, description: '' },
142+
layers: this.getSelfAppreciableLayers()
143+
});
144+
}
145+
};
146+
}

0 commit comments

Comments
 (0)