# all files / ol/source/ tilearcgisrestsource.js

93.65% Statements 59/63
66.67% Branches 16/24
83.33% Functions 5/6
93.65% Lines 59/63

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

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

    1×

    17×

    17×

    17×

    17×

    17×

    1×

    1×
    3×

    1×

    9×
    9×

    9×

    9×
    9×
    9×
    9×
    9×

    9×
    9×
    8×

    1×
    1×

    9×
    9×

    9×
    8×

    1×
    1×

    9×

    1×

    1×

    9×
    9×
    9×

    9×

    9×

    9×

    9×
    2×

    9×

    9×

    9×

    1×
    4×
    4×

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

    goog.require('goog.asserts');
    goog.require('goog.math');
    goog.require('goog.object');
    goog.require('goog.string');
    goog.require('goog.uri.utils');
    goog.require('ol');
    goog.require('ol.TileCoord');
    goog.require('ol.extent');
    goog.require('ol.proj');
    goog.require('ol.size');
    goog.require('ol.source.TileImage');
    goog.require('ol.tilecoord');

    /**
     * @classdesc
     * Layer source for tile data from ArcGIS Rest services. Map and Image
     * Services are supported.
     *
     * For cached ArcGIS services, better performance is available using the
     * {@link ol.source.XYZ} data source.
     *
     * @constructor
     * @extends {ol.source.TileImage}
     * @param {olx.source.TileArcGISRestOptions=} opt_options Tile ArcGIS Rest
     *     options.
     * @api
     */
    ol.source.TileArcGISRest = function(opt_options) {

      var options = opt_options || {};

      var params = options.params !== undefined ? options.params : {};

      goog.base(this, {
        attributions: options.attributions,
        crossOrigin: options.crossOrigin,
        logo: options.logo,
        projection: options.projection,
        reprojectionErrorThreshold: options.reprojectionErrorThreshold,
        tileGrid: options.tileGrid,
        tileLoadFunction: options.tileLoadFunction,
        tileUrlFunction: goog.bind(this.tileUrlFunction_, this),
        url: options.url,
        urls: options.urls,
        wrapX: options.wrapX !== undefined ? options.wrapX : true
      });

      /**
       * @private
       * @type {Object}
       */
      this.params_ = params;

      /**
       * @private
       * @type {ol.Extent}
       */
      this.tmpExtent_ = ol.extent.createEmpty();

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

    /**
     * Get the user-provided params, i.e. those passed to the constructor through
     * the "params" option, and possibly updated using the updateParams method.
     * @return {Object} Params.
     * @api
     */
    ol.source.TileArcGISRest.prototype.getParams = function() {
      return this.params_;
    };

    /**
     * @param {ol.TileCoord} tileCoord Tile coordinate.
     * @param {ol.Size} tileSize Tile size.
     * @param {ol.Extent} tileExtent Tile extent.
     * @param {number} pixelRatio Pixel ratio.
     * @param {ol.proj.Projection} projection Projection.
     * @param {Object} params Params.
     * @return {string|undefined} Request URL.
     * @private
     */
    ol.source.TileArcGISRest.prototype.getRequestUrl_ =
        function(tileCoord, tileSize, tileExtent,
            pixelRatio, projection, params) {

      var urls = this.urls;
      Iif (!urls) {
        return undefined;
      }

      // ArcGIS Server only wants the numeric portion of the projection ID.
      var srid = projection.getCode().split(':').pop();

      params['SIZE'] = tileSize[0] + ',' + tileSize[1];
      params['BBOX'] = tileExtent.join(',');
      params['BBOXSR'] = srid;
      params['IMAGESR'] = srid;
      params['DPI'] = Math.round(
          params['DPI'] ? params['DPI'] * pixelRatio : 90 * pixelRatio
          );

      var url;
      if (urls.length == 1) {
        url = urls[0];
      } else {
        var index = goog.math.modulo(ol.tilecoord.hash(tileCoord), urls.length);
        url = urls[index];
      }

      Eif (!goog.string.endsWith(url, '/')) {
        url = url + '/';
      }

      // If a MapServer, use export. If an ImageServer, use exportImage.
      if (goog.string.endsWith(url, 'MapServer/')) {
        url = url + 'export';
      }
      else Eif (goog.string.endsWith(url, 'ImageServer/')) {
        url = url + 'exportImage';
      }
      else {
        goog.asserts.fail('Unknown Rest Service', url);
      }

      return goog.uri.utils.appendParamsFromMap(url, params);
    };

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

    /**
     * @param {ol.TileCoord} tileCoord Tile coordinate.
     * @param {number} pixelRatio Pixel ratio.
     * @param {ol.proj.Projection} projection Projection.
     * @return {string|undefined} Tile URL.
     * @private
     */
    ol.source.TileArcGISRest.prototype.tileUrlFunction_ =
        function(tileCoord, pixelRatio, projection) {

      var tileGrid = this.getTileGrid();
      Eif (!tileGrid) {
        tileGrid = this.getTileGridForProjection(projection);
      }

      Iif (tileGrid.getResolutions().length <= tileCoord[0]) {
        return undefined;
      }

      var tileExtent = tileGrid.getTileCoordExtent(
          tileCoord, this.tmpExtent_);
      var tileSize = ol.size.toSize(
          tileGrid.getTileSize(tileCoord[0]), this.tmpSize);

      if (pixelRatio != 1) {
        tileSize = ol.size.scale(tileSize, pixelRatio, this.tmpSize);
      }

      // Apply default params and override with user specified values.
      var baseParams = {
        'F': 'image',
        'FORMAT': 'PNG32',
        'TRANSPARENT': true
      };
      goog.object.extend(baseParams, this.params_);

      return this.getRequestUrl_(tileCoord, tileSize, tileExtent,
          pixelRatio, projection, baseParams);
    };

    /**
     * Update the user-provided params.
     * @param {Object} params Params.
     * @api stable
     */
    ol.source.TileArcGISRest.prototype.updateParams = function(params) {
      goog.object.extend(this.params_, params);
      this.changed();
    };