Created
January 22, 2015 23:01
-
-
Save veltman/7e65d96b93403f8cbc77 to your computer and use it in GitHub Desktop.
Saving tiles locally
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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