# all files / ol/reproj/ tile.js

85.29% Statements 116/136
67.69% Branches 44/65
90% Functions 9/10
85.29% Lines 116/136

    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
    3401×
    1×

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

    1×

    1×

    9×

    9×

    9×

    9×

    9×

    9×

    9×

    9×

    9×

    9×

    9×

    9×
    9×
    9×

    9×

    9×

    1×
    1×

    8×
    8×
    7×

    7×

    8×

    8×
    8×

    8×

    8×

    8×

    8×

    1×
    1×

    7×
    7×

    7×
    7×
    6×

    6×

    1×

    7×

    7×

    7×
    7×

    7×
    7×
    10×
    10×
    10×

    7×

    1×

    1×

    1×
    4×

    4×

    1×
    7×
    7×
    10×
    6×

    7×

    7×
    7×
    7×
    7×
    7×
    7×

    7×

    7×

    7×
    7×

    1×
    7×
    7×
    7×

    7×

    7×

    7×
    7×
    10×
    10×
    10×

    10×
    10×

    20×
    20×

    10×
    10×
    10×

    10×
    7×
    7×

    10×

    7×
    10×
    10×
    10×

    7×

    1×
    7×

    7×
    7×

     goog.provide('ol.reproj.Tile');
    goog.provide('ol.reproj.TileFunctionType');

    goog.require('goog.asserts');
    goog.require('goog.events');
    goog.require('goog.events.EventType');
    goog.require('goog.math');
    goog.require('goog.object');
    goog.require('ol.Tile');
    goog.require('ol.TileState');
    goog.require('ol.extent');
    goog.require('ol.math');
    goog.require('ol.proj');
    goog.require('ol.reproj');
    goog.require('ol.reproj.Triangulation');

    /**
     * @typedef {function(number, number, number, number) : ol.Tile}
     */
    ol.reproj.TileFunctionType;

    /**
     * @classdesc
     * Class encapsulating single reprojected tile.
     * See {@link ol.source.TileImage}.
     *
     * @constructor
     * @extends {ol.Tile}
     * @param {ol.proj.Projection} sourceProj Source projection.
     * @param {ol.tilegrid.TileGrid} sourceTileGrid Source tile grid.
     * @param {ol.proj.Projection} targetProj Target projection.
     * @param {ol.tilegrid.TileGrid} targetTileGrid Target tile grid.
     * @param {ol.TileCoord} tileCoord Coordinate of the tile.
     * @param {ol.TileCoord} wrappedTileCoord Coordinate of the tile wrapped in X.
     * @param {number} pixelRatio Pixel ratio.
     * @param {ol.reproj.TileFunctionType} getTileFunction
     *     Function returning source tiles (z, x, y, pixelRatio).
     * @param {number=} opt_errorThreshold Acceptable reprojection error (in px).
     * @param {boolean=} opt_renderEdges Render reprojection edges.
     */
    ol.reproj.Tile = function(sourceProj, sourceTileGrid,
        targetProj, targetTileGrid, tileCoord, wrappedTileCoord,
        pixelRatio, getTileFunction,
        opt_errorThreshold,
        opt_renderEdges) {
      goog.base(this, tileCoord, ol.TileState.IDLE);

      /**
       * @private
       * @type {boolean}
       */
      this.renderEdges_ = opt_renderEdges !== undefined ? opt_renderEdges : false;

      /**
       * @private
       * @type {number}
       */
      this.pixelRatio_ = pixelRatio;

      /**
       * @private
       * @type {HTMLCanvasElement}
       */
      this.canvas_ = null;

      /**
       * @private
       * @type {Object.<number, HTMLCanvasElement>}
       */
      this.canvasByContext_ = {};

      /**
       * @private
       * @type {ol.tilegrid.TileGrid}
       */
      this.sourceTileGrid_ = sourceTileGrid;

      /**
       * @private
       * @type {ol.tilegrid.TileGrid}
       */
      this.targetTileGrid_ = targetTileGrid;

      /**
       * @private
       * @type {ol.TileCoord}
       */
      this.wrappedTileCoord_ = wrappedTileCoord ? wrappedTileCoord : tileCoord;

      /**
       * @private
       * @type {!Array.<ol.Tile>}
       */
      this.sourceTiles_ = [];

      /**
       * @private
       * @type {Array.<goog.events.Key>}
       */
      this.sourcesListenerKeys_ = null;

      /**
       * @private
       * @type {number}
       */
      this.sourceZ_ = 0;

      var targetExtent = targetTileGrid.getTileCoordExtent(this.wrappedTileCoord_);
      var maxTargetExtent = this.targetTileGrid_.getExtent();
      var maxSourceExtent = this.sourceTileGrid_.getExtent();

      var limitedTargetExtent = maxTargetExtent ?
          ol.extent.getIntersection(targetExtent, maxTargetExtent) : targetExtent;

      if (ol.extent.getArea(limitedTargetExtent) === 0) {
        // Tile is completely outside range -> EMPTY
        // TODO: is it actually correct that the source even creates the tile ?
        this.state = ol.TileState.EMPTY;
        return;
      }

      var sourceProjExtent = sourceProj.getExtent();
      if (sourceProjExtent) {
        Iif (!maxSourceExtent) {
          maxSourceExtent = sourceProjExtent;
        } else {
          maxSourceExtent = ol.extent.getIntersection(
              maxSourceExtent, sourceProjExtent);
        }
      }

      var targetResolution = targetTileGrid.getResolution(
          this.wrappedTileCoord_[0]);

      var targetCenter = ol.extent.getCenter(limitedTargetExtent);
      var sourceResolution = ol.reproj.calculateSourceResolution(
          sourceProj, targetProj, targetCenter, targetResolution);

      Iif (!goog.math.isFiniteNumber(sourceResolution) || sourceResolution <= 0) {
        // invalid sourceResolution -> EMPTY
        // probably edges of the projections when no extent is defined
        this.state = ol.TileState.EMPTY;
        return;
      }

      var errorThresholdInPixels = opt_errorThreshold !== undefined ?
          opt_errorThreshold : ol.DEFAULT_RASTER_REPROJECTION_ERROR_THRESHOLD;

      /**
       * @private
       * @type {!ol.reproj.Triangulation}
       */
      this.triangulation_ = new ol.reproj.Triangulation(
          sourceProj, targetProj, limitedTargetExtent, maxSourceExtent,
          sourceResolution * errorThresholdInPixels);

      if (this.triangulation_.getTriangles().length === 0) {
        // no valid triangles -> EMPTY
        this.state = ol.TileState.EMPTY;
        return;
      }

      this.sourceZ_ = sourceTileGrid.getZForResolution(sourceResolution);
      var sourceExtent = this.triangulation_.calculateSourceExtent();

      Eif (maxSourceExtent) {
        if (sourceProj.canWrapX()) {
          sourceExtent[1] = ol.math.clamp(
              sourceExtent[1], maxSourceExtent[1], maxSourceExtent[3]);
          sourceExtent[3] = ol.math.clamp(
              sourceExtent[3], maxSourceExtent[1], maxSourceExtent[3]);
        } else {
          sourceExtent = ol.extent.getIntersection(sourceExtent, maxSourceExtent);
        }
      }

      Iif (!ol.extent.getArea(sourceExtent)) {
        this.state = ol.TileState.EMPTY;
      } else {
        var sourceRange = sourceTileGrid.getTileRangeForExtentAndZ(
            sourceExtent, this.sourceZ_);

        var tilesRequired = sourceRange.getWidth() * sourceRange.getHeight();
        Iif (!goog.asserts.assert(
            tilesRequired < ol.RASTER_REPROJECTION_MAX_SOURCE_TILES,
            'reasonable number of tiles is required')) {
          this.state = ol.TileState.ERROR;
          return;
        }
        for (var srcX = sourceRange.minX; srcX <= sourceRange.maxX; srcX++) {
          for (var srcY = sourceRange.minY; srcY <= sourceRange.maxY; srcY++) {
            var tile = getTileFunction(this.sourceZ_, srcX, srcY, pixelRatio);
            Eif (tile) {
              this.sourceTiles_.push(tile);
            }
          }
        }

        Iif (this.sourceTiles_.length === 0) {
          this.state = ol.TileState.EMPTY;
        }
      }
    };
    goog.inherits(ol.reproj.Tile, ol.Tile);

    /**
     * @inheritDoc
     */
    ol.reproj.Tile.prototype.disposeInternal = function() {
      if (this.state == ol.TileState.LOADING) {
        this.unlistenSources_();
      }
      goog.base(this, 'disposeInternal');
    };

    /**
     * @inheritDoc
     */
    ol.reproj.Tile.prototype.getImage = function(opt_context) {
      Iif (opt_context !== undefined) {
        var image;
        var key = goog.getUid(opt_context);
        if (key in this.canvasByContext_) {
          return this.canvasByContext_[key];
        } else if (goog.object.isEmpty(this.canvasByContext_)) {
          image = this.canvas_;
        } else {
          image = /** @type {HTMLCanvasElement} */ (this.canvas_.cloneNode(false));
        }
        this.canvasByContext_[key] = image;
        return image;
      } else {
        return this.canvas_;
      }
    };

    /**
     * @private
     */
    ol.reproj.Tile.prototype.reproject_ = function() {
      var sources = [];
      this.sourceTiles_.forEach(function(tile, i, arr) {
        if (tile && tile.getState() == ol.TileState.LOADED) {
          sources.push({
            extent: this.sourceTileGrid_.getTileCoordExtent(tile.tileCoord),
            image: tile.getImage()
          });
        }
      }, this);
      this.sourceTiles_.length = 0;

      var z = this.wrappedTileCoord_[0];
      var size = this.targetTileGrid_.getTileSize(z);
      var width = goog.isNumber(size) ? size : size[0];
      var height = goog.isNumber(size) ? size : size[1];
      var targetResolution = this.targetTileGrid_.getResolution(z);
      var sourceResolution = this.sourceTileGrid_.getResolution(this.sourceZ_);

      var targetExtent = this.targetTileGrid_.getTileCoordExtent(
          this.wrappedTileCoord_);
      this.canvas_ = ol.reproj.render(width, height, this.pixelRatio_,
          sourceResolution, this.sourceTileGrid_.getExtent(),
          targetResolution, targetExtent, this.triangulation_, sources,
          this.renderEdges_);

      this.state = ol.TileState.LOADED;
      this.changed();
    };

    /**
     * @inheritDoc
     */
    ol.reproj.Tile.prototype.load = function() {
      Eif (this.state == ol.TileState.IDLE) {
        this.state = ol.TileState.LOADING;
        this.changed();

        var leftToLoad = 0;

        goog.asserts.assert(!this.sourcesListenerKeys_,
            'this.sourcesListenerKeys_ should be null');

        this.sourcesListenerKeys_ = [];
        this.sourceTiles_.forEach(function(tile, i, arr) {
          var state = tile.getState();
          Eif (state == ol.TileState.IDLE || state == ol.TileState.LOADING) {
            leftToLoad++;

            var sourceListenKey;
            sourceListenKey = tile.listen(goog.events.EventType.CHANGE,
                function(e) {
                  var state = tile.getState();
                  if (state == ol.TileState.LOADED ||
                      state == ol.TileState.ERROR ||
                      state == ol.TileState.EMPTY) {
                    goog.events.unlistenByKey(sourceListenKey);
                    leftToLoad--;
                    goog.asserts.assert(leftToLoad >= 0,
                        'leftToLoad should not be negative');
                    if (leftToLoad === 0) {
                      this.unlistenSources_();
                      this.reproject_();
                    }
                  }
                }, false, this);
            this.sourcesListenerKeys_.push(sourceListenKey);
          }
        }, this);

        this.sourceTiles_.forEach(function(tile, i, arr) {
          var state = tile.getState();
          Eif (state == ol.TileState.IDLE) {
            tile.load();
          }
        });

        Iif (leftToLoad === 0) {
          goog.global.setTimeout(goog.bind(this.reproject_, this), 0);
        }
      }
    };

    /**
     * @private
     */
    ol.reproj.Tile.prototype.unlistenSources_ = function() {
      goog.asserts.assert(this.sourcesListenerKeys_,
          'this.sourcesListenerKeys_ should not be null');
      this.sourcesListenerKeys_.forEach(goog.events.unlistenByKey);
      this.sourcesListenerKeys_ = null;
    };