From a758efb2f205ace39c4031eaf5b13ddff1ba5af0 Mon Sep 17 00:00:00 2001 From: euzuro Date: Tue, 3 Oct 2006 03:21:45 +0000 Subject: [PATCH] added getScaleFromResolution() function to util, also added tests for both directions of the scale<->resolution tranformation functions, as well as the normalizeScale() function git-svn-id: http://svn.openlayers.org/trunk/openlayers@1534 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf --- lib/OpenLayers/Util.js | 20 ++++++++++++++++++++ tests/test_Util.html | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/lib/OpenLayers/Util.js b/lib/OpenLayers/Util.js index 741ed06860..7c7a1b553a 100644 --- a/lib/OpenLayers/Util.js +++ b/lib/OpenLayers/Util.js @@ -518,6 +518,26 @@ OpenLayers.Util.getResolutionFromScale = function (scale, units) { return resolution; }; +/** + * @param {float} resolution + * @param {String} units Index into OpenLayers.INCHES_PER_UNIT hashtable. + * Default is degrees + * + * @returns The corresponding scale given passed-in resolution and unit + * parameters. + * @type float + */ +OpenLayers.Util.getScaleFromResolution = function (resolution, units) { + + if (units == null) { + units = "degrees"; + } + + var scale = 1 / (resolution * OpenLayers.INCHES_PER_UNIT[units] + * OpenLayers.DOTS_PER_INCH); + return scale; +}; + /** Safely stop the propagation of an event *without* preventing * the default browser action from occurring. * diff --git a/tests/test_Util.html b/tests/test_Util.html index 8f0dbcca1f..5e98da912c 100644 --- a/tests/test_Util.html +++ b/tests/test_Util.html @@ -418,6 +418,40 @@ } + + function test_13_Util_normalizeScale(t) { + t.plan(2); + + //normal scale + var scale = 1/5; + t.eq( OpenLayers.Util.normalizeScale(scale), scale, "normalizing a normal scale does nothing"); + + //funky scale + var scale = 5; + t.eq( OpenLayers.Util.normalizeScale(scale), 1/5, "normalizing a wrong scale works!"); + + + } + + function test_13_Util_getScaleResolutionTranslation(t) { + t.plan(4); + + var scale = 1/150000000; + var resolution = OpenLayers.Util.getResolutionFromScale(scale); + t.eq(resolution.toFixed(6), "0.476217", "Calculated correct resolution for " + scale); + + var scale = 1/150000000; + var resolution = OpenLayers.Util.getResolutionFromScale(scale, 'm'); + t.eq(resolution.toFixed(6), "52916.638092", "Calculated correct resolution for " + scale); + + scale = 150000000; + resolution = OpenLayers.Util.getResolutionFromScale(scale); + t.eq(resolution.toFixed(6), "0.476217", "Calculated correct resolution for " + scale); + + scale = 1/150000000; + resolution = OpenLayers.Util.getResolutionFromScale(scale); + t.eq(OpenLayers.Util.getScaleFromResolution(resolution), scale, "scale->resolution->scale works"); + } // -->