OpenLayers OpenLayers

Changeset 2544

Show
Ignore:
Timestamp:
03/08/07 15:19:41 (2 years ago)
Author:
sderle
Message:

Fix MapServer request string handling by overriding getFullRequestUrl(). Fixes #508.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/openlayers/lib/OpenLayers/Layer/MapServer.js

    r2090 r2544  
    8686     */ 
    8787    getURL: function (bounds) { 
     88         
     89        // Make a list, so that getFullRequestString uses literal ","  
     90        var extent = [bounds.left, bounds. bottom, bounds.right, bounds.top]; 
     91         
     92        // make lists, so that literal ','s are used  
     93        var url = this.getFullRequestString( 
     94                     {mapext:   extent, 
     95                      imgext:   extent, 
     96                      map_size: [this.tileSize.w,this.tileSize.h], 
     97                      imgx:     this.tileSize.w/2, 
     98                      imgy:     this.tileSize.h/2, 
     99                      imgxy:    [this.tileSize.w,this.tileSize.h] 
     100                      }); 
     101         
     102        return url; 
     103    }, 
     104     
     105    /**  
     106     * getFullRequestString on MapServer layers is special, because we  
     107     * do a regular expression replace on ',' in parameters to '+'. 
     108     * This is why it is subclassed here. 
     109     * 
     110     * @param {Object} newParams Parameters to add to the default parameters 
     111     *                           for the layer. 
     112     * @param {String} altUrl    Alternative base URL to use. 
     113     */ 
     114    getFullRequestString:function(newParams, altUrl) { 
     115         
     116     
     117        // use layer's url unless altUrl passed in 
     118        var url = (altUrl == null) ? this.url : altUrl; 
     119         
     120        // if url is not a string, it should be an array of strings,  
     121        //  in which case we will randomly select one of them in order 
     122        //  to evenly distribute requests to different urls. 
     123        if (typeof url == "object") { 
     124            url = url[Math.floor(Math.random()*url.length)]; 
     125        }    
     126        // requestString always starts with url 
     127        var requestString = url;         
    88128 
    89         var url = this.getFullRequestString( 
    90                      {mapext:bounds.toBBOX().replace(/,/g,"+"), 
    91                       imgext:bounds.toBBOX().replace(/,/g,"+"), 
    92                       map_size:this.tileSize.w+'+'+this.tileSize.h, 
    93                       imgx: this.tileSize.w/2, 
    94                       imgy: this.tileSize.h/2, 
    95                       imgxy: this.tileSize.w+"+"+this.tileSize.h 
    96                       }); 
    97         return url; 
     129        // create a new params hashtable with all the layer params and the  
     130        // new params together. then convert to string 
     131        var allParams = OpenLayers.Util.extend(new Object(), this.params); 
     132        allParams = OpenLayers.Util.extend(allParams, newParams); 
     133        // ignore parameters that are already in the url search string 
     134        var urlParams = OpenLayers.Util.upperCaseObject( 
     135                            OpenLayers.Util.getArgs(url)); 
     136        for(var key in allParams) { 
     137            if(key.toUpperCase() in urlParams) { 
     138                delete allParams[key]; 
     139            } 
     140        } 
     141        var paramsString = OpenLayers.Util.getParameterString(allParams); 
     142         
     143        /* MapServer needs '+' seperating things like bounds/height/width. 
     144           Since typically this is URL encoded, we use a slight hack: we 
     145           depend on the list-like functionality of getParameterString to 
     146           leave ',' only in the case of list items (since otherwise it is 
     147           encoded) then do a regular expression replace on the , characters 
     148           to '+' */ 
     149        paramsString = paramsString.replace(/,/g, "+"); 
     150         
     151        if (paramsString != "") { 
     152            var lastServerChar = url.charAt(url.length - 1); 
     153            if ((lastServerChar == "&") || (lastServerChar == "?")) { 
     154                requestString += paramsString; 
     155            } else { 
     156                if (url.indexOf('?') == -1) { 
     157                    //serverPath has no ? -- add one 
     158                    requestString += '?' + paramsString; 
     159                } else { 
     160                    //serverPath contains ?, so must already have paramsString at the end 
     161                    requestString += '&' + paramsString; 
     162                } 
     163            } 
     164        } 
     165        return requestString; 
    98166    }, 
    99167    /** @final @type String */