WMS.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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.WMS
  10. * Instances of OpenLayers.Layer.WMS are used to display data from OGC Web
  11. * Mapping Services. Create a new WMS layer with the <OpenLayers.Layer.WMS>
  12. * constructor.
  13. *
  14. * Inherits from:
  15. * - <OpenLayers.Layer.Grid>
  16. */
  17. OpenLayers.Layer.WMS = OpenLayers.Class(OpenLayers.Layer.Grid, {
  18. /**
  19. * Constant: DEFAULT_PARAMS
  20. * {Object} Hashtable of default parameter key/value pairs
  21. */
  22. DEFAULT_PARAMS: { service: "WMS",
  23. version: "1.1.1",
  24. request: "GetMap",
  25. styles: "",
  26. format: "image/jpeg"
  27. },
  28. /**
  29. * APIProperty: isBaseLayer
  30. * {Boolean} Default is true for WMS layer
  31. */
  32. isBaseLayer: true,
  33. /**
  34. * APIProperty: encodeBBOX
  35. * {Boolean} Should the BBOX commas be encoded? The WMS spec says 'no',
  36. * but some services want it that way. Default false.
  37. */
  38. encodeBBOX: false,
  39. /**
  40. * APIProperty: noMagic
  41. * {Boolean} If true, the image format will not be automagicaly switched
  42. * from image/jpeg to image/png or image/gif when using
  43. * TRANSPARENT=TRUE. Also isBaseLayer will not changed by the
  44. * constructor. Default false.
  45. */
  46. noMagic: false,
  47. /**
  48. * Property: yx
  49. * {Object} Keys in this object are EPSG codes for which the axis order
  50. * is to be reversed (yx instead of xy, LatLon instead of LonLat), with
  51. * true as value. This is only relevant for WMS versions >= 1.3.0, and
  52. * only if yx is not set in <OpenLayers.Projection.defaults> for the
  53. * used projection.
  54. */
  55. yx: {},
  56. /**
  57. * Constructor: OpenLayers.Layer.WMS
  58. * Create a new WMS layer object
  59. *
  60. * Examples:
  61. *
  62. * The code below creates a simple WMS layer using the image/jpeg format.
  63. * (code)
  64. * var wms = new OpenLayers.Layer.WMS("NASA Global Mosaic",
  65. * "http://wms.jpl.nasa.gov/wms.cgi",
  66. * {layers: "modis,global_mosaic"});
  67. * (end)
  68. * Note the 3rd argument (params). Properties added to this object will be
  69. * added to the WMS GetMap requests used for this layer's tiles. The only
  70. * mandatory parameter is "layers". Other common WMS params include
  71. * "transparent", "styles" and "format". Note that the "srs" param will
  72. * always be ignored. Instead, it will be derived from the baseLayer's or
  73. * map's projection.
  74. *
  75. * The code below creates a transparent WMS layer with additional options.
  76. * (code)
  77. * var wms = new OpenLayers.Layer.WMS("NASA Global Mosaic",
  78. * "http://wms.jpl.nasa.gov/wms.cgi",
  79. * {
  80. * layers: "modis,global_mosaic",
  81. * transparent: true
  82. * }, {
  83. * opacity: 0.5,
  84. * singleTile: true
  85. * });
  86. * (end)
  87. * Note that by default, a WMS layer is configured as baseLayer. Setting
  88. * the "transparent" param to true will apply some magic (see <noMagic>).
  89. * The default image format changes from image/jpeg to image/png, and the
  90. * layer is not configured as baseLayer.
  91. *
  92. * Parameters:
  93. * name - {String} A name for the layer
  94. * url - {String} Base url for the WMS
  95. * (e.g. http://wms.jpl.nasa.gov/wms.cgi)
  96. * params - {Object} An object with key/value pairs representing the
  97. * GetMap query string parameters and parameter values.
  98. * options - {Object} Hashtable of extra options to tag onto the layer.
  99. * These options include all properties listed above, plus the ones
  100. * inherited from superclasses.
  101. */
  102. initialize: function(name, url, params, options) {
  103. var newArguments = [];
  104. //uppercase params
  105. params = OpenLayers.Util.upperCaseObject(params);
  106. if (parseFloat(params.VERSION) >= 1.3 && !params.EXCEPTIONS) {
  107. params.EXCEPTIONS = "INIMAGE";
  108. }
  109. newArguments.push(name, url, params, options);
  110. OpenLayers.Layer.Grid.prototype.initialize.apply(this, newArguments);
  111. OpenLayers.Util.applyDefaults(
  112. this.params,
  113. OpenLayers.Util.upperCaseObject(this.DEFAULT_PARAMS)
  114. );
  115. //layer is transparent
  116. if (!this.noMagic && this.params.TRANSPARENT &&
  117. this.params.TRANSPARENT.toString().toLowerCase() == "true") {
  118. // unless explicitly set in options, make layer an overlay
  119. if ( (options == null) || (!options.isBaseLayer) ) {
  120. this.isBaseLayer = false;
  121. }
  122. // jpegs can never be transparent, so intelligently switch the
  123. // format, depending on the browser's capabilities
  124. if (this.params.FORMAT == "image/jpeg") {
  125. this.params.FORMAT = OpenLayers.Util.alphaHack() ? "image/gif"
  126. : "image/png";
  127. }
  128. }
  129. },
  130. /**
  131. * Method: clone
  132. * Create a clone of this layer
  133. *
  134. * Returns:
  135. * {<OpenLayers.Layer.WMS>} An exact clone of this layer
  136. */
  137. clone: function (obj) {
  138. if (obj == null) {
  139. obj = new OpenLayers.Layer.WMS(this.name,
  140. this.url,
  141. this.params,
  142. this.getOptions());
  143. }
  144. //get all additions from superclasses
  145. obj = OpenLayers.Layer.Grid.prototype.clone.apply(this, [obj]);
  146. // copy/set any non-init, non-simple values here
  147. return obj;
  148. },
  149. /**
  150. * APIMethod: reverseAxisOrder
  151. * Returns true if the axis order is reversed for the WMS version and
  152. * projection of the layer.
  153. *
  154. * Returns:
  155. * {Boolean} true if the axis order is reversed, false otherwise.
  156. */
  157. reverseAxisOrder: function() {
  158. var projCode = this.projection.getCode();
  159. return parseFloat(this.params.VERSION) >= 1.3 &&
  160. !!(this.yx[projCode] || (OpenLayers.Projection.defaults[projCode] &&
  161. OpenLayers.Projection.defaults[projCode].yx));
  162. },
  163. /**
  164. * Method: getURL
  165. * Return a GetMap query string for this layer
  166. *
  167. * Parameters:
  168. * bounds - {<OpenLayers.Bounds>} A bounds representing the bbox for the
  169. * request.
  170. *
  171. * Returns:
  172. * {String} A string with the layer's url and parameters and also the
  173. * passed-in bounds and appropriate tile size specified as
  174. * parameters.
  175. */
  176. getURL: function (bounds) {
  177. bounds = this.adjustBounds(bounds);
  178. var imageSize = this.getImageSize();
  179. var newParams = {};
  180. // WMS 1.3 introduced axis order
  181. var reverseAxisOrder = this.reverseAxisOrder();
  182. newParams.BBOX = this.encodeBBOX ?
  183. bounds.toBBOX(null, reverseAxisOrder) :
  184. bounds.toArray(reverseAxisOrder);
  185. newParams.WIDTH = imageSize.w;
  186. newParams.HEIGHT = imageSize.h;
  187. var requestString = this.getFullRequestString(newParams);
  188. return requestString;
  189. },
  190. /**
  191. * APIMethod: mergeNewParams
  192. * Catch changeParams and uppercase the new params to be merged in
  193. * before calling changeParams on the super class.
  194. *
  195. * Once params have been changed, the tiles will be reloaded with
  196. * the new parameters.
  197. *
  198. * Parameters:
  199. * newParams - {Object} Hashtable of new params to use
  200. */
  201. mergeNewParams:function(newParams) {
  202. var upperParams = OpenLayers.Util.upperCaseObject(newParams);
  203. var newArguments = [upperParams];
  204. return OpenLayers.Layer.Grid.prototype.mergeNewParams.apply(this,
  205. newArguments);
  206. },
  207. /**
  208. * APIMethod: getFullRequestString
  209. * Combine the layer's url with its params and these newParams.
  210. *
  211. * Add the SRS parameter from projection -- this is probably
  212. * more eloquently done via a setProjection() method, but this
  213. * works for now and always.
  214. *
  215. * Parameters:
  216. * newParams - {Object}
  217. * altUrl - {String} Use this as the url instead of the layer's url
  218. *
  219. * Returns:
  220. * {String}
  221. */
  222. getFullRequestString:function(newParams, altUrl) {
  223. var mapProjection = this.map.getProjectionObject();
  224. var projectionCode = this.projection && this.projection.equals(mapProjection) ?
  225. this.projection.getCode() :
  226. mapProjection.getCode();
  227. var value = (projectionCode == "none") ? null : projectionCode;
  228. if (parseFloat(this.params.VERSION) >= 1.3) {
  229. this.params.CRS = value;
  230. } else {
  231. this.params.SRS = value;
  232. }
  233. if (typeof this.params.TRANSPARENT == "boolean") {
  234. newParams.TRANSPARENT = this.params.TRANSPARENT ? "TRUE" : "FALSE";
  235. }
  236. return OpenLayers.Layer.Grid.prototype.getFullRequestString.apply(
  237. this, arguments);
  238. },
  239. CLASS_NAME: "OpenLayers.Layer.WMS"
  240. });