Упрощенный порядок вызова - с передачей координат вместо номера тайла.
[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,forced_max_zoom=None):
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,forced_max_zoom)
16             
17 def queue_tiles(db,minlat,minlon,maxlat,maxlon,forced_max_zoom=None):
18
19   conn = sqlite3.connect(db)
20   
21   # определяем примерный стартовый зум
22   
23   minzoom=8
24   if forced_max_zoom:
25     maxzoom=forced_max_zoom
26   else:
27     maxzoom=16  
28   
29   ins = conn.cursor()
30   print minlat,maxlat,minlon,maxlon,minzoom,maxzoom
31   ins.execute('insert into render_queue(minlat,maxlat,minlon,maxlon,minzoom,maxzoom) values(?,?,?,?,?,?)',(minlat,maxlat,minlon,maxlon,minzoom,maxzoom))
32
33   conn.commit()        
34
35 def process_queue(db,map,force=False):
36
37   from os import system
38
39   conn = sqlite3.connect(db)
40   cur = conn.cursor()
41   cur.execute('select id,minlat,maxlat,minlon,maxlon,minzoom,maxzoom from render_queue')
42   list=cur.fetchall()
43
44   for rec in list:
45   
46     id,minlat,maxlat,minlon,maxlon,minzoom,maxzoom=rec
47
48     command = 'map='+map+ \
49       ' z='+str(minzoom)+'-'+str(maxzoom)+ \
50       ' lat='+str(minlat)+','+str(maxlat)+ \
51       ' lon='+str(minlon)+','+str(maxlon)
52
53     if force:
54       command = 'tirex-batch -n 0 '+command
55     else:
56       command = 'tirex-batch -n 0 '+command+' -f not-exists'  
57     
58     print command  
59       
60     if system(command)==0:
61       dcur=conn.cursor()
62       dcur.execute('delete from render_queue where id=?',(id,))
63       conn.commit()
64
65 def main():
66
67   from optparse import OptionParser
68   
69   parser = OptionParser()
70   parser.add_option("-d", "--data", dest="directory",
71     help="Data directory", metavar="DIR")
72   parser.add_option("-m", "--map", dest="map",
73     help="Map name", metavar="MAP")
74   parser.add_option("-z", "--zoom", dest="zoom",
75     help="Maximal zoom (forced), used with coordinates pairs (minlat minlon maxlat maxlon) or filename in arguments", metavar="MAP")
76   parser.add_option("-f", "--force", dest="force", 
77     help="Force tile regeneration (on/off), default off")
78   (options, args) = parser.parse_args()  
79
80   print options,args
81
82   db=options.directory+'/gpx.db'
83   map=options.map  
84   zoom=options.zoom
85   force=(options.force=='on')
86
87   if zoom:
88     if len(args)==1:
89       filename,=args
90       print "Rendering file: "+filename+"\n"
91       queue_render(db,filename)
92     else:
93       minlat,minlon,maxlat,maxlon=args
94       queue_tiles(db,float(minlat),float(minlon),float(maxlat),float(maxlon),int(zoom))
95
96   if map:
97     process_queue(db,map,force)
98   
99 if __name__ == "__main__":
100
101   main()          
102