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