# all files / ol/source/ clustersource.js

31.88% Statements 22/69
10% Branches 1/10
14.29% Functions 1/7
31.88% Lines 22/69

    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

    1×

    1×
    1×
    1×
    1×
    1×
    1×
    1×
    1×

    1×
    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

     // FIXME keep cluster cache by resolution ?
    // FIXME distance not respected because of the centroid

    goog.provide('ol.source.Cluster');

    goog.require('goog.asserts');
    goog.require('goog.events.EventType');
    goog.require('goog.object');
    goog.require('ol.Feature');
    goog.require('ol.coordinate');
    goog.require('ol.extent');
    goog.require('ol.geom.Point');
    goog.require('ol.source.Vector');

    /**
     * @classdesc
     * Layer source to cluster vector data.
     *
     * @constructor
     * @param {olx.source.ClusterOptions} options
     * @extends {ol.source.Vector}
     * @api
     */
    ol.source.Cluster = function(options) {
      goog.base(this, {
        attributions: options.attributions,
        extent: options.extent,
        logo: options.logo,
        projection: options.projection,
        wrapX: options.wrapX
      });

      /**
       * @type {number|undefined}
       * @private
       */
      this.resolution_ = undefined;

      /**
       * @type {number}
       * @private
       */
      this.distance_ = options.distance !== undefined ? options.distance : 20;

      /**
       * @type {Array.<ol.Feature>}
       * @private
       */
      this.features_ = [];

      /**
       * @type {ol.source.Vector}
       * @private
       */
      this.source_ = options.source;

      this.source_.on(goog.events.EventType.CHANGE,
          ol.source.Cluster.prototype.onSourceChange_, this);
    };
    goog.inherits(ol.source.Cluster, ol.source.Vector);

    /**
     * Get a reference to the wrapped source.
     * @return {ol.source.Vector} Source.
     * @api
     */
    ol.source.Cluster.prototype.getSource = function() {
      return this.source_;
    };

    /**
     * @inheritDoc
     */
    ol.source.Cluster.prototype.loadFeatures = function(extent, resolution,
        projection) {
      this.source_.loadFeatures(extent, resolution, projection);
      if (resolution !== this.resolution_) {
        this.clear();
        this.resolution_ = resolution;
        this.cluster_();
        this.addFeatures(this.features_);
      }
    };

    /**
     * handle the source changing
     * @private
     */
    ol.source.Cluster.prototype.onSourceChange_ = function() {
      this.clear();
      this.cluster_();
      this.addFeatures(this.features_);
      this.changed();
    };

    /**
     * @private
     */
    ol.source.Cluster.prototype.cluster_ = function() {
      if (this.resolution_ === undefined) {
        return;
      }
      this.features_.length = 0;
      var extent = ol.extent.createEmpty();
      var mapDistance = this.distance_ * this.resolution_;
      var features = this.source_.getFeatures();

      /**
       * @type {Object.<string, boolean>}
       */
      var clustered = {};

      for (var i = 0, ii = features.length; i < ii; i++) {
        var feature = features[i];
        if (!goog.object.containsKey(clustered, goog.getUid(feature).toString())) {
          var geometry = feature.getGeometry();
          goog.asserts.assert(geometry instanceof ol.geom.Point,
              'feature geometry is a ol.geom.Point instance');
          var coordinates = geometry.getCoordinates();
          ol.extent.createOrUpdateFromCoordinate(coordinates, extent);
          ol.extent.buffer(extent, mapDistance, extent);

          var neighbors = this.source_.getFeaturesInExtent(extent);
          goog.asserts.assert(neighbors.length >= 1, 'at least one neighbor found');
          neighbors = neighbors.filter(function(neighbor) {
            var uid = goog.getUid(neighbor).toString();
            if (!goog.object.containsKey(clustered, uid)) {
              clustered[uid] = true;
              return true;
            } else {
              return false;
            }
          });
          this.features_.push(this.createCluster_(neighbors));
        }
      }
      goog.asserts.assert(
          goog.object.getCount(clustered) == this.source_.getFeatures().length,
          'number of clustered equals number of features in the source');
    };

    /**
     * @param {Array.<ol.Feature>} features Features
     * @return {ol.Feature}
     * @private
     */
    ol.source.Cluster.prototype.createCluster_ = function(features) {
      var length = features.length;
      var centroid = [0, 0];
      for (var i = 0; i < length; i++) {
        var geometry = features[i].getGeometry();
        goog.asserts.assert(geometry instanceof ol.geom.Point,
            'feature geometry is a ol.geom.Point instance');
        var coordinates = geometry.getCoordinates();
        ol.coordinate.add(centroid, coordinates);
      }
      ol.coordinate.scale(centroid, 1 / length);

      var cluster = new ol.Feature(new ol.geom.Point(centroid));
      cluster.set('features', features);
      return cluster;
    };