From 8dfb4c3ce9caed5017534b7515a8ccfb6b469cfe Mon Sep 17 00:00:00 2001 From: crschmidt Date: Wed, 4 Oct 2006 14:05:34 +0000 Subject: [PATCH] Add rounding support to the default toBBOX() call. Now, toBBOX() will by default return values which are rounded to 6 decimal places. If you wish more or less accuracy, you can change it by passing a numeric argument with the accuracy you want to toBBOX(), and that accuracy will be used instead. Updated tests to test rounding to various levels. This functionality should help prevent different browsers from hitting different caches. At the equator, this rounding difference is only 4", and smaller as you head towards the poles. git-svn-id: http://svn.openlayers.org/trunk/openlayers@1558 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf --- lib/OpenLayers/BaseTypes.js | 14 +++++++++++--- tests/test_Bounds.html | 11 ++++++++++- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/lib/OpenLayers/BaseTypes.js b/lib/OpenLayers/BaseTypes.js index 7ac2c5c066..d42ae9e962 100644 --- a/lib/OpenLayers/BaseTypes.js +++ b/lib/OpenLayers/BaseTypes.js @@ -364,9 +364,17 @@ OpenLayers.Bounds.prototype = { * (ex. "5,42,10,45") * @type String */ - toBBOX:function() { - return (this.left + "," + this.bottom + "," - + this.right + "," + this.top); + toBBOX:function(power) { + var mult; + if (power) { + mult = Math.pow(10,power); + } else { + mult = Math.pow(10,6); + } + return (Math.round(this.left*mult)/mult + "," + + Math.round(this.bottom*mult)/mult + "," + + Math.round(this.right*mult)/mult + "," + + Math.round(this.top*mult)/mult); }, /** diff --git a/tests/test_Bounds.html b/tests/test_Bounds.html index cc5d84af4b..947febdea7 100644 --- a/tests/test_Bounds.html +++ b/tests/test_Bounds.html @@ -41,9 +41,18 @@ } function test_02_Bounds_toBBOX(t) { - t.plan( 1 ); + t.plan( 5 ); bounds = new OpenLayers.Bounds(1,2,3,4); t.eq( bounds.toBBOX(), "1,2,3,4", "toBBOX() returns correct value." ); + bounds = new OpenLayers.Bounds(1.00000001,2,3,4); + t.eq( bounds.toBBOX(), "1,2,3,4", "toBBOX() rounds off small differences." ); + bounds = new OpenLayers.Bounds(1.00000001,2.5,3,4); + t.eq( bounds.toBBOX(), "1,2.5,3,4", "toBBOX() returns correct value. for a half number" ); + bounds = new OpenLayers.Bounds(1,2.5555555,3,4); + t.eq( bounds.toBBOX(), "1,2.555556,3,4", "toBBOX() rounds to correct value." ); + bounds = new OpenLayers.Bounds(1,2.5555555,3,4); + t.eq( bounds.toBBOX(1), "1,2.6,3,4", "toBBOX() rounds to correct value with power provided." ); + bounds = new OpenLayers.Bounds(1,2.5555555,3,4); } function test_03_Bounds_toString(t) {