ArcGIS93Rest.js 8.0 KB

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