[OpenLayers-Users] Problem setting the correct resolution with a grid layer

Dragos Ion dragospenet at yahoo.com
Fri Dec 4 08:45:48 EST 2009


Hi,

I want to use OpenLayers Api for displaying a custom map of objects (like a grid over the entire map, so over all map tiles), taking the functionality of zooming and panning; I'll need three custom zoom levels, using 256x256 px tile size, with the full map size of:

zoom level 0: SIZE
zoom level 1: SIZE * 5
zoom level 2: SIZE * 10

The SIZE is actually dynamic over time, so I'll cache the slices for a fixed amount of time.

Now, the grid like objects I want to draw on each slice will have 1px, 5px and respectively 10px (both on width and height), corresponding to each of the zoom levels.

Now, I have the following JavaScript code to initialize & draw the map:

var map, layer, layer0, layer1, layer2;

OpenLayers.Layer.TMS.prototype.getURL = function ( bounds )
{
    bounds=this.adjustBounds(bounds);
    var res=this.map.getResolution();
    var x=Math.round((bounds.left-this.tileOrigin.lon)/(res*this.tileSize.w));
    var y=Math.round((bounds.bottom-this.tileOrigin.lat)/(res*this.tileSize.h));
    var z=this.map.getZoom();
    var path='slice.php?layer='+this.layer+'&x='+x+'&y='+y+'&z='+z;
    var url=this.url;
    if(url instanceof Array)
            url=this.selectUrl(path,url);
    return url+path;
};

function init()
{
    map = new OpenLayers.Map('map', {units:'m'});

    layer = new OpenLayers.Layer.TMS("3 Zoom Levels", "http://local.server/", {layer: -1,    resolutions: [1,2,10],
    maxExtent: new OpenLayers.Bounds(0, 0, Math.ceil($x*10/256)*256, Math.ceil($y*10/256)*256),
    tileSize: new OpenLayers.Size(256,256), buffer:0});

    /* for testing >

    layer0 = new OpenLayers.Layer.TMS("Zoom 0 Like", "http://local.server/", {layer: 0,    resolutions: [1],
    maxExtent: new OpenLayers.Bounds(0, 0, Math.ceil(x*1/256)*256, Math.ceil(y*1/256)*256),
    tileSize: new OpenLayers.Size(256,256), buffer:0});

    layer1 = new OpenLayers.Layer.TMS("Zoom 1 like", "http://local.server/", {layer: 1,    resolutions: [1],
    maxExtent: new OpenLayers.Bounds(0, 0, Math.ceil(x*5/256)*256, Math.ceil(y*5/256)*256),
    tileSize: new OpenLayers.Size(256,256), buffer:0});

    layer2 = new OpenLayers.Layer.TMS("Zoom 2 like", "http://local.server/", {layer: 2,    resolutions: [1],
    maxExtent: new OpenLayers.Bounds(0, 0, Math.ceil(x*10/256)*256, Math.ceil(y*10/256)*256),
    tileSize: new OpenLayers.Size(256,256), buffer:0});

    */ < for testing

    map.addLayer(layer);
   
    map.addControl(new OpenLayers.Control.LayerSwitcher());
    map.zoomToMaxExtent();
}

init();

With the [Math.ceil(x*5/256)*256] and [Math.ceil(y*5/256)*256)] (for example) I get the exact number of slices I need for a zoom level (here for the zoom level #1 as I multiply with #5)

x and y are the maximum number of grid objects on the entire map (horizontal and vertical)

so for level 0, when each object have 1px (on the slice) we'll have:
Math.ceil(x*1/256)*256, Math.ceil(y*1/256)*256) as the maxExtent

for zoom level 2:
Math.ceil(x*10/256)*256, Math.ceil(y*10/256)*256) as the maxExtent

Now, the problem I have is that I don't know why the map it's not ok (some offsets for the zoom level 0, it is not centered..) and also the x (slice number on x) going into the request on the server 
(path='slice.php?layer='+this.layer+'&x='+x+'&y='+y+'&z='+z;)
differs between the zoom levels with 1 (one).

If I run the code from [for testing] with each layer corresponding to a zoom level, I get the right map, with all slices correctly shown on the map (including centered for zoom level 0).

If I got it right, "resolutions" (on the map) must be set so that it represents how much of the map units are on one pixel.

I have 10 map units / pixel for zoom level 0 (object grid on slice at 1 pixel), 2 map units / pixel for zoom level 1 (object grid on slice at 5 pixels) and 1 map unit / pixel for zoom level 2 (object grid on slice at 10 pixels).

But this seem to be not ok, where I'm mistaken?

Dragos.





      
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.osgeo.org/pipermail/openlayers-users/attachments/20091204/2b4001cc/attachment.html


More information about the Users mailing list