Minor rewrites
[pyrungps.git] / render_tiles.py
1 #!/usr/bin/env python
2 # coding: UTF-8
3
4 import sqlite3
5 import math
6 from pprint import pprint
7
8 def queue_render(db,filename):
9
10   conn = sqlite3.connect(db)
11   cur = conn.cursor()
12         
13   cur.execute("select minlat,minlon,maxlat,maxlon from tracks where filename=?" , (filename.decode('UTF-8'),))
14   minlat,minlon,maxlat,maxlon=cur.fetchone()
15   queue_tiles(db,minlat,minlon,maxlat,maxlon)
16             
17 def queue_tiles(db,minlat,minlon,maxlat,maxlon):
18
19   conn = sqlite3.connect(db)
20   
21   # определяем примерный стартовый зум
22   
23   minlatrad = math.radians(minlat)
24   maxlatrad = math.radians(maxlat)
25   
26   minx = (minlon + 180.0)/360.0
27   maxx = (maxlon + 180.0)/360.0
28   
29   miny = (1.0 - math.log(math.tan(minlatrad) + (1 / math.cos(minlatrad))) / math.pi) / 2.0
30   maxy = (1.0 - math.log(math.tan(maxlatrad) + (1 / math.cos(maxlatrad))) / math.pi) / 2.0
31
32   if minx>maxx:
33     minx,maxx = maxx,minx
34     
35   if miny>maxy:
36     miny,maxy = maxy,miny
37   
38   for zoom in range(9,16):
39     
40     n = 2 ** zoom
41     
42     minxt = int(minx * n)
43     minyt = int(miny * n)
44     maxxt = int(maxx * n)
45     maxyt = int(maxy * n)
46     
47     ins = conn.cursor()
48     print zoom,minxt,maxxt,minyt,maxyt
49     ins.execute('insert into render_queue(zoom,minx,maxx,miny,maxy) values(?,?,?,?,?)',(zoom,minxt,maxxt,minyt,maxyt))
50
51     if (maxxt-minxt>16) or (maxyt-minyt>12):
52       conn.commit()
53       break
54
55   conn.commit()        
56
57 def process_queue(db,map):
58
59   from os import system
60
61   conn = sqlite3.connect(db)
62   cur = conn.cursor()
63   cur.execute('select id,zoom,minx,maxx,miny,maxy from render_queue')
64   list=cur.fetchall()
65
66   for rec in list:
67   
68     id,zoom,minx,maxx,miny,maxy=rec
69
70     command = 'render_list -a -m '+map+ \
71       ' -z '+str(zoom)+' -Z '+str(zoom)+ \
72       ' -x '+str(minx)+' -X '+str(maxx)+ \
73       ' -y '+str(miny)+' -Y '+str(maxy)
74     if system(command)==0:
75       dcur=conn.cursor()
76       dcur.execute('delete from render_queue where id=?',(id,))
77       conn.commit()
78
79 def main():
80
81   from optparse import OptionParser
82   
83   parser = OptionParser()
84   parser.add_option("-d", "--data", dest="directory",
85     help="Data directory", metavar="DIR")
86   parser.add_option("-m", "--map", dest="map",
87     help="Map name", metavar="MAP")
88   (options, args) = parser.parse_args()  
89
90   db=options.directory+'/gpx.db'
91   map=options.map  
92
93   process_queue(db,map)
94   
95 if __name__ == "__main__":
96
97   main()          
98