# all files / ol/renderer/webgl/ webglvectorlayerrenderer.js

15.75% Statements 20/127
0% Branches 0/58
0% Functions 0/12
15.75% Lines 20/127

    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
    3251×

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

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

     goog.provide('ol.renderer.webgl.VectorLayer');

    goog.require('goog.asserts');
    goog.require('goog.events');
    goog.require('ol.ViewHint');
    goog.require('ol.extent');
    goog.require('ol.layer.Vector');
    goog.require('ol.render.webgl.ReplayGroup');
    goog.require('ol.renderer.vector');
    goog.require('ol.renderer.webgl.Layer');
    goog.require('ol.vec.Mat4');

    /**
     * @constructor
     * @extends {ol.renderer.webgl.Layer}
     * @param {ol.renderer.webgl.Map} mapRenderer Map renderer.
     * @param {ol.layer.Vector} vectorLayer Vector layer.
     */
    ol.renderer.webgl.VectorLayer = function(mapRenderer, vectorLayer) {

      goog.base(this, mapRenderer, vectorLayer);

      /**
       * @private
       * @type {boolean}
       */
      this.dirty_ = false;

      /**
       * @private
       * @type {number}
       */
      this.renderedRevision_ = -1;

      /**
       * @private
       * @type {number}
       */
      this.renderedResolution_ = NaN;

      /**
       * @private
       * @type {ol.Extent}
       */
      this.renderedExtent_ = ol.extent.createEmpty();

      /**
       * @private
       * @type {function(ol.Feature, ol.Feature): number|null}
       */
      this.renderedRenderOrder_ = null;

      /**
       * @private
       * @type {ol.render.webgl.ReplayGroup}
       */
      this.replayGroup_ = null;

      /**
       * The last layer state.
       * @private
       * @type {?ol.layer.LayerState}
       */
      this.layerState_ = null;

    };
    goog.inherits(ol.renderer.webgl.VectorLayer, ol.renderer.webgl.Layer);

    /**
     * @inheritDoc
     */
    ol.renderer.webgl.VectorLayer.prototype.composeFrame =
        function(frameState, layerState, context) {
      this.layerState_ = layerState;
      var viewState = frameState.viewState;
      var replayGroup = this.replayGroup_;
      if (replayGroup && !replayGroup.isEmpty()) {
        replayGroup.replay(context,
            viewState.center, viewState.resolution, viewState.rotation,
            frameState.size, frameState.pixelRatio, layerState.opacity,
            layerState.managed ? frameState.skippedFeatureUids : {});
      }

    };

    /**
     * @inheritDoc
     */
    ol.renderer.webgl.VectorLayer.prototype.disposeInternal = function() {
      var replayGroup = this.replayGroup_;
      if (replayGroup) {
        var context = this.mapRenderer.getContext();
        replayGroup.getDeleteResourcesFunction(context)();
        this.replayGroup_ = null;
      }
      goog.base(this, 'disposeInternal');
    };

    /**
     * @inheritDoc
     */
    ol.renderer.webgl.VectorLayer.prototype.forEachFeatureAtCoordinate =
        function(coordinate, frameState, callback, thisArg) {
      if (!this.replayGroup_ || !this.layerState_) {
        return undefined;
      } else {
        var context = this.mapRenderer.getContext();
        var viewState = frameState.viewState;
        var layer = this.getLayer();
        var layerState = this.layerState_;
        /** @type {Object.<string, boolean>} */
        var features = {};
        return this.replayGroup_.forEachFeatureAtCoordinate(coordinate,
            context, viewState.center, viewState.resolution, viewState.rotation,
            frameState.size, frameState.pixelRatio, layerState.opacity,
            layerState.managed ? frameState.skippedFeatureUids : {},
            /**
             * @param {ol.Feature} feature Feature.
             * @return {?} Callback result.
             */
            function(feature) {
              goog.asserts.assert(feature !== undefined, 'received a feature');
              var key = goog.getUid(feature).toString();
              if (!(key in features)) {
                features[key] = true;
                return callback.call(thisArg, feature, layer);
              }
            });
      }
    };

    /**
     * @inheritDoc
     */
    ol.renderer.webgl.VectorLayer.prototype.hasFeatureAtCoordinate =
        function(coordinate, frameState) {
      if (!this.replayGroup_ || !this.layerState_) {
        return false;
      } else {
        var context = this.mapRenderer.getContext();
        var viewState = frameState.viewState;
        var layerState = this.layerState_;
        return this.replayGroup_.hasFeatureAtCoordinate(coordinate,
            context, viewState.center, viewState.resolution, viewState.rotation,
            frameState.size, frameState.pixelRatio, layerState.opacity,
            frameState.skippedFeatureUids);
      }
    };

    /**
     * @inheritDoc
     */
    ol.renderer.webgl.VectorLayer.prototype.forEachLayerAtPixel =
        function(pixel, frameState, callback, thisArg) {
      var coordinate = pixel.slice();
      ol.vec.Mat4.multVec2(
          frameState.pixelToCoordinateMatrix, coordinate, coordinate);
      var hasFeature = this.hasFeatureAtCoordinate(coordinate, frameState);

      if (hasFeature) {
        return callback.call(thisArg, this.getLayer());
      } else {
        return undefined;
      }
    };

    /**
     * Handle changes in image style state.
     * @param {goog.events.Event} event Image style change event.
     * @private
     */
    ol.renderer.webgl.VectorLayer.prototype.handleStyleImageChange_ =
        function(event) {
      this.renderIfReadyAndVisible();
    };

    /**
     * @inheritDoc
     */
    ol.renderer.webgl.VectorLayer.prototype.prepareFrame =
        function(frameState, layerState, context) {

      var vectorLayer = /** @type {ol.layer.Vector} */ (this.getLayer());
      goog.asserts.assertInstanceof(vectorLayer, ol.layer.Vector,
          'layer is an instance of ol.layer.Vector');
      var vectorSource = vectorLayer.getSource();

      this.updateAttributions(
          frameState.attributions, vectorSource.getAttributions());
      this.updateLogos(frameState, vectorSource);

      var animating = frameState.viewHints[ol.ViewHint.ANIMATING];
      var interacting = frameState.viewHints[ol.ViewHint.INTERACTING];
      var updateWhileAnimating = vectorLayer.getUpdateWhileAnimating();
      var updateWhileInteracting = vectorLayer.getUpdateWhileInteracting();

      if (!this.dirty_ && (!updateWhileAnimating && animating) ||
          (!updateWhileInteracting && interacting)) {
        return true;
      }

      var frameStateExtent = frameState.extent;
      var viewState = frameState.viewState;
      var projection = viewState.projection;
      var resolution = viewState.resolution;
      var pixelRatio = frameState.pixelRatio;
      var vectorLayerRevision = vectorLayer.getRevision();
      var vectorLayerRenderBuffer = vectorLayer.getRenderBuffer();
      var vectorLayerRenderOrder = vectorLayer.getRenderOrder();

      if (vectorLayerRenderOrder === undefined) {
        vectorLayerRenderOrder = ol.renderer.vector.defaultOrder;
      }

      var extent = ol.extent.buffer(frameStateExtent,
          vectorLayerRenderBuffer * resolution);

      if (!this.dirty_ &&
          this.renderedResolution_ == resolution &&
          this.renderedRevision_ == vectorLayerRevision &&
          this.renderedRenderOrder_ == vectorLayerRenderOrder &&
          ol.extent.containsExtent(this.renderedExtent_, extent)) {
        return true;
      }

      if (this.replayGroup_) {
        frameState.postRenderFunctions.push(
            this.replayGroup_.getDeleteResourcesFunction(context));
      }

      this.dirty_ = false;

      var replayGroup = new ol.render.webgl.ReplayGroup(
          ol.renderer.vector.getTolerance(resolution, pixelRatio),
          extent, vectorLayer.getRenderBuffer());
      vectorSource.loadFeatures(extent, resolution, projection);
      var renderFeature =
          /**
           * @param {ol.Feature} feature Feature.
           * @this {ol.renderer.webgl.VectorLayer}
           */
          function(feature) {
        var styles;
        var styleFunction = feature.getStyleFunction();
        if (styleFunction) {
          styles = styleFunction.call(feature, resolution);
        } else {
          styleFunction = vectorLayer.getStyleFunction();
          if (styleFunction) {
            styles = styleFunction(feature, resolution);
          }
        }
        if (styles) {
          var dirty = this.renderFeature(
              feature, resolution, pixelRatio, styles, replayGroup);
          this.dirty_ = this.dirty_ || dirty;
        }
      };
      if (vectorLayerRenderOrder) {
        /** @type {Array.<ol.Feature>} */
        var features = [];
        vectorSource.forEachFeatureInExtent(extent,
            /**
             * @param {ol.Feature} feature Feature.
             */
            function(feature) {
              features.push(feature);
            }, this);
        features.sort(vectorLayerRenderOrder);
        features.forEach(renderFeature, this);
      } else {
        vectorSource.forEachFeatureInExtent(extent, renderFeature, this);
      }
      replayGroup.finish(context);

      this.renderedResolution_ = resolution;
      this.renderedRevision_ = vectorLayerRevision;
      this.renderedRenderOrder_ = vectorLayerRenderOrder;
      this.renderedExtent_ = extent;
      this.replayGroup_ = replayGroup;

      return true;
    };

    /**
     * @param {ol.Feature} feature Feature.
     * @param {number} resolution Resolution.
     * @param {number} pixelRatio Pixel ratio.
     * @param {(ol.style.Style|Array.<ol.style.Style>)} styles The style or array of
     *     styles.
     * @param {ol.render.webgl.ReplayGroup} replayGroup Replay group.
     * @return {boolean} `true` if an image is loading.
     */
    ol.renderer.webgl.VectorLayer.prototype.renderFeature =
        function(feature, resolution, pixelRatio, styles, replayGroup) {
      if (!styles) {
        return false;
      }
      var loading = false;
      if (goog.isArray(styles)) {
        for (var i = 0, ii = styles.length; i < ii; ++i) {
          loading = ol.renderer.vector.renderFeature(
              replayGroup, feature, styles[i],
              ol.renderer.vector.getSquaredTolerance(resolution, pixelRatio),
              this.handleStyleImageChange_, this) || loading;
        }
      } else {
        loading = ol.renderer.vector.renderFeature(
            replayGroup, feature, styles,
            ol.renderer.vector.getSquaredTolerance(resolution, pixelRatio),
            this.handleStyleImageChange_, this) || loading;
      }
      return loading;
    };