# all files / ol/source/ vectortile.js

55.81% Statements 24/43
47.06% Branches 16/34
16.67% Functions 1/6
55.81% Lines 24/43

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

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

    1×
    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

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

    goog.require('ol');
    goog.require('ol.TileState');
    goog.require('ol.VectorImageTile');
    goog.require('ol.VectorTile');
    goog.require('ol.size');
    goog.require('ol.tilegrid');
    goog.require('ol.source.UrlTile');

    /**
     * @classdesc
     * Class for layer sources providing vector data divided into a tile grid, to be
     * used with {@link ol.layer.VectorTile}. Although this source receives tiles
     * with vector features from the server, it is not meant for feature editing.
     * Features are optimized for rendering, their geometries are clipped at or near
     * tile boundaries and simplified for a view resolution. See
     * {@link ol.source.Vector} for vector sources that are suitable for feature
     * editing.
     *
     * @constructor
     * @fires ol.source.Tile.Event
     * @extends {ol.source.UrlTile}
     * @param {olx.source.VectorTileOptions} options Vector tile options.
     * @api
     */
    ol.source.VectorTile = function(options) {
      var projection = options.projection || 'EPSG:3857';

      var extent = options.extent || ol.tilegrid.extentFromProjection(projection);

      var tileGrid = options.tileGrid || ol.tilegrid.createXYZ({
        extent: extent,
        maxZoom: options.maxZoom || 22,
        minZoom: options.minZoom,
        tileSize: options.tileSize || 512
      });

      ol.source.UrlTile.call(this, {
        attributions: options.attributions,
        cacheSize: options.cacheSize !== undefined ? options.cacheSize : 128,
        extent: extent,
        logo: options.logo,
        opaque: false,
        projection: projection,
        state: options.state,
        tileGrid: tileGrid,
        tileLoadFunction: options.tileLoadFunction ?
          options.tileLoadFunction : ol.VectorImageTile.defaultLoadFunction,
        tileUrlFunction: options.tileUrlFunction,
        url: options.url,
        urls: options.urls,
        wrapX: options.wrapX === undefined ? true : options.wrapX
      });

      /**
       * @private
       * @type {ol.format.Feature}
       */
      this.format_ = options.format ? options.format : null;

      /**
       * @private
       * @type {Object.<string,ol.VectorTile>}
       */
      this.sourceTiles_ = {};

      /**
       * @private
       * @type {boolean}
       */
      this.overlaps_ = options.overlaps == undefined ? true : options.overlaps;

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

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

    };
    ol.inherits(ol.source.VectorTile, ol.source.UrlTile);

    /**
     * @return {boolean} The source can have overlapping geometries.
     */
    ol.source.VectorTile.prototype.getOverlaps = function() {
      return this.overlaps_;
    };

    /**
     * @inheritDoc
     */
    ol.source.VectorTile.prototype.getTile = function(z, x, y, pixelRatio, projection) {
      var tileCoordKey = this.getKeyZXY(z, x, y);
      if (this.tileCache.containsKey(tileCoordKey)) {
        return /** @type {!ol.Tile} */ (this.tileCache.get(tileCoordKey));
      } else {
        var tileCoord = [z, x, y];
        var urlTileCoord = this.getTileCoordForTileUrlFunction(
            tileCoord, projection);
        var tileUrl = urlTileCoord ?
          this.tileUrlFunction(urlTileCoord, pixelRatio, projection) : undefined;
        var tile = new ol.VectorImageTile(
            tileCoord,
            tileUrl !== undefined ? ol.TileState.IDLE : ol.TileState.EMPTY,
            tileUrl !== undefined ? tileUrl : '',
            this.format_, this.tileLoadFunction, urlTileCoord, this.tileUrlFunction,
            this.tileGrid, this.getTileGridForProjection(projection),
            this.sourceTiles_, pixelRatio, projection, this.tileClass,
            this.handleTileChange.bind(this));

        this.tileCache.set(tileCoordKey, tile);
        return tile;
      }
    };

    /**
     * @inheritDoc
     */
    ol.source.VectorTile.prototype.getTileGridForProjection = function(projection) {
      var code = projection.getCode();
      var tileGrid = this.tileGrids_[code];
      if (!tileGrid) {
        // A tile grid that matches the tile size of the source tile grid is more
        // likely to have 1:1 relationships between source tiles and rendered tiles.
        var sourceTileGrid = this.tileGrid;
        tileGrid = this.tileGrids_[code] = ol.tilegrid.createForProjection(projection, undefined,
            sourceTileGrid ? sourceTileGrid.getTileSize(sourceTileGrid.getMinZoom()) : undefined);
      }
      return tileGrid;
    };

    /**
     * @inheritDoc
     */
    ol.source.VectorTile.prototype.getTilePixelRatio = function(pixelRatio) {
      return pixelRatio;
    };

    /**
     * @inheritDoc
     */
    ol.source.VectorTile.prototype.getTilePixelSize = function(z, pixelRatio, projection) {
      var tileSize = ol.size.toSize(this.getTileGridForProjection(projection).getTileSize(z));
      return [Math.round(tileSize[0] * pixelRatio), Math.round(tileSize[1] * pixelRatio)];
    };