Skip to content

Instantly share code, notes, and snippets.

@mpetroff
Created October 4, 2014 17:53
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 mpetroff/e763e5fde9d1e9cb0f76 to your computer and use it in GitHub Desktop.
Save mpetroff/e763e5fde9d1e9cb0f76 to your computer and use it in GitHub Desktop.
Convert between tms and xyz map tile structures
#!/usr/bin/python
# Converts between tms and xyz map tile structures
# October 2014, Matthew Petroff <http://mpetroff.net>
# Released into the public domain using the CC0 Public Domain Dedication
# See <http://creativecommons.org/publicdomain/zero/1.0/>
import os
import subprocess
base_dir = '.'
ext = '.png'
for z in os.listdir(base_dir):
z_dir = os.path.join(base_dir, z)
if os.path.isdir(z_dir):
for x in os.listdir(z_dir):
x_dir = os.path.join(z_dir, x)
tmp_x_dir = os.path.join(z_dir, x + '_tmp')
subprocess.call(['mv', x_dir, tmp_x_dir])
os.mkdir(x_dir)
for y in os.listdir(tmp_x_dir):
old_tile = os.path.join(tmp_x_dir, y)
new_tile = os.path.join(x_dir, str(2 ** int(z) \
- int(y.split('.')[0]) - 1) + ext)
subprocess.call(['mv', old_tile, new_tile])
subprocess.call(['rmdir', tmp_x_dir])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment