# all files / ol/source/ tilejsonsource.js

100% Statements 41/41
62.5% Branches 10/16
100% Functions 3/3
100% Lines 41/41

    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

    1×
    1×

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

    1×

    2×

    2×
    2×

    1×

    1×

    1×

    1×
    1×
    1×
    1×

    1×

    1×
    1×

    1×
    1×
    1×

    1×

    1×

    1×
    1×

    1×
    1×
    1×
    9×
    9×

    1×

    1×

    1×
    1×

     // FIXME check order of async callbacks

    /**
     * @see http://mapbox.com/developers/api/
     */

    goog.provide('ol.source.TileJSON');
    goog.provide('ol.tilejson');

    goog.require('goog.asserts');
    goog.require('goog.net.Jsonp');
    goog.require('ol.Attribution');
    goog.require('ol.TileRange');
    goog.require('ol.TileUrlFunction');
    goog.require('ol.extent');
    goog.require('ol.proj');
    goog.require('ol.source.State');
    goog.require('ol.source.TileImage');

    /**
     * @classdesc
     * Layer source for tile data in TileJSON format.
     *
     * @constructor
     * @extends {ol.source.TileImage}
     * @param {olx.source.TileJSONOptions} options TileJSON options.
     * @api stable
     */
    ol.source.TileJSON = function(options) {

      goog.base(this, {
        attributions: options.attributions,
        crossOrigin: options.crossOrigin,
        projection: ol.proj.get('EPSG:3857'),
        reprojectionErrorThreshold: options.reprojectionErrorThreshold,
        state: ol.source.State.LOADING,
        tileLoadFunction: options.tileLoadFunction,
        wrapX: options.wrapX !== undefined ? options.wrapX : true
      });

      var request = new goog.net.Jsonp(options.url);
      request.send(undefined, goog.bind(this.handleTileJSONResponse, this),
          goog.bind(this.handleTileJSONError, this));

    };
    goog.inherits(ol.source.TileJSON, ol.source.TileImage);

    /**
     * @protected
     * @param {TileJSON} tileJSON Tile JSON.
     */
    ol.source.TileJSON.prototype.handleTileJSONResponse = function(tileJSON) {

      var epsg4326Projection = ol.proj.get('EPSG:4326');

      var sourceProjection = this.getProjection();
      var extent;
      Eif (tileJSON.bounds !== undefined) {
        var transform = ol.proj.getTransformFromProjections(
            epsg4326Projection, sourceProjection);
        extent = ol.extent.applyTransform(tileJSON.bounds, transform);
      }

      Eif (tileJSON.scheme !== undefined) {
        goog.asserts.assert(tileJSON.scheme == 'xyz', 'tileJSON-scheme is "xyz"');
      }
      var minZoom = tileJSON.minzoom || 0;
      var maxZoom = tileJSON.maxzoom || 22;
      var tileGrid = ol.tilegrid.createXYZ({
        extent: ol.tilegrid.extentFromProjection(sourceProjection),
        maxZoom: maxZoom,
        minZoom: minZoom
      });
      this.tileGrid = tileGrid;

      this.tileUrlFunction =
          ol.TileUrlFunction.createFromTemplates(tileJSON.tiles, tileGrid);

      Eif (tileJSON.attribution !== undefined && !this.getAttributions()) {
        var attributionExtent = extent !== undefined ?
            extent : epsg4326Projection.getExtent();
        /** @type {Object.<string, Array.<ol.TileRange>>} */
        var tileRanges = {};
        var z, zKey;
        for (z = minZoom; z <= maxZoom; ++z) {
          zKey = z.toString();
          tileRanges[zKey] =
              [tileGrid.getTileRangeForExtentAndZ(attributionExtent, z)];
        }
        this.setAttributions([
          new ol.Attribution({
            html: tileJSON.attribution,
            tileRanges: tileRanges
          })
        ]);
      }

      this.setState(ol.source.State.READY);

    };

    /**
     * @protected
     */
    ol.source.TileJSON.prototype.handleTileJSONError = function() {
      this.setState(ol.source.State.ERROR);
    };