OpenLayers OpenLayers

Ticket #482: gettilebounds.patch

File gettilebounds.patch, 5.4 kB (added by crschmidt, 1 year ago)
  • tests/Layer/test_Grid.html

    old new  
    528528        height = layer.tileSize.h; 
    529529        t.ok(width == parseInt(width) && height == parseInt(height), "calculated tileSize width/height are integer values"); 
    530530    } 
     531    function test_12_Layer_Grid_getTileBounds(t) { 
     532        t.plan(2); 
     533        var map = new OpenLayers.Map("map2"); 
     534        var url = "http://octo.metacarta.com/cgi-bin/mapserv"; 
     535        layer = new OpenLayers.Layer.WMS(name, url, params); 
     536         
     537        var newParams = { layers: 'sooper',  
     538                          chickpeas: 'image/png'}; 
    531539 
     540        map.addLayer(layer); 
     541        map.zoomToMaxExtent(); 
     542        map.zoomIn(); 
     543        var bounds = layer.getTileBounds(new OpenLayers.Pixel(200,200)); 
     544        t.eq(bounds.toBBOX(), "-180,-90,0,90", "get tile bounds returns correct bounds");  
     545        map.pan(200,0); 
     546        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");  
     548    } 
     549 
    532550    function test_99_Layer_Grid_destroy (t) { 
    533551 
    534552        t.plan( 5 ); 
     
    563581</head> 
    564582<body> 
    565583<div id="map" style="width:499px;height:549px;display:none"></div> 
     584<div id="map2" style="width:500px;height:550px;display:none"></div> 
    566585</body> 
    567586</html> 
  • tests/Layer/test_KaMap.html

    old new  
    189189        t.ok( layer.tileSize != null, "tileSize has been set"); 
    190190        t.ok( (layer.tileSize.h == 50) && (layer.tileSize.w == 500), "tileSize has been set correctly"); 
    191191    } 
     192    function test_12_Layer_KaMap_getTileBounds(t) { 
     193        t.plan(2); 
     194        var map = new OpenLayers.Map("map"); 
     195        var url = "http://octo.metacarta.com/cgi-bin/mapserv"; 
     196        layer = new OpenLayers.Layer.KaMap(name, url, params); 
     197         
     198        var newParams = { layers: 'sooper',  
     199                          chickpeas: 'image/png'}; 
    192200 
     201        map.addLayer(layer); 
     202        map.zoomToMaxExtent(); 
     203        map.zoomIn(); 
     204        var bounds = layer.getTileBounds(new OpenLayers.Pixel(200,200)); 
     205        t.eq(bounds.toBBOX(), "-180,0,0,180", "get tile bounds returns correct bounds");  
     206        map.pan(200,0); 
     207        var bounds = layer.getTileBounds(new OpenLayers.Pixel(200,200)); 
     208        t.eq(bounds.toBBOX(), "0,0,180,180", "get tile bounds returns correct bounds after pan");  
     209    } 
     210 
    193211    function test_99_Layer_KaMap_destroy (t) { 
    194212 
    195213        t.plan( 3 ); 
  • lib/OpenLayers/Layer/Grid.js

    old new  
    673673      } 
    674674    }, 
    675675     
     676    /** 
     677     * APIMethod: getTileBounds 
     678     * Returns The tile bounds for a layer given a pixel location 
     679     * 
     680     * Parameters:  
     681     * viewPortPx - {OpenLayers.Pixel} The location in the viewport 
     682     */ 
     683    getTileBounds: function(viewPortPx) { 
     684        var maxExtent = this.map.getMaxExtent(); 
     685        var resolution = this.getResolution(); 
     686        var tileMapWidth = resolution * this.tileSize.w; 
     687        var tileMapHeight = resolution * this.tileSize.h; 
     688        var mapPoint = this.getLonLatFromViewPortPx(viewPortPx); 
     689        var tileLeft = maxExtent.left + (tileMapWidth * 
     690                                         Math.floor((mapPoint.lon - 
     691                                                     maxExtent.left) / 
     692                                                    tileMapWidth)); 
     693        var tileBottom = maxExtent.bottom + (tileMapHeight * 
     694                                             Math.floor((mapPoint.lat - 
     695                                                         maxExtent.bottom) / 
     696                                                        tileMapHeight)); 
     697        return new OpenLayers.Bounds(tileLeft, tileBottom, 
     698                                     tileLeft + tileMapWidth, 
     699                                     tileBottom + tileMapHeight); 
     700    }, 
     701     
    676702    CLASS_NAME: "OpenLayers.Layer.Grid" 
    677703}); 
  • lib/OpenLayers/Layer/KaMap.js

    old new  
    225225 
    226226        return obj; 
    227227    },     
     228     
     229    /** 
     230     * APIMethod: getTileBounds 
     231     * Returns The tile bounds for a layer given a pixel location. 
     232     * 
     233     * Parameters: 
     234     * viewPortPx - {OpenLayers.Pixel} The location in the viewport 
     235     */ 
     236    getTileBounds: function(viewPortPx) { 
     237        var resolution = this.getResolution(); 
     238        var tileMapWidth = resolution * this.tileSize.w; 
     239        var tileMapHeight = resolution * this.tileSize.h; 
     240        var mapPoint = this.getLonLatFromViewPortPx(viewPortPx); 
     241        var tileLeft = tileMapWidth * Math.floor(mapPoint.lon / tileMapWidth); 
     242        var tileBottom = tileMapHeight * Math.floor(mapPoint.lat / tileMapHeight); 
     243        return new OpenLayers.Bounds(tileLeft, tileBottom, 
     244                                     tileLeft + tileMapWidth, 
     245                                     tileBottom + tileMapHeight); 
     246    }, 
    228247 
    229248    CLASS_NAME: "OpenLayers.Layer.KaMap" 
    230249});