source: subversion/sites/tile.openstreetmap.org/render_from_list.py @ 5396

Last change on this file since 5396 was 4384, checked in by jonb, 13 years ago

update tile render_from_list script to match live server

  • 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 call
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/jburgess/live/osm.xml"
53tile_dir = '/tmp/tiles/'
54maxZoom = 18
55
56
57gprj = GoogleProjection(maxZoom+1)
58m = Map(2 * 256,2 * 256)
59load_map(m,mapfile)
60prj = Projection("+proj=merc +datum=WGS84")
61
62def dotile(x,y,z):
63  x_str = "%s" % x
64  y_str = "%s" % y
65  z_str = "%s" % z
66
67  p0 = gprj.fromPixelToLL((x * 256, (y+1) * 256),z)
68  p1 = gprj.fromPixelToLL(((x+1) * 256, y*  256),z)
69
70  print z,x,y,p0,p1
71  # render a new tile and store it on filesystem
72  c0 = prj.forward(Coord(p0[0],p0[1]))
73  c1 = prj.forward(Coord(p1[0],p1[1]))
74
75  bbox = Envelope(c0.x,c0.y,c1.x,c1.y)
76  bbox.width(bbox.width() * 2)
77  bbox.height(bbox.height() * 2)
78  m.zoom_to_box(bbox)
79
80  #if not os.path.isdir(tile_dir + z_str):
81  #  os.mkdir(tile_dir + z_str)
82  #if not os.path.isdir(tile_dir + z_str + '/' + x_str):
83  #  os.mkdir(tile_dir + z_str + '/' + x_str)
84
85  tile_uri = tile_dir + z_str + '-' + x_str + '-' + y_str + '.png'
86  # This can happen when 2 renderers step on each other
87  if os.path.isfile(tile_uri):
88    return
89
90  im = Image(512, 512)
91  render(m, im)
92  im = fromstring('RGBA', (512, 512), rawdata(im))
93  im = im.crop((128,128,512-128,512-128))
94  fh = open(tile_uri,'w+b')
95  im.save(fh, 'PNG', quality=100)
96  #call(["convert", "-colors", "255", tile_uri, tile_uri])
97  call(["color255", tile_uri]) 
98  call(["/home/jburgess/live/insert_tile.rb", x_str, y_str, z_str, tile_uri])
99 
100if not os.path.isdir(tile_dir):
101  os.mkdir(tile_dir)
102
103for line in fileinput.input():
104  tile_data = line.rstrip('\n').split(' ')
105#  if(eval(tile_data[0]) > 11):
106  dotile(eval(tile_data[0]), eval(tile_data[1]), eval(tile_data[2]))
107
108
Note: See TracBrowser for help on using the repository browser.