-
Notifications
You must be signed in to change notification settings - Fork 288
Expand file tree
/
Copy pathMapStyle.js
More file actions
184 lines (176 loc) · 6.36 KB
/
MapStyle.js
File metadata and controls
184 lines (176 loc) · 6.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import { WebMapService } from './WebMapService';
import { SourceListModelV2 } from './utils/SourceListModelV2';
import { createAppreciableLayerId, isSameRasterLayer } from './utils/util';
export function createMapStyleExtending(SuperClass, { MapManager, crsManager }) {
return class MapStyle extends SuperClass {
constructor(id, options = {}, mapOptions = {}) {
super();
this.options = options;
this.mapOptions = mapOptions;
this.webMapService = new WebMapService(id, options);
this._layerIdRenameMapList = [];
this._appendLayers = false;
this._baseProjection = '';
}
initializeMap(_, map) {
this._baseProjection = this._registerMapCRS(this.mapOptions);
if (map) {
if (!crsManager.isSameProjection(map, this._baseProjection)) {
this.fire('projectionnotmatch');
return;
}
this._appendLayers = true;
this.map = map;
this._addLayersToMap();
return;
}
this.mapOptions.container = this.options.target;
if (!this.mapOptions.transformRequest) {
this.mapOptions.transformRequest = (url, resourceType) => {
let proxy = '';
if (typeof this.options.proxy === 'string') {
let proxyType = 'data';
if (resourceType === 'Tile') {
proxyType = 'image';
}
proxy = this.webMapService.handleProxy(proxyType);
}
return {
url: proxy ? `${proxy}${encodeURIComponent(url)}` : url,
credentials: this.webMapService.handleWithCredentials(proxy, url, false)
? 'include'
: undefined,
...(this.options.tileTransformRequest && this.options.tileTransformRequest(url))
};
};
}
this.mapOptions.center = this.mapOptions.center || [0, 0];
this.mapOptions.zoom = this.mapOptions.zoom || 0;
let fadeDuration = 0;
if (Object.prototype.hasOwnProperty.call(this.mapOptions, 'fadeDuration')) {
fadeDuration = this.mapOptions.fadeDuration;
}
this.map = new MapManager({ ...this.mapOptions, fadeDuration, crs: this._baseProjection });
this.fire('mapinitialized', { map: this.map });
this.map.on('load', () => {
this._sendMapToUser();
});
}
clean(removeMap = true) {
if (this.map) {
if (this._sourceListModel) {
this._sourceListModel.destroy();
this._sourceListModel = null;
}
removeMap && this.map.remove();
this.map = null;
}
}
_registerMapCRS(mapOptions) {
const { crs } = mapOptions;
let epsgCode = crs;
if (typeof crs === 'object' && crs.epsgCode) {
const { epsgCode: name, WKT: wkt, extent, unit } = crs;
crsManager.registerCRS({ name, wkt, extent, unit });
epsgCode = name;
}
return epsgCode;
}
_addLayersToMap() {
const { sources, layers, layerIdMapList } = this._setUniqueId(this.mapOptions.style);
layers.forEach((layer) => {
const matchRenameLayer = layerIdMapList.find(sub => sub.renderId === layer.id);
if (matchRenameLayer && matchRenameLayer.reused) {
return;
}
layer.source && !this.map.getSource(layer.source) && this.map.addSource(layer.source, sources[layer.source]);
this.map.addLayer(layer);
});
Object.assign(this.mapOptions.style, { layers, sources });
this._layerIdRenameMapList = layerIdMapList;
this._sendMapToUser();
}
_setUniqueId(style) {
const layersToMap = JSON.parse(JSON.stringify(style.layers));
const nextSources = {};
const sourcesIdChangedMap = {};
const layerIdToChange = [];
const timestamp = `_${+new Date()}`;
for (const sourceId in style.sources) {
let nextSourceId = sourceId;
if (this.map.getSource(sourceId)) {
nextSourceId = sourceId + timestamp;
}
sourcesIdChangedMap[nextSourceId] = sourceId;
nextSources[nextSourceId] = style.sources[sourceId];
for (const layer of layersToMap) {
if (layer.source === sourceId) {
layer.source = nextSourceId;
}
}
}
for (const layer of layersToMap) {
const originId = layer.id;
let reused;
const existLayer = this.map.getLayer(layer.id);
if (existLayer) {
// 此时用 getSource(xx).tiles 为空
if (
this.options.checkSameLayer &&
isSameRasterLayer(nextSources[layer.source], this.map.getStyle().sources[existLayer.source])
) {
reused = true;
layer.source = sourcesIdChangedMap[layer.source];
} else {
const layerId = layer.id + timestamp;
layer.id = layerId;
}
}
layerIdToChange.push({ originId: originId, renderId: layer.id, reused });
}
return {
sources: nextSources,
layers: layersToMap,
layerIdMapList: layerIdToChange
};
}
_generateAppreciableLayers() {
return this.mapOptions.style.layers.reduce((layers, layer) => {
const nameLayer = layer['source-layer'] ? layer : { ...layer, layerInfo: { id: layer.id } };
const id = createAppreciableLayerId(nameLayer);
const matchLayer = layers.find(item => item.id === id);
if (matchLayer) {
matchLayer.renderLayers.push(layer.id);
} else {
const matchRenameLayer = this._layerIdRenameMapList.find((item) => item.renderId === layer.id);
layers.push({
...layer,
id,
name: matchRenameLayer && matchRenameLayer.originId,
renderLayers: [layer.id],
reused: matchRenameLayer && matchRenameLayer.reused
});
}
return layers;
}, []);
}
_sendMapToUser() {
const layersFromStyle = this._generateAppreciableLayers();
this._sourceListModel = new SourceListModelV2({
map: this.map,
layers: layersFromStyle,
appendLayers: this._appendLayers
});
this._sourceListModel.on({
layerupdatechanged: (params) => {
this.fire('layerupdatechanged', params);
}
});
this.fire('mapcreatesucceeded', {
map: this.map,
mapparams: { title: this.mapOptions.name, description: '' },
layers: this.getSelfAppreciableLayers()
});
}
};
}