OpenLayers OpenLayers

Changeset 5778

Show
Ignore:
Timestamp:
01/16/08 14:50:32 (1 year ago)
Author:
ianmayo
Message:

Include Andrew's updated WorldWind file

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • sandbox/ianmayo/branches/app2/openlayers/lib/OpenLayers/Layer/WorldWind.js

    r5614 r5778  
    1 /* Copyright (c) 2006-2008 MetaCarta, Inc., published under the Clear BSD 
    2  * license.  See http://svn.openlayers.org/trunk/openlayers/license.txt for the 
    3  * full text of the license. */ 
    4  
     1/* Copyright (c) 2006-2007 MetaCarta, Inc., published under the BSD license. 
     2 * See http://svn.openlayers.org/trunk/openlayers/release-license.txt  
     3 * for the full text of the license. */ 
     4 
     5/* 
     6Modified 18th Nov, 2007. Added support for local tile caches. Andrew Larcombe. 
     7*/ 
    58 
    69/** 
    710 * @requires OpenLayers/Layer/Grid.js 
    8  */ 
    9  
    10 /** 
     11 * 
    1112 * Class: OpenLayers.Layer.WorldWind 
    1213 *  
     
    3738     */ 
    3839    zoomLevels: null, 
     40 
     41     
     42    /** 
     43    * APIProperty: cacheDir 
     44    * An array of directory names, one for each level for those that have local caches 
     45    */ 
     46    cacheDir: null, 
    3947     
    4048    /** 
     
    4755     * zoomLevels - {Int} number of zoom levels 
    4856     * params - {Object} additional parameters 
    49      * options - {Object} additional options 
     57     * options - {Object} additional options. 
     58     * 
     59     * If the option localCache is set, or the url for the layer is of a file schema (ie starts file://) 
     60     * then intialize will use this as the base directory for a cached set of worldwind data. Level n data should 
     61     * be present in a subdirectory named n. eg if the url of the layer is set to file:///home/wwcache/ then worldwind 
     62     * level 1 data is expected to be found in the directory /home/wwcache/1. 
     63     * If running on Internet Explorer or Firefox then this function tests for the presence 
     64     * of the file 0000/0000_0000.jpg to confirm the presence of the cache files. 
    5065     */ 
    5166    initialize: function(name, url, lzd, zoomLevels, params, options) { 
    52         this.lzd = lzd; 
     67     
     68        this.cacheDir = new Array(); 
     69     
     70        if((options != undefined && options.localCache) || (url.substr(0,7) == 'file://')) { 
     71         
     72            var fso = false; 
     73            var mozFile = false; 
     74 
     75            if(url.substr(0,7) != 'file://') { 
     76                 
     77                try { 
     78                    fso = new ActiveXObject("Scripting.FileSystemObject"); 
     79                } 
     80                catch(ex) { 
     81                } 
     82             
     83                try { 
     84                    netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect"); 
     85                    var mozFile = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile); 
     86                } 
     87                catch(ex) { 
     88                } 
     89                 
     90            } 
     91             
     92            var base; 
     93            if(url.substr(0,7) == 'file://') { 
     94                base = url.substring(7); 
     95            }  
     96            else { 
     97                base = options.localCache; 
     98            } 
     99         
     100            var dir; 
     101            var zeroFileExists; 
     102            var zeroFilePath; 
     103         
     104            for(i = 0; i <= zoomLevels; i++ ) { 
     105                dir = base + "/" + i; 
     106                zeroFilePath = dir + "/0000/0000_0000.jpg"; 
     107                zeroFileExists = false; 
     108             
     109                if(mozFile) { 
     110                    try { 
     111                        mozFile.initWithPath(zeroFilePath); 
     112                        zeroFileExists = mozFile.exists(); 
     113                    } 
     114                    catch(ex) { 
     115                    } 
     116                } 
     117                else if(fso) { 
     118                    zeroFileExists = fso.FileExists(zeroFilePath); 
     119                } 
     120                else { 
     121                    zeroFileExists = true; 
     122                } 
     123 
     124                if (zeroFileExists) { 
     125                    this.cacheDir[i] = dir; 
     126                } 
     127            } 
     128             
     129             
     130        } 
     131         
     132        this.lzd = lzd; 
    53133        this.zoomLevels = zoomLevels; 
    54134        var newArguments = []; 
     
    78158        var zoom = this.map.getZoom(); 
    79159        var extent = this.map.getMaxExtent(); 
    80         zoom = zoom - Math.log(this.maxResolution / (this.lzd/512))/Math.log(2); 
     160/*        zoom = zoom - Math.log(this.maxResolution / (this.lzd/512))/Math.log(2);*/ 
     161        zoom = zoom - Math.log(this.maxResolution / (this.lzd/Math.min(this.tileSize.w,512)))/Math.log(2); 
    81162        return zoom; 
    82163    }, 
     
    97178        var zoom = this.getZoom(); 
    98179        var extent = this.map.getMaxExtent(); 
    99         var deg = this.lzd/Math.pow(2,this.getZoom()); 
     180        var deg = this.lzd/Math.pow(2,Math.min(zoom,this.zoomLevels)); 
    100181        var x = Math.floor((bounds.left - extent.left)/deg); 
    101182        var y = Math.floor((bounds.bottom - extent.bottom)/deg); 
    102         if (this.map.getResolution() <= (this.lzd/512) 
    103             && this.getZoom() <= this.zoomLevels) { 
    104             return this.getFullRequestString( 
    105               { L: zoom,  
     183        //if (this.map.getResolution() <= (this.lzd/512) && this.getZoom() <= this.zoomLevels) { 
     184        //if (this.map.getResolution() <= (this.lzd/this.tileSize.w) && zoom <= this.zoomLevels) { 
     185            return this.getURLForTile(zoom,x,y); 
     186        //} else { 
     187        //    return OpenLayers.Util.getImagesLocation() + "blank.gif"; 
     188        //} 
     189 
     190    }, 
     191 
     192 
     193    /** 
     194    * Method: getURLForTile 
     195    * 
     196    * Parameters: 
     197    * z - {Integer} zoom level 
     198    * x - {Integer} x 
     199    * y - {Integer} y 
     200    * 
     201    * 
     202    * Returns: 
     203    * {String} A string containing the URL of the tile depending on whether the tile 
     204    * should be retrieved from a local cache or a remote server. If this.cachedir[z] is 
     205    * defined then the function will return the URI to the image file in the local cache, 
     206    * else it'll return the result of a call to getFullRequestString 
     207    * 
     208    */ 
     209    getURLForTile: function(z,x,y) { 
     210         
     211        if(this.map.getResolution() > (this.lzd/this.tileSize.w)) { 
     212            if(this.adjustTileSizeAndRedraw(512 / (2*(this.getZoomForResolution(this.lzd/512) - this.map.getZoom())))) { 
     213                return;  
     214            } 
     215        } 
     216        else if(this.tileSize.w < 512 && (this.map.getResolution() <= (this.lzd/512)) ) { 
     217            if(this.adjustTileSizeAndRedraw(512)) { 
     218                return; 
     219            } 
     220        } 
     221        else if(this.tileSize.w > 512 && z <= this.zoomLevels) { 
     222            if(this.adjustTileSizeAndRedraw(512)) { 
     223                return; 
     224            } 
     225        } 
     226        else if(this.getZoom() > this.zoomLevels) { 
     227            if(this.adjustTileSizeAndRedraw(512 * (2*(this.getZoom() - this.zoomLevels)))) { 
     228                return; 
     229            } 
     230        } 
     231         
     232        z = Math.min(z,this.zoomLevels); 
     233         
     234        if(this.cacheDir[z] != undefined) { 
     235            x = "0000" + x; 
     236            y = "0000" + y; 
     237            return this.cacheDir[z]+"/"+y.substr(y.length-4)+"/"+y.substr(y.length-4)+"_"+x.substr(x.length-4)+".jpg"; 
     238        } 
     239        else { 
     240            return this.getFullRequestString( 
     241              { L: z,  
    106242                X: x, 
    107243                Y: y 
    108244              }); 
    109         } else { 
    110             return OpenLayers.Util.getImagesLocation() + "blank.gif"; 
    111         } 
    112  
    113     }, 
     245        } 
     246         
     247    }, 
     248     
     249    adjustTileSizeAndRedraw: function(tileSize) { 
     250        if(this.tileSize.w != tileSize)  { 
     251            this.clearGrid(); 
     252            this.tileSize = new OpenLayers.Size(tileSize,tileSize); 
     253            this.redraw(); 
     254            return true; 
     255        } 
     256        return false; 
     257    }, 
     258 
    114259 
    115260    CLASS_NAME: "OpenLayers.Layer.WorldWind" 
    116261}); 
     262