Created
April 3, 2012 08:37
-
-
Save deciob/2290472 to your computer and use it in GitHub Desktop.
Move map tiles from "OpenLayers" system to TMS system
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
import os | |
import shutil | |
class FilesystemDiver(object): | |
"""crawls the filesystem and extracts | |
filepath and filenames information""" | |
def __init__(self, path): | |
self.path = path | |
def get_folders_in_filepath(self): | |
return [f for f in os.listdir(self.path) | |
if not os.path.isfile(os.path.join(self.path, f))] | |
def yield_filepath_info(self): | |
"""yields a tuple with filepath and filenames""" | |
for root, dirs, files in os.walk(self.path): | |
yield (root, dirs, files) | |
# if root.find('design') != -1: | |
# pdfs = [f for f in files if f.find('.pdf') != -1] | |
# yield (root, pdfs) | |
def reformat(list): | |
z = str(int(list[0])) | |
x = "%i%i%i" % ( int(list[1]), int(list[2]), int(list[3]) ) | |
y = "%i%i%i" % ( int(list[4]), int(list[5]), int(list[6]) ) | |
#print y, 2 ^ int(z) - 1 - int(y) | |
#y = 2 ^ int(z) - 1 - int(y) | |
return [int(z), int(x), int(y)] | |
def rewrite(file, reformatted): | |
new_dir = 'osm/' + str(reformatted[0]) + '/' + str(reformatted[1]) | |
file = os.path.join(os.getcwd(), file) | |
new_file = os.path.join(os.getcwd(), new_dir + '/' + str(reformatted[2]) + '.png') | |
if not os.path.exists(new_dir): | |
os.makedirs(new_dir) | |
# print file | |
# print new_dir + '/' + str(reformatted[2]) + '.png' | |
# | |
#shutil.copyfile(file, new_file) | |
shutil.move(file, new_file) | |
# try: | |
# | |
# print 'found file' | |
# except IOError: | |
# print IOError | |
filesystem = FilesystemDiver('osm_cache_EPSG4326') | |
counter_a = 0 | |
counter_b = 0 | |
for info in filesystem.yield_filepath_info(): | |
for f in info[2]: | |
file = info[0] + '/' +f | |
r = info[0].replace('osm_cache_EPSG4326', '') | |
f = f.replace('.png', '') | |
full_path = r + '/' + f | |
full_path_list = full_path.split('/')[1:] | |
reformatted = reformat(full_path_list) | |
rewrite(file, reformatted) | |
#print reformatted, file |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment