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
This commit is contained in:
euzuro
2006-10-03 03:21:45 +00:00
parent 642c438c2c
commit a758efb2f2
2 changed files with 54 additions and 0 deletions

View File

@@ -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.
*

View File

@@ -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");
}
// -->
</script>