WMS.js 10 KB

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