MapServer.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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.MapServer
  10. * Instances of OpenLayers.Layer.MapServer are used to display
  11. * data from a MapServer CGI instance.
  12. *
  13. * Inherits from:
  14. * - <OpenLayers.Layer.Grid>
  15. */
  16. OpenLayers.Layer.MapServer = OpenLayers.Class(OpenLayers.Layer.Grid, {
  17. /**
  18. * Constant: DEFAULT_PARAMS
  19. * {Object} Hashtable of default parameter key/value pairs
  20. */
  21. DEFAULT_PARAMS: {
  22. mode: "map",
  23. map_imagetype: "png"
  24. },
  25. /**
  26. * Constructor: OpenLayers.Layer.MapServer
  27. * Create a new MapServer layer object
  28. *
  29. * Parameters:
  30. * name - {String} A name for the layer
  31. * url - {String} Base url for the MapServer CGI
  32. * (e.g. http://www2.dmsolutions.ca/cgi-bin/mapserv)
  33. * params - {Object} An object with key/value pairs representing the
  34. * GetMap query string parameters and parameter values.
  35. * options - {Object} Hashtable of extra options to tag onto the layer
  36. */
  37. initialize: function(name, url, params, options) {
  38. OpenLayers.Layer.Grid.prototype.initialize.apply(this, arguments);
  39. this.params = OpenLayers.Util.applyDefaults(
  40. this.params, this.DEFAULT_PARAMS
  41. );
  42. // unless explicitly set in options, if the layer is transparent,
  43. // it will be an overlay
  44. if (options == null || options.isBaseLayer == null) {
  45. this.isBaseLayer = ((this.params.transparent != "true") &&
  46. (this.params.transparent != true));
  47. }
  48. },
  49. /**
  50. * Method: clone
  51. * Create a clone of this layer
  52. *
  53. * Returns:
  54. * {<OpenLayers.Layer.MapServer>} An exact clone of this layer
  55. */
  56. clone: function (obj) {
  57. if (obj == null) {
  58. obj = new OpenLayers.Layer.MapServer(this.name,
  59. this.url,
  60. this.params,
  61. this.getOptions());
  62. }
  63. //get all additions from superclasses
  64. obj = OpenLayers.Layer.Grid.prototype.clone.apply(this, [obj]);
  65. // copy/set any non-init, non-simple values here
  66. return obj;
  67. },
  68. /**
  69. * Method: getURL
  70. * Return a query string for this layer
  71. *
  72. * Parameters:
  73. * bounds - {<OpenLayers.Bounds>} A bounds representing the bbox
  74. * for the request
  75. *
  76. * Returns:
  77. * {String} A string with the layer's url and parameters and also
  78. * the passed-in bounds and appropriate tile size specified
  79. * as parameters.
  80. */
  81. getURL: function (bounds) {
  82. bounds = this.adjustBounds(bounds);
  83. // Make a list, so that getFullRequestString uses literal ","
  84. var extent = [bounds.left, bounds. bottom, bounds.right, bounds.top];
  85. var imageSize = this.getImageSize();
  86. // make lists, so that literal ','s are used
  87. var url = this.getFullRequestString(
  88. {mapext: extent,
  89. imgext: extent,
  90. map_size: [imageSize.w, imageSize.h],
  91. imgx: imageSize.w / 2,
  92. imgy: imageSize.h / 2,
  93. imgxy: [imageSize.w, imageSize.h]
  94. });
  95. return url;
  96. },
  97. /**
  98. * Method: getFullRequestString
  99. * combine the layer's url with its params and these newParams.
  100. *
  101. * Parameters:
  102. * newParams - {Object} New parameters that should be added to the
  103. * request string.
  104. * altUrl - {String} (optional) Replace the URL in the full request
  105. * string with the provided URL.
  106. *
  107. * Returns:
  108. * {String} A string with the layer's url and parameters embedded in it.
  109. */
  110. getFullRequestString:function(newParams, altUrl) {
  111. // use layer's url unless altUrl passed in
  112. var url = (altUrl == null) ? this.url : altUrl;
  113. // create a new params hashtable with all the layer params and the
  114. // new params together. then convert to string
  115. var allParams = OpenLayers.Util.extend({}, this.params);
  116. allParams = OpenLayers.Util.extend(allParams, newParams);
  117. var paramsString = OpenLayers.Util.getParameterString(allParams);
  118. // if url is not a string, it should be an array of strings,
  119. // in which case we will deterministically select one of them in
  120. // order to evenly distribute requests to different urls.
  121. if (OpenLayers.Util.isArray(url)) {
  122. url = this.selectUrl(paramsString, url);
  123. }
  124. // ignore parameters that are already in the url search string
  125. var urlParams = OpenLayers.Util.upperCaseObject(
  126. OpenLayers.Util.getParameters(url));
  127. for(var key in allParams) {
  128. if(key.toUpperCase() in urlParams) {
  129. delete allParams[key];
  130. }
  131. }
  132. paramsString = OpenLayers.Util.getParameterString(allParams);
  133. // requestString always starts with url
  134. var requestString = url;
  135. // MapServer needs '+' seperating things like bounds/height/width.
  136. // Since typically this is URL encoded, we use a slight hack: we
  137. // depend on the list-like functionality of getParameterString to
  138. // leave ',' only in the case of list items (since otherwise it is
  139. // encoded) then do a regular expression replace on the , characters
  140. // to '+'
  141. //
  142. paramsString = paramsString.replace(/,/g, "+");
  143. if (paramsString != "") {
  144. var lastServerChar = url.charAt(url.length - 1);
  145. if ((lastServerChar == "&") || (lastServerChar == "?")) {
  146. requestString += paramsString;
  147. } else {
  148. if (url.indexOf('?') == -1) {
  149. //serverPath has no ? -- add one
  150. requestString += '?' + paramsString;
  151. } else {
  152. //serverPath contains ?, so must already have paramsString at the end
  153. requestString += '&' + paramsString;
  154. }
  155. }
  156. }
  157. return requestString;
  158. },
  159. CLASS_NAME: "OpenLayers.Layer.MapServer"
  160. });