# all files / ol/source/ tileimage.js

82.84% Statements 111/134
73.49% Branches 61/83
86.67% Functions 13/15
82.84% Lines 111/134

    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
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    3731×

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

    1×

    136×

    136×

    136×

    136×

    136×

    136×

    136×

    1×

    1×
    56×

    56×

    56×
    4×

    56×

    1×

    1×
    65×

    3×

    62×

    1×
    59×

    1×
    39×

    39×

    1×
    487×

    487×
    487×

    346×

    141×
    141×
    33×

    141×

    1×
    267×

    267×
    267×
    220×

    47×
    47×
    5×

    47×

    1×
    249×
    249×

    249×

    249×

    249×
    249×

    249×

    1×
    518×

    500×

    18×
    18×
    18×
    18×
    18×
    10×

    18×
    18×
    10×

    8×
    8×
    8×
    8×

    8×

    14×

    8×

    8×

    8×

    8×

    1×
    523×
    523×
    523×
    523×
    244×
    244×

    279×
    279×

    5×
    5×

    5×

    2×

    3×

    5×
    5×

    523×

    1×

    1×
    2×
    2×
    2×
    2×
    2×
    2×

    1×
    66×

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

    goog.require('ol');
    goog.require('ol.ImageTile');
    goog.require('ol.TileCache');
    goog.require('ol.TileState');
    goog.require('ol.events');
    goog.require('ol.events.EventType');
    goog.require('ol.proj');
    goog.require('ol.reproj.Tile');
    goog.require('ol.source.UrlTile');
    goog.require('ol.tilegrid');

    /**
     * @classdesc
     * Base class for sources providing images divided into a tile grid.
     *
     * @constructor
     * @fires ol.source.Tile.Event
     * @extends {ol.source.UrlTile}
     * @param {olx.source.TileImageOptions} options Image tile options.
     * @api
     */
    ol.source.TileImage = function(options) {

      ol.source.UrlTile.call(this, {
        attributions: options.attributions,
        cacheSize: options.cacheSize,
        extent: options.extent,
        logo: options.logo,
        opaque: options.opaque,
        projection: options.projection,
        state: options.state,
        tileGrid: options.tileGrid,
        tileLoadFunction: options.tileLoadFunction ?
          options.tileLoadFunction : ol.source.TileImage.defaultTileLoadFunction,
        tilePixelRatio: options.tilePixelRatio,
        tileUrlFunction: options.tileUrlFunction,
        url: options.url,
        urls: options.urls,
        wrapX: options.wrapX
      });

      /**
       * @protected
       * @type {?string}
       */
      this.crossOrigin =
          options.crossOrigin !== undefined ? options.crossOrigin : null;

      /**
       * @protected
       * @type {function(new: ol.ImageTile, ol.TileCoord, ol.TileState, string,
       *        ?string, ol.TileLoadFunctionType)}
       */
      this.tileClass = options.tileClass !== undefined ?
        options.tileClass : ol.ImageTile;

      /**
       * @protected
       * @type {Object.<string, ol.TileCache>}
       */
      this.tileCacheForProjection = {};

      /**
       * @protected
       * @type {Object.<string, ol.tilegrid.TileGrid>}
       */
      this.tileGridForProjection = {};

      /**
       * @private
       * @type {number|undefined}
       */
      this.reprojectionErrorThreshold_ = options.reprojectionErrorThreshold;

      /**
       * @private
       * @type {boolean}
       */
      this.renderReprojectionEdges_ = false;
    };
    ol.inherits(ol.source.TileImage, ol.source.UrlTile);

    /**
     * @inheritDoc
     */
    ol.source.TileImage.prototype.canExpireCache = function() {
      Iif (!ol.ENABLE_RASTER_REPROJECTION) {
        return ol.source.UrlTile.prototype.canExpireCache.call(this);
      }
      Iif (this.tileCache.canExpireCache()) {
        return true;
      } else {
        for (var key in this.tileCacheForProjection) {
          Iif (this.tileCacheForProjection[key].canExpireCache()) {
            return true;
          }
        }
      }
      return false;
    };

    /**
     * @inheritDoc
     */
    ol.source.TileImage.prototype.expireCache = function(projection, usedTiles) {
      if (!ol.ENABLE_RASTER_REPROJECTION) {
        ol.source.UrlTile.prototype.expireCache.call(this, projection, usedTiles);
        return;
      }
      var usedTileCache = this.getTileCacheForProjection(projection);

      this.tileCache.expireCache(this.tileCache == usedTileCache ? usedTiles : {});
      for (var id in this.tileCacheForProjection) {
        var tileCache = this.tileCacheForProjection[id];
        tileCache.expireCache(tileCache == usedTileCache ? usedTiles : {});
      }
    };

    /**
     * @inheritDoc
     */
    ol.source.TileImage.prototype.getGutter = function(projection) {
      if (ol.ENABLE_RASTER_REPROJECTION &&
          this.getProjection() && projection &&
          !ol.proj.equivalent(this.getProjection(), projection)) {
        return 0;
      } else {
        return this.getGutterInternal();
      }
    };

    /**
     * @protected
     * @return {number} Gutter.
     */
    ol.source.TileImage.prototype.getGutterInternal = function() {
      return 0;
    };

    /**
     * @inheritDoc
     */
    ol.source.TileImage.prototype.getOpaque = function(projection) {
      Iif (ol.ENABLE_RASTER_REPROJECTION &&
          this.getProjection() && projection &&
          !ol.proj.equivalent(this.getProjection(), projection)) {
        return false;
      } else {
        return ol.source.UrlTile.prototype.getOpaque.call(this, projection);
      }
    };

    /**
     * @inheritDoc
     */
    ol.source.TileImage.prototype.getTileGridForProjection = function(projection) {
      Iif (!ol.ENABLE_RASTER_REPROJECTION) {
        return ol.source.UrlTile.prototype.getTileGridForProjection.call(this, projection);
      }
      var thisProj = this.getProjection();
      if (this.tileGrid &&
          (!thisProj || ol.proj.equivalent(thisProj, projection))) {
        return this.tileGrid;
      } else {
        var projKey = ol.getUid(projection).toString();
        if (!(projKey in this.tileGridForProjection)) {
          this.tileGridForProjection[projKey] =
              ol.tilegrid.getForProjection(projection);
        }
        return /** @type {!ol.tilegrid.TileGrid} */ (this.tileGridForProjection[projKey]);
      }
    };

    /**
     * @inheritDoc
     */
    ol.source.TileImage.prototype.getTileCacheForProjection = function(projection) {
      Iif (!ol.ENABLE_RASTER_REPROJECTION) {
        return ol.source.UrlTile.prototype.getTileCacheForProjection.call(this, projection);
      }
      var thisProj = this.getProjection();
      if (!thisProj || ol.proj.equivalent(thisProj, projection)) {
        return this.tileCache;
      } else {
        var projKey = ol.getUid(projection).toString();
        if (!(projKey in this.tileCacheForProjection)) {
          this.tileCacheForProjection[projKey] = new ol.TileCache(this.tileCache.highWaterMark);
        }
        return this.tileCacheForProjection[projKey];
      }
    };

    /**
     * @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.
     * @param {string} key The key set on the tile.
     * @return {!ol.Tile} Tile.
     * @private
     */
    ol.source.TileImage.prototype.createTile_ = function(z, x, y, pixelRatio, projection, key) {
      var tileCoord = [z, x, y];
      var urlTileCoord = this.getTileCoordForTileUrlFunction(
          tileCoord, projection);
      var tileUrl = urlTileCoord ?
        this.tileUrlFunction(urlTileCoord, pixelRatio, projection) : undefined;
      var tile = new this.tileClass(
          tileCoord,
          tileUrl !== undefined ? ol.TileState.IDLE : ol.TileState.EMPTY,
          tileUrl !== undefined ? tileUrl : '',
          this.crossOrigin,
          this.tileLoadFunction);
      tile.key = key;
      ol.events.listen(tile, ol.events.EventType.CHANGE,
          this.handleTileChange, this);
      return tile;
    };

    /**
     * @inheritDoc
     */
    ol.source.TileImage.prototype.getTile = function(z, x, y, pixelRatio, projection) {
      if (!ol.ENABLE_RASTER_REPROJECTION ||
          !this.getProjection() ||
          !projection ||
          ol.proj.equivalent(this.getProjection(), projection)) {
        return this.getTileInternal(z, x, y, pixelRatio, /** @type {!ol.proj.Projection} */ (projection));
      } else {
        var cache = this.getTileCacheForProjection(projection);
        var tileCoord = [z, x, y];
        var tile;
        var tileCoordKey = this.getKeyZXY.apply(this, tileCoord);
        if (cache.containsKey(tileCoordKey)) {
          tile = /** @type {!ol.Tile} */ (cache.get(tileCoordKey));
        }
        var key = this.getKey();
        if (tile && tile.key == key) {
          return tile;
        } else {
          var sourceProjection = /** @type {!ol.proj.Projection} */ (this.getProjection());
          var sourceTileGrid = this.getTileGridForProjection(sourceProjection);
          var targetTileGrid = this.getTileGridForProjection(projection);
          var wrappedTileCoord =
              this.getTileCoordForTileUrlFunction(tileCoord, projection);
          var newTile = new ol.reproj.Tile(
              sourceProjection, sourceTileGrid,
              projection, targetTileGrid,
              tileCoord, wrappedTileCoord, this.getTilePixelRatio(pixelRatio),
              this.getGutterInternal(),
              function(z, x, y, pixelRatio) {
                return this.getTileInternal(z, x, y, pixelRatio, sourceProjection);
              }.bind(this), this.reprojectionErrorThreshold_,
              this.renderReprojectionEdges_);
          newTile.key = key;

          Iif (tile) {
            newTile.interimTile = tile;
            newTile.refreshInterimChain();
            cache.replace(tileCoordKey, newTile);
          } else {
            cache.set(tileCoordKey, newTile);
          }
          return newTile;
        }
      }
    };

    /**
     * @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.
     * @protected
     */
    ol.source.TileImage.prototype.getTileInternal = function(z, x, y, pixelRatio, projection) {
      var tile = null;
      var tileCoordKey = this.getKeyZXY(z, x, y);
      var key = this.getKey();
      if (!this.tileCache.containsKey(tileCoordKey)) {
        tile = this.createTile_(z, x, y, pixelRatio, projection, key);
        this.tileCache.set(tileCoordKey, tile);
      } else {
        tile = this.tileCache.get(tileCoordKey);
        if (tile.key != key) {
          // The source's params changed. If the tile has an interim tile and if we
          // can use it then we use it. Otherwise we create a new tile.  In both
          // cases we attempt to assign an interim tile to the new tile.
          var interimTile = tile;
          tile = this.createTile_(z, x, y, pixelRatio, projection, key);

          //make the new tile the head of the list,
          if (interimTile.getState() == ol.TileState.IDLE) {
            //the old tile hasn't begun loading yet, and is now outdated, so we can simply discard it
            tile.interimTile = interimTile.interimTile;
          } else {
            tile.interimTile = interimTile;
          }
          tile.refreshInterimChain();
          this.tileCache.replace(tileCoordKey, tile);
        }
      }
      return tile;
    };

    /**
     * Sets whether to render reprojection edges or not (usually for debugging).
     * @param {boolean} render Render the edges.
     * @api
     */
    ol.source.TileImage.prototype.setRenderReprojectionEdges = function(render) {
      if (!ol.ENABLE_RASTER_REPROJECTION ||
          this.renderReprojectionEdges_ == render) {
        return;
      }
      this.renderReprojectionEdges_ = render;
      for (var id in this.tileCacheForProjection) {
        this.tileCacheForProjection[id].clear();
      }
      this.changed();
    };

    /**
     * Sets the tile grid to use when reprojecting the tiles to the given
     * projection instead of the default tile grid for the projection.
     *
     * This can be useful when the default tile grid cannot be created
     * (e.g. projection has no extent defined) or
     * for optimization reasons (custom tile size, resolutions, ...).
     *
     * @param {ol.ProjectionLike} projection Projection.
     * @param {ol.tilegrid.TileGrid} tilegrid Tile grid to use for the projection.
     * @api
     */
    ol.source.TileImage.prototype.setTileGridForProjection = function(projection, tilegrid) {
      Eif (ol.ENABLE_RASTER_REPROJECTION) {
        var proj = ol.proj.get(projection);
        Eif (proj) {
          var projKey = ol.getUid(proj).toString();
          Eif (!(projKey in this.tileGridForProjection)) {
            this.tileGridForProjection[projKey] = tilegrid;
          }
        }
      }
    };

    /**
     * @param {ol.ImageTile} imageTile Image tile.
     * @param {string} src Source.
     */
    ol.source.TileImage.defaultTileLoadFunction = function(imageTile, src) {
      imageTile.getImage().src = src;
    };