# all files / ol/source/ imagearcgisrest.js

92.05% Statements 81/88
44.83% Branches 13/29
77.78% Functions 7/9
92.05% Lines 81/88

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

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

    1×

    13×

    13×

    13×

    13×

    13×

    13×

    13×

    13×

    13×

    13×

    13×

    1×

    1×
    3×

    1×

    7×

    7×
    7×

    7×
    7×

    7×

    7×

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

    7×

    7×
    7×

    7×
    7×
    7×
    7×

    7×
    7×

    7×

    7×

    7×

    7×

    7×

    1×

    1×

    7×

    7×
    7×
    7×
    7×
    7×

    7×

    7×

    7×

    7×

    1×
    3×

    1×

    1×
    2×
    2×
    2×
    2×

    1×
    4×
    4×
    4×

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

    goog.require('ol');
    goog.require('ol.Image');
    goog.require('ol.asserts');
    goog.require('ol.events');
    goog.require('ol.events.EventType');
    goog.require('ol.extent');
    goog.require('ol.obj');
    goog.require('ol.source.Image');
    goog.require('ol.uri');

    /**
     * @classdesc
     * Source for data from ArcGIS Rest services providing single, untiled images.
     * Useful when underlying map service has labels.
     *
     * If underlying map service is not using labels,
     * take advantage of ol image caching and use
     * {@link ol.source.TileArcGISRest} data source.
     *
     * @constructor
     * @fires ol.source.Image.Event
     * @extends {ol.source.Image}
     * @param {olx.source.ImageArcGISRestOptions=} opt_options Image ArcGIS Rest Options.
     * @api
     */
    ol.source.ImageArcGISRest = function(opt_options) {

      var options = opt_options || {};

      ol.source.Image.call(this, {
        attributions: options.attributions,
        logo: options.logo,
        projection: options.projection,
        resolutions: options.resolutions
      });

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

      /**
       * @private
       * @type {boolean}
       */
      this.hidpi_ = options.hidpi !== undefined ? options.hidpi : true;

      /**
       * @private
       * @type {string|undefined}
       */
      this.url_ = options.url;

      /**
       * @private
       * @type {ol.ImageLoadFunctionType}
       */
      this.imageLoadFunction_ = options.imageLoadFunction !== undefined ?
        options.imageLoadFunction : ol.source.Image.defaultImageLoadFunction;

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

      /**
       * @private
       * @type {ol.Image}
       */
      this.image_ = null;

      /**
       * @private
       * @type {ol.Size}
       */
      this.imageSize_ = [0, 0];

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

      /**
       * @private
       * @type {number}
       */
      this.ratio_ = options.ratio !== undefined ? options.ratio : 1.5;

    };
    ol.inherits(ol.source.ImageArcGISRest, ol.source.Image);

    /**
     * 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.ImageArcGISRest.prototype.getParams = function() {
      return this.params_;
    };

    /**
     * @inheritDoc
     */
    ol.source.ImageArcGISRest.prototype.getImageInternal = function(extent, resolution, pixelRatio, projection) {

      Iif (this.url_ === undefined) {
        return null;
      }

      resolution = this.findNearestResolution(resolution);
      pixelRatio = this.hidpi_ ? pixelRatio : 1;

      var image = this.image_;
      Iif (image &&
          this.renderedRevision_ == this.getRevision() &&
          image.getResolution() == resolution &&
          image.getPixelRatio() == pixelRatio &&
          ol.extent.containsExtent(image.getExtent(), extent)) {
        return image;
      }

      var params = {
        'F': 'image',
        'FORMAT': 'PNG32',
        'TRANSPARENT': true
      };
      ol.obj.assign(params, this.params_);

      extent = extent.slice();
      var centerX = (extent[0] + extent[2]) / 2;
      var centerY = (extent[1] + extent[3]) / 2;
      Eif (this.ratio_ != 1) {
        var halfWidth = this.ratio_ * ol.extent.getWidth(extent) / 2;
        var halfHeight = this.ratio_ * ol.extent.getHeight(extent) / 2;
        extent[0] = centerX - halfWidth;
        extent[1] = centerY - halfHeight;
        extent[2] = centerX + halfWidth;
        extent[3] = centerY + halfHeight;
      }

      var imageResolution = resolution / pixelRatio;

      // Compute an integer width and height.
      var width = Math.ceil(ol.extent.getWidth(extent) / imageResolution);
      var height = Math.ceil(ol.extent.getHeight(extent) / imageResolution);

      // Modify the extent to match the integer width and height.
      extent[0] = centerX - imageResolution * width / 2;
      extent[2] = centerX + imageResolution * width / 2;
      extent[1] = centerY - imageResolution * height / 2;
      extent[3] = centerY + imageResolution * height / 2;

      this.imageSize_[0] = width;
      this.imageSize_[1] = height;

      var url = this.getRequestUrl_(extent, this.imageSize_, pixelRatio,
          projection, params);

      this.image_ = new ol.Image(extent, resolution, pixelRatio,
          this.getAttributions(), url, this.crossOrigin_, this.imageLoadFunction_);

      this.renderedRevision_ = this.getRevision();

      ol.events.listen(this.image_, ol.events.EventType.CHANGE,
          this.handleImageChange, this);

      return this.image_;

    };

    /**
     * Return the image load function of the source.
     * @return {ol.ImageLoadFunctionType} The image load function.
     * @api
     */
    ol.source.ImageArcGISRest.prototype.getImageLoadFunction = function() {
      return this.imageLoadFunction_;
    };

    /**
     * @param {ol.Extent} extent Extent.
     * @param {ol.Size} size Size.
     * @param {number} pixelRatio Pixel ratio.
     * @param {ol.proj.Projection} projection Projection.
     * @param {Object} params Params.
     * @return {string} Request URL.
     * @private
     */
    ol.source.ImageArcGISRest.prototype.getRequestUrl_ = function(extent, size, pixelRatio, projection, params) {
      // ArcGIS Server only wants the numeric portion of the projection ID.
      var srid = projection.getCode().split(':').pop();

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

      var url = this.url_;

      var modifiedUrl = url
          .replace(/MapServer\/?$/, 'MapServer/export')
          .replace(/ImageServer\/?$/, 'ImageServer/exportImage');
      Iif (modifiedUrl == url) {
        ol.asserts.assert(false, 50); // `options.featureTypes` should be an Array
      }
      return ol.uri.appendParams(modifiedUrl, params);
    };

    /**
     * Return the URL used for this ArcGIS source.
     * @return {string|undefined} URL.
     * @api
     */
    ol.source.ImageArcGISRest.prototype.getUrl = function() {
      return this.url_;
    };

    /**
     * Set the image load function of the source.
     * @param {ol.ImageLoadFunctionType} imageLoadFunction Image load function.
     * @api
     */
    ol.source.ImageArcGISRest.prototype.setImageLoadFunction = function(imageLoadFunction) {
      this.image_ = null;
      this.imageLoadFunction_ = imageLoadFunction;
      this.changed();
    };

    /**
     * Set the URL to use for requests.
     * @param {string|undefined} url URL.
     * @api
     */
    ol.source.ImageArcGISRest.prototype.setUrl = function(url) {
      Eif (url != this.url_) {
        this.url_ = url;
        this.image_ = null;
        this.changed();
      }
    };

    /**
     * Update the user-provided params.
     * @param {Object} params Params.
     * @api
     */
    ol.source.ImageArcGISRest.prototype.updateParams = function(params) {
      ol.obj.assign(this.params_, params);
      this.image_ = null;
      this.changed();
    };