Skip to content

Instantly share code, notes, and snippets.

@M165437
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save M165437/9549734 to your computer and use it in GitHub Desktop.
Save M165437/9549734 to your computer and use it in GitHub Desktop.
TMS to OpenStreetMap/Google Maps tile naming – node.js script that inverts row order/y axis
/**
* TMS to OSM/Google Maps tile naming – node.js script that inverts row order/y axis
* assumes subfolders ./tiles/{z}/{x}/{y}.png
*/
(function() {
// ShellJS
require('shelljs/global');
// Variables
var dirs, dir, numeric, tiles, tmp,
path = require('path'),
extImg = '.png',
extTmp = '.tmp';
// Sort helper
numeric = function(a, b) {
var rx = /\D+/g;
return a.replace(rx, '') - b.replace(rx, '');
};
// Create directory list
dirs = find('./tiles').filter(function(el) {
return test('-d', el);
});
// Walk directories
for (var i = 0; i < dirs.length; i++) {
dir = dirs[i];
// Rename files
tiles = ls(dir + '/*' + extImg).sort(numeric);
for (var j = 0; j < tiles.length; j++) {
mv(tiles[j], dir + '/' + (tiles.length-1-j) + extImg + extTmp);
}
// Clean up temporaries
tiles = ls(dir + '/*' + extTmp).sort(numeric);
for (var k = 0; k < tiles.length; k++) {
mv(tiles[k], tiles[k].replace(extTmp, ''));
}
}
}).call(this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment