2023西安数博会CIM演示-【前端】-Web
AdaKing88
2023-08-21 bc03b832caa49bbcd2674fe4cae3701b5059bf95
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/**
 * @module olcs.AbstractSynchronizer
 */
import {unByKey as olObservableUnByKey} from 'ol/Observable.js';
import olLayerGroup from 'ol/layer/Group.js';
import {olcsListen, getUid} from './util.js';
 
 
class AbstractSynchronizer {
  /**
   * @param {!ol.Map} map
   * @param {!Cesium.Scene} scene
   * @template T
   * @abstract
   * @api
   */
  constructor(map, scene) {
    /**
     * @type {!ol.Map}
     * @protected
     */
    this.map = map;
 
    /**
     * @type {ol.View}
     * @protected
     */
    this.view = map.getView();
 
    /**
     * @type {!Cesium.Scene}
     * @protected
     */
    this.scene = scene;
 
    /**
     * @type {ol.Collection.<ol.layer.Base>}
     * @protected
     */
    this.olLayers = map.getLayerGroup().getLayers();
 
    /**
     * @type {ol.layer.Group}
     */
    this.mapLayerGroup = map.getLayerGroup();
 
    /**
     * Map of OpenLayers layer ids (from getUid) to the Cesium ImageryLayers.
     * Null value means, that we are unable to create equivalent layers.
     * @type {Object.<string, ?Array.<T>>}
     * @protected
     */
    this.layerMap = {};
 
    /**
     * Map of listen keys for OpenLayers layer layers ids (from getUid).
     * @type {!Object.<string, Array<ol.EventsKey>>}
     * @protected
     */
    this.olLayerListenKeys = {};
 
    /**
     * Map of listen keys for OpenLayers layer groups ids (from getUid).
     * @type {!Object.<string, !Array.<ol.EventsKey>>}
     * @private
     */
    this.olGroupListenKeys_ = {};
  }
 
  /**
   * Destroy all and perform complete synchronization of the layers.
   * @api
   */
  synchronize() {
    this.destroyAll();
    this.addLayers_(this.mapLayerGroup);
  }
 
  /**
   * Order counterparts using the same algorithm as the Openlayers renderer:
   * z-index then original sequence order.
   * @protected
   */
  orderLayers() {
    // Ordering logics is handled in subclasses.
  }
 
  /**
   * Add a layer hierarchy.
   * @param {ol.layer.Base} root
   * @private
   */
  addLayers_(root) {
    /** @type {Array<import('olsc/core.js').LayerWithParents>} */
    const fifo = [{
      layer: root,
      parents: []
    }];
    while (fifo.length > 0) {
      const olLayerWithParents = fifo.splice(0, 1)[0];
      const olLayer = olLayerWithParents.layer;
      const olLayerId = getUid(olLayer).toString();
      this.olLayerListenKeys[olLayerId] = [];
      console.assert(!this.layerMap[olLayerId]);
 
      let cesiumObjects = null;
      if (olLayer instanceof olLayerGroup) {
        this.listenForGroupChanges_(olLayer);
        if (olLayer !== this.mapLayerGroup) {
          cesiumObjects = this.createSingleLayerCounterparts(olLayerWithParents);
        }
        if (!cesiumObjects) {
          olLayer.getLayers().forEach((l) => {
            if (l) {
              const newOlLayerWithParents = {
                layer: l,
                parents: olLayer === this.mapLayerGroup ?
                  [] :
                  [olLayerWithParents.layer].concat(olLayerWithParents.parents)
              };
              fifo.push(newOlLayerWithParents);
            }
          });
        }
      } else {
        cesiumObjects = this.createSingleLayerCounterparts(olLayerWithParents);
        if (!cesiumObjects) {
          // keep an eye on the layers that once failed to be added (might work when the layer is updated)
          // for example when a source is set after the layer is added to the map
          const layerId = olLayerId;
          const layerWithParents = olLayerWithParents;
          const onLayerChange = (e) => {
            const cesiumObjs = this.createSingleLayerCounterparts(layerWithParents);
            if (cesiumObjs) {
              // unsubscribe event listener
              layerWithParents.layer.un('change', onLayerChange);
              this.addCesiumObjects_(cesiumObjs, layerId, layerWithParents.layer);
              this.orderLayers();
            }
          };
          this.olLayerListenKeys[olLayerId].push(olcsListen(layerWithParents.layer, 'change', onLayerChange));
        }
      }
      // add Cesium layers
      if (cesiumObjects) {
        this.addCesiumObjects_(cesiumObjects, olLayerId, olLayer);
      }
    }
 
    this.orderLayers();
  }
 
  /**
   * Add Cesium objects.
   * @param {Array.<T>} cesiumObjects
   * @param {string} layerId
   * @param {ol.layer.Base} layer
   * @private
   */
  addCesiumObjects_(cesiumObjects, layerId, layer) {
    this.layerMap[layerId] = cesiumObjects;
    this.olLayerListenKeys[layerId].push(olcsListen(layer, 'change:zIndex', () => this.orderLayers()));
    cesiumObjects.forEach((cesiumObject) => {
      this.addCesiumObject(cesiumObject);
    });
  }
 
  /**
   * Remove and destroy a single layer.
   * @param {ol.layer.Layer} layer
   * @return {boolean} counterpart destroyed
   * @private
   */
  removeAndDestroySingleLayer_(layer) {
    const uid = getUid(layer).toString();
    const counterparts = this.layerMap[uid];
    if (!!counterparts) {
      counterparts.forEach((counterpart) => {
        this.removeSingleCesiumObject(counterpart, false);
        this.destroyCesiumObject(counterpart);
      });
      this.olLayerListenKeys[uid].forEach(olObservableUnByKey);
      delete this.olLayerListenKeys[uid];
    }
    delete this.layerMap[uid];
    return !!counterparts;
  }
 
  /**
   * Unlisten a single layer group.
   * @param {ol.layer.Group} group
   * @private
   */
  unlistenSingleGroup_(group) {
    if (group === this.mapLayerGroup) {
      return;
    }
    const uid = getUid(group).toString();
    const keys = this.olGroupListenKeys_[uid];
    keys.forEach((key) => {
      olObservableUnByKey(key);
    });
    delete this.olGroupListenKeys_[uid];
    delete this.layerMap[uid];
  }
 
  /**
   * Remove layer hierarchy.
   * @param {ol.layer.Base} root
   * @private
   */
  removeLayer_(root) {
    if (!!root) {
      const fifo = [root];
      while (fifo.length > 0) {
        const olLayer = fifo.splice(0, 1)[0];
        const done = this.removeAndDestroySingleLayer_(olLayer);
        if (olLayer instanceof olLayerGroup) {
          this.unlistenSingleGroup_(olLayer);
          if (!done) {
            // No counterpart for the group itself so removing
            // each of the child layers.
            olLayer.getLayers().forEach((l) => {
              fifo.push(l);
            });
          }
        }
      }
    }
  }
 
  /**
   * Register listeners for single layer group change.
   * @param {ol.layer.Group} group
   * @private
   */
  listenForGroupChanges_(group) {
    const uuid = getUid(group).toString();
 
    console.assert(this.olGroupListenKeys_[uuid] === undefined);
 
    const listenKeyArray = [];
    this.olGroupListenKeys_[uuid] = listenKeyArray;
 
    // only the keys that need to be relistened when collection changes
    let contentKeys = [];
    const listenAddRemove = (function() {
      const collection = group.getLayers();
      if (collection) {
        contentKeys = [
          collection.on('add', (event) => {
            this.addLayers_(event.element);
          }),
          collection.on('remove', (event) => {
            this.removeLayer_(event.element);
          })
        ];
        listenKeyArray.push(...contentKeys);
      }
    }).bind(this);
 
    listenAddRemove();
 
    listenKeyArray.push(group.on('change:layers', (e) => {
      contentKeys.forEach((el) => {
        const i = listenKeyArray.indexOf(el);
        if (i >= 0) {
          listenKeyArray.splice(i, 1);
        }
        olObservableUnByKey(el);
      });
      listenAddRemove();
    }));
  }
 
  /**
   * Destroys all the created Cesium objects.
   * @protected
   */
  destroyAll() {
    this.removeAllCesiumObjects(true); // destroy
    let objKey;
    for (objKey in this.olGroupListenKeys_) {
      const keys = this.olGroupListenKeys_[objKey];
      keys.forEach(olObservableUnByKey);
    }
    for (objKey in this.olLayerListenKeys) {
      this.olLayerListenKeys[objKey].forEach(olObservableUnByKey);
    }
    this.olGroupListenKeys_ = {};
    this.olLayerListenKeys = {};
    this.layerMap = {};
  }
 
  /**
   * Adds a single Cesium object to the collection.
   * @param {!T} object
   * @abstract
   * @protected
   */
  addCesiumObject(object) {}
 
  /**
   * @param {!T} object
   * @abstract
   * @protected
   */
  destroyCesiumObject(object) {}
 
  /**
   * Remove single Cesium object from the collection.
   * @param {!T} object
   * @param {boolean} destroy
   * @abstract
   * @protected
   */
  removeSingleCesiumObject(object, destroy) {}
 
  /**
   * Remove all Cesium objects from the collection.
   * @param {boolean} destroy
   * @abstract
   * @protected
   */
  removeAllCesiumObjects(destroy) {}
 
  /**
   * @param {import('olsc/core.js').LayerWithParents} olLayerWithParents
   * @return {?Array.<T>}
   * @abstract
   * @protected
   */
  createSingleLayerCounterparts(olLayerWithParents) {}
}
 
 
export default AbstractSynchronizer;