Created
January 31, 2019 12:05
-
-
Save samhattangady/051ac3426ce132dc5388052c409bd1ac to your computer and use it in GitHub Desktop.
Converting tiles from TMS to XYZ format
This file contains hidden or 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
""" | |
Script to convert all tiles from TMS format to XYZ format. | |
gdal2tiles.py creates tiles in TMS format by default. XYZ | |
is a little easier to consume. | |
This script expects a root directory, and a zoom level. | |
""" | |
import os | |
import sys | |
# Get the arguments and check that they are correct | |
root_directory, zoom_level = sys.argv[1:] | |
if not os.path.isdir(root_directory): | |
raise ValueError(f'Could not find root directory : {root_directory}') | |
if not os.path.isdir(os.path.join(root_directory, zoom_level)): | |
raise ValueError(f'Could not find zoom level {zoom_level} in root directory') | |
# Then we find all the tiles in that directory and rename them to xyz format | |
# Source: https://gist.github.com/tmcw/4954721 | |
root = os.path.join(root_directory, zoom_level) | |
xs = [f for f in os.listdir(root) if os.path.isdir(os.path.join(root, f))] | |
for x in xs: | |
ys = [f for f in os.listdir(os.path.join(root,x)) if f.lower().endswith('.png')] | |
for y in ys: | |
new_y = (2**int(zoom_level)) - int(y[:-4]) - 1 | |
os.rename(os.path.join(root,x,y), os.path.join(root,x,f'{new_y}.png')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment