source: subversion/tile.openstreetmap.org/render_from_list.py @ 2176

Last change on this file since 2176 was 2029, checked in by steve, 14 years ago

add hack tile stuff

  • Property svn:executable set to *
File size: 2.6 KB
Line 
1#!/usr/bin/python
2
3import fileinput
4from math import pi,cos,sin,log,exp,atan
5from subprocess import Popen
6
7DEG_TO_RAD = pi/180
8RAD_TO_DEG = 180/pi
9
10def minmax (a,b,c):
11  a = max(a,b)
12  a = min(a,c)
13  return a
14
15class GoogleProjection:
16  def __init__(self,levels=18):
17    self.Bc = []
18    self.Cc = []
19    self.zc = []
20    self.Ac = []
21    c = 256
22    for d in range(0,levels):
23      e = c/2;
24      self.Bc.append(c/360.0)
25      self.Cc.append(c/(2 * pi))
26      self.zc.append((e,e))
27      self.Ac.append(c)
28      c *= 2
29
30  def fromLLtoPixel(self,ll,zoom):
31    d = self.zc[zoom]
32    e = round(d[0] + ll[0] * self.Bc[zoom])
33    f = minmax(sin(DEG_TO_RAD * ll[1]),-0.9999,0.9999)
34    g = round(d[1] + 0.5*log((1+f)/(1-f))*-self.Cc[zoom])
35    return (e,g)
36
37  def fromPixelToLL(self,px,zoom):
38    e = self.zc[zoom]
39    f = (px[0] - e[0])/self.Bc[zoom]
40    g = (px[1] - e[1])/-self.Cc[zoom]
41    h = RAD_TO_DEG * ( 2 * atan(exp(g)) - 0.5 * pi)
42    return (f,h)
43
44
45import os
46from PIL.Image import fromstring, new
47from PIL.ImageDraw import Draw
48from StringIO import StringIO
49from mapnik import *
50
51
52mapfile = "/home/steve/osm.xml"
53tile_dir = '/tmp/tiles/'
54maxZoom = 18
55
56gprj = GoogleProjection(maxZoom+1)
57m = Map(2 * 256,2 * 256)
58load_map(m,mapfile)
59prj = Projection("+proj=merc +datum=WGS84")
60
61def dotile(x,y,z):
62  x_str = "%s" % x
63  y_str = "%s" % y
64  z_str = "%s" % z
65
66  p0 = gprj.fromPixelToLL((x * 256, (y+1) * 256),z)
67  p1 = gprj.fromPixelToLL(((x+1) * 256, y*  256),z)
68
69  print z,x,y,p0,p1
70  # render a new tile and store it on filesystem
71  c0 = prj.forward(Coord(p0[0],p0[1]))
72  c1 = prj.forward(Coord(p1[0],p1[1]))
73
74  bbox = Envelope(c0.x,c0.y,c1.x,c1.y)
75  bbox.width(bbox.width() * 2)
76  bbox.height(bbox.height() * 2)
77  m.zoom_to_box(bbox)
78
79  if not os.path.isdir(tile_dir + z_str):
80    os.mkdir(tile_dir + z_str)
81  if not os.path.isdir(tile_dir + z_str + '/' + x_str):
82    os.mkdir(tile_dir + z_str + '/' + x_str)
83
84  tile_uri = tile_dir + z_str + '-' + x_str + '-' + y_str + '.png'
85  im = Image(512, 512)
86  render(m, im)
87  im = fromstring('RGBA', (512, 512), rawdata(im))
88  im = im.crop((128,128,512-128,512-128))
89  fh = open(tile_uri,'w+b')
90  im.save(fh, 'PNG', quality=100)
91  command = "convert  -colors 255 %s %s" % (tile_uri,tile_uri)
92  p = Popen(command, shell=True)
93  sts = os.waitpid(p.pid, 0)
94
95  command = "/home/steve/insert_tile.rb %s %s %s %s" % (x_str, y_str, z_str, tile_uri)
96  p = Popen(command, shell=True)
97  sts = os.waitpid(p.pid, 0)
98
99for line in fileinput.input():
100  tile_data = line.rstrip('\n').split(' ')
101#  if(eval(tile_data[0]) > 11):
102  dotile(eval(tile_data[0]), eval(tile_data[1]), eval(tile_data[2]))
103
104
Note: See TracBrowser for help on using the repository browser.