OpenLayers OpenLayers

Ticket #328: layerclone.patch

File layerclone.patch, 10.5 kB (added by euzuro, 2 years ago)

suggested patch to fix layer.clone() including updated and new tests

  • tests/test_Layer.html

    old new  
    3333    } 
    3434 
    3535    function test_02_Layer_clone (t) { 
    36         t.plan( 6 ); 
     36        t.plan( 8 ); 
    3737         
    38         var map = new OpenLayers.Map('map');  
    39         var options = { chicken: 151, foo: "bar" }; 
     38        var mapone = new OpenLayers.Map('map');  
     39        var options = { chicken: 151, foo: "bar", maxResolution: "auto" }; 
    4040        var layer = new OpenLayers.Layer('Test Layer', options); 
    41         map.addLayer(layer); 
     41        mapone.addLayer(layer); 
    4242 
    4343        // randomly assigned property 
    4444        layer.chocolate = 5; 
    4545 
    4646        var clone = layer.clone(); 
    4747 
     48        t.ok( clone.map == null, "cloned layer has map property set to null") 
     49 
     50        var maptwo = new OpenLayers.Map('map2');  
     51        maptwo.addLayer(clone); 
     52 
    4853        t.ok( clone instanceof OpenLayers.Layer, "new OpenLayers.Layer returns object" ); 
    4954        t.eq( clone.name, "Test Layer", "default clone.name is correct" ); 
    5055        t.ok( ((clone.options["chicken"] == 151) && (clone.options["foo"] == "bar")), "clone.options correctly set" ); 
     
    5358        layer.addOptions({chicken:152}); 
    5459        t.eq(clone.options["chicken"], 151, "made a clean copy of options");         
    5560 
     61        t.ok( (layer.maxResolution != clone.maxResolution), "maxresolution of clone reset to new map div"); 
     62        t.ok( (layer.minResolution != clone.minResolution), "minresolution of clone reset to new map div"); 
    5663         
    57         t.ok( clone.map == null, "cloned layer has map property set to null") 
    58          
    5964    } 
    6065 
    6166    function test_03_Layer_setName (t) { 
     
    174179  </script> 
    175180</head> 
    176181<body> 
    177   <div id="map"></div> 
     182  <div id="map" style="width:500px;height:500px"></div> 
     183  <div id="map2" style="width:100px;height:100px"></div> 
    178184</body> 
    179185</html> 
  • lib/OpenLayers/Layer.js

    old new  
    206206    setMap: function(map) { 
    207207        this.map = map; 
    208208         
    209         var properties = new Array( 
    210           'projection', 'units', 
    211           'scales', 'resolutions', 
    212           'maxScale', 'minScale',  
    213           'maxResolution', 'minResolution',  
    214           'minExtent', 'maxExtent', 
    215           'numZoomLevels' 
    216         ); 
    217         if (this.map.maxZoomLevel && !this.numZoomLevels) { 
    218             this.numZoomLevels = this.map.maxZoomLevel + 1; 
    219         } 
    220         for(var i=0; i < properties.length; i++) { 
    221             if (this[properties[i]] == null) { 
    222                 this[properties[i]] = this.map[properties[i]]; 
    223             }     
    224         } 
    225209        this.initResolutions(); 
    226210         
    227211        this.inRange = this.calculateInRange(); 
     
    322306     * @private 
    323307     */ 
    324308    initResolutions: function() { 
    325          
    326         if ((this.scales != null) || (this.resolutions != null)) { 
     309 
     310        // These are the relevant options which are used for calculating  
     311        //  resolutions information. 
     312        // 
     313        var props = new Array( 
     314          'projection', 'units', 
     315          'scales', 'resolutions', 
     316          'maxScale', 'minScale',  
     317          'maxResolution', 'minResolution',  
     318          'minExtent', 'maxExtent', 
     319          'numZoomLevels', 'maxZoomLevel' 
     320        ); 
     321 
     322        // First we create a new object where we will store all of the  
     323        //  resolution-related properties that we find in either the layer's 
     324        //  'options' array or from the map. 
     325        // 
     326        var confProps = new Object();         
     327        for(var i=0; i < props.length; i++) { 
     328            var property = props[i]; 
     329            confProps[property] = this.options[property] || this.map[property]; 
     330        } 
     331 
     332        // If numZoomLevels hasn't been set and the maxZoomLevel *has*,  
     333        //  then use maxZoomLevel to calculate numZoomLevels 
     334        // 
     335        if ( (!confProps.numZoomLevels) && (confProps.maxZoomLevel) ) { 
     336            confProps.numZoomLevels = confProps.maxZoomLevel + 1; 
     337        } 
     338 
     339        // First off, we take whatever hodge-podge of values we have and  
     340        //  calculate/distill them down into a resolutions[] array 
     341        // 
     342        if ((confProps.scales != null) || (confProps.resolutions != null)) { 
    327343          //preset levels 
    328             if (this.scales != null) { 
    329                 this.resolutions = new Array(); 
    330                 for(var i = 0; i < this.scales.length; i++) { 
    331                     this.resolutions[i] =  
    332                        OpenLayers.Util.getResolutionFromScale(this.scales[i],  
    333                                                               this.units); 
     344            if (confProps.scales != null) { 
     345                confProps.resolutions = new Array(); 
     346                for(var i = 0; i < confProps.scales.length; i++) { 
     347                    var scale = confProps.scales[i]; 
     348                    confProps.resolutions[i] =  
     349                       OpenLayers.Util.getResolutionFromScale(scale,  
     350                                                              confProps.units); 
    334351                } 
    335352            } 
    336             this.numZoomLevels = this.resolutions.length; 
     353            confProps.numZoomLevels = confProps.resolutions.length; 
    337354 
    338355        } else { 
    339           //maxResolution and numZoomLevels 
     356          //maxResolution and numZoomLevels based calculation 
    340357             
    341             this.resolutions = new Array(); 
     358            confProps.resolutions = new Array(); 
    342359             
    343360            // determine maxResolution 
    344             if (this.minScale) { 
    345                 this.maxResolution =  
    346                     OpenLayers.Util.getResolutionFromScale(this.minScale,  
    347                                                            this.units); 
    348             } else if (this.maxResolution == "auto") { 
     361            if (confProps.minScale) { 
     362                confProps.maxResolution =  
     363                    OpenLayers.Util.getResolutionFromScale(confProps.minScale,  
     364                                                           confProps.units); 
     365            } else if (confProps.maxResolution == "auto") { 
    349366                var viewSize = this.map.getSize(); 
    350                 var wRes = this.maxExtent.getWidth() / viewSize.w; 
    351                 var hRes = this.maxExtent.getHeight()/ viewSize.h; 
    352                 this.maxResolution = Math.max(wRes, hRes); 
     367                var wRes = confProps.maxExtent.getWidth() / viewSize.w; 
     368                var hRes = confProps.maxExtent.getHeight()/ viewSize.h; 
     369                confProps.maxResolution = Math.max(wRes, hRes); 
    353370            }  
    354371 
    355372            // determine minResolution 
    356             if (this.maxScale != null) {            
    357                 this.minResolution =  
    358                     OpenLayers.Util.getResolutionFromScale(this.maxScale); 
    359             } else if ((this.minResolution == "auto") &&  
    360                        (this.minExtent != null))
     373            if (confProps.maxScale != null) {            
     374                confProps.minResolution =  
     375                    OpenLayers.Util.getResolutionFromScale(confProps.maxScale); 
     376            } else if ( (confProps.minResolution == "auto") &&  
     377                        (confProps.minExtent != null) )
    361378                var viewSize = this.map.getSize(); 
    362                 var wRes = this.minExtent.getWidth() / viewSize.w; 
    363                 var hRes = this.minExtent.getHeight()/ viewSize.h; 
    364                 this.minResolution = Math.max(wRes, hRes); 
     379                var wRes = confProps.minExtent.getWidth() / viewSize.w; 
     380                var hRes = confProps.minExtent.getHeight()/ viewSize.h; 
     381                confProps.minResolution = Math.max(wRes, hRes); 
    365382            }  
    366383 
    367384            // determine numZoomLevels 
    368             if (this.minResolution != null) { 
    369                 var ratio = this.maxResolution / this.minResolution; 
    370                 this.numZoomLevels =  
     385            if (confProps.minResolution != null) { 
     386                var ratio = confProps.maxResolution / confProps.minResolution; 
     387                confProps.numZoomLevels =  
    371388                    Math.floor(Math.log(ratio) / Math.log(2)) + 1; 
    372389            } 
    373390             
    374391            // now we have numZoomLevels and maxResolution,  
    375392            //  we can populate the resolutions array 
    376             for (var i=0; i < this.numZoomLevels; i++) { 
    377                 this.resolutions.push(this.maxResolution / Math.pow(2, i)); 
     393            for (var i=0; i < confProps.numZoomLevels; i++) { 
     394                var res = confProps.maxResolution / Math.pow(2, i) 
     395                confProps.resolutions.push(res); 
    378396            }     
    379397        } 
     398         
     399        //sort resolutions array ascendingly 
     400        // 
     401        confProps.resolutions.sort( function(a, b) { return(b-a); } ); 
    380402 
    381         this.resolutions.sort( function(a,b) {  
    382                                    return(b-a); 
    383                                }); 
     403        // now set our newly calculated values back to the layer  
     404        //  Note: We specifically do *not* set them to layer.options, which we  
     405        //        will preserve as it was when we added this layer to the map.  
     406        //        this way cloned layers reset themselves to new map div  
     407        //        dimensions) 
     408        // 
     409        this.projection = confProps.projection; 
     410        this.units = confProps.units; 
    384411 
    385         this.minResolution = this.resolutions[this.resolutions.length - 1]; 
    386         this.maxResolution = this.resolutions[0]; 
     412        this.resolutions = confProps.resolutions; 
     413        this.maxResolution = confProps.resolutions[0]; 
     414        var lastIndex = confProps.resolutions.length - 1; 
     415        this.minResolution = confProps.resolutions[lastIndex]; 
    387416         
    388         this.minScale =  
    389             OpenLayers.Util.getScaleFromResolution(this.maxResolution,  
    390                                                    this.units); 
    391         this.maxScale =  
    392             OpenLayers.Util.getScaleFromResolution(this.minResolution,  
    393                                                    this.units); 
     417        this.scales = new Array(); 
     418        for(var i = 0; i < confProps.resolutions.length; i++) { 
     419            this.scales[i] =  
     420               OpenLayers.Util.getScaleFromResolution(confProps.resolutions[i],  
     421                                                      confProps.units); 
     422        } 
     423        this.minScale = this.scales[0]; 
     424        this.maxScale = this.scales[this.scales.length - 1]; 
     425         
     426        this.minExtent = confProps.minExtent; 
     427        this.maxExtent = confProps.maxExtent;         
     428         
     429        this.numZoomLevels = confProps.numZoomLevels; 
    394430    }, 
    395431 
    396432    /**