OpenLayers OpenLayers

Changeset 1302

Show
Ignore:
Timestamp:
08/18/06 22:09:29 (2 years ago)
Author:
euzuro
Message:

Remove the concept of min/max zoom level from Map. Replace it with concept of num zoom levels. Bit of rearrangement in the initResolutions() function in HTTPRequest.js. Adapt all of OL to deal with numZoomLevels instead of min/max. Fix PanZoomBar so that it listens for change of baselayer and redraws itself. fix all tests so they pass. Add zoomLevels.html example for playing around with different methods of setting zoomlevels.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/openlayers/2.0/examples/urban.html

    r697 r1302  
    1212        <!-- 
    1313        function init(){ 
    14             var map = new OpenLayers.Map('map', {'maxResolution': 1.6, maxZoomLevel:20}); 
    15  
     14            var mapOptions = { maxResolution: 1.6, numZoomLevels: 21}; 
     15            var map = new OpenLayers.Map('map', mapOptions); 
     16             
    1617            var ol_wms = new OpenLayers.Layer.WMS( "OpenLayers WMS",  
    1718                "http://labs.metacarta.com/wms/vmap0?", {layers: 'basic'} ); 
     
    1920            var ww = new OpenLayers.Layer.WorldWind( "Urban",  
    2021                "http://worldwind25.arc.nasa.gov/tile/tile.aspx?", .8, 9, 
    21                 {T:"104"}); 
    22             ww.setTileSize(new OpenLayers.Size(512,512)); 
     22                {T:"104"}, { tileSize: new OpenLayers.Size(512,512) }); 
    2323 
    2424 
  • branches/openlayers/2.0/lib/OpenLayers/Control/PanZoomBar.js

    r1206 r1302  
    2929        // put code here to catch "changebaselayer" event from map, because 
    3030        //  we are going to have to redraw this thing each time, because  
    31         //  maxZoom will/might change. 
    32     }, 
    33  
     31        //  numZoomLevels will/might change. 
     32    }, 
     33 
     34    /** 
     35     * @param {OpenLayers.Map} map 
     36     */ 
     37    setMap: function(map) { 
     38        OpenLayers.Control.PanZoom.prototype.setMap.apply(this, arguments); 
     39         
     40        this.map.events.register("changebaselayer", this, this.redraw); 
     41    }, 
     42 
     43    /** clear the div and start over. 
     44     *  
     45     */ 
     46    redraw: function() { 
     47        if (this.div != null) { 
     48            this.div.innerHTML = ""; 
     49        }   
     50        this.draw(); 
     51    }, 
     52     
    3453    /** 
    3554    * @param {OpenLayers.Pixel} px 
     
    6685        var slider = OpenLayers.Util.createAlphaImageDiv(id, 
    6786                       centered.add(-1,  
    68                         (this.map.getMaxZoomLevel())*this.zoomStopHeight),  
     87                        (this.map.getNumZoomLevels()-1) * this.zoomStopHeight),  
    6988                       new OpenLayers.Size(20,9),  
    7089                       imgLocation+"slider.png", 
     
    8099         
    81100        sz = new OpenLayers.Size(); 
    82         sz.h = this.zoomStopHeight*(this.map.getMaxZoomLevel()+1); 
     101        sz.h = this.zoomStopHeight * this.map.getNumZoomLevels(); 
    83102        sz.w = this.zoomStopWidth; 
    84103        var div = null 
     
    116135 
    117136        centered = centered.add(0,  
    118             this.zoomStopHeight*(this.map.getMaxZoomLevel()+1)); 
     137            this.zoomStopHeight * this.map.getNumZoomLevels()); 
    119138        return centered;  
    120139    }, 
     
    137156        var top = Position.page(evt.object)[1]; 
    138157        var levels = Math.floor((y - top)/this.zoomStopHeight); 
    139         this.map.zoomTo(this.map.getMaxZoomLevel() - levels); 
     158        this.map.zoomTo((this.map.getNumZoomLevels() -1) - levels); 
    140159        Event.stop(evt); 
    141160    }, 
     
    199218    moveZoomBar:function() { 
    200219        var newTop =  
    201             (this.map.getMaxZoomLevel() - this.map.getZoom()) * this.zoomStopHeight 
    202             + this.startTop + 1; 
     220            ((this.map.getNumZoomLevels()-1) - this.map.getZoom()) *  
     221            this.zoomStopHeight + this.startTop + 1; 
    203222        this.slider.style.top = newTop + "px"; 
    204223    },     
  • branches/openlayers/2.0/lib/OpenLayers/Layer.js

    r1294 r1302  
    5252 
    5353    /** @type int */ 
    54     minZoomLevel: null, 
    55      
    56     /** @type int */ 
    57     maxZoomLevel: null, 
     54    numZoomLevels: null, 
    5855    
    5956    /** @type float */ 
     
    179176         
    180177        var properties = new Array( 
    181           'projection', 'minExtent', 'maxExtent', 
    182           'minScale', 'maxScale', 
     178          'projection', 'units', 
     179          'scales', 'resolutions', 
     180          'maxScale', 'minScale',  
    183181          'maxResolution', 'minResolution',  
    184           'minZoomLevel', 'maxZoomLevel', 'units', 
    185           'scales', 'resolutions' 
    186            
     182          'minExtent', 'maxExtent', 
     183          'numZoomLevels' 
    187184        ); 
    188185        for(var i=0; i < properties.length; i++) { 
     
    252249     
    253250    /** 
    254      * @returns The minimum zoom level that can be reached in this layer 
     251     * @returns The total number of zoom levels this layer can reach 
    255252     * @type int 
    256253     */ 
    257     getMinZoomLevel: function() { 
    258         return this.minZoomLevel; 
    259     }, 
    260  
    261     /** 
    262      * @returns The maximum zoom level that can be reached in this layer 
    263      * @type int 
    264      */ 
    265     getMaxZoomLevel: function() { 
    266         return this.maxZoomLevel; 
     254    getNumZoomLevels: function() { 
     255        return this.numZoomLevels; 
    267256    }, 
    268257 
  • branches/openlayers/2.0/lib/OpenLayers/Layer/Google.js

    r1275 r1302  
    5050 
    5151    /** @type int */ 
    52     minZoomLevel: -1, 
    53      
    54     /** @type int */ 
    55     maxZoomLevel: 16, 
     52    numZoomLevels: 16, 
    5653 
    5754     
  • branches/openlayers/2.0/lib/OpenLayers/Layer/Grid.js

    r1288 r1302  
    445445        var zoom = Math.floor( (Math.log(maxRes/degPerPixel)) / Math.log(2) ); 
    446446 
    447         var maxZoomLevel = this.map.getMaxZoomLevel(); 
    448         var minZoomLevel = this.map.getMinZoomLevel(); 
    449      
    450447        //make sure zoom is within bounds     
    451         zoom = Math.min( Math.max(zoom, minZoomLevel),  
    452                          maxZoomLevel ); 
     448        zoom = Math.min( Math.max(zoom, 0),  
     449                         this.getNumZoomLevels() - 1); 
    453450 
    454451        return zoom; 
  • branches/openlayers/2.0/lib/OpenLayers/Layer/HTTPRequest.js

    r1296 r1302  
    3737    }, 
    3838 
     39    /** When the layer is added to the map, once it has taken all the  
     40     *   relevant properties from the map (in Layer.setMap()), we will 
     41     *   make the call to initialize the layer's resolutions array. 
     42     *  
     43     * @param {OpenLayers.Map} map 
     44     */ 
    3945    setMap: function(map) { 
    4046        OpenLayers.Layer.prototype.setMap.apply(this, arguments); 
    4147        this.initResolutions(); 
    4248    }, 
     49     
    4350    /** 
    4451     *  
     
    132139    }, 
    133140     
     141    /** This method's responsibility is to set up the 'resolutions' array  
     142     *   for the layer -- this array is what the layer will use to interface 
     143     *   between the zoom levels of the map and the resolution display of the 
     144     *   layer. 
     145     *  
     146     *  The user has several options that determine how the array is set up. 
     147     *   
     148     *  For a detailed explanation, see the following wiki from the  
     149     *   openlayers.org homepage: 
     150     *  
     151     *  http://trac.openlayers.org/wiki/SettingZoomLevels 
     152     *  
     153     * @private 
     154     */ 
    134155    initResolutions: function() { 
    135         if (this.scales != null) { 
     156         
     157        if ((this.scales != null) || (this.resolutions != null)) { 
     158          //preset levels 
     159            if (this.scales != null) { 
     160                this.resolutions = new Array(); 
     161                for(var i = 0; i < this.scales.length; i++) { 
     162                    this.resolutions[i] =  
     163                       OpenLayers.Util.getResolutionFromScale(this.scales[i],  
     164                                                              this.units); 
     165                } 
     166            } 
     167            this.numZoomLevels = this.resolutions.length; 
     168 
     169        } else { 
     170          //maxResolution and numZoomLevels 
     171             
    136172            this.resolutions = new Array(); 
    137             for(var i = 0; i < this.scales.length; i++)  
    138                 this.resolutions[i] = OpenLayers.Util.getResolutionFromScale(this.scales[i], this.units); 
    139             this.maxZoomLevel = this.resolutions.length; 
    140         } else if (this.resolutions != null) { 
    141             this.maxZoomLevel = this.resolutions.length; 
    142         } else { 
    143             this.resolutions = new Array(); 
    144             if (this.minScale) 
    145                 this.maxResolution = OpenLayers.Util.getResolutionFromScale(this.minScale, this.units); 
    146             var maxRes = this.getMaxResolution(); 
    147             if (this.maxScale) { 
    148                 /* This will cause this.map.getMaxZoomLevel() to be set the next time 
    149                  * it is called, which means that the next portion here will succeed. */ 
    150                 var minRes = OpenLayers.Util.getResolutionFromScale(this.maxScale); 
    151                 this.maxZoomLevel = Math.floor(Math.log(maxRes/minRes) / Math.log(2)); 
    152             }     
    153             for (var i=this.getMinZoomLevel(); i <= this.getMaxZoomLevel(); i++) { 
    154                 this.resolutions.push(maxRes / Math.pow(2, i)); 
     173             
     174            // determine maxResolution 
     175            if (this.minScale) { 
     176                this.maxResolution =  
     177                    OpenLayers.Util.getResolutionFromScale(this.minScale,  
     178                                                           this.units); 
     179            } else if (this.maxResolution == "auto") { 
     180                var maxExtent = this.getMaxExtent(); 
     181                var viewSize = this.map.getSize(); 
     182                var wRes = maxExtent.getWidth() / viewSize.w; 
     183                var hRes = maxExtent.getHeight()/ viewSize.h; 
     184                this.maxResolution = Math.max(wRes, hRes); 
     185            }  
     186 
     187            // determine numZoomLevels 
     188             
     189            if (this.maxScale != null) {            
     190                this.minResolution =  
     191                    OpenLayers.Util.getResolutionFromScale(this.maxScale); 
     192            } 
     193 
     194            if (this.minResolution != null) { 
     195                var ratio = this.maxResolution / this.minResolution; 
     196                this.numZoomLevels =  
     197                    Math.floor(Math.log(ratio) / Math.log(2)) + 1; 
     198            } 
     199             
     200            // now we have numZoomLevels and maxResolution,  
     201            //  we can populate the resolutions array 
     202            for (var i=0; i < this.numZoomLevels; i++) { 
     203                this.resolutions.push(this.maxResolution / Math.pow(2, i)); 
    155204            }     
    156205        } 
    157206    }, 
    158207     
     208    /** 
     209     * @returns The currently selected resolution of the map, taken from the 
     210     *          resolutions array, indexed by current zoom level. 
     211     * @type float 
     212     */ 
    159213    getResolution: function() { 
    160214        var zoom = this.map.getZoom(); 
  • branches/openlayers/2.0/lib/OpenLayers/Layer/VirtualEarth.js

    r1235 r1302  
    1414    /** @type VEMap */ 
    1515    vemap: null, 
     16     
     17    /** @type int */ 
     18    numZoomLevels: 17, 
    1619     
    1720    /** 
     
    212215        var zoom = Math.floor( (Math.log(maxRes/degPerPixel)) / Math.log(2) ); 
    213216 
    214         var maxZoomLevel = this.map.getMaxZoomLevel(); 
    215         var minZoomLevel = this.map.getMinZoomLevel(); 
    216      
    217217        //make sure zoom is within bounds     
    218         zoom = Math.min( Math.max(zoom, minZoomLevel),  
    219                          maxZoomLevel ); 
     218        zoom = Math.min( Math.max(zoom, 0),  
     219                         this.numZoomLevels-1); 
    220220 
    221221        return zoom; 
  • branches/openlayers/2.0/lib/OpenLayers/Map.js

    r1296 r1302  
    7979  // Options 
    8080 
     81    /** @type OpenLayers.Size */ 
     82    tileSize: null, 
     83 
    8184    /** @type String */ 
    8285    projection: "EPSG:4326",     
    8386         
     87    /** @type String */ 
     88    units: 'degrees', 
     89 
     90    /** default max is 360 deg / 256 px, which corresponds to 
     91     *    zoom level 0 on gmaps 
     92     *  
     93     * @type float */ 
     94    maxResolution: 1.40625, 
     95 
     96    /** @type float */ 
     97    minResolution: null, 
     98 
     99    /** @type float */ 
     100    maxScale: null, 
     101 
     102    /** @type float */ 
     103    minScale: null, 
     104 
    84105    /** @type OpenLayers.Bounds */ 
    85106    maxExtent: null, 
    86107     
    87     /** default max is 360 deg / 256 px, which corresponds to 
    88      *    zoom level 0 on gmaps 
    89      *  
    90      * @type float */ 
    91     maxResolution: 1.40625, 
    92  
     108    /** @type OpenLayers.Bounds */ 
     109    minExtent: null, 
     110     
    93111    /** @type int */ 
    94     minZoomLevel: 0, 
    95      
    96     /** @type int */ 
    97     maxZoomLevel: 16, 
    98  
    99     /** @type OpenLayers.Size */ 
    100     tileSize: null, 
    101  
    102     /** @type String */ 
    103     units: 'degrees', 
    104  
    105     /** @type Float */ 
    106     minScale: null, 
    107      
     112    numZoomLevels: 16, 
    108113 
    109114 
     
    208213        //  (these will override defaults) 
    209214        Object.extend(this, options); 
    210  
    211         // if maxResolution is specified as "auto", calculate it  
    212         //  based on the maxExtent and the viewSize 
    213         // 
    214         if (this.maxResolution == "auto" || this.maxResolution == null) { 
    215             var maxExtent = this.getMaxExtent(); 
    216             var viewSize = this.getSize(); 
    217             this.maxResolution = Math.max(maxExtent.getWidth()  / viewSize.w, 
    218                                           maxExtent.getHeight() / viewSize.h ); 
    219         } 
    220215    }, 
    221216 
     
    648643    isValidZoomLevel: function(zoomLevel) { 
    649644       return ( (zoomLevel != null) && 
    650                 (zoomLevel >= this.getMinZoomLevel()) &&  
    651                 (zoomLevel <= this.getMaxZoomLevel()) ); 
     645                (zoomLevel >= 0) &&  
     646                (zoomLevel < this.getNumZoomLevels()) ); 
    652647    }, 
    653648     
     
    685680    getProjection: function() { 
    686681        var projection = null; 
    687          
    688682        if (this.baseLayer != null) { 
    689683            projection = this.baseLayer.getProjection(); 
    690684        } 
    691          
    692         if (projection == null) { 
    693             projection = this.projection; 
    694         } 
    695  
    696685        return projection; 
    697686    }, 
     
    703692    getMaxResolution: function() { 
    704693        var maxResolution = null; 
    705          
    706694        if (this.baseLayer != null) { 
    707695            maxResolution = this.baseLayer.getMaxResolution(); 
    708696        } 
    709  
    710         if (maxResolution == null) { 
    711             maxResolution = this.maxResolution; 
    712         } 
    713  
    714697        return maxResolution; 
    715698    }, 
     
    720703    getMaxExtent: function () { 
    721704        var maxExtent = null; 
    722          
    723705        if (this.baseLayer != null) { 
    724706            maxExtent = this.baseLayer.getMaxExtent(); 
    725         } 
    726      
    727         if (maxExtent == null) { 
    728             maxExtent = this.maxExtent; 
    729         } 
    730          
     707        }         
    731708        return maxExtent; 
    732709    }, 
    733710     
    734711    /** 
    735      * @returns The maximum zoom level that can be reached in the map 
     712     * @returns The total number of zoom levels that can be displayed by the  
     713     *           current baseLayer. 
    736714     * @type int 
    737715     */ 
    738     getMaxZoomLevel: function() { 
    739         var maxZoomLevel = null; 
    740          
     716    getNumZoomLevels: function() { 
     717        var numZoomLevels = null; 
    741718        if (this.baseLayer != null) { 
    742             maxZoomLevel = this.baseLayer.getMaxZoomLevel(); 
    743         } 
    744  
    745         if (maxZoomLevel == null) { 
    746             maxZoomLevel = this.maxZoomLevel; 
    747         } 
    748          
    749         return maxZoomLevel; 
    750     }, 
    751  
    752     /** 
    753      * @returns The minimum zoom level that can be reached in the map 
    754      * @type int 
    755      */ 
    756     getMinZoomLevel: function() { 
    757         var minZoomLevel = null; 
    758          
    759         if (this.baseLayer != null) { 
    760             minZoomLevel = this.baseLayer.getMinZoomLevel(); 
    761         } 
    762          
    763         if (minZoomLevel == null) { 
    764             minZoomLevel = this.minZoomLevel; 
    765         } 
    766          
    767         return minZoomLevel; 
    768     }, 
    769  
     719            numZoomLevels = this.baseLayer.getNumZoomLevels(); 
     720        } 
     721        return numZoomLevels; 
     722    }, 
    770723 
    771724  /********************************************************/ 
     
    788741    getExtent: function () { 
    789742        var extent = null; 
    790          
    791743        if (this.baseLayer != null) { 
    792744            extent = this.baseLayer.getExtent(); 
     
    802754    getResolution: function () { 
    803755        var resolution = null; 
    804          
    805756        if (this.baseLayer != null) { 
    806757            resolution = this.baseLayer.getResolution(); 
     
    816767    getScale: function () { 
    817768        var scale = null; 
    818  
    819769        if (this.baseLayer != null) { 
    820770            var res = this.getResolution(); 
     
    836786    getZoomForExtent: function (bounds) { 
    837787        zoom = null; 
    838          
    839788        if (this.baseLayer != null) { 
    840789            zoom = this.baseLayer.getZoomForExtent(bounds); 
  • branches/openlayers/2.0/tests/test_Layer.html

    r1210 r1302  
    9090    function test_04_Layer_StandardOptionsAccessors (t) { 
    9191 
    92         t.plan( 5 ); 
     92        t.plan( 4 ); 
    9393 
    9494        var projection = "chicken"; 
    9595        var maxExtent = new OpenLayers.Bounds(50,50,100,100); 
    9696        var maxResolution = 1.5726; 
    97         var minZoomLevel = 5; 
    98         var maxZoomLevel = 15; 
     97        var numZoomLevels = 11; 
    9998 
    10099        var options = { projection: projection,  
    101100                        maxExtent: maxExtent, 
    102101                        maxResolution: maxResolution, 
    103                         minZoomLevel: minZoomLevel, 
    104                         maxZoomLevel: maxZoomLevel 
     102                        numZoomLevels: numZoomLevels, 
    105103                        }; 
    106104 
     
    110108        t.ok(layer.getMaxExtent().equals(maxExtent), "getMaxExtent() works"); 
    111109        t.eq(layer.getMaxResolution(), maxResolution, "getMaxResolution() works"); 
    112         t.eq(layer.getMinZoomLevel(), minZoomLevel, "getMinZoomLevel() works"); 
    113         t.eq(layer.getMaxZoomLevel(), maxZoomLevel, "getMaxZoomLevel() works"); 
     110        t.eq(layer.getNumZoomLevels(), numZoomLevels, "getNumZoomLevels() works"); 
    114111    } 
    115112 
  • branches/openlayers/2.0/tests/test_Map.html

    r1244 r1302  
    2828        t.ok( map.events instanceof OpenLayers.Events, "map.events is an OpenLayers.Events" ); 
    2929        t.ok( map.getMaxExtent() instanceof OpenLayers.Bounds, "map.maxExtent is an OpenLayers.Bounds" ); 
    30         t.ok( map.getMaxZoomLevel() > 0, "map.maxZoomLevel is set" ); 
     30        t.ok( map.getNumZoomLevels() > 0, "map has a default numZoomLevels" ); 
    3131    } 
    3232    function test_02_Map_center(t) { 
     
    6464    function test_04_Map_options(t) { 
    6565        t.plan(2); 
    66         map = new OpenLayers.Map($('map'), {maxZoomLevel: 5, maxResolution: 3.14159}); 
    67         t.eq( map.maxZoomLevel, 5, "map.maxZoomLevel set correctly via options hashtable" ); 
     66        map = new OpenLayers.Map($('map'), {numZoomLevels: 6, maxResolution: 3.14159}); 
     67        t.eq( map.numZoomLevels, 6, "map.numZoomLevels set correctly via options hashtable" ); 
    6868        t.eq( map.maxResolution, 3.14159, "map.maxResolution set correctly via options hashtable" ); 
    6969    } 
     
    152152        t.ok(!found, "popup.div successfully removed from the map's viewPort"); 
    153153    } 
    154     function test_10_Map_auto_res(t) { 
    155         t.plan(2); 
    156         map = new OpenLayers.Map($('map'), {maxZoomLevel: 5, maxResolution: 'auto'}); 
    157         t.eq( map.maxResolution, 1/3, "map.maxResolution set correctly via auto" ); 
    158         map = new OpenLayers.Map($('map'), {maxZoomLevel: 5, maxResolution: null}); 
    159         t.eq( map.maxResolution, 1/3, "map.maxResolution set correctly via null" ); 
    160     }  
    161  
    162154/***  THIS IS A GOOD TEST, BUT IT SHOULD BE MOVED TO WMS.  
    163155 *     Also, it won't work until we figure out the viewSize bug