OpenLayers OpenLayers

Changeset 4381

Show
Ignore:
Timestamp:
09/18/07 20:23:26 (1 year ago)
Author:
tschaub
Message:

New behavior for layer.getZoomForResolution. This method now returns the index of the resolution closest to the passed in resolution - making for fewer unwanted resolution changes, and a generally happier populace (see #990).

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/openlayers/lib/OpenLayers/Layer.js

    r4231 r4381  
    737737     *     that still contains the passed-in extent. We do this by calculating 
    738738     *     the ideal resolution for the given exteng (based on the map size) 
    739      *     and then find the smallest resolution that is greater than this 
    740      *     ideal resolution. 
     739     *     and then find the closest resolution to this ideal resolution. 
    741740     */ 
    742741    getZoomForExtent: function(extent) { 
     
    762761    /** 
    763762     * APIMethod: getZoomForResolution 
    764      *  
    765      * Parameters: 
    766      * resolution - {Float} 
     763     * Get the index for the closest resolution in the layers resolutions array. 
     764     *  
     765     * Parameters: 
     766     * resolution - {Float} Map units per pixel. 
    767767     *  
    768768     * Returns: 
    769769     * {Integer} The index of the zoomLevel (entry in the resolutions array)  
    770      *     that is the smallest resolution that is greater than the passed-in 
    771      *     resolution. 
     770     *     that is the closest to the passed-in resolution. 
    772771     */ 
    773772    getZoomForResolution: function(resolution) { 
    774          
    775         for(var i=1; i < this.resolutions.length; i++) { 
    776             if ( this.resolutions[i] < resolution) { 
     773        var zoom, diff; 
     774        var minDiff = Number.POSITIVE_INFINITY; 
     775        for(var i=0; i < this.resolutions.length; i++) { 
     776            diff = Math.abs(this.resolutions[i] - resolution); 
     777            if(diff < minDiff) { 
     778                zoom = i; 
     779                minDiff = diff; 
     780            } else if(diff > minDiff) { 
    777781                break; 
    778782            } 
    779783        } 
    780         return (i - 1)
     784        return zoom
    781785    }, 
    782786     
  • trunk/openlayers/tests/Control/test_OverviewMap.html

    r4337 r4381  
    4444        t.eq(overviewCenter.lon, -71, "Overviewmap center lon correct"); 
    4545        t.eq(overviewCenter.lat, 42, "Overviewmap center lat correct"); 
    46         t.eq(overviewZoom, 8, "Overviewmap zoomcorrect"); 
     46        t.eq(overviewZoom, 9, "Overviewmap zoomcorrect"); 
    4747         
    4848        control.mapDivClick({'xy':new OpenLayers.Pixel(5,5)}); 
    4949         
     50        // There are box model issues that keep browsers from giving us 
     51        // identical results here.  Test the normalized difference against 
     52        // a tolerance instead of testing equality. 
     53        function normalizedDiff(a, b) { 
     54            return Math.abs((a - b) / (a + b)); 
     55        } 
     56        var tolerance = 1e-4; 
     57         
    5058        var cent = map.getCenter(); 
    51         t.eq(cent.lon, -71.3515625, "Clicking on the Overview Map has the correct effect on map lon"); 
    52         t.eq(cent.lat, 42.17578125, "Clicking on the Overview Map has the correct effect on map lat"); 
     59        t.ok(normalizedDiff(cent.lon, -71.00390625) < tolerance, 
     60             "Clicking on the Overview Map has the correct effect on map lon"); 
     61        t.ok(normalizedDiff(cent.lat, 42.00390625) < tolerance, 
     62             "Clicking on the Overview Map has the correct effect on map lat"); 
    5363 
    5464        control.rectMouseDown({'xy':new OpenLayers.Pixel(5,5), 'which':1}); 
     
    5767         
    5868        var cent = map.getCenter(); 
    59         t.eq(cent.lon, -71.2734375, "Dragging on the Overview Map has the correct effect on map lon"); 
    60         t.eq(cent.lat, 42.09765625, "Dragging on the Overview Map has the correct effect on map lat"); 
     69        t.ok(normalizedDiff(cent.lon, -70.83984375) < tolerance, 
     70             "Dragging on the Overview Map has the correct effect on map lon"); 
     71        t.ok(normalizedDiff(cent.lat, 41.84765625) < tolerance, 
     72             "Dragging on the Overview Map has the correct effect on map lat"); 
    6173         
    6274        map.setCenter(new OpenLayers.LonLat(0,0), 0); 
  • trunk/openlayers/tests/Layer/test_Grid.html

    r4356 r4381  
    151151        zoom = layer.getZoomForExtent(bounds); 
    152152 
    153         t.eq( zoom, 8, "getZoomForExtent() returns correct value"); 
     153        /** 
     154         * ideal resolution: 2 map units / 500px = 0.004 
     155         * layer.resolutions = [1.40625, 0.703125, 0.3515625, 0.17578125, 
     156         *                      0.087890625, 0.0439453125, 0.02197265625, 
     157         *                      0.010986328125, 0.0054931640625, 0.00274658203125, 
     158         *                      0.001373291015625, 0.0006866455078125, 0.00034332275390625, 
     159         *                      0.000171661376953125, 0.0000858306884765625, 0.00004291534423828125] 
     160         * 
     161         * So, we expect a zoom of 9 because it is the closest resolution. 
     162         */ 
     163        t.eq( zoom, 9, "getZoomForExtent() returns correct value"); 
    154164 
    155165        bounds = new OpenLayers.Bounds(10,10,100,100); 
    156166        zoom = layer.getZoomForExtent(bounds); 
    157167 
    158         t.eq( zoom, 2, "getZoomForExtent() returns correct value"); 
     168        /** 
     169         * ideal resolution: 90 map units / 500px = 0.18 
     170         * So, we expect a zoom of 3 because it is the closest. 
     171         */ 
     172        t.eq( zoom, 3, "getZoomForExtent() returns correct value"); 
    159173    }    
    160174     
     
    542556        map.zoomIn(); 
    543557        var bounds = layer.getTileBounds(new OpenLayers.Pixel(200,200)); 
    544         t.eq(bounds.toBBOX(), "-180,-90,0,90", "get tile bounds returns correct bounds");  
     558        t.eq(bounds.toBBOX(), "-90,0,0,90", "get tile bounds returns correct bounds");  
    545559        map.pan(200,0); 
    546560        var bounds = layer.getTileBounds(new OpenLayers.Pixel(200,200)); 
    547         t.eq(bounds.toBBOX(), "0,-90,180,90", "get tile bounds returns correct bounds after pan");  
     561        t.eq(bounds.toBBOX(), "0,0,90,90", "get tile bounds returns correct bounds after pan");  
    548562    } 
    549563 
  • trunk/openlayers/tests/Layer/test_KaMap.html

    r4317 r4381  
    129129        zoom = layer.getZoomForExtent(bounds); 
    130130 
    131         t.eq( zoom, 8, "getZoomForExtent() returns correct value"); 
     131        /** 
     132         * ideal resolution: 2 map units / 500px = 0.004 
     133         * layer.resolutions = [1.40625, 0.703125, 0.3515625, 0.17578125, 
     134         *                      0.087890625, 0.0439453125, 0.02197265625, 
     135         *                      0.010986328125, 0.0054931640625, 0.00274658203125, 
     136         *                      0.001373291015625, 0.0006866455078125, 0.00034332275390625, 
     137         *                      0.000171661376953125, 0.0000858306884765625, 0.00004291534423828125] 
     138         * 
     139         * So, we expect a zoom of 9 because it is the closest resolution. 
     140         */ 
     141        t.eq( zoom, 9, "getZoomForExtent() returns correct value"); 
    132142 
    133143        bounds = new OpenLayers.Bounds(10,10,100,100); 
    134144        zoom = layer.getZoomForExtent(bounds); 
    135145 
    136         t.eq( zoom, 2, "getZoomForExtent() returns correct value"); 
     146        /** 
     147         * ideal resolution: 90 map units / 500px = 0.18 
     148         * So, we expect a zoom of 3 because it is the closest. 
     149         */ 
     150        t.eq( zoom, 3, "getZoomForExtent() returns correct value"); 
    137151    }    
    138152     
     
    237251        map.zoomIn(); 
    238252        var bounds = layer.getTileBounds(new OpenLayers.Pixel(200,200)); 
    239         t.eq(bounds.toBBOX(), "-180,0,0,180", "get tile bounds returns correct bounds");  
     253        t.eq(bounds.toBBOX(), "-90,0,0,90", "get tile bounds returns correct bounds");  
    240254        map.pan(200,0); 
    241255        var bounds = layer.getTileBounds(new OpenLayers.Pixel(200,200)); 
    242         t.eq(bounds.toBBOX(), "0,0,180,180", "get tile bounds returns correct bounds after pan");  
     256        t.eq(bounds.toBBOX(), "0,0,90,90", "get tile bounds returns correct bounds after pan");  
    243257    } 
    244258 
  • trunk/openlayers/tests/Layer/test_TMS.html

    r4059 r4381  
    7878        bounds = new OpenLayers.Bounds(10,10,12,12); 
    7979        zoom = layer.getZoomForExtent(bounds); 
    80  
    81         t.eq( zoom, 8, "getZoomForExtent() returns correct value"); 
     80        /** 
     81         * ideal resolution: 2 map units / 500px = 0.004 
     82         * layer.resolutions = [1.40625, 0.703125, 0.3515625, 0.17578125, 
     83         *                      0.087890625, 0.0439453125, 0.02197265625, 
     84         *                      0.010986328125, 0.0054931640625, 0.00274658203125, 
     85         *                      0.001373291015625, 0.0006866455078125, 0.00034332275390625, 
     86         *                      0.000171661376953125, 0.0000858306884765625, 0.00004291534423828125] 
     87         * 
     88         * So, we expect a zoom of 9 because it is the closest resolution. 
     89         */ 
     90         
     91        t.eq( zoom, 9, "getZoomForExtent() returns correct value"); 
    8292 
    8393        bounds = new OpenLayers.Bounds(10,10,100,100); 
    8494        zoom = layer.getZoomForExtent(bounds); 
    85  
    86         t.eq( zoom, 2, "getZoomForExtent() returns correct value"); 
     95        /** 
     96         * ideal resolution: 90 map units / 500px = 0.18 
     97         * So, we expect a zoom of 3 because it is the closest. 
     98         */ 
     99        t.eq( zoom, 3, "getZoomForExtent() returns correct value"); 
    87100    }    
    88101 
  • trunk/openlayers/tests/Layer/test_TileCache.html

    r4059 r4381  
    7979        bounds = new OpenLayers.Bounds(10,10,12,12); 
    8080        zoom = layer.getZoomForExtent(bounds); 
    81  
    82         t.eq( zoom, 7, "getZoomForExtent() returns correct value"); 
     81        /** 
     82         * ideal resolution: 2 map units / 500px = 0.004 
     83         * layer.resolutions = [0.703125, 0.3515625, 0.17578125, 
     84         *                      0.087890625, 0.0439453125, 0.02197265625, 
     85         *                      0.010986328125, 0.0054931640625, 0.00274658203125, 
     86         *                      0.001373291015625, 0.0006866455078125, 0.00034332275390625, 
     87         *                      0.000171661376953125, 0.0000858306884765625, 0.00004291534423828125, 
     88         *                      0.000021457672119140625] 
     89         * 
     90         * So, we expect a zoom of 8 because it is the closest resolution. 
     91         */ 
     92        t.eq( zoom, 8, "getZoomForExtent() returns correct value"); 
    8393 
    8494        bounds = new OpenLayers.Bounds(10,10,100,100); 
    8595        zoom = layer.getZoomForExtent(bounds); 
     96        /** 
     97         * ideal resolution: 90 map units / 500px = 0.18 
     98         * So, we expect a zoom of 2 because it is the closest. 
     99         */ 
    86100 
    87         t.eq( zoom, 1, "getZoomForExtent() returns correct value"); 
     101        t.eq( zoom, 2, "getZoomForExtent() returns correct value"); 
    88102    }    
    89103 
  • trunk/openlayers/tests/Layer/test_WrapDateLine.html

    r4335 r4381  
    132132        m.addLayer(layer); 
    133133        m.zoomToMaxExtent(); 
    134         t.eq(layer.grid[3][0].url, "http://octo.metacarta.com/cgi-bin/mapserv?MAP=%2Fmapdata%2Fvmap_wms.map&LAYERS=basic&FORMAT=image%2Fpng&SERVICE=WMS&VERSION=1.1.1&REQUEST=GetMap&STYLES=&EXCEPTIONS=application%2Fvnd.ogc.se_inimage&SRS=EPSG%3A4326&BBOX=0%2C-90%2C180%2C90&WIDTH=256&HEIGHT=256", "cell [3][0] is wrapped around the world."); 
    135         t.eq(layer.grid[0][0].url, "http://octo.metacarta.com/cgi-bin/mapserv?MAP=%2Fmapdata%2Fvmap_wms.map&LAYERS=basic&FORMAT=image%2Fpng&SERVICE=WMS&VERSION=1.1.1&REQUEST=GetMap&STYLES=&EXCEPTIONS=application%2Fvnd.ogc.se_inimage&SRS=EPSG%3A4326&BBOX=0%2C450%2C180%2C630&WIDTH=256&HEIGHT=256", "cell [0][0] is wrapped around the world lon, but not lat"); 
    136         t.eq(layer.grid[0][3].url, "http://octo.metacarta.com/cgi-bin/mapserv?MAP=%2Fmapdata%2Fvmap_wms.map&LAYERS=basic&FORMAT=image%2Fpng&SERVICE=WMS&VERSION=1.1.1&REQUEST=GetMap&STYLES=&EXCEPTIONS=application%2Fvnd.ogc.se_inimage&SRS=EPSG%3A4326&BBOX=-180%2C450%2C0%2C630&WIDTH=256&HEIGHT=256", "cell [3][0] is not wrapped at all."); 
     134        t.eq(layer.grid[3][0].url, "http://octo.metacarta.com/cgi-bin/mapserv?MAP=%2Fmapdata%2Fvmap_wms.map&LAYERS=basic&FORMAT=image%2Fpng&SERVICE=WMS&VERSION=1.1.1&REQUEST=GetMap&STYLES=&EXCEPTIONS=application%2Fvnd.ogc.se_inimage&SRS=EPSG%3A4326&BBOX=0%2C0%2C90%2C90&WIDTH=256&HEIGHT=256", "cell [3][0] is wrapped around the world."); 
     135        t.eq(layer.grid[0][0].url, "http://octo.metacarta.com/cgi-bin/mapserv?MAP=%2Fmapdata%2Fvmap_wms.map&LAYERS=basic&FORMAT=image%2Fpng&SERVICE=WMS&VERSION=1.1.1&REQUEST=GetMap&STYLES=&EXCEPTIONS=application%2Fvnd.ogc.se_inimage&SRS=EPSG%3A4326&BBOX=0%2C270%2C90%2C360&WIDTH=256&HEIGHT=256", "cell [0][0] is wrapped around the world lon, but not lat"); 
     136        t.eq(layer.grid[0][3].url, "http://octo.metacarta.com/cgi-bin/mapserv?MAP=%2Fmapdata%2Fvmap_wms.map&LAYERS=basic&FORMAT=image%2Fpng&SERVICE=WMS&VERSION=1.1.1&REQUEST=GetMap&STYLES=&EXCEPTIONS=application%2Fvnd.ogc.se_inimage&SRS=EPSG%3A4326&BBOX=-90%2C270%2C0%2C360&WIDTH=256&HEIGHT=256", "cell [3][0] is not wrapped at all."); 
    137137 
    138138    } 
     
    147147        m.addLayer(layer); 
    148148        m.zoomToMaxExtent(); 
    149         t.eq(layer.grid[0][0].url, "http://www.openlayers.org/world/index.php?g=satellite&map=world&i=jpeg&t=-1280&l=0&s=221471921.25", "grid[0][0] kamap is okay"); 
    150         t.eq(layer.grid[0][3].url, "http://www.openlayers.org/world/index.php?g=satellite&map=world&i=jpeg&t=-1280&l=-256&s=221471921.25", "grid[0][3] kamap is okay"); 
    151         t.eq(layer.grid[3][0].url, "http://www.openlayers.org/world/index.php?g=satellite&map=world&i=jpeg&t=-512&l=0&s=221471921.25", "grid[3][0] is okay"); 
     149        t.eq(layer.grid[0][0].url, "http://www.openlayers.org/world/index.php?g=satellite&map=world&i=jpeg&t=-1280&l=0&s=110735960.625", "grid[0][0] kamap is okay"); 
     150        t.eq(layer.grid[0][3].url, "http://www.openlayers.org/world/index.php?g=satellite&map=world&i=jpeg&t=-1280&l=-256&s=110735960.625", "grid[0][3] kamap is okay"); 
     151        t.eq(layer.grid[3][0].url, "http://www.openlayers.org/world/index.php?g=satellite&map=world&i=jpeg&t=-512&l=0&s=110735960.625", "grid[3][0] is okay"); 
    152152    } 
    153153    function test_Layer_WrapDateLine_WMS_Overlay (t) { 
     
    164164        m.addLayers([baselayer,layer]); 
    165165        m.zoomToMaxExtent(); 
    166         t.eq(layer.grid[0][0].url, "http://www2.dmsolutions.ca/cgi-bin/mswms_gmap?LAYERS=bathymetry%2Cland_fn%2Cpark%2Cdrain_fn%2Cdrainage%2Cprov_bound%2Cfedlimit%2Crail%2Croad%2Cpopplace&TRANSPARENT=true&FORMAT=image%2Fpng&SERVICE=WMS&VERSION=1.1.1&REQUEST=GetMap&STYLES=&EXCEPTIONS=application%2Fvnd.ogc.se_inimage&SRS=EPSG%3A4326&BBOX=0%2C450%2C180%2C630&WIDTH=256&HEIGHT=256", "grid[0][0] wms overlay is okay"); 
    167         t.eq(layer.grid[0][3].url, "http://www2.dmsolutions.ca/cgi-bin/mswms_gmap?LAYERS=bathymetry%2Cland_fn%2Cpark%2Cdrain_fn%2Cdrainage%2Cprov_bound%2Cfedlimit%2Crail%2Croad%2Cpopplace&TRANSPARENT=true&FORMAT=image%2Fpng&SERVICE=WMS&VERSION=1.1.1&REQUEST=GetMap&STYLES=&EXCEPTIONS=application%2Fvnd.ogc.se_inimage&SRS=EPSG%3A4326&BBOX=-180%2C450%2C0%2C630&WIDTH=256&HEIGHT=256", "grid[0][3] wms overlay is okay"); 
    168         t.eq(layer.grid[3][0].url, "http://www2.dmsolutions.ca/cgi-bin/mswms_gmap?LAYERS=bathymetry%2Cland_fn%2Cpark%2Cdrain_fn%2Cdrainage%2Cprov_bound%2Cfedlimit%2Crail%2Croad%2Cpopplace&TRANSPARENT=true&FORMAT=image%2Fpng&SERVICE=WMS&VERSION=1.1.1&REQUEST=GetMap&STYLES=&EXCEPTIONS=application%2Fvnd.ogc.se_inimage&SRS=EPSG%3A4326&BBOX=0%2C-90%2C180%2C90&WIDTH=256&HEIGHT=256", "grid[3][0] wms overlay okay"); 
     166        t.eq(layer.grid[0][0].url, "http://www2.dmsolutions.ca/cgi-bin/mswms_gmap?LAYERS=bathymetry%2Cland_fn%2Cpark%2Cdrain_fn%2Cdrainage%2Cprov_bound%2Cfedlimit%2Crail%2Croad%2Cpopplace&TRANSPARENT=true&FORMAT=image%2Fpng&SERVICE=WMS&VERSION=1.1.1&REQUEST=GetMap&STYLES=&EXCEPTIONS=application%2Fvnd.ogc.se_inimage&SRS=EPSG%3A4326&BBOX=0%2C270%2C90%2C360&WIDTH=256&HEIGHT=256", "grid[0][0] wms overlay is okay"); 
     167        t.eq(layer.grid[0][3].url, "http://www2.dmsolutions.ca/cgi-bin/mswms_gmap?LAYERS=bathymetry%2Cland_fn%2Cpark%2Cdrain_fn%2Cdrainage%2Cprov_bound%2Cfedlimit%2Crail%2Croad%2Cpopplace&TRANSPARENT=true&FORMAT=image%2Fpng&SERVICE=WMS&VERSION=1.1.1&REQUEST=GetMap&STYLES=&EXCEPTIONS=application%2Fvnd.ogc.se_inimage&SRS=EPSG%3A4326&BBOX=-90%2C270%2C0%2C360&WIDTH=256&HEIGHT=256", "grid[0][3] wms overlay is okay"); 
     168        t.eq(layer.grid[3][0].url, "http://www2.dmsolutions.ca/cgi-bin/mswms_gmap?LAYERS=bathymetry%2Cland_fn%2Cpark%2Cdrain_fn%2Cdrainage%2Cprov_bound%2Cfedlimit%2Crail%2Croad%2Cpopplace&TRANSPARENT=true&FORMAT=image%2Fpng&SERVICE=WMS&VERSION=1.1.1&REQUEST=GetMap&STYLES=&EXCEPTIONS=application%2Fvnd.ogc.se_inimage&SRS=EPSG%3A4326&BBOX=0%2C0%2C90%2C90&WIDTH=256&HEIGHT=256", "grid[3][0] wms overlay okay"); 
    169169    } 
    170170 
  • trunk/openlayers/tests/test_Layer.html

    r4229 r4381  
    156156    function test_06_Layer_getZoomForResolution(t) { 
    157157 
    158         t.plan(4); 
     158        t.plan(6); 
    159159 
    160160        var layer = new OpenLayers.Layer('Test Layer'); 
     
    164164         
    165165        t.eq(layer.getZoomForResolution(200), 0, "zoom all the way out"); 
     166        t.eq(layer.getZoomForResolution(65), 1, "index closest to 65"); 
     167        t.eq(layer.getZoomForResolution(63), 1, "index closest to 63"); 
    166168        t.eq(layer.getZoomForResolution(25), 2, "zoom in middle"); 
    167169        t.eq(layer.getZoomForResolution(3), 5, "zoom allmost all the way in");