Skip to content

Commit

Permalink
removing the restrictedMinZoom property, and allow for restricting zo…
Browse files Browse the repository at this point in the history
…om levels with maxResolution and numZoomLevels. Thanks tschaub for the doc, test and examples improvements. r=tschaub (see #3338)

git-svn-id: http://svn.openlayers.org/trunk/openlayers@12106 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
  • Loading branch information
ahocevar committed Jun 18, 2011
1 parent 6248cdc commit a9d3b8b
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 105 deletions.
2 changes: 1 addition & 1 deletion examples/bing-tiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ map.addControl(new OpenLayers.Control.LayerSwitcher());
map.setCenter(new OpenLayers.LonLat(-71.147, 42.472).transform(
new OpenLayers.Projection("EPSG:4326"),
map.getProjectionObject()
), 12);
), 11);
39 changes: 23 additions & 16 deletions lib/OpenLayers/Layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -275,18 +275,6 @@ OpenLayers.Layer = OpenLayers.Class({
*/
numZoomLevels: null,

/**
* Property: restrictedMinZoom
* {Integer} Restriction of the minimum zoom level. This is used for layers
* that only use a subset of the resolutions in the <resolutions>
* array. This is independent of <numResolutions>, which always starts
* counting at zoom level 0. If restrictedMinZoom is e.g. set to 2,
* the first two zoom levels (0 and 1) will not be used by this layer.
* If the layer is a base layer, zooming to the map's maxExtent means
* setting the map's zoom to 2.
*/
restrictedMinZoom: 0,

/**
* APIProperty: minScale
* {Float}
Expand Down Expand Up @@ -474,8 +462,12 @@ OpenLayers.Layer = OpenLayers.Class({
*
* Parameters:
* newOptions - {Object}
* reinitialize - {Boolean} If set to true, and if resolution options of the
* current baseLayer were changed, the map will be recentered to make
* sure that it is displayed with a valid resolution, and a
* changebaselayer event will be triggered.
*/
addOptions: function (newOptions) {
addOptions: function (newOptions, reinitialize) {

if (this.options == null) {
this.options = {};
Expand All @@ -502,6 +494,8 @@ OpenLayers.Layer = OpenLayers.Class({
// properties of the "properties" array defined below is set
// in the new options
if(this.map) {
// store current resolution so we can try to restore it later
var resolution = this.map.getResolution();
var properties = this.RESOLUTION_PROPERTIES.concat(
["projection", "units", "minExtent", "maxExtent"]
);
Expand All @@ -510,6 +504,20 @@ OpenLayers.Layer = OpenLayers.Class({
OpenLayers.Util.indexOf(properties, o) >= 0) {

this.initResolutions();
if (reinitialize && this.map.baseLayer === this) {
// update map position, and restore previous resolution
this.map.setCenter(this.map.getCenter(),
this.map.getZoomForResolution(resolution),
false, true
);
// trigger a changebaselayer event to make sure that
// all controls (especially
// OpenLayers.Control.PanZoomBar) get notified of the
// new options
this.map.events.triggerEvent("changebaselayer", {
layer: this
});
}
break;
}
}
Expand Down Expand Up @@ -763,8 +771,7 @@ OpenLayers.Layer = OpenLayers.Class({
} else {
if (this.map) {
var resolution = this.map.getResolution();
inRange = ( this.map.getZoom() >= this.restrictedMinZoom &&
(resolution >= this.minResolution) &&
inRange = ( (resolution >= this.minResolution) &&
(resolution <= this.maxResolution) );
}
}
Expand Down Expand Up @@ -1197,7 +1204,7 @@ OpenLayers.Layer = OpenLayers.Class({
}
zoom = Math.max(0, i-1);
}
return Math.max(this.restrictedMinZoom, zoom);
return zoom;
},

/**
Expand Down
30 changes: 23 additions & 7 deletions lib/OpenLayers/Layer/Bing.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,21 @@
*/
OpenLayers.Layer.Bing = OpenLayers.Class(OpenLayers.Layer.XYZ, {

/**
* Property: serverResolutions
* {Array} the resolutions provided by the Bing servers.
*/
serverResolutions: [
156543.03390625, 78271.516953125, 39135.7584765625,
19567.87923828125, 9783.939619140625, 4891.9698095703125,
2445.9849047851562, 1222.9924523925781, 611.4962261962891,
305.74811309814453, 152.87405654907226, 76.43702827453613,
38.218514137268066, 19.109257068634033, 9.554628534317017,
4.777314267158508, 2.388657133579254, 1.194328566789627,
0.5971642833948135, 0.29858214169740677, 0.14929107084870338,
0.07464553542435169
],

/**
* Property: attributionTemplate
* {String}
Expand Down Expand Up @@ -80,7 +95,6 @@ OpenLayers.Layer.Bing = OpenLayers.Class(OpenLayers.Layer.XYZ, {
*/
initialize: function(options) {
options = OpenLayers.Util.applyDefaults({
restrictedMinZoom: 1,
sphericalMercator: true
}, options);
var name = options.name || "Bing " + (options.type || this.type);
Expand Down Expand Up @@ -127,12 +141,14 @@ OpenLayers.Layer.Bing = OpenLayers.Class(OpenLayers.Layer.XYZ, {
this.url.push(url.replace("{subdomain}", res.imageUrlSubdomains[i]));
};
this.addOptions({
restrictedMinZoom: res.zoomMin,
numZoomLevels: res.zoomMax + 1
});
this.updateAttribution();
// redraw to replace "blank.gif" tiles with real tiles
this.redraw();
maxResolution: Math.min(
this.serverResolutions[res.zoomMin], this.maxResolution
),
zoomOffset: res.zoomMin,
numZoomLevels: Math.min(
res.zoomMax + 1 - res.zoomMin, this.numZoomLevels
)
}, true);
},

/**
Expand Down
42 changes: 23 additions & 19 deletions lib/OpenLayers/Layer/GoogleNG.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,21 @@ OpenLayers.Layer.GoogleNG = OpenLayers.Class(OpenLayers.Layer.XYZ, {
*/
SUPPORTED_TRANSITIONS: [],

/**
* Property: serverResolutions
* {Array} the resolutions provided by the Google API.
*/
serverResolutions: [
156543.03390625, 78271.516953125, 39135.7584765625,
19567.87923828125, 9783.939619140625, 4891.9698095703125,
2445.9849047851562, 1222.9924523925781, 611.4962261962891,
305.74811309814453, 152.87405654907226, 76.43702827453613,
38.218514137268066, 19.109257068634033, 9.554628534317017,
4.777314267158508, 2.388657133579254, 1.194328566789627,
0.5971642833948135, 0.29858214169740677, 0.14929107084870338,
0.07464553542435169, 0.037322767712175846
],

/**
* Property: attributionTemplate
* {String}
Expand Down Expand Up @@ -94,7 +109,6 @@ OpenLayers.Layer.GoogleNG = OpenLayers.Class(OpenLayers.Layer.XYZ, {
var newArgs = [options.name, null, options];
OpenLayers.Layer.XYZ.prototype.initialize.apply(this, newArgs);

this.options.numZoomLevels = options.numZoomLevels;
if (!OpenLayers.Layer.GoogleNG.mapObject) {
OpenLayers.Layer.GoogleNG.mapObject =
new google.maps.Map(document.createElement("div"));
Expand All @@ -121,26 +135,16 @@ OpenLayers.Layer.GoogleNG = OpenLayers.Class(OpenLayers.Layer.XYZ, {
this.setName("Google " + mapType.name);
}

var numZoomLevels = mapType.maxZoom + 1;
if (this.options.numZoomLevels != null) {
numZoomLevels = Math.min(numZoomLevels, this.options.numZoomLevels);
}
var restrictedMinZoom;
if (this.restrictedMinZoom || mapType.minZoom) {
restrictedMinZoom = Math.max(
mapType.minZoom || 0, this.restrictedMinZoom || 0
);
}

var minZoom = mapType.minZoom || 0;
this.addOptions({
restrictedMinZoom: restrictedMinZoom,
numZoomLevels: numZoomLevels,
tileSize: new OpenLayers.Size(
mapType.tileSize.width, mapType.tileSize.height
maxResolution: Math.min(
this.serverResolutions[minZoom], this.maxResolution
),
zoomOffset: minZoom,
numZoomLevels: Math.min(
mapType.maxZoom + 1 - minZoom, this.numZoomLevels
)
});
// redraw to populate tiles with content
this.redraw();
}, true);
},

/**
Expand Down
18 changes: 2 additions & 16 deletions lib/OpenLayers/Map.js
Original file line number Diff line number Diff line change
Expand Up @@ -1927,8 +1927,8 @@ OpenLayers.Map = OpenLayers.Class({
*/
isValidZoomLevel: function(zoomLevel) {
return ( (zoomLevel != null) &&
(zoomLevel >= this.getRestrictedMinZoom()) &&
(zoomLevel < this.getNumZoomLevels()) );
(zoomLevel >= 0) &&
(zoomLevel < this.getNumZoomLevels()) );
},

/**
Expand Down Expand Up @@ -2031,20 +2031,6 @@ OpenLayers.Map = OpenLayers.Class({
return maxExtent;
},

/**
* Method: getRestricteMinZoom
*
* Returns:
* {Integer} the minimum zoom level allowed for the current baseLayer.
*/
getRestrictedMinZoom: function() {
var minZoom = null;
if (this.baseLayer != null) {
minZoom = this.baseLayer.restrictedMinZoom;
}
return minZoom;
},

/**
* APIMethod: getNumZoomLevels
*
Expand Down
21 changes: 14 additions & 7 deletions tests/Layer.html
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@

function test_Layer_addOptions (t) {

t.plan( 19 );
t.plan( 20 );

var map = new OpenLayers.Map("map");
var map = new OpenLayers.Map("map", {allOverlays: true});
var options = { chicken: 151, foo: "bar" };
var layer = new OpenLayers.Layer('Test Layer', options);
map.addLayer(layer);
Expand Down Expand Up @@ -156,6 +156,17 @@
layer.addOptions({projection: "EPSG:900913"});
t.ok(layer.projection instanceof OpenLayers.Projection,
"addOptions creates a Projection object when given a projection string");

log = null;
// adding a 2nd layer to see if it gets reinitialized properly
var layer2 = new OpenLayers.Layer(null, {
moveTo: function(bounds) {
log = bounds;
}
});
map.addLayer(layer2);
layer.addOptions({maxResolution: 0.00034332275390625}, true);
t.eq(log.toBBOX(), map.getExtent().toBBOX(), "when reinitialize is set to true, changing base layer's resolution property reinitializes all layers.");

map.removeLayer(layer);
log = 0;
Expand Down Expand Up @@ -556,7 +567,7 @@

function test_Layer_getZoomForResolution(t) {

t.plan(13);
t.plan(12);

var layer = new OpenLayers.Layer('Test Layer');
layer.map = {};
Expand Down Expand Up @@ -584,10 +595,6 @@
"(fractionalZoom) doesn't return zoom below zero");
t.eq(layer.getZoomForResolution(1).toPrecision(6), (layer.resolutions.length - 1).toPrecision(6),
"(fractionalZoom) doesn't return zoom above highest index");

layer.restrictedMinZoom = 1;
t.eq(layer.getZoomForResolution(200), 1, "zoom all the way out, but we have a restrictedMinZoom of 1");

}

function test_Layer_redraw(t) {
Expand Down
13 changes: 7 additions & 6 deletions tests/Layer/GoogleNG.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
var log = {};
layer = new OpenLayers.Layer.GoogleNG({
numZoomLevels: 10,
restrictedMinZoom: 2,
maxResolution: 39135.7584765625,
initLayer: function() {
log[layer.id] = true;
OpenLayers.Layer.GoogleNG.prototype.initLayer.apply(this, arguments);
Expand All @@ -30,13 +30,14 @@
map.zoomToMaxExtent();

var map2 = new OpenLayers.Map("map2");
var minZoom = 1;
var layer2 = new OpenLayers.Layer.GoogleNG({
numZoomLevels: 24,
initLayer: function() {
log[layer2.id] = true;
var origMinZoom = OpenLayers.Layer.GoogleNG.mapObject.mapTypes[layer2.type].minZoom;
// pretend the API reports a minZoom of 1
OpenLayers.Layer.GoogleNG.mapObject.mapTypes[layer2.type].minZoom = 1;
// pretend the API reports a different minZoom
OpenLayers.Layer.GoogleNG.mapObject.mapTypes[layer2.type].minZoom = minZoom;
OpenLayers.Layer.GoogleNG.prototype.initLayer.apply(this, arguments);
OpenLayers.Layer.GoogleNG.mapObject.mapTypes[layer2.type].minZoom = origMinZoom;
}
Expand All @@ -49,10 +50,10 @@
t.eq(log[layer2.id], true, "initLayer called for 2nd layer");

t.eq(layer.numZoomLevels, 10, "numZoomLevels from configuration takes precedence if lower");
t.eq(layer2.numZoomLevels, OpenLayers.Layer.GoogleNG.mapObject.mapTypes[layer2.type].maxZoom+1, "numZoomLevels from API takes precedence if lower");
t.eq(layer2.numZoomLevels, OpenLayers.Layer.GoogleNG.mapObject.mapTypes[layer2.type].maxZoom + 1 - minZoom, "numZoomLevels from API takes precedence if lower");

t.eq(layer.restrictedMinZoom, 2, "restrictedMinZoom from configuration takes precedence if higher");
t.eq(layer2.restrictedMinZoom, 1, "restrictedMinZoom from API takes precedence if higher");
t.eq(layer.maxResolution, 39135.7584765625, "maxResolution from configuration takes precedence if higher");
t.eq(layer2.maxResolution, 78271.516953125, "maxResolution from API takes precedence if higher");

map.destroy();
map2.destroy();
Expand Down
38 changes: 5 additions & 33 deletions tests/Map.html
Original file line number Diff line number Diff line change
Expand Up @@ -338,31 +338,24 @@
*/

function test_Map_isValidZoomLevel(t) {
t.plan(6);
t.plan(4);
var map = new OpenLayers.Map("map");
map.addLayer(new OpenLayers.Layer(null, {
isBaseLayer: true, numZoomLevels: 19
}))
var valid;

valid = OpenLayers.Map.prototype.isValidZoomLevel.apply(map, [-1]);
t.eq(valid, false, "-1 is not a valid zoomLevel");

valid = OpenLayers.Map.prototype.isValidZoomLevel.apply(map, [0]);
t.eq(valid, true, "0 is a valid zoomLevel when baseLayer has no restrictedMinZoom");
t.eq(valid, true, "0 is a valid zoomLevel");

valid = OpenLayers.Map.prototype.isValidZoomLevel.apply(map, [18]);
t.eq(valid, true, "18 is a valid zoomLevel");

valid = OpenLayers.Map.prototype.isValidZoomLevel.apply(map, [19]);
t.eq(valid, false, "19 is not a valid zoomLevel");

map.baseLayer.restrictedMinZoom = 1;
valid = OpenLayers.Map.prototype.isValidZoomLevel.apply(map, [0]);
t.eq(valid, false, "0 is not a valid zoomLevel when baseLayer has restrictedMinZoom of 1");

valid = OpenLayers.Map.prototype.isValidZoomLevel.apply(map, [1]);
t.eq(valid, true, "1 is a valid zoomLevel");

valid = OpenLayers.Map.prototype.isValidZoomLevel.apply(map, [19]);
t.eq(valid, false, "19 is not a valid zoomLevel when baseLayer has restrictedMinZoom of 1");

map.destroy();
}
Expand Down Expand Up @@ -1296,27 +1289,6 @@
var maxExtent = OpenLayers.Map.prototype.getMaxExtent.apply(map, [options]);
t.ok(maxExtent == map.baseLayer.maxExtent, "null options, valid baseLayer returns map.baseLayer.maxExtent");
}

function test_Map_getRestrictedMinZoom(t){
t.plan(3);

var map = {};

//no baseLayer
var minZoom = OpenLayers.Map.prototype.getRestrictedMinZoom.apply(map);
t.eq(minZoom, null, "no baseLayer returns null");

map.baseLayer = new OpenLayers.Layer(null, {isBaseLayer: true});

//baseLayer
minZoom = OpenLayers.Map.prototype.getRestrictedMinZoom.apply(map);
t.eq(minZoom, 0, "default baseLayer.restrictedMinZoom returns 0");

//custom minZoomLevel on baseLayer
map.baseLayer.restrictedMinZoom = 1;
minZoom = OpenLayers.Map.prototype.getRestrictedMinZoom.apply(map);
t.eq(minZoom, map.baseLayer.restrictedMinZoom, "custom baseLayer.restrictedMinZoom returns map.baseLayer.restrictedMinZoom");
}

function test_Map_zoomToMaxExtent(t){
t.plan(4)
Expand Down

0 comments on commit a9d3b8b

Please sign in to comment.