1 /* Copyright (c) 2006-2008 MetaCarta, Inc., published under the Clear BSD
2 * license. See http://svn.openlayers.org/trunk/openlayers/license.txt for the
3 * full text of the license. */
6 * @requires OpenLayers/Layer/Grid.js
10 * Class: OpenLayers.Layer.MapServer
11 * Instances of OpenLayers.Layer.MapServer are used to display
12 * data from a MapServer CGI instance.
15 * - <OpenLayers.Layer.Grid>
17 OpenLayers.Layer.MapServer = OpenLayers.Class(OpenLayers.Layer.Grid, {
20 * Constant: DEFAULT_PARAMS
21 * {Object} Hashtable of default parameter key/value pairs
29 * Constructor: OpenLayers.Layer.MapServer
30 * Create a new MapServer layer object
33 * name - {String} A name for the layer
34 * url - {String} Base url for the MapServer CGI
35 * (e.g. http://www2.dmsolutions.ca/cgi-bin/mapserv)
36 * params - {Object} An object with key/value pairs representing the
37 * GetMap query string parameters and parameter values.
38 * options - {Ojbect} Hashtable of extra options to tag onto the layer
40 initialize: function(name, url, params, options) {
41 var newArguments = [];
42 newArguments.push(name, url, params, options);
43 OpenLayers.Layer.Grid.prototype.initialize.apply(this, newArguments);
45 this.params = OpenLayers.Util.applyDefaults(
46 this.params, this.DEFAULT_PARAMS
49 // unless explicitly set in options, if the layer is transparent,
50 // it will be an overlay
51 if (options == null || options.isBaseLayer == null) {
52 this.isBaseLayer = ((this.params.transparent != "true") &&
53 (this.params.transparent != true));
59 * Create a clone of this layer
62 * {<OpenLayers.Layer.MapServer>} An exact clone of this layer
64 clone: function (obj) {
66 obj = new OpenLayers.Layer.MapServer(this.name,
71 //get all additions from superclasses
72 obj = OpenLayers.Layer.Grid.prototype.clone.apply(this, [obj]);
74 // copy/set any non-init, non-simple values here
81 * Creates a tile, initializes it, and adds it to the layer div.
84 * bounds - {<OpenLayers.Bounds>}
85 * position - {<OpenLayers.Pixel>}
88 * {<OpenLayers.Tile.Image>} The added OpenLayers.Tile.Image
90 addTile:function(bounds,position) {
91 return new OpenLayers.Tile.Image(this, position, bounds,
97 * Return a query string for this layer
100 * bounds - {<OpenLayers.Bounds>} A bounds representing the bbox
104 * {String} A string with the layer's url and parameters and also
105 * the passed-in bounds and appropriate tile size specified
108 getURL: function (bounds) {
109 bounds = this.adjustBounds(bounds);
110 // Make a list, so that getFullRequestString uses literal ","
111 var extent = [bounds.left, bounds. bottom, bounds.right, bounds.top];
113 var imageSize = this.getImageSize();
115 // make lists, so that literal ','s are used
116 var url = this.getFullRequestString(
119 map_size: [imageSize.w, imageSize.h],
120 imgx: imageSize.w / 2,
121 imgy: imageSize.h / 2,
122 imgxy: [imageSize.w, imageSize.h]
129 * Method: getFullRequestString
130 * combine the layer's url with its params and these newParams.
133 * newParams - {Object} New parameters that should be added to the
135 * altUrl - {String} (optional) Replace the URL in the full request
136 * string with the provided URL.
139 * {String} A string with the layer's url and parameters embedded in it.
141 getFullRequestString:function(newParams, altUrl) {
142 // use layer's url unless altUrl passed in
143 var url = (altUrl == null) ? this.url : altUrl;
145 // create a new params hashtable with all the layer params and the
146 // new params together. then convert to string
147 var allParams = OpenLayers.Util.extend({}, this.params);
148 allParams = OpenLayers.Util.extend(allParams, newParams);
149 var paramsString = OpenLayers.Util.getParameterString(allParams);
151 // if url is not a string, it should be an array of strings,
152 // in which case we will deterministically select one of them in
153 // order to evenly distribute requests to different urls.
154 if (url instanceof Array) {
155 url = this.selectUrl(paramsString, url);
158 // ignore parameters that are already in the url search string
159 var urlParams = OpenLayers.Util.upperCaseObject(
160 OpenLayers.Util.getParameters(url));
161 for(var key in allParams) {
162 if(key.toUpperCase() in urlParams) {
163 delete allParams[key];
166 paramsString = OpenLayers.Util.getParameterString(allParams);
168 // requestString always starts with url
169 var requestString = url;
171 // MapServer needs '+' seperating things like bounds/height/width.
172 // Since typically this is URL encoded, we use a slight hack: we
173 // depend on the list-like functionality of getParameterString to
174 // leave ',' only in the case of list items (since otherwise it is
175 // encoded) then do a regular expression replace on the , characters
178 paramsString = paramsString.replace(/,/g, "+");
180 if (paramsString != "") {
181 var lastServerChar = url.charAt(url.length - 1);
182 if ((lastServerChar == "&") || (lastServerChar == "?")) {
183 requestString += paramsString;
185 if (url.indexOf('?') == -1) {
186 //serverPath has no ? -- add one
187 requestString += '?' + paramsString;
189 //serverPath contains ?, so must already have paramsString at the end
190 requestString += '&' + paramsString;
194 return requestString;
197 CLASS_NAME: "OpenLayers.Layer.MapServer"