MapServer.js 6.6 KB

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