source: subversion/sites/other/tilesAtHome_tahngo/tah_intern/LegacyTileset.py @ 9359

Revision 9359, 3.7 KB checked in by spaetz, 6 years ago (diff)

call LegacyTileset? only on tilesetfile that don't exist yet.

Line 
1import sys,os
2# we need to insert the basedir to the python path (strip 2 path components) if we want to directly execute this file
3sys.path.insert(0, os.path.dirname(os.path.dirname(sys.path[0])))
4os.environ['DJANGO_SETTINGS_MODULE'] = "tah.settings"
5from time import clock, time
6from tah.tah_intern.Tileset import Tileset
7from tah.tah_intern.Tile import Tile
8from tah.tah_intern.models import Settings,Layer
9
10class LegacyTileset(Tileset):
11  leg_basetiledir='/mnt/agami/openstreetmap/tah/Tiles/'
12
13
14  def get_blank(self,tile):
15    if self.blankness != None: return self.blankness
16    #Legacy layer numbers in blank db:
17    legacy_layer_num={'tile':1,'maplint':3, 'lowzoom':4, 'captionless':6, 'caption':7}
18    if not legacy_layer_num.has_key(tile.layer.name):
19      print "Unknown layer %s!" % tile.layer.name
20      return 0;
21    if tile.z < 12:
22      # Don't handle low zoom blanks
23      return 0
24    try:
25          #Lookup in the oceans tile
26          #For z12 and above the file will tell us directly what type it is
27          (layern, base_z,base_x,base_y) = tile.basetileset()
28          f_oceant = open(os.path.join('/var/www/tah/Tiles',"oceantiles_12.dat"),'rb')
29          f_oceant.seek((4096*base_y + base_x) >> 2)
30          data = ord(f_oceant.read(1));
31          #take 2 bits of the char
32          bit_off = 3 - (base_x % 4);
33          type = (data >> 2*bit_off) & 3
34          # map tile type to blankness type used in the server
35          blank = [0,2,1,0]
36          self.blankness = blank[type]
37    except: self.blankness= 0
38    return self.blankness
39
40  def convert (self,layer,base_z,base_x,base_y,base_tile_path):
41    """
42          Take all tile files from the old one file-per-tile system
43          that belong to a tileset and save it in the new format.
44    """
45    self.blankness = None
46    if not base_z in [0,6,12]: return (0,"Invalid base zoom level.")
47    starttime = time()   
48    # cache if the layer is transparent
49    layer_transparent = layer.transparent
50    files  = 0
51    blanks = 0
52
53    for z in range(base_z,base_z+6):
54      for y in range(base_y*pow(2,z-base_z),(1+base_y)*pow(2,z-base_z)):
55        for x in range(base_x*pow(2,z-base_z),(1+base_x)*pow(2,z-base_z)):
56          fname = os.path.join(self.leg_basetiledir,layer.name,"%02d"%(z),"%03d"%(x//1000),"%.3d"%(x),"%03d"%(y//1000),"%.3d.png"%(y))
57          t = Tile(layer,z,x,y)
58          if os.path.isfile(fname): 
59            #print "add %d %d %d %s" % (z,x,y,fname)
60            self.add_tile(t,fname)
61            files += 1
62          else:
63            blanks += 1
64            if not self.tiles.has_key(x) or not self.tiles[x].has_key(y):
65              #the tile does not exist yet (not created by subtile blanking)
66              if layer_transparent: 
67                b = 3
68              else:
69                b = self.get_blank(t)
70              t.set_blank(b)
71              #print "add blank (%d) %d %d %d (basetile %s %s %s)" % (b,t.z,t.x,t.y,self.base_z,self.x,self.y)
72              self.add_tile(t,None)
73
74    savetime = time()
75    if self.save(base_tile_path):
76      now=time()
77      if base_y%50 == 0:print "%d blanks %d tiles. Saving %.1f sec. Total %.1f sec" % (files,blanks,now-savetime,now-starttime)
78      return (1,'OK')
79    else:
80      return (0,'Unknown error while saving tileset.')
81
82if __name__ == '__main__':
83  base_tile_path = Settings().getSetting(name='base_tile_path')
84  layer=Layer.objects.get(name='tile')
85  for x in range (0,4096):
86      for y in range (0,4096):
87        if y%50==0: print "Converting: %s (12,%d,%d)" % (layer.name,x,y)
88        tilesetfile = os.path.join(base_tile_path,"%s_%d" % (layer.name,12),"%04d" % x, str(x)+'_'+str(y))
89        if not os.path.isfile(tilesetfile):
90            lt = LegacyTileset()
91            lt.convert(layer,12,x,y,base_tile_path)
Note: See TracBrowser for help on using the repository browser.