Skip to content

Instantly share code, notes, and snippets.

@tmcw
Created August 10, 2010 17:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tmcw/517644 to your computer and use it in GitHub Desktop.
Save tmcw/517644 to your computer and use it in GitHub Desktop.
/**
* Method: initResolutions
* This method's responsibility is to set up the 'resolutions' array
* for the layer -- this array is what the layer will use to interface
* between the zoom levels of the map and the resolution display
* of the layer.
*
* The user has several options that determine how the array is set up.
*
* For a detailed explanation, see the following wiki from the
* openlayers.org homepage:
* http://trac.openlayers.org/wiki/SettingZoomLevels
*/
initResolutions: function() {
// ok we want resolutions, here's our strategy:
//
// 1. if resolutions are defined in the layer config, use them
// 2. else, if scales are defined in the layer config then derive
// resolutions from these scales
// 3. else, attempt to calculate resolutions from maxResolution,
// minResolution, numZoomLevels, maxZoomLevel set in the
// layer config
// 4. if we still don't have resolutions, and if resolutions
// are defined in the same, use them
// 5. else, if scales are defined in the map then derive
// resolutions from these scales
// 6. else, attempt to calculate resolutions from maxResolution,
// minResolution, numZoomLevels, maxZoomLevel set in the
// map
// 7. hope for the best!
var i, len;
var props = {}, alwaysInRange = true;
// get resolution data from layer config
// (we also set alwaysInRange in the layer as appropriate)
for(i=0, len=this.RESOLUTION_PROPERTIES.length; i<len; i++) {
var p = this.RESOLUTION_PROPERTIES[i]
props[p] = this.options[p];
if(alwaysInRange && this.options[p]) {
alwaysInRange = false;
}
}
if(this.alwaysInRange == null) {
this.alwaysInRange = alwaysInRange;
}
// if we don't have resolutions then attempt to derive them from scales
if(props.resolutions == null) {
props.resolutions = this.resolutionsFromScales(props.scales);
}
// if we still don't have resolutions then attempt to calculate them
if(props.resolutions == null) {
props.resolutions = this.calculateResolutions(props);
}
// if we couldn't calculate resolutions then we look at we have
// in the map
if(props.resolutions == null) {
for(i=0, len=this.RESOLUTION_PROPERTIES.length; i<len; i++) {
var p = this.RESOLUTION_PROPERTIES[i]
props[p] = this.options[p] != null ?
this.options[p] : this.map[p];
}
if(props.resolutions == null) {
props.resolutions = this.resolutionsFromScales(props.scales);
}
if(props.resolutions == null) {
props.resolutions = this.calculateResolutions(props);
}
}
// ok, we new need to set properties in the instance
// get maxResolution from the config if it's defined there
var maxResolution;
if(this.options.maxResolution &&
this.options.maxResolution !== "auto") {
maxResolution = this.options.maxResolution;
}
if(this.options.minScale) {
maxResolution = OpenLayers.Util.getResolutionFromScale(
this.options.minScale, this.units);
}
// get minResolution from the config if it's defined there
var minResolution;
if(this.options.minResolution &&
this.options.minResolution !== "auto") {
minResolution = this.options.minResolution;
}
if(this.options.maxScale) {
minResolution = OpenLayers.Util.getResolutionFromScale(
this.options.maxScale, this.units);
}
if(props.resolutions) {
//sort resolutions array descendingly
props.resolutions.sort(function(a, b) {
return (b - a);
});
// if we still don't have a maxResolution get it from the
// resolutions array
if(!maxResolution) {
maxResolution = props.resolutions[0];
}
// if we still don't have a minResolution get it from the
// resolutions array
if(!minResolution) {
var lastIdx = props.resolutions.length - 1;
minResolution = props.resolutions[lastIdx];
}
}
this.resolutions = props.resolutions;
if(this.resolutions) {
len = this.resolutions.length;
this.scales = new Array(len);
for(i=0; i<len; i++) {
this.scales[i] = OpenLayers.Util.getScaleFromResolution(
this.resolutions[i], this.units);
}
this.numZoomLevels = len;
}
this.minResolution = minResolution;
if(minResolution) {
this.maxScale = OpenLayers.Util.getScaleFromResolution(
minResolution, this.units);
}
this.maxResolution = maxResolution;
if(maxResolution) {
this.minScale = OpenLayers.Util.getScaleFromResolution(
maxResolution, this.units);
}
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment