source: subversion/applications/rendering/kah/tilesetToFile.py @ 11728

Last change on this file since 11728 was 11728, checked in by ojw, 12 years ago

Python implementation of http://wiki.openstreetmap.org/index.php/Tiles%40home/Tileset_as_one_file

File size: 1.9 KB
Line 
1#!/usr/bin/env python
2#-------------------------------------------------------------------------
3# Packs a directory of PNG map images into one file, as described in:
4#
5# http://wiki.openstreetmap.org/index.php/Tiles%40home/Tileset_as_one_file
6#
7# Written by Oliver White, 2008
8# This file is public-domain
9#-------------------------------------------------------------------------
10import os
11import struct
12
13def readfile(filename):
14    """Reads a binary file"""
15    f = open(filename, "rb")
16    data = f.read()
17    f.close()
18    return(data)
19
20def packTileset(directory, baseX, baseY, baseZ, outputFile):
21    """Create outputFile, containing images from directory"""
22   
23    f = open(outputFile, "wb+")
24    for i in range(0,1366):
25        f.write("*---")
26
27    index = 0
28    offset = 1366*4
29    sizes = []
30
31    for zi in range(0,6): # increasing zoom level from baseZ
32        size = 2 ** zi
33        z = baseZ + zi
34        for yi in range(0,size):
35            for xi in range(0,size):
36                x = baseX * size + xi
37                y = baseY * size + yi
38                filename = "%s/%d/%d/%d.png" % (directory,z,x,y)
39                data = ''
40                filesize = 0
41                if(os.path.exists(filename)):
42                    data = readfile(filename)
43                    filesize = len(data)
44                else:
45                    print "No %s" % filename
46                   
47                if(filesize):
48                    f.write(data)
49
50                sizes.append(offset) # will be written to beginning of file later
51                offset += filesize
52                index += 1
53
54    # Add the final offset, to mark the size of the last tile
55    sizes.append(offset)
56
57    # Write offsets to beginning of file
58    f.seek(0, 0)
59    for i in range(0,1366):
60        f.write(struct.pack("I", (sizes[i])))
61    f.close()
62
63
64if(__name__ == "__main__"):
65    packTileset(".", 2042, 1362, 12, "output.dat")
Note: See TracBrowser for help on using the repository browser.