<commit_before>goog.provide('olcs.VectorSynchronizer');

goog.require('goog.events');

goog.require('olcs.core');



/**
 * Unidirectionally synchronize OpenLayers vector layers to Cesium.
 * @param {!ol.Map} map
 * @param {!Cesium.Scene} scene
 * @constructor
 */
olcs.VectorSynchronizer = function(map, scene) {

  /**
   * @type {!Cesium.PrimitiveCollection}
   * @private
   */
  this.csAllPrimitives_ = scene.primitives.add(new Cesium.PrimitiveCollection());

  // Initialize core library
  olcs.core.glAliasedLineWidthRange = scene.maximumAliasedLineWidth;

  /**
   * @type {!ol.Map}
   * @private
   */
  this.map_ = map;

  /**
   * Map of ol3 layer ids (from goog.getUid) to the Cesium PrimitiveCollection.
   * null value means, that we are unable to create equivalent layer.
   * @type {Object.<number, ?Cesium.PrimitiveCollection>}
   * @private
   */
  this.layerMap_ = {};
  var layers = map.getLayers(); // FIXME: have ol3 guarantee the layer
                                //reference never changes
  goog.events.listen(/** @type {!goog.events.EventTarget} */(layers),
      ['change', 'add', 'remove'], function(e) {
        this.synchronize();
      }, false, this);
};


/**
 * Performs complete synchronization of the vector layers.
 */
olcs.VectorSynchronizer.prototype.synchronize = function() {
  var view = this.map_.getView(); // view reference might change
  if (!goog.isDefAndNotNull(view)) {
    return;
  }
  var olLayers = this.map_.getLayers();
  var unusedCesiumPrimitives = goog.object.transpose(this.layerMap_);
  this.csAllPrimitives_.destroyPrimitives = false;
  this.csAllPrimitives_.removeAll();

  var synchronizeLayer = goog.bind(function(olLayer) {
    // handle layer groups
    if (olLayer instanceof ol.layer.Group) {
      var sublayers = olLayer.getLayers();
      if (goog.isDef(sublayers)) {
        sublayers.forEach(function(el, i, arr) {
          synchronizeLayer(el);
        });
      }
      return;
    }

    var olLayerId = goog.getUid(olLayer);
    var csPrimitives = this.layerMap_[olLayerId];

    // no mapping -> create new layer and set up synchronization
    if (!goog.isDef(csPrimitives)) {
      view = /** @type {!ol.View} */ (view);
      csPrimitives = olcs.core.olVectorLayerToCesium(olLayer, view);

      if (csPrimitives) {
        olLayer.on('change:visible', function(e) {
          csPrimitives.show = olLayer.getVisible();
        });
      }
      this.layerMap_[olLayerId] = csPrimitives;
    }

    // add Cesium layers
    if (csPrimitives) {
      this.csAllPrimitives_.add(csPrimitives);
      delete unusedCesiumPrimitives[csPrimitives];
    }
  }, this);

  olLayers.forEach(function(el, i, arr) {
    if (el instanceof ol.layer.Vector)
      synchronizeLayer(el);
  });

  // destroy unused Cesium primitives
  goog.object.getValues(unusedCesiumPrimitives).forEach(function(el, i, arr) {
        var layerId = el;
        var primitives = this.layerMap_[layerId];
        if (goog.isDef(primitives)) {
          delete this.layerMap_[layerId];
          primitives.destroy();
        }
      }, this);
};
<commit_msg>Fix vector synchronizer with layer groups.<commit_after>goog.provide('olcs.VectorSynchronizer');

goog.require('goog.events');

goog.require('olcs.core');



/**
 * Unidirectionally synchronize OpenLayers vector layers to Cesium.
 * @param {!ol.Map} map
 * @param {!Cesium.Scene} scene
 * @constructor
 */
olcs.VectorSynchronizer = function(map, scene) {

  /**
   * @type {!Cesium.PrimitiveCollection}
   * @private
   */
  this.csAllPrimitives_ = scene.primitives.add(new Cesium.PrimitiveCollection());

  // Initialize core library
  olcs.core.glAliasedLineWidthRange = scene.maximumAliasedLineWidth;

  /**
   * @type {!ol.Map}
   * @private
   */
  this.map_ = map;

  /**
   * Map of ol3 layer ids (from goog.getUid) to the Cesium PrimitiveCollection.
   * null value means, that we are unable to create equivalent layer.
   * @type {Object.<number, ?Cesium.PrimitiveCollection>}
   * @private
   */
  this.layerMap_ = {};
  var layers = map.getLayers(); // FIXME: have ol3 guarantee the layer
                                //reference never changes
  goog.events.listen(/** @type {!goog.events.EventTarget} */(layers),
      ['change', 'add', 'remove'], function(e) {
        this.synchronize();
      }, false, this);
};


/**
 * Performs complete synchronization of the vector layers.
 */
olcs.VectorSynchronizer.prototype.synchronize = function() {
  var view = this.map_.getView(); // view reference might change
  if (!goog.isDefAndNotNull(view)) {
    return;
  }
  var olLayers = this.map_.getLayers();
  var unusedCesiumPrimitives = goog.object.transpose(this.layerMap_);
  this.csAllPrimitives_.destroyPrimitives = false;
  this.csAllPrimitives_.removeAll();


  var synchronizeLayer = goog.bind(function(olLayer) {
    // handle layer groups
    if (olLayer instanceof ol.layer.Group) {
      var sublayers = olLayer.getLayers();
      if (goog.isDef(sublayers)) {
        sublayers.forEach(function(el, i, arr) {
          synchronizeLayer(el);
        });
      }
      return;
    } else if (!(olLayer instanceof ol.layer.Vector)) {
      return;
    }

    var olLayerId = goog.getUid(olLayer);
    var csPrimitives = this.layerMap_[olLayerId];

    // no mapping -> create new layer and set up synchronization
    if (!goog.isDef(csPrimitives)) {
      view = /** @type {!ol.View} */ (view);
      csPrimitives = olcs.core.olVectorLayerToCesium(olLayer, view);

      if (csPrimitives) {
        olLayer.on('change:visible', function(e) {
          csPrimitives.show = olLayer.getVisible();
        });
      }
      this.layerMap_[olLayerId] = csPrimitives;
    }

    // add Cesium layers
    if (csPrimitives) {
      this.csAllPrimitives_.add(csPrimitives);
      delete unusedCesiumPrimitives[csPrimitives];
    }
  }, this);


  olLayers.forEach(function(el, i, arr) {
    synchronizeLayer(el);
  });

  // destroy unused Cesium primitives
  goog.object.getValues(unusedCesiumPrimitives).forEach(function(el, i, arr) {
        var layerId = el;
        var primitives = this.layerMap_[layerId];
        if (goog.isDef(primitives)) {
          delete this.layerMap_[layerId];
          primitives.destroy();
        }
      }, this);
};

{'fraction_non_alphanumeric': 0.13440688775510204, 'fraction_numerical': 0.0006377551020408163, 'mean_word_length': 2.957728706624606, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5417473', 'n_tokens_mistral': 2073, 'n_tokens_neox': 1962, 'n_words': 411}