ArcGIS93Rest.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /* Copyright (c) 2006-2013 by OpenLayers Contributors (see authors.txt for
  2. * full list of contributors). Published under the 2-clause BSD license.
  3. * See license.txt in the OpenLayers distribution or repository for the
  4. * full text of the license. */
  5. /**
  6. * @requires OpenLayers/Layer/Grid.js
  7. */
  8. /**
  9. * Class: OpenLayers.Layer.ArcGIS93Rest
  10. * Instances of OpenLayers.Layer.ArcGIS93Rest are used to display data from
  11. * ESRI ArcGIS Server 9.3 (and up?) Mapping Services using the REST API.
  12. * Create a new ArcGIS93Rest layer with the <OpenLayers.Layer.ArcGIS93Rest>
  13. * constructor. More detail on the REST API is available at
  14. * http://sampleserver1.arcgisonline.com/ArcGIS/SDK/REST/index.html ;
  15. * specifically, the URL provided to this layer should be an export service
  16. * URL: http://sampleserver1.arcgisonline.com/ArcGIS/SDK/REST/export.html
  17. *
  18. * Inherits from:
  19. * - <OpenLayers.Layer.Grid>
  20. */
  21. OpenLayers.Layer.ArcGIS93Rest = OpenLayers.Class(OpenLayers.Layer.Grid, {
  22. /**
  23. * Constant: DEFAULT_PARAMS
  24. * {Object} Hashtable of default parameter key/value pairs
  25. */
  26. DEFAULT_PARAMS: {
  27. format: "png"
  28. },
  29. /**
  30. * APIProperty: isBaseLayer
  31. * {Boolean} Default is true for ArcGIS93Rest layer
  32. */
  33. isBaseLayer: true,
  34. /**
  35. * Constructor: OpenLayers.Layer.ArcGIS93Rest
  36. * Create a new ArcGIS93Rest layer object.
  37. *
  38. * Example:
  39. * (code)
  40. * var arcims = new OpenLayers.Layer.ArcGIS93Rest("MyName",
  41. * "http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StateCityHighway_USA/MapServer/export",
  42. * {
  43. * layers: "0,1,2"
  44. * });
  45. * (end)
  46. *
  47. * Parameters:
  48. * name - {String} A name for the layer
  49. * url - {String} Base url for the ArcGIS server REST service
  50. * options - {Object} An object with key/value pairs representing the
  51. * options and option values.
  52. *
  53. * Valid Options:
  54. * format - {String} MIME type of desired image type.
  55. * layers - {String} Comma-separated list of layers to display.
  56. * srs - {String} Projection ID.
  57. */
  58. initialize: function(name, url, params, options) {
  59. var newArguments = [];
  60. //uppercase params
  61. params = OpenLayers.Util.upperCaseObject(params);
  62. newArguments.push(name, url, params, options);
  63. OpenLayers.Layer.Grid.prototype.initialize.apply(this, newArguments);
  64. OpenLayers.Util.applyDefaults(
  65. this.params,
  66. OpenLayers.Util.upperCaseObject(this.DEFAULT_PARAMS)
  67. );
  68. //layer is transparent
  69. if (this.params.TRANSPARENT &&
  70. this.params.TRANSPARENT.toString().toLowerCase() == "true") {
  71. // unless explicitly set in options, make layer an overlay
  72. if ( (options == null) || (!options.isBaseLayer) ) {
  73. this.isBaseLayer = false;
  74. }
  75. // jpegs can never be transparent, so intelligently switch the
  76. // format, depending on the browser's capabilities
  77. if (this.params.FORMAT == "jpg") {
  78. this.params.FORMAT = OpenLayers.Util.alphaHack() ? "gif"
  79. : "png";
  80. }
  81. }
  82. },
  83. /**
  84. * Method: clone
  85. * Create a clone of this layer
  86. *
  87. * Returns:
  88. * {<OpenLayers.Layer.ArcGIS93Rest>} An exact clone of this layer
  89. */
  90. clone: function (obj) {
  91. if (obj == null) {
  92. obj = new OpenLayers.Layer.ArcGIS93Rest(this.name,
  93. this.url,
  94. this.params,
  95. this.getOptions());
  96. }
  97. //get all additions from superclasses
  98. obj = OpenLayers.Layer.Grid.prototype.clone.apply(this, [obj]);
  99. // copy/set any non-init, non-simple values here
  100. return obj;
  101. },
  102. /**
  103. * Method: getURL
  104. * Return an image url this layer.
  105. *
  106. * Parameters:
  107. * bounds - {<OpenLayers.Bounds>} A bounds representing the bbox for the
  108. * request.
  109. *
  110. * Returns:
  111. * {String} A string with the map image's url.
  112. */
  113. getURL: function (bounds) {
  114. bounds = this.adjustBounds(bounds);
  115. // ArcGIS Server only wants the numeric portion of the projection ID.
  116. var projWords = this.projection.getCode().split(":");
  117. var srid = projWords[projWords.length - 1];
  118. var imageSize = this.getImageSize();
  119. var newParams = {
  120. 'BBOX': bounds.toBBOX(),
  121. 'SIZE': imageSize.w + "," + imageSize.h,
  122. // We always want image, the other options were json, image with a whole lotta html around it, etc.
  123. 'F': "image",
  124. 'BBOXSR': srid,
  125. 'IMAGESR': srid
  126. };
  127. // Now add the filter parameters.
  128. if (this.layerDefs) {
  129. var layerDefStrList = [];
  130. var layerID;
  131. for(layerID in this.layerDefs) {
  132. if (this.layerDefs.hasOwnProperty(layerID)) {
  133. if (this.layerDefs[layerID]) {
  134. layerDefStrList.push(layerID);
  135. layerDefStrList.push(":");
  136. layerDefStrList.push(this.layerDefs[layerID]);
  137. layerDefStrList.push(";");
  138. }
  139. }
  140. }
  141. if (layerDefStrList.length > 0) {
  142. newParams['LAYERDEFS'] = layerDefStrList.join("");
  143. }
  144. }
  145. var requestString = this.getFullRequestString(newParams);
  146. return requestString;
  147. },
  148. /**
  149. * Method: setLayerFilter
  150. * addTile creates a tile, initializes it, and adds it to the layer div.
  151. *
  152. * Parameters:
  153. * id - {String} The id of the layer to which the filter applies.
  154. * queryDef - {String} A sql-ish query filter, for more detail see the ESRI
  155. * documentation at http://sampleserver1.arcgisonline.com/ArcGIS/SDK/REST/export.html
  156. */
  157. setLayerFilter: function ( id, queryDef ) {
  158. if (!this.layerDefs) {
  159. this.layerDefs = {};
  160. }
  161. if (queryDef) {
  162. this.layerDefs[id] = queryDef;
  163. } else {
  164. delete this.layerDefs[id];
  165. }
  166. },
  167. /**
  168. * Method: clearLayerFilter
  169. * Clears layer filters, either from a specific layer,
  170. * or all of them.
  171. *
  172. * Parameters:
  173. * id - {String} The id of the layer from which to remove any
  174. * filter. If unspecified/blank, all filters
  175. * will be removed.
  176. */
  177. clearLayerFilter: function ( id ) {
  178. if (id) {
  179. delete this.layerDefs[id];
  180. } else {
  181. delete this.layerDefs;
  182. }
  183. },
  184. /**
  185. * APIMethod: mergeNewParams
  186. * Catch changeParams and uppercase the new params to be merged in
  187. * before calling changeParams on the super class.
  188. *
  189. * Once params have been changed, the tiles will be reloaded with
  190. * the new parameters.
  191. *
  192. * Parameters:
  193. * newParams - {Object} Hashtable of new params to use
  194. */
  195. mergeNewParams:function(newParams) {
  196. var upperParams = OpenLayers.Util.upperCaseObject(newParams);
  197. var newArguments = [upperParams];
  198. return OpenLayers.Layer.Grid.prototype.mergeNewParams.apply(this,
  199. newArguments);
  200. },
  201. CLASS_NAME: "OpenLayers.Layer.ArcGIS93Rest"
  202. });