Skip to content

Instantly share code, notes, and snippets.

@areichman
Created June 29, 2015 21:36
Show Gist options
  • Save areichman/5a583e690403d0dee96e to your computer and use it in GitHub Desktop.
Save areichman/5a583e690403d0dee96e to your computer and use it in GitHub Desktop.
Iterate through all image tiles for a given bounds and zoom range
// Iterate through all map tiles for a given bounds and zoom range
// lat-lon to tile conversions from http://wiki.openstreetmap.org/wiki/Slippy_map_tilenames#Derivation_of_tile_names
function lat2tile(lat, zoom) {
return (Math.floor((1-Math.log(Math.tan(lat*Math.PI/180) + 1/Math.cos(lat*Math.PI/180))/Math.PI)/2 *Math.pow(2,zoom)))
}
function lon2tile(lon, zoom) {
return (Math.floor((lon+180)/360*Math.pow(2,zoom)))
}
function iterator(z, x, y) {
console.log(z + '/' + x + '/' + y + '.png')
// do something more useful, like copy to s3
}
var lat_min = 42.5
var lat_max = 43.5
var lon_min = -80
var lon_max = -75
var z_min = 8
var z_max = 8
var total = 0
for (var z = z_min; z < z_max + 1; z++) {
var y_min = lat2tile(lat_max, z) // 0,0 is top left
var y_max = lat2tile(lat_min, z)
var x_min = lon2tile(lon_min, z)
var x_max = lon2tile(lon_max, z)
for (var x = x_min; x < x_max + 1; x++) {
for (var y = y_min; y < y_max + 1; y++) {
iterator(z, x, y)
total++
}
}
}
console.log(total)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment