[OpenLayers-Users] Downloading tiles to local file system

allart allart.kooiman at sovon.nl
Thu Jul 26 08:28:13 PDT 2012


Hi Jon,

For the first item, you shoud calculate the tiles for a certain zoom
level and bounds. Search for calculatebounds in the openlayers
documentation, how to calculate bounds.

Once you have the bounds you can calculate the upper left and bottom
right tiles (the functions can be found on the openstreetmap wiki and
other sources):

----
var tile_first = ctile(bounds.left,bounds.top,zoom);      
var tile_last = ctile(bounds.right,bounds.bottom,zoom);

ctile = function(x,y,zoom) {      
      var tile = {}
      x = (1 + (x * Math.PI / 180)/ Math.PI)/2;         
      tile.x = Math.floor(x*Math.pow(2,zoom)); //klopt            
      tile.y = Math.floor((1-Math.log(Math.tan(y*Math.PI/180) + 1/Math.cos(y*Math.PI/180))/Math.PI)/2 *Math.pow(2,zoom));
      tile.path = zoom + '/'+ tile.x + '/'+ tile.y +'.png';
      return tile;
   }
---

And loop to create an array with all tiles
--
var tiles = [];
      var x= tile_first.x;
      var y= tile_first.y;

      for(x;x <= tile_last.x;x++) {
         y = tile_first.y;
         tiles.push({'x':x,'y':y,'path': zoom + '/'+ x + '/'+ y +'.png'})
         for(y++;y <= tile_last.y;y++) {
          tiles.push({'x':x,'y':y,'path': zoom + '/'+ x + '/'+ y +'.png'})
         }         
      } 
--
Once you have the tiles, you can convert them to base64 text files (server side or with javascript canvas) and store them using the filesystem api. 

Good luck!




More information about the Users mailing list