# all files / ol/renderer/ layerrenderer.js

75.93% Statements 82/108
56.82% Branches 25/44
81.25% Functions 13/16
75.93% Lines 82/108

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

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

    1×

    137×

    137×

    1×

    1×

    1×

    1×

    1×
    5×

    1×

    16×

    1×
    780×

    1×
    94×
    94×
    33×

    1×
    116×
    116×

    63×

    63×

    116×
    61×
    61×
    61×

    116×

    1×
    33×
    33×
    33×

    1×
    5×

    1×
    137×

    1×
    139×
    139×

    1×

    5×
    5×
    5×

    5×
    5×

    1×
    2×

    1×

    5×
    5×
    5×

    5×
    5×
    5×
    5×
    5×
    11×
    11×
    11×
    17×
    17×
    7×
    7×
    2×
    2×
    2×

    7×

    10×

     goog.provide('ol.renderer.Layer');

    goog.require('goog.asserts');
    goog.require('ol.events');
    goog.require('ol.events.EventType');
    goog.require('goog.functions');
    goog.require('ol');
    goog.require('ol.ImageState');
    goog.require('ol.Observable');
    goog.require('ol.TileRange');
    goog.require('ol.TileState');
    goog.require('ol.layer.Layer');
    goog.require('ol.source.Source');
    goog.require('ol.source.State');
    goog.require('ol.source.Tile');
    goog.require('ol.vec.Mat4');

    /**
     * @constructor
     * @extends {ol.Observable}
     * @param {ol.layer.Layer} layer Layer.
     * @struct
     */
    ol.renderer.Layer = function(layer) {

      goog.base(this);

      /**
       * @private
       * @type {ol.layer.Layer}
       */
      this.layer_ = layer;

    };
    goog.inherits(ol.renderer.Layer, ol.Observable);

    /**
     * @param {ol.Coordinate} coordinate Coordinate.
     * @param {olx.FrameState} frameState Frame state.
     * @param {function(this: S, (ol.Feature|ol.render.Feature), ol.layer.Layer): T}
     *     callback Feature callback.
     * @param {S} thisArg Value to use as `this` when executing `callback`.
     * @return {T|undefined} Callback result.
     * @template S,T
     */
    ol.renderer.Layer.prototype.forEachFeatureAtCoordinate = ol.nullFunction;

    /**
     * @param {ol.Pixel} pixel Pixel.
     * @param {olx.FrameState} frameState Frame state.
     * @param {function(this: S, ol.layer.Layer): T} callback Layer callback.
     * @param {S} thisArg Value to use as `this` when executing `callback`.
     * @return {T|undefined} Callback result.
     * @template S,T
     */
    ol.renderer.Layer.prototype.forEachLayerAtPixel = function(pixel, frameState, callback, thisArg) {
      var coordinate = pixel.slice();
      ol.vec.Mat4.multVec2(
          frameState.pixelToCoordinateMatrix, coordinate, coordinate);

      var hasFeature = this.forEachFeatureAtCoordinate(
          coordinate, frameState, goog.functions.TRUE, this);

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

    /**
     * @param {ol.Coordinate} coordinate Coordinate.
     * @param {olx.FrameState} frameState Frame state.
     * @return {boolean} Is there a feature at the given coordinate?
     */
    ol.renderer.Layer.prototype.hasFeatureAtCoordinate = goog.functions.FALSE;

    /**
     * Create a function that adds loaded tiles to the tile lookup.
     * @param {ol.source.Tile} source Tile source.
     * @param {ol.proj.Projection} projection Projection of the tiles.
     * @param {Object.<number, Object.<string, ol.Tile>>} tiles Lookup of loaded
     *     tiles by zoom level.
     * @return {function(number, ol.TileRange):boolean} A function that can be
     *     called with a zoom level and a tile range to add loaded tiles to the
     *     lookup.
     * @protected
     */
    ol.renderer.Layer.prototype.createLoadedTileFinder = function(source, projection, tiles) {
      return (
          /**
           * @param {number} zoom Zoom level.
           * @param {ol.TileRange} tileRange Tile range.
           * @return {boolean} The tile range is fully loaded.
           */
          function(zoom, tileRange) {
            function callback(tile) {
              if (!tiles[zoom]) {
                tiles[zoom] = {};
              }
              tiles[zoom][tile.tileCoord.toString()] = tile;
            }
            return source.forEachLoadedTile(projection, zoom, tileRange, callback);
          });
    };

    /**
     * @return {ol.layer.Layer} Layer.
     */
    ol.renderer.Layer.prototype.getLayer = function() {
      return this.layer_;
    };

    /**
     * Handle changes in image state.
     * @param {ol.events.Event} event Image change event.
     * @private
     */
    ol.renderer.Layer.prototype.handleImageChange_ = function(event) {
      var image = /** @type {ol.Image} */ (event.target);
      if (image.getState() === ol.ImageState.LOADED) {
        this.renderIfReadyAndVisible();
      }
    };

    /**
     * Load the image if not already loaded, and register the image change
     * listener if needed.
     * @param {ol.ImageBase} image Image.
     * @return {boolean} `true` if the image is already loaded, `false`
     *     otherwise.
     * @protected
     */
    ol.renderer.Layer.prototype.loadImage = function(image) {
      var imageState = image.getState();
      if (imageState != ol.ImageState.LOADED &&
          imageState != ol.ImageState.ERROR) {
        // the image is either "idle" or "loading", register the change
        // listener (a noop if the listener was already registered)
        goog.asserts.assert(imageState == ol.ImageState.IDLE ||
            imageState == ol.ImageState.LOADING,
            'imageState is "idle" or "loading"');
        ol.events.listen(image, ol.events.EventType.CHANGE,
            this.handleImageChange_, this);
      }
      if (imageState == ol.ImageState.IDLE) {
        image.load();
        imageState = image.getState();
        goog.asserts.assert(imageState == ol.ImageState.LOADING ||
            imageState == ol.ImageState.LOADED,
            'imageState is "loading" or "loaded"');
      }
      return imageState == ol.ImageState.LOADED;
    };

    /**
     * @protected
     */
    ol.renderer.Layer.prototype.renderIfReadyAndVisible = function() {
      var layer = this.getLayer();
      Eif (layer.getVisible() && layer.getSourceState() == ol.source.State.READY) {
        this.changed();
      }
    };

    /**
     * @param {olx.FrameState} frameState Frame state.
     * @param {ol.source.Tile} tileSource Tile source.
     * @protected
     */
    ol.renderer.Layer.prototype.scheduleExpireCache = function(frameState, tileSource) {
      Iif (tileSource.canExpireCache()) {
        frameState.postRenderFunctions.push(
            /** @type {ol.PostRenderFunction} */ (goog.partial(
                /**
                 * @param {ol.source.Tile} tileSource Tile source.
                 * @param {ol.Map} map Map.
                 * @param {olx.FrameState} frameState Frame state.
                 */
                function(tileSource, map, frameState) {
                  var tileSourceKey = goog.getUid(tileSource).toString();
                  tileSource.expireCache(frameState.viewState.projection,
                                         frameState.usedTiles[tileSourceKey]);
                }, tileSource)));
      }
    };

    /**
     * @param {Object.<string, ol.Attribution>} attributionsSet Attributions
     *     set (target).
     * @param {Array.<ol.Attribution>} attributions Attributions (source).
     * @protected
     */
    ol.renderer.Layer.prototype.updateAttributions = function(attributionsSet, attributions) {
      Iif (attributions) {
        var attribution, i, ii;
        for (i = 0, ii = attributions.length; i < ii; ++i) {
          attribution = attributions[i];
          attributionsSet[goog.getUid(attribution).toString()] = attribution;
        }
      }
    };

    /**
     * @param {olx.FrameState} frameState Frame state.
     * @param {ol.source.Source} source Source.
     * @protected
     */
    ol.renderer.Layer.prototype.updateLogos = function(frameState, source) {
      var logo = source.getLogo();
      Iif (logo !== undefined) {
        if (typeof logo === 'string') {
          frameState.logos[logo] = '';
        } else if (goog.isObject(logo)) {
          goog.asserts.assertString(logo.href, 'logo.href is a string');
          goog.asserts.assertString(logo.src, 'logo.src is a string');
          frameState.logos[logo.src] = logo.href;
        }
      }
    };

    /**
     * @param {Object.<string, Object.<string, ol.TileRange>>} usedTiles Used tiles.
     * @param {ol.source.Tile} tileSource Tile source.
     * @param {number} z Z.
     * @param {ol.TileRange} tileRange Tile range.
     * @protected
     */
    ol.renderer.Layer.prototype.updateUsedTiles = function(usedTiles, tileSource, z, tileRange) {
      // FIXME should we use tilesToDrawByZ instead?
      var tileSourceKey = goog.getUid(tileSource).toString();
      var zKey = z.toString();
      Iif (tileSourceKey in usedTiles) {
        if (zKey in usedTiles[tileSourceKey]) {
          usedTiles[tileSourceKey][zKey].extend(tileRange);
        } else {
          usedTiles[tileSourceKey][zKey] = tileRange;
        }
      } else {
        usedTiles[tileSourceKey] = {};
        usedTiles[tileSourceKey][zKey] = tileRange;
      }
    };

    /**
     * @param {ol.Coordinate} center Center.
     * @param {number} resolution Resolution.
     * @param {ol.Size} size Size.
     * @protected
     * @return {ol.Coordinate} Snapped center.
     */
    ol.renderer.Layer.prototype.snapCenterToPixel = function(center, resolution, size) {
      return [
        resolution * (Math.round(center[0] / resolution) + (size[0] % 2) / 2),
        resolution * (Math.round(center[1] / resolution) + (size[1] % 2) / 2)
      ];
    };

    /**
     * Manage tile pyramid.
     * This function performs a number of functions related to the tiles at the
     * current zoom and lower zoom levels:
     * - registers idle tiles in frameState.wantedTiles so that they are not
     *   discarded by the tile queue
     * - enqueues missing tiles
     * @param {olx.FrameState} frameState Frame state.
     * @param {ol.source.Tile} tileSource Tile source.
     * @param {ol.tilegrid.TileGrid} tileGrid Tile grid.
     * @param {number} pixelRatio Pixel ratio.
     * @param {ol.proj.Projection} projection Projection.
     * @param {ol.Extent} extent Extent.
     * @param {number} currentZ Current Z.
     * @param {number} preload Load low resolution tiles up to 'preload' levels.
     * @param {function(this: T, ol.Tile)=} opt_tileCallback Tile callback.
     * @param {T=} opt_this Object to use as `this` in `opt_tileCallback`.
     * @protected
     * @template T
     */
    ol.renderer.Layer.prototype.manageTilePyramid = function(
        frameState, tileSource, tileGrid, pixelRatio, projection, extent,
        currentZ, preload, opt_tileCallback, opt_this) {
      var tileSourceKey = goog.getUid(tileSource).toString();
      Eif (!(tileSourceKey in frameState.wantedTiles)) {
        frameState.wantedTiles[tileSourceKey] = {};
      }
      var wantedTiles = frameState.wantedTiles[tileSourceKey];
      var tileQueue = frameState.tileQueue;
      var minZoom = tileGrid.getMinZoom();
      var tile, tileRange, tileResolution, x, y, z;
      for (z = currentZ; z >= minZoom; --z) {
        tileRange = tileGrid.getTileRangeForExtentAndZ(extent, z, tileRange);
        tileResolution = tileGrid.getResolution(z);
        for (x = tileRange.minX; x <= tileRange.maxX; ++x) {
          for (y = tileRange.minY; y <= tileRange.maxY; ++y) {
            if (currentZ - z <= preload) {
              tile = tileSource.getTile(z, x, y, pixelRatio, projection);
              if (tile.getState() == ol.TileState.IDLE) {
                wantedTiles[tile.tileCoord.toString()] = true;
                Eif (!tileQueue.isKeyQueued(tile.getKey())) {
                  tileQueue.enqueue([tile, tileSourceKey,
                    tileGrid.getTileCoordCenter(tile.tileCoord), tileResolution]);
                }
              }
              Iif (opt_tileCallback !== undefined) {
                opt_tileCallback.call(opt_this, tile);
              }
            } else {
              tileSource.useTile(z, x, y, projection);
            }
          }
        }
      }
    };