OpenLayers OpenLayers

Changeset 2212

Show
Ignore:
Timestamp:
02/09/07 10:58:59 (2 years ago)
Author:
emanuel
Message:

version 1.2

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • sandbox/emanuel/animatedZooming/lib/lib/OpenLayers/Control/OverviewMap.js

    r2190 r2212  
    384384                (this.ovmap.getExtent().containsBounds(testExtent))); 
    385385    }, 
    386      
    387     updateOverview: function() { 
    388         var mapRes = this.map.getResolution(); 
     386 
     387    /*  
     388     * Updates the overview map. If the map is scaling (by zooming) the parameters 
     389     * define the values of the new resolution and bounds. 
     390     * 
     391     * @param {float} newResolution 
     392     * @param {OpenLayers.Bounds} bounds 
     393     */     
     394    updateOverview: function(newResolution, bounds) { 
     395        //set new map resolution only by scaling 
     396        if (newResolution) 
     397            var mapRes = newResolution; 
     398        else 
     399            var mapRes = this.map.getResolution(); 
    389400        var targetRes = this.ovmap.getResolution(); 
    390401        var resRatio = targetRes / mapRes; 
     
    398409        this.ovmap.setCenter(this.map.center, 
    399410                            this.ovmap.getZoomForResolution(targetRes)); 
    400         this.updateRectToMap(); 
     411        this.updateRectToMap(bounds); 
    401412    }, 
    402413     
     
    422433         
    423434    /** 
    424      * Updates the extent rectangle position and size to match the map extent 
    425      */ 
    426     updateRectToMap: function() { 
     435     * Updates the extent rectangle position and size to match the map extent   
     436     * If the map is scaling (by zooming) the parameter defines the 
     437     * new bounds of the current map 
     438     * 
     439     * @param {OpenLayers.Bounds} bounds 
     440     */ 
     441    updateRectToMap: function(bounds) { 
    427442        // The base layer for overview map needs to be in the same projection 
    428443        // as the base layer for the main map.  This should be made more robust. 
     
    432447            } 
    433448        } 
    434         var pxBounds = this.getRectBoundsFromMapBounds(this.map.getExtent()); 
     449 
     450        if (!bounds) 
     451            bounds=this.map.getExtent(); 
     452        var pxBounds = this.getRectBoundsFromMapBounds(bounds); 
    435453        this.setRectPxBounds(pxBounds); 
    436454    }, 
  • sandbox/emanuel/animatedZooming/lib/lib/OpenLayers/Control/PanZoomBar.js

    r1721 r2212  
    2424    zoomStopHeight: 11, 
    2525 
     26    /** @type int */ 
     27    zoomlevel_startScale: null,   
     28     
     29    /** @type float */ 
     30    zoomlevel_scale: null,    
     31      
     32    /** @type OpenLayers.Size */ 
     33    tileSize_startScale: null,  
     34     
     35    /** @type OpenLayers.Size */ 
     36    tileSize_scale: null, 
     37  
     38    /** @type float */ 
     39    resolution_scale: null, 
     40 
    2641    initialize: function() { 
    2742        OpenLayers.Control.PanZoom.prototype.initialize.apply(this, arguments); 
    2843        this.position = new OpenLayers.Pixel(OpenLayers.Control.PanZoomBar.X, 
    2944                                             OpenLayers.Control.PanZoomBar.Y); 
     45        this.tileSize_scale = new OpenLayers.Size(); 
    3046    }, 
    3147 
     
    169185        this.div.style.cursor = "move"; 
    170186        OpenLayers.Event.stop(evt); 
     187 
     188        // only for WMS layer 
     189        if (this.map.baseLayer.CLASS_NAME == "OpenLayers.Layer.WMS") { 
     190            // get current zoomlevel 
     191            this.zoomlevel_startScale = this.map.zoom; 
     192 
     193            // get original tile size 
     194            this.tileSize_startScale = this.map.getTileSize(); 
     195 
     196            // find and set tile, which contains the center of the viewport 
     197            this.map.baseLayer.setCenterTile();        
     198 
     199            // set zoomOutTile visible   
     200            this.map.baseLayer.zoomOutTile.imgDiv.style.display = "block"; 
     201        } 
    171202    }, 
    172203     
     
    188219            this.mouseDragStart = evt.xy.clone(); 
    189220            OpenLayers.Event.stop(evt); 
     221 
     222            // only for WMS layer 
     223            if (this.map.baseLayer.CLASS_NAME == "OpenLayers.Layer.WMS") { 
     224 
     225                // convert current sliderposition to new zoomlevel 
     226                var deltaY_zoomlevel = this.zoomStart.y - evt.xy.y 
     227                this.zoomlevel_scale=this.zoomlevel_startScale +  
     228                  deltaY_zoomlevel/this.zoomStopHeight; 
     229                
     230                // check the zoomlevel validity  
     231                if (this.zoomlevel_scale < 0) { 
     232                    this.zoomlevel_scale = 0;  
     233                }  
     234                if (this.zoomlevel_scale > (this.map.getNumZoomLevels()-1)) {  
     235                    this.zoomlevel_scale = this.map.getNumZoomLevels() - 1;  
     236                } 
     237                 
     238                // calculate the new tile size for the scale... 
     239                // ...zoomIn 
     240                if (this.zoomlevel_startScale < this.zoomlevel_scale) { 
     241                    this.tileSize_scale.w =  
     242                      Math.pow(2,(this.zoomlevel_scale - 
     243                      this.zoomlevel_startScale)) * this.tileSize_startScale.w; 
     244                    this.tileSize_scale.h =  
     245                      Math.pow(2,(this.zoomlevel_scale - 
     246                      this.zoomlevel_startScale)) * this.tileSize_startScale.h; 
     247 
     248                    // set new map resolution 
     249                    this.resolution_scale =  
     250                      1/(Math.pow(2,(this.zoomlevel_scale -  
     251                      this.zoomlevel_startScale))) *  this.map.getResolution();     
     252                } 
     253                // ...zoomOut / no zoom changes 
     254                if (this.zoomlevel_startScale >= this.zoomlevel_scale) {              
     255                    this.tileSize_scale.w = 
     256                      1/(Math.pow(2,(this.zoomlevel_startScale - 
     257                      this.zoomlevel_scale))) * this.tileSize_startScale.w; 
     258                    this.tileSize_scale.h = 
     259                      1/(Math.pow(2,(this.zoomlevel_startScale -  
     260                      this.zoomlevel_scale))) * this.tileSize_startScale.h; 
     261 
     262                    // set new map resolution 
     263                    this.resolution_scale =  
     264                      Math.pow(2,(this.zoomlevel_startScale -  
     265                      this.zoomlevel_scale)) * this.map.getResolution();  
     266                } 
     267 
     268 
     269                // scale all tiles to the new scaled size            
     270                this.map.baseLayer.scaleTileTo(this.tileSize_scale); 
     271                 
     272                // scale the zoomOut tile (to prevent a white frame during 
     273                // zoomOut) 
     274                this.map.baseLayer.scaleZoomOutTile(this.tileSize_scale, 
     275                                                    this.zoomlevel_scale,  
     276                                                    this.resolution_scale); 
     277 
     278                // set new scaled map resolution 
     279                this.map.setScaleResolution(this.resolution_scale); 
     280            } 
    190281        } 
    191282    }, 
     
    207298            this.mouseDragStart = null; 
    208299            OpenLayers.Event.stop(evt); 
     300     
     301            // only for WMS layer 
     302            if (this.map.baseLayer.CLASS_NAME == "OpenLayers.Layer.WMS") { 
     303                // set zoomOutTile invisible  
     304                this.map.baseLayer.zoomOutTile.imgDiv.style.display = "none"; 
     305            } 
    209306        } 
    210307    }, 
  • sandbox/emanuel/animatedZooming/lib/lib/OpenLayers/Layer/Grid.js

    r2091 r2212  
    2323    /** @type Integer */ 
    2424    buffer: 2, 
     25 
     26    /** @type OpenLayers.Tile */ 
     27    centerTile: null, 
     28 
     29    /** @type Integer */ 
     30    centerRow: null, 
     31 
     32    /** @type Integer */ 
     33    centerCol: null, 
     34 
     35    /** @type OpenLayers.Pixel */    
     36    centerTileStartScalePos: null, 
     37 
     38    /** @type OpenLayers.Tile */ 
     39    zoomOutTile: null, 
     40 
     41    layerClone: null, 
     42     
    2543 
    2644    /** 
     
    3654                                                                arguments); 
    3755        this.grid = new Array(); 
     56        this.layerClone = new Array(); 
    3857    }, 
    3958 
     
    140159    }, 
    141160     
     161 
     162    /**  
     163     * Finds and sets the tile, which contains the center  
     164     * of the viewport. 
     165     * 
     166     */ 
     167    setCenterTile:function() { 
     168         
     169        //get the center of the map (viewport) 
     170        var center = this.map.getCenter(); 
     171 
     172        //find tile, which contains the center of the viewport 
     173        this.centerTile = null; 
     174        if (this.grid) { 
     175            for (var iRow=0; iRow < this.grid.length; iRow++) { 
     176                for (var iCol=0; iCol < this.grid[iRow].length; iCol++) { 
     177                    var tileBounds = this.grid[iRow][iCol].bounds; 
     178                    if (tileBounds.containsLonLat(center, true)) { 
     179                        this.centerTile = this.grid[iRow][iCol]; 
     180                        this.centerRow = iRow; 
     181                        this.centerCol = iCol; 
     182                        break;  
     183                    } 
     184                } 
     185                if (this.centerTile) 
     186                    break; 
     187            } 
     188        } 
     189         
     190        //define real position of the center tile before scaling 
     191        //(It is tricky if you pan the map before scaling. Then the 
     192        //layerContainerDiv is only slided and you have to add this 
     193        //difference to the tile position) 
     194        var x = parseInt(this.centerTile.imgDiv.style.left)  
     195          + parseInt(this.map.layerContainerDiv.style.left); 
     196        var y = parseInt(this.centerTile.imgDiv.style.top) 
     197          + parseInt(this.map.layerContainerDiv.style.top); 
     198 
     199        this.centerTileStartScalePos = new OpenLayers.Pixel(x,y); 
     200 
     201 
     202        this.grid_visible = []; 
     203        var extent = this.map.getExtent(); 
     204 
     205        // find all visible tiles of each active layers 
     206        for (var i = 0; i < this.map.layers.length; i++) { 
     207            // find only active layers 
     208            if (this.map.layers[i].visibility) { 
     209                var layerGrid = this.map.layers[i].grid; 
     210                 
     211                this.layerClone[i] = this.map.layers[i].clone(); 
     212                // go through the whole grid of every layer 
     213                for (var iRow=0; iRow < layerGrid.length; iRow++) { 
     214                    for (var iCol=0; iCol < layerGrid[iRow].length; iCol++) {  
     215                        var tile = layerGrid[iRow][iCol]; 
     216                        if (tile.imgDiv){ 
     217                            // if tile outside the viewport, set it 
     218                            // invisible  
     219                            if (extent.containsBounds(tile.bounds,true, false)) { 
     220                                tile.imgDiv.style.display = "block"; 
     221                            } 
     222                        } 
     223                    } 
     224                }  
     225            } 
     226        }             
     227 
     228    }, 
     229 
     230 
     231    /** 
     232     * Scales all tiles of each active layer to the new tile size  
     233     * (changes size and position of every tile). 
     234     *  
     235     * @param {OpenLayers.Size} newTileSize 
     236     */ 
     237    scaleTileTo:function(newTileSize) { 
     238         
     239        //convert the current center of the map in pixel 
     240        var centerPx = this.map.getPixelFromLonLat(this.map.getCenter()); 
     241 
     242        //reposition the center tile  
     243        if (this.centerTile ) { 
     244            //set new left position 
     245            var leftOffset = newTileSize.w / this.centerTile.size.w 
     246              * ( centerPx.x -  this.centerTileStartScalePos.x); 
     247            leftOffset = Math.round(leftOffset); 
     248            var newLeftPos = centerPx.x - leftOffset 
     249              - parseInt(this.map.layerContainerDiv.style.left); 
     250            this.centerTile.imgDiv.style.left = newLeftPos + "px"; 
     251 
     252            //set new top position 
     253            var topOffset = newTileSize.h / this.centerTile.size.h 
     254              * (centerPx.y - this.centerTileStartScalePos.y); 
     255            topOffset = Math.round(topOffset); 
     256            var newTopPos = centerPx.y - topOffset  
     257              - parseInt(this.map.layerContainerDiv.style.top); 
     258            this.centerTile.imgDiv.style.top = newTopPos + "px"; 
     259        } 
     260         
     261        // reposition and resize all _visible_ tiles of all active layers 
     262        for (var i = 0; i < this.map.layers.length; i++) { 
     263            // find only active layers 
     264            if (this.map.layers[i].visibility) { 
     265                var layerGrid = this.map.layers[i].grid; 
     266 
     267                // go through the whole grid of every layer 
     268                for (var iRow=0; iRow < layerGrid.length; iRow++) { 
     269                    for (var iCol=0; iCol < layerGrid[iRow].length; iCol++) {                          
     270                        var tile = layerGrid[iRow][iCol]; 
     271                        if (tile.imgDiv) { 
     272                            if (tile.imgDiv.style.display == "block") { 
     273                                //define the factors for rows and columns 
     274                                //(relates to the center tile) 
     275                                var deltaRow = iRow - this.centerRow; 
     276                                var deltaCol = iCol - this.centerCol; 
     277                                 
     278                                //set new left position 
     279                                var newLeftPos = parseInt(this.centerTile.imgDiv.style.left) 
     280                                  + deltaCol * newTileSize.w ; 
     281                                tile.imgDiv.style.left = newLeftPos + "px"; 
     282 
     283                                //set new top position 
     284                                var newTopPos = parseInt(this.centerTile.imgDiv.style.top) 
     285                                  + deltaRow * newTileSize.h ; 
     286                                tile.imgDiv.style.top = newTopPos + "px"; 
     287 
     288                                //set new width and height 
     289                                tile.imgDiv.style.width = newTileSize.w + "px"; 
     290                                tile.imgDiv.style.height = newTileSize.h + "px"; 
     291                            } 
     292                        } 
     293                    } 
     294                } 
     295            } 
     296        } 
     297    }, 
     298 
     299     
     300    /** 
     301     * Returns the only tile of zoomlevel 0. 
     302     * 
     303     * @returns the tile of zoomlevel 0 
     304     * @type OpenLayers.Tile 
     305     */ 
     306    getZoomOutTile:function() { 
     307        var bounds = this.map.getMaxExtent(); 
     308        var resolution = this.map.getMaxResolution(); 
     309 
     310        var tilelon = resolution * this.tileSize.w; 
     311        var tilelat = resolution * this.tileSize.h; 
     312         
     313        var tileoffsetlon = bounds.left; 
     314        var tileoffsetlat = bounds.bottom; 
     315         
     316        var tileBounds = new OpenLayers.Bounds(tileoffsetlon,  
     317                                               tileoffsetlat,  
     318                                               tileoffsetlon + tilelon, 
     319                                               tileoffsetlat + tilelat); 
     320 
     321        this.zoomOutTile = this.addTile(tileBounds, null); 
     322        return this.zoomOutTile; 
     323    }, 
     324 
     325 
     326    /** 
     327     * Initializes zoomOut tile and sets a bigger tile size. 
     328     * 
     329     */ 
     330    setZoomOutTile:function() { 
     331        //get zoomOut tile 
     332        this.zoomOutTile = this.getZoomOutTile(); 
     333 
     334        // draw tile -> sets imgDiv 
     335        this.zoomOutTile.draw(); 
     336 
     337        // hid tile until the user drags the zoombar 
     338        this.zoomOutTile.imgDiv.style.width = 0; 
     339        this.zoomOutTile.imgDiv.style.height = 0; 
     340 
     341        // set a bigger tile size for zoomOutTile (change WMS url in imgDiv) 
     342        var newTileSize = new OpenLayers.Size(this.tileSize.w*4, this.tileSize.h * 4);         
     343        var wmsUrl = this.zoomOutTile.imgDiv.src; 
     344        var sizeStr_old = "WIDTH="+this.tileSize.w+"&HEIGHT="+this.tileSize.h; 
     345        var sizeStr_new = "WIDTH="+newTileSize.w+"&HEIGHT="+newTileSize.h; 
     346 
     347        wmsUrl = url.replace(sizeStr_old,sizeStr_new);  
     348        this.zoomOutTile.imgDiv.src = wmsUrl; 
     349    }, 
     350 
     351 
     352    /** 
     353     * Reposition and resize the zoomOut tile to prevent a white frame 
     354     * during zoomOut. 
     355     * 
     356     * @param {OpenLayers.Size} newTileSize 
     357     * @param {float} newZoomlevel 
     358     * @param {float} newResolution 
     359     */ 
     360    scaleZoomOutTile: function(newTileSize, newZoomlevel, newResolution) { 
     361 
     362        //convert the current center of the map in pixel 
     363        var centerPx = this.map.getPixelFromLonLat(this.map.getCenter()); 
     364    
     365        //set new size (width and height) 
     366        this.zoomOutTile.imgDiv.style.width = this.map.tileSize.w  
     367          * Math.pow(2, newZoomlevel); 
     368        this.zoomOutTile.imgDiv.style.height = this.map.tileSize.h  
     369          * Math.pow(2, newZoomlevel); 
     370 
     371        //set new position (top and left) 
     372        var bounds = this.zoomOutTile.bounds; 
     373        var extent = this.map.getExtent(); 
     374        var maxExtent = this.map.getMaxExtent(); 
     375 
     376        var resolution = this.map.getMaxResolution(); 
     377        var centerLonLat = extent.getCenterLonLat(); 
     378          
     379        if( bounds.containsLonLat(centerLonLat) 
     380            && !(extent.equals(maxExtent))) { 
     381            var offsetlon = bounds.left - centerLonLat.lon; 
     382            var offsetlat = -bounds.top + centerLonLat.lat; 
     383             
     384            var offsetx = offsetlon / newResolution 
     385              - parseInt(this.map.layerContainerDiv.style.left); 
     386            var offsety = offsetlat / newResolution 
     387              - parseInt(this.map.layerContainerDiv.style.top); 
     388             
     389            this.zoomOutTile.imgDiv.style.left = centerPx.x + offsetx;   
     390            this.zoomOutTile.imgDiv.style.top = centerPx.y + offsety; 
     391        } 
     392    }, 
     393     
     394     
    142395    /** 
    143396     * @private 
  • sandbox/emanuel/animatedZooming/lib/lib/OpenLayers/Map.js

    r2167 r2212  
    301301                // set the first baselaye we add as the baselayer 
    302302                this.setBaseLayer(layer); 
     303 
     304                // only for WMS layer: preload a bigger tile for zooming out 
     305                if (layer.CLASS_NAME == "OpenLayers.Layer.WMS") { 
     306                    layer.setZoomOutTile(); 
     307                } 
     308               
    303309            } else { 
    304310                layer.setVisibility(false); 
     
    709715        } 
    710716         
    711         var zoomChanged = (this.isValidZoomLevel(zoom)) &&  
    712                           (zoom != this.getZoom()); 
     717        var zoomChanged; 
     718        //if zoomlevel not changed -> is at least one overlay active? 
     719        //The active (temporarily invisible) overlays will not 
     720        //reposition after scaling to the _same_ zoomlevel.  
     721        //So, it it is necessary to redraw the grid by calling this  
     722        //setCenter function. 
     723        if (zoom == this.getZoom()) 
     724        { 
     725            for (var i = 0; i < this.layers.length; i++) { 
     726                var layer = this.layers[i]; 
     727                if (!layer.isBaseLayer) { 
     728                    if (layer.getVisibility()) 
     729                        zoomChanged = true; 
     730                } 
     731            } 
     732        } 
     733        else { //zoomlevel changed 
     734            zoomChanged = true; 
     735        } 
     736         
     737        zoomChanged = (this.isValidZoomLevel(zoom) && zoomChanged); 
    713738 
    714739        var centerChanged = (this.isValidLonLat(lonlat)) &&  
     
    835860    }, 
    836861 
     862 
     863    /** Sets the map resolution during zooming/scaling. 
     864     *  
     865     * @param {float} newResolution 
     866     */  
     867    setScaleResolution: function (newResolution) { 
     868        //calculate the current bounds of the new viewport 
     869        var newScaleBounds = this.calculateBounds(this.getCenter(), newResolution); 
     870         
     871        //update the overviewMap 
     872        for (i=0; i<this.controls.length; i++) { 
     873            if (this.controls[i].CLASS_NAME == "OpenLayers.Control.OverviewMap")  
     874                this.controls[i].updateOverview(newResolution, newScaleBounds); 
     875        }           
     876    }, 
     877 
     878 
     879 
    837880  /********************************************************/ 
    838881  /*                                                      */