|
| 1 | +goog.provide('ol.source.TileArcGISRest'); |
| 2 | + |
| 3 | +goog.require('goog.array'); |
| 4 | +goog.require('goog.asserts'); |
| 5 | +goog.require('goog.math'); |
| 6 | +goog.require('goog.object'); |
| 7 | +goog.require('goog.string'); |
| 8 | +goog.require('goog.uri.utils'); |
| 9 | +goog.require('ol'); |
| 10 | +goog.require('ol.TileCoord'); |
| 11 | +goog.require('ol.TileUrlFunction'); |
| 12 | +goog.require('ol.extent'); |
| 13 | +goog.require('ol.proj'); |
| 14 | +goog.require('ol.source.TileImage'); |
| 15 | +goog.require('ol.tilecoord'); |
| 16 | + |
| 17 | + |
| 18 | + |
| 19 | +/** |
| 20 | + * @classdesc |
| 21 | + * Layer source for tile data from ArcGIS Rest services. Map and Image |
| 22 | + * Services are supported. |
| 23 | + * |
| 24 | + * For cached ArcGIS services, better performance is available using the |
| 25 | + * {@link ol.source.XYZ} data source. |
| 26 | + * |
| 27 | + * @constructor |
| 28 | + * @extends {ol.source.TileImage} |
| 29 | + * @param {olx.source.TileArcGISRestOptions=} opt_options Tile ArcGIS Rest |
| 30 | + * options. |
| 31 | + * @api |
| 32 | + */ |
| 33 | +ol.source.TileArcGISRest = function(opt_options) { |
| 34 | + |
| 35 | + var options = goog.isDef(opt_options) ? opt_options : {}; |
| 36 | + |
| 37 | + var params = goog.isDef(options.params) ? options.params : {}; |
| 38 | + |
| 39 | + goog.base(this, { |
| 40 | + attributions: options.attributions, |
| 41 | + logo: options.logo, |
| 42 | + projection: options.projection, |
| 43 | + tileGrid: options.tileGrid, |
| 44 | + tileLoadFunction: options.tileLoadFunction, |
| 45 | + tileUrlFunction: goog.bind(this.tileUrlFunction_, this) |
| 46 | + }); |
| 47 | + |
| 48 | + var urls = options.urls; |
| 49 | + if (!goog.isDef(urls) && goog.isDef(options.url)) { |
| 50 | + urls = ol.TileUrlFunction.expandUrl(options.url); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * @private |
| 55 | + * @type {!Array.<string>} |
| 56 | + */ |
| 57 | + this.urls_ = goog.isDefAndNotNull(urls) ? urls : []; |
| 58 | + |
| 59 | + /** |
| 60 | + * @private |
| 61 | + * @type {Object} |
| 62 | + */ |
| 63 | + this.params_ = params; |
| 64 | + |
| 65 | + /** |
| 66 | + * @private |
| 67 | + * @type {ol.Extent} |
| 68 | + */ |
| 69 | + this.tmpExtent_ = ol.extent.createEmpty(); |
| 70 | + |
| 71 | +}; |
| 72 | +goog.inherits(ol.source.TileArcGISRest, ol.source.TileImage); |
| 73 | + |
| 74 | + |
| 75 | +/** |
| 76 | + * Get the user-provided params, i.e. those passed to the constructor through |
| 77 | + * the "params" option, and possibly updated using the updateParams method. |
| 78 | + * @return {Object} Params. |
| 79 | + * @api |
| 80 | + */ |
| 81 | +ol.source.TileArcGISRest.prototype.getParams = function() { |
| 82 | + return this.params_; |
| 83 | +}; |
| 84 | + |
| 85 | + |
| 86 | +/** |
| 87 | + * @param {ol.TileCoord} tileCoord Tile coordinate. |
| 88 | + * @param {number} tileSize Tile size. |
| 89 | + * @param {ol.Extent} tileExtent Tile extent. |
| 90 | + * @param {number} pixelRatio Pixel ratio. |
| 91 | + * @param {ol.proj.Projection} projection Projection. |
| 92 | + * @param {Object} params Params. |
| 93 | + * @return {string|undefined} Request URL. |
| 94 | + * @private |
| 95 | + */ |
| 96 | +ol.source.TileArcGISRest.prototype.getRequestUrl_ = |
| 97 | + function(tileCoord, tileSize, tileExtent, |
| 98 | + pixelRatio, projection, params) { |
| 99 | + |
| 100 | + var urls = this.urls_; |
| 101 | + if (goog.array.isEmpty(urls)) { |
| 102 | + return undefined; |
| 103 | + } |
| 104 | + |
| 105 | + // ArcGIS Server only wants the numeric portion of the projection ID. |
| 106 | + var srid = projection.getCode().split(':').pop(); |
| 107 | + |
| 108 | + params['SIZE'] = tileSize + ',' + tileSize; |
| 109 | + params['BBOX'] = tileExtent.join(','); |
| 110 | + params['BBOXSR'] = srid; |
| 111 | + params['IMAGESR'] = srid; |
| 112 | + |
| 113 | + var url; |
| 114 | + if (urls.length == 1) { |
| 115 | + url = urls[0]; |
| 116 | + } else { |
| 117 | + var index = goog.math.modulo(ol.tilecoord.hash(tileCoord), urls.length); |
| 118 | + url = urls[index]; |
| 119 | + } |
| 120 | + |
| 121 | + if (!goog.string.endsWith(url, '/')) { |
| 122 | + url = url + '/'; |
| 123 | + } |
| 124 | + |
| 125 | + // If a MapServer, use export. If an ImageServer, use exportImage. |
| 126 | + if (goog.string.endsWith(url, 'MapServer/')) { |
| 127 | + url = url + 'export'; |
| 128 | + } |
| 129 | + else if (goog.string.endsWith(url, 'ImageServer/')) { |
| 130 | + url = url + 'exportImage'; |
| 131 | + } |
| 132 | + else { |
| 133 | + goog.asserts.fail('Unknown Rest Service', url); |
| 134 | + } |
| 135 | + |
| 136 | + return goog.uri.utils.appendParamsFromMap(url, params); |
| 137 | +}; |
| 138 | + |
| 139 | + |
| 140 | +/** |
| 141 | + * Return the URLs used for this ArcGIS source. |
| 142 | + * @return {!Array.<string>} URLs. |
| 143 | + * @api stable |
| 144 | + */ |
| 145 | +ol.source.TileArcGISRest.prototype.getUrls = function() { |
| 146 | + return this.urls_; |
| 147 | +}; |
| 148 | + |
| 149 | + |
| 150 | +/** |
| 151 | + * @param {string|undefined} url URL. |
| 152 | + * @api stable |
| 153 | + */ |
| 154 | +ol.source.TileArcGISRest.prototype.setUrl = function(url) { |
| 155 | + var urls = goog.isDef(url) ? ol.TileUrlFunction.expandUrl(url) : null; |
| 156 | + this.setUrls(urls); |
| 157 | +}; |
| 158 | + |
| 159 | + |
| 160 | +/** |
| 161 | + * @param {Array.<string>|undefined} urls URLs. |
| 162 | + * @api stable |
| 163 | + */ |
| 164 | +ol.source.TileArcGISRest.prototype.setUrls = function(urls) { |
| 165 | + this.urls_ = goog.isDefAndNotNull(urls) ? urls : []; |
| 166 | + this.changed(); |
| 167 | +}; |
| 168 | + |
| 169 | + |
| 170 | +/** |
| 171 | + * @param {ol.TileCoord} tileCoord Tile coordinate. |
| 172 | + * @param {number} pixelRatio Pixel ratio. |
| 173 | + * @param {ol.proj.Projection} projection Projection. |
| 174 | + * @return {string|undefined} Tile URL. |
| 175 | + * @private |
| 176 | + */ |
| 177 | +ol.source.TileArcGISRest.prototype.tileUrlFunction_ = |
| 178 | + function(tileCoord, pixelRatio, projection) { |
| 179 | + |
| 180 | + var tileGrid = this.getTileGrid(); |
| 181 | + if (goog.isNull(tileGrid)) { |
| 182 | + tileGrid = this.getTileGridForProjection(projection); |
| 183 | + } |
| 184 | + |
| 185 | + if (tileGrid.getResolutions().length <= tileCoord[0]) { |
| 186 | + return undefined; |
| 187 | + } |
| 188 | + |
| 189 | + var tileExtent = tileGrid.getTileCoordExtent( |
| 190 | + tileCoord, this.tmpExtent_); |
| 191 | + var tileSize = tileGrid.getTileSize(tileCoord[0]); |
| 192 | + |
| 193 | + if (pixelRatio != 1) { |
| 194 | + tileSize = (tileSize * pixelRatio + 0.5) | 0; |
| 195 | + } |
| 196 | + |
| 197 | + // Apply default params and override with user specified values. |
| 198 | + var baseParams = { |
| 199 | + 'F': 'image', |
| 200 | + 'FORMAT': 'PNG32', |
| 201 | + 'TRANSPARENT': true |
| 202 | + }; |
| 203 | + goog.object.extend(baseParams, this.params_); |
| 204 | + |
| 205 | + return this.getRequestUrl_(tileCoord, tileSize, tileExtent, |
| 206 | + pixelRatio, projection, baseParams); |
| 207 | +}; |
| 208 | + |
| 209 | + |
| 210 | +/** |
| 211 | + * Update the user-provided params. |
| 212 | + * @param {Object} params Params. |
| 213 | + * @api stable |
| 214 | + */ |
| 215 | +ol.source.TileArcGISRest.prototype.updateParams = function(params) { |
| 216 | + goog.object.extend(this.params_, params); |
| 217 | + this.changed(); |
| 218 | +}; |
0 commit comments