final fix for #410

git-svn-id: http://svn.openlayers.org/trunk/openlayers@2852 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
euzuro
2007-03-22 17:47:40 +00:00
parent 1d3e144dd2
commit 78c16b973e
3 changed files with 50 additions and 21 deletions

View File

@@ -12,10 +12,21 @@ OpenLayers.Layer.HTTPRequest = OpenLayers.Class.create();
OpenLayers.Layer.HTTPRequest.prototype = OpenLayers.Layer.HTTPRequest.prototype =
OpenLayers.Class.inherit( OpenLayers.Layer, { OpenLayers.Class.inherit( OpenLayers.Layer, {
/** @type String */ /** Used to hash URL param strings for multi-WMS server selection.
* Set to the Golden Ratio per Knuth's recommendation.
*
* @type Numeric
* @private
*/
URL_HASH_FACTOR: (Math.sqrt(5) - 1) / 2,
/** This is either an array of url strings or a single url string.
*
* @type Array(String) or String */
url: null, url: null,
/** Hashtable of key/value parameters /** Hashtable of key/value parameters
*
* @type Object */ * @type Object */
params: null, params: null,
@@ -32,7 +43,7 @@ OpenLayers.Layer.HTTPRequest.prototype =
* @constructor * @constructor
* *
* @param {String} name * @param {String} name
* @param {String} url * @param {Array(String) or String} url
* @param {Object} params * @param {Object} params
* @param {Object} options Hashtable of extra options to tag onto the layer * @param {Object} options Hashtable of extra options to tag onto the layer
*/ */
@@ -91,13 +102,24 @@ OpenLayers.Layer.HTTPRequest.prototype =
}, },
/** /**
* selectUrl() implements the standard floating-point multiplicative
* hash function described by Knuth, and hashes the contents of the
* given param string into a float between 0 and 1. This float is then
* scaled to the size of the provided urls array, and used to select
* a URL.
*
* @param {String} paramString * @param {String} paramString
* @param {Array(String)} urls * @param {Array(String)} urls
*
* @returns An entry from the urls array, deterministically selected based
* on the paramString.
* @type String
*
*/ */
selectUrl: function(paramString, urls) { selectUrl: function(paramString, urls) {
var product = 1; var product = 1;
for (var i = 0; i < paramString.length; i++) { for (var i = 0; i < paramString.length; i++) {
product *= paramString.charCodeAt(i) * (Math.sqrt(5) - 1) / Math.sqrt(3); product *= paramString.charCodeAt(i) * this.URL_HASH_FACTOR;
product -= Math.floor(product); product -= Math.floor(product);
} }
return urls[Math.floor(product * urls.length)]; return urls[Math.floor(product * urls.length)];
@@ -111,20 +133,33 @@ OpenLayers.Layer.HTTPRequest.prototype =
* return in formatted string like this: * return in formatted string like this:
* "server?key1=value1&key2=value2&key3=value3" * "server?key1=value1&key2=value2&key3=value3"
* *
* WARNING: The altUrl parameter is deprecated and will be removed in 3.0.
*
* @param {Object} newParams * @param {Object} newParams
* @param {String} altUrl Use this as the url instead of the layer's url * @param {String} altUrl Use this as the url instead of the layer's url
*
* *
* @type String * @type String
*/ */
getFullRequestString:function(newParams, altUrl) { getFullRequestString:function(newParams, altUrl) {
// use layer's url unless altUrl passed in // if not altUrl passed in, use layer's url
var url = (altUrl == null) ? this.url : altUrl; var url = altUrl || this.url;
// create a new params hashtable with all the layer params and the // create a new params hashtable with all the layer params and the
// new params together. then convert to string // new params together. then convert to string
var allParams = OpenLayers.Util.extend(new Object(), this.params); var allParams = OpenLayers.Util.extend(new Object(), this.params);
allParams = OpenLayers.Util.extend(allParams, newParams); allParams = OpenLayers.Util.extend(allParams, newParams);
var paramsString = OpenLayers.Util.getParameterString(allParams);
// if url is not a string, it should be an array of strings,
// in which case we will deterministically select one of them in
// order to evenly distribute requests to different urls.
//
if (url instanceof Array) {
url = this.selectUrl(paramsString, url);
}
// ignore parameters that are already in the url search string // ignore parameters that are already in the url search string
var urlParams = var urlParams =
OpenLayers.Util.upperCaseObject(OpenLayers.Util.getArgs(url)); OpenLayers.Util.upperCaseObject(OpenLayers.Util.getArgs(url));
@@ -133,14 +168,7 @@ OpenLayers.Layer.HTTPRequest.prototype =
delete allParams[key]; delete allParams[key];
} }
} }
var paramsString = OpenLayers.Util.getParameterString(allParams); paramsString = OpenLayers.Util.getParameterString(allParams);
// if url is not a string, it should be an array of strings,
// in which case we will deterministically select one of them in
// order to evenly distribute requests to different urls.
if (url instanceof Array) {
url = this.selectUrl(paramsString, url);
}
// requestString always starts with url // requestString always starts with url
var requestString = url; var requestString = url;

View File

@@ -158,9 +158,9 @@
tUrl = ["http://octo.metacarta.com/cgi-bin/mapserv","http://labs.metacarta.com/cgi-bin/mapserv"]; tUrl = ["http://octo.metacarta.com/cgi-bin/mapserv","http://labs.metacarta.com/cgi-bin/mapserv"];
layer = new OpenLayers.Layer.HTTPRequest(name, tUrl, tParams, null); layer = new OpenLayers.Layer.HTTPRequest(name, tUrl, tParams, null);
str = layer.getFullRequestString(); str = layer.getFullRequestString();
t.eq(str, tUrl[0] + '?' + OpenLayers.Util.getParameterString(tParams), "getFullRequestString() works for list of two urls"); t.eq(str, tUrl[1] + '?' + OpenLayers.Util.getParameterString(tParams), "getFullRequestString() works for list of two urls");
str = layer.getFullRequestString({'a':'f'}); str = layer.getFullRequestString({'a':'b'});
t.eq(str, tUrl[1] + '?' + OpenLayers.Util.getParameterString(OpenLayers.Util.extend(tParams,{'a':'f'})), "getFullRequestString() works for list of two urls and is deterministic"); t.eq(str, tUrl[0] + '?' + OpenLayers.Util.getParameterString(OpenLayers.Util.extend(tParams,{'a':'b'})), "getFullRequestString() works for list of two urls and is deterministic");
} }
@@ -170,10 +170,10 @@
layer = new OpenLayers.Layer.HTTPRequest(name, url, params, options); layer = new OpenLayers.Layer.HTTPRequest(name, url, params, options);
urls = ["wms1", "wms2", "wms3", "wms4"]; urls = ["wms1", "wms2", "wms3", "wms4"];
t.eq( layer.selectUrl("bbox=-180,0,0,90", urls), "wms4", "selectUrl(-90,-180) returns 4" ); t.eq( layer.selectUrl("bbox=-180,0,0,90", urls), "wms3", "selectUrl(-90,-180) returns 4" );
t.eq( layer.selectUrl("bbox=-180,-90,0,0", urls), "wms3", "selectUrl(90,-180) returns 3" ); t.eq( layer.selectUrl("bbox=-180,-90,0,0", urls), "wms1", "selectUrl(90,-180) returns 3" );
t.eq( layer.selectUrl("bbox=0,90,180,0", urls), "wms1", "selectUrl(-90,180) returns 1" ); t.eq( layer.selectUrl("bbox=0,90,180,0", urls), "wms1", "selectUrl(-90,180) returns 1" );
t.eq( layer.selectUrl("bbox=0,0,180,90", urls), "wms2", "selectUrl(90,180) returns 2" ); t.eq( layer.selectUrl("bbox=0,0,180,90", urls), "wms4", "selectUrl(90,180) returns 2" );
} }
function test_99_Layer_HTTPRequest_destroy (t) { function test_99_Layer_HTTPRequest_destroy (t) {

View File

@@ -213,7 +213,8 @@
wmslayer.isBaseLayer=false; wmslayer.isBaseLayer=false;
map.addLayer(wmslayer); map.addLayer(wmslayer);
map.setCenter(new OpenLayers.LonLat(0,0), 5); map.setCenter(new OpenLayers.LonLat(0,0), 5);
var tile = layer.grid[0][0];
var tile = wmslayer.grid[0][0];
t.eq( tile.bounds.left, -22.5, "left side matches" ); t.eq( tile.bounds.left, -22.5, "left side matches" );
t.eq( tile.bounds.right, -11.25, "top side matches" ); t.eq( tile.bounds.right, -11.25, "top side matches" );
t.eq( tile.bounds.bottom.toFixed(6), '11.178402', "bottom side matches" ); t.eq( tile.bounds.bottom.toFixed(6), '11.178402', "bottom side matches" );