Skip to content

Commit 6ed7afd

Browse files
mapboxgl maplibregl 提取 webmap公共代码 review by xiongjj
1 parent 867365c commit 6ed7afd

29 files changed

+5898
-5752
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@
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",

src/common/mapping/MapStyle.js

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

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy