Skip to content

Instantly share code, notes, and snippets.

@veltman
Created January 22, 2015 23:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save veltman/7e65d96b93403f8cbc77 to your computer and use it in GitHub Desktop.
Save veltman/7e65d96b93403f8cbc77 to your computer and use it in GitHub Desktop.
Saving tiles locally
var request = require("request"),
fs = require("fs"),
xyz = require("xyz-affair"),
queue = require("queue-async")(10), //max concurrency
path = require("path"),
mkdirp = require("mkdirp");
var bounds = [
[
-74.00596618652344,
40.64417760251725,
],
[
-73.90502929687499,
40.797957124643666
]
];
var zoom = 16;
var tiles = xyz(bounds,zoom),
numTiles = tiles.length;
tiles.forEach(function(t,i){
queue.defer(getTile,t,i);
});
queue.awaitAll(function(err,results){
console.log(err);
});
function getTile(tile,c,cb) {
var fn = path.join(tile.z+"",tile.x+"",tile.y+".png"),
url = "http://sometileurl.com/"+fn;
c++;
mkdirp.sync(path.join(tile.z+"",tile.x+""));
request(url)
.on('error', function(err) {
cb(err,console.log(err));
})
.on('end',function(){
console.log(c+"/"+numTiles);
cb(null,null);
})
.pipe(fs.createWriteStream(fn));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment