--- Tile_orig.py	2008-07-12 23:04:59.000000000 +0100
+++ Tile.py	2008-07-12 23:14:46.000000000 +0100
@@ -1,4 +1,7 @@
-import struct,os.path
+import struct,os.path,sys
+
+os.environ["DJANGO_SETTINGS_MODULE"]="tah.settings"
+from tah.tah_intern.models import Layer
 
 class Tile:
   x=None
@@ -6,6 +9,9 @@
   z=None
   layer=None
   blankness=None
+  basetilepath='/usr/local/src/tah/Tiles'
+  leg_basetiledir='/mnt/agami/openstreetmap/tah/Tiles'
+  blankpng=['unknown.png','sea.png','land.png','transparent.png']
 
   def __init__(self, layer, z, x, y):
     self.layer=layer
@@ -56,8 +62,8 @@
     (layer, base_z,base_x,base_y) = self.basetileset() 
     # basetileset returns (None,None,None,None) if invalid!
     # basetilepath could be take from the Settings, hardcode for efficiency
-    basetilepath='/usr/local/src/tah/Tiles'
-    tilesetfile = os.path.join(basetilepath,layername+'_'+str(base_z),str(base_x)+'_'+str(base_y))
+
+    tilesetfile = os.path.join(self.basetilepath,layername+'_'+str(base_z),str(base_x)+'_'+str(base_y))
     try:
       f = open(tilesetfile,'rb')
       #calculate file offset
@@ -84,8 +90,7 @@
       data = f.read(d_offset_next-d_offset)
     else:
       # return blankness value
-      blankpng = ['unknown.png','sea.png','land.png','transparent.png']
-      f_png = open(os.path.join(basetilepath,blankpng[d_offset]),'rb')
+      f_png = open(os.path.join(self.basetilepath,self.blankpng[d_offset]),'rb')
       data = f_png.read()
       f_png.close()
     try: f.close()
@@ -95,13 +100,39 @@
 
   def serve_legacy_tile(self,layername):
     """ Return the PNG data of a legacy tile """
-    leg_basetiledir='/mnt/agami/openstreetmap/tah/Tiles'
-    fname = os.path.join(leg_basetiledir,layername,"%02d"%(self.z),"%03d"%(self.x//1000),"%03d"%(self.x%1000),"%03d"%(self.y//1000),"%03d.png"%(self.y%1000))
+    fname = os.path.join(self.leg_basetiledir,layername,"%02d"%(self.z),"%03d"%(self.x//1000),"%03d"%(self.x%1000),"%03d"%(self.y//1000),"%03d.png"%(self.y%1000))
     try:
       f = open(fname,'rb')
       data = f.read()
       f.close()
       return data
     except:
+      #There is no legacy tile either, so fall back to a blanktile database
+      layer = Layer.objects.get(name=layername);
+      type = 0
+      if layer.transparent:
+        type = 3;
+      else:
+        if self.z < 12:
+          #We only handle z12 and above this way. Lowzooms are required to have actual tiles
+    	  return None
+	else:
+	    #Lookup in the oceans tile
+	    #For z12 and above the file will tell us directly what type it is
+	    try:
+		(layern, base_z,base_x,base_y) = self.basetileset()
+		oceant = open(os.path.join(self.basetilepath,"oceantiles_12.dat"),'rb')
+		oceant.seek((4096*base_y + base_x) >> 2)
+		data = ord(oceant.read(1));
+		bit_off = 3 - ((4096*base_y + base_x) % 4);
+		type = (data & (3 << 2*(bit_off))) >> 2*(bit_off)
+
+		#remap the oceantiles type to the one used on the serverside
+		type = {0: 0, 1:2, 2:1, 3:0}[type];
+	    except:
       return None
+      f_png = open(os.path.join(self.basetilepath,self.blankpng[type]),'rb')
+      data = f_png.read()
+      return data
  
\ No newline at end of file
