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

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

blank file detect (first pass, compares to default image)

File size: 2.2 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    Blank = readfile("blank/blank.png")
24   
25    f = open(outputFile, "wb+")
26    for i in range(0,1366):
27        f.write("*---")
28
29    index = 0
30    offset = 1366*4
31    sizes = []
32    num_blank = 0
33
34    for zi in range(0,6): # increasing zoom level from baseZ
35        size = 2 ** zi
36        z = baseZ + zi
37        for yi in range(0,size):
38            for xi in range(0,size):
39                x = baseX * size + xi
40                y = baseY * size + yi
41                filename = "%s/%d/%d/%d.png" % (directory,z,x,y)
42                data = ''
43                filesize = 0
44                if(os.path.exists(filename)):
45                    data = readfile(filename)
46                    filesize = len(data)
47                else:
48                    print "No %s" % filename
49               
50                if(data == Blank):
51                  sizes.append(1)
52                  num_blank += 1
53                else:
54                  if(filesize):
55                      f.write(data)
56                  sizes.append(offset) # will be written to beginning of file later
57                  offset += filesize
58                index += 1
59
60    print "%d blank of %d total" % (num_blank, index)
61
62    # Add the final offset, to mark the size of the last tile
63    sizes.append(offset)
64
65    # Write offsets to beginning of file
66    f.seek(0, 0)
67    for i in range(0,1366):
68        f.write(struct.pack("I", (sizes[i])))
69    f.close()
70
71
72if(__name__ == "__main__"):
73    packTileset(".", 2042, 1362, 12, "output.dat")
Note: See TracBrowser for help on using the repository browser.