# all files / ol/source/ tilesource.js

93.02% Statements 80/86
83.33% Branches 25/30
85.71% Functions 12/14
93.02% Lines 80/86

    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
    338
    339
    340
    341
    3421×
    1×
    1×

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

    1×

    1×

    82×

    82×

    82×

    82×

    82×

    82×

    1×

    1×
    1×

    1×

    1×

    14×
    14×

    14×
    14×
    14×
    34×
    158×
    158×
    158×
    16×
    16×
    16×
    14×

    158×
    148×

    14×

    1×
    2×

    1×

    1×
    2×

    1×

    1×

    1×
    40×

    1×
    16×
    7×

    9×

    1×
    6×
    6×

    6×

    1×
    4×

    1×

    5×
    5×

    1×

    49×

    49×
    49×
    49×
    40×

    49×

    1×

    1×

    12×

    12×

    1×

    1×

     goog.provide('ol.source.Tile');
    goog.provide('ol.source.TileEvent');
    goog.provide('ol.source.TileOptions');

    goog.require('goog.asserts');
    goog.require('goog.events.Event');
    goog.require('ol');
    goog.require('ol.Attribution');
    goog.require('ol.Extent');
    goog.require('ol.TileCache');
    goog.require('ol.TileRange');
    goog.require('ol.TileState');
    goog.require('ol.proj');
    goog.require('ol.size');
    goog.require('ol.source.Source');
    goog.require('ol.tilecoord');
    goog.require('ol.tilegrid.TileGrid');

    /**
     * @typedef {{attributions: (Array.<ol.Attribution>|undefined),
     *            cacheSize: (number|undefined),
     *            extent: (ol.Extent|undefined),
     *            logo: (string|olx.LogoOptions|undefined),
     *            opaque: (boolean|undefined),
     *            tilePixelRatio: (number|undefined),
     *            projection: ol.proj.ProjectionLike,
     *            state: (ol.source.State|undefined),
     *            tileGrid: (ol.tilegrid.TileGrid|undefined),
     *            wrapX: (boolean|undefined)}}
     */
    ol.source.TileOptions;

    /**
     * @classdesc
     * Abstract base class; normally only used for creating subclasses and not
     * instantiated in apps.
     * Base class for sources providing images divided into a tile grid.
     *
     * @constructor
     * @extends {ol.source.Source}
     * @param {ol.source.TileOptions} options Tile source options.
     * @api
     */
    ol.source.Tile = function(options) {

      goog.base(this, {
        attributions: options.attributions,
        extent: options.extent,
        logo: options.logo,
        projection: options.projection,
        state: options.state,
        wrapX: options.wrapX
      });

      /**
       * @private
       * @type {boolean}
       */
      this.opaque_ = options.opaque !== undefined ? options.opaque : false;

      /**
       * @private
       * @type {number}
       */
      this.tilePixelRatio_ = options.tilePixelRatio !== undefined ?
          options.tilePixelRatio : 1;

      /**
       * @protected
       * @type {ol.tilegrid.TileGrid}
       */
      this.tileGrid = options.tileGrid !== undefined ? options.tileGrid : null;

      /**
       * @protected
       * @type {ol.TileCache}
       */
      this.tileCache = new ol.TileCache(options.cacheSize);

      /**
       * @protected
       * @type {ol.Size}
       */
      this.tmpSize = [0, 0];

    };
    goog.inherits(ol.source.Tile, ol.source.Source);

    /**
     * @return {boolean} Can expire cache.
     */
    ol.source.Tile.prototype.canExpireCache = function() {
      return this.tileCache.canExpireCache();
    };

    /**
     * @param {ol.proj.Projection} projection Projection.
     * @param {Object.<string, ol.TileRange>} usedTiles Used tiles.
     */
    ol.source.Tile.prototype.expireCache = function(projection, usedTiles) {
      var tileCache = this.getTileCacheForProjection(projection);
      if (tileCache) {
        tileCache.expireCache(usedTiles);
      }
    };

    /**
     * @param {ol.proj.Projection} projection Projection.
     * @param {number} z Zoom level.
     * @param {ol.TileRange} tileRange Tile range.
     * @param {function(ol.Tile):(boolean|undefined)} callback Called with each
     *     loaded tile.  If the callback returns `false`, the tile will not be
     *     considered loaded.
     * @return {boolean} The tile range is fully covered with loaded tiles.
     */
    ol.source.Tile.prototype.forEachLoadedTile =
        function(projection, z, tileRange, callback) {
      var tileCache = this.getTileCacheForProjection(projection);
      Iif (!tileCache) {
        return false;
      }

      var covered = true;
      var tile, tileCoordKey, loaded;
      for (var x = tileRange.minX; x <= tileRange.maxX; ++x) {
        for (var y = tileRange.minY; y <= tileRange.maxY; ++y) {
          tileCoordKey = this.getKeyZXY(z, x, y);
          loaded = false;
          if (tileCache.containsKey(tileCoordKey)) {
            tile = /** @type {!ol.Tile} */ (tileCache.get(tileCoordKey));
            loaded = tile.getState() === ol.TileState.LOADED;
            if (loaded) {
              loaded = (callback(tile) !== false);
            }
          }
          if (!loaded) {
            covered = false;
          }
        }
      }
      return covered;
    };

    /**
     * @return {number} Gutter.
     */
    ol.source.Tile.prototype.getGutter = function() {
      return 0;
    };

    /**
     * @param {number} z Z.
     * @param {number} x X.
     * @param {number} y Y.
     * @return {string} Key.
     * @protected
     */
    ol.source.Tile.prototype.getKeyZXY = ol.tilecoord.getKeyZXY;

    /**
     * @return {boolean} Opaque.
     */
    ol.source.Tile.prototype.getOpaque = function() {
      return this.opaque_;
    };

    /**
     * @inheritDoc
     */
    ol.source.Tile.prototype.getResolutions = function() {
      return this.tileGrid.getResolutions();
    };

    /**
     * @param {number} z Tile coordinate z.
     * @param {number} x Tile coordinate x.
     * @param {number} y Tile coordinate y.
     * @param {number} pixelRatio Pixel ratio.
     * @param {ol.proj.Projection} projection Projection.
     * @return {!ol.Tile} Tile.
     */
    ol.source.Tile.prototype.getTile = goog.abstractMethod;

    /**
     * Return the tile grid of the tile source.
     * @return {ol.tilegrid.TileGrid} Tile grid.
     * @api stable
     */
    ol.source.Tile.prototype.getTileGrid = function() {
      return this.tileGrid;
    };

    /**
     * @param {ol.proj.Projection} projection Projection.
     * @return {ol.tilegrid.TileGrid} Tile grid.
     */
    ol.source.Tile.prototype.getTileGridForProjection = function(projection) {
      if (!this.tileGrid) {
        return ol.tilegrid.getForProjection(projection);
      } else {
        return this.tileGrid;
      }
    };

    /**
     * @param {ol.proj.Projection} projection Projection.
     * @return {ol.TileCache} Tile cache.
     * @protected
     */
    ol.source.Tile.prototype.getTileCacheForProjection = function(projection) {
      var thisProj = this.getProjection();
      Iif (thisProj && !ol.proj.equivalent(thisProj, projection)) {
        return null;
      } else {
        return this.tileCache;
      }
    };

    /**
     * @return {number} Tile pixel ratio.
     */
    ol.source.Tile.prototype.getTilePixelRatio = function() {
      return this.tilePixelRatio_;
    };

    /**
     * @param {number} z Z.
     * @param {number} pixelRatio Pixel ratio.
     * @param {ol.proj.Projection} projection Projection.
     * @return {ol.Size} Tile size.
     */
    ol.source.Tile.prototype.getTilePixelSize =
        function(z, pixelRatio, projection) {
      var tileGrid = this.getTileGridForProjection(projection);
      return ol.size.scale(ol.size.toSize(tileGrid.getTileSize(z), this.tmpSize),
          this.tilePixelRatio_, this.tmpSize);
    };

    /**
     * Returns a tile coordinate wrapped around the x-axis. When the tile coordinate
     * is outside the resolution and extent range of the tile grid, `null` will be
     * returned.
     * @param {ol.TileCoord} tileCoord Tile coordinate.
     * @param {ol.proj.Projection=} opt_projection Projection.
     * @return {ol.TileCoord} Tile coordinate to be passed to the tileUrlFunction or
     *     null if no tile URL should be created for the passed `tileCoord`.
     */
    ol.source.Tile.prototype.getTileCoordForTileUrlFunction =
        function(tileCoord, opt_projection) {
      var projection = opt_projection !== undefined ?
          opt_projection : this.getProjection();
      var tileGrid = this.getTileGridForProjection(projection);
      goog.asserts.assert(tileGrid, 'tile grid needed');
      if (this.getWrapX() && projection.isGlobal()) {
        tileCoord = ol.tilecoord.wrapX(tileCoord, tileGrid, projection);
      }
      return ol.tilecoord.withinExtentAndZ(tileCoord, tileGrid) ? tileCoord : null;
    };

    /**
     * Marks a tile coord as being used, without triggering a load.
     * @param {number} z Tile coordinate z.
     * @param {number} x Tile coordinate x.
     * @param {number} y Tile coordinate y.
     * @param {ol.proj.Projection} projection Projection.
     */
    ol.source.Tile.prototype.useTile = ol.nullFunction;

    /**
     * @classdesc
     * Events emitted by {@link ol.source.Tile} instances are instances of this
     * type.
     *
     * @constructor
     * @extends {goog.events.Event}
     * @implements {oli.source.TileEvent}
     * @param {string} type Type.
     * @param {ol.Tile} tile The tile.
     */
    ol.source.TileEvent = function(type, tile) {

      goog.base(this, type);

      /**
       * The tile related to the event.
       * @type {ol.Tile}
       * @api
       */
      this.tile = tile;

    };
    goog.inherits(ol.source.TileEvent, goog.events.Event);

    /**
     * @enum {string}
     */
    ol.source.TileEventType = {

      /**
       * Triggered when a tile starts loading.
       * @event ol.source.TileEvent#tileloadstart
       * @api stable
       */
      TILELOADSTART: 'tileloadstart',

      /**
       * Triggered when a tile finishes loading.
       * @event ol.source.TileEvent#tileloadend
       * @api stable
       */
      TILELOADEND: 'tileloadend',

      /**
       * Triggered if tile loading results in an error.
       * @event ol.source.TileEvent#tileloaderror
       * @api stable
       */
      TILELOADERROR: 'tileloaderror'

    };