6 from pprint import pprint
8 def queue_render(db,filename,forced_max_zoom=None):
10 conn = sqlite3.connect(db)
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)
17 def deg2num(lat_deg, lon_deg, zoom):
18 lat_rad = math.radians(lat_deg)
20 xtile = int((lon_deg + 180.0) / 360.0 * n)
21 ytile = int((1.0 - math.log(math.tan(lat_rad) + (1 / math.cos(lat_rad))) / math.pi) / 2.0 * n)
24 def queue_tiles(db,minlat,minlon,maxlat,maxlon,forced_max_zoom=None):
26 conn = sqlite3.connect(db)
28 # определяем примерный стартовый зум
33 maxzoom=forced_max_zoom
37 minx,miny=deg2num(minlat,minlon,maxzoom)
38 maxx,maxy=deg2num(maxlat,maxlon,maxzoom)
39 print maxzoom,':',minx,'-',maxx,'/',miny,'-',maxy
40 if (maxx-minx>32) or (maxy-miny>24) or (maxzoom==16):
46 print minlat,maxlat,minlon,maxlon,minzoom,maxzoom
47 ins.execute('insert into render_queue(minlat,maxlat,minlon,maxlon,minzoom,maxzoom) values(?,?,?,?,?,?)',(minlat,maxlat,minlon,maxlon,minzoom,maxzoom))
51 def process_queue(db,map,force=False):
55 conn = sqlite3.connect(db)
57 cur.execute('select id,minlat,maxlat,minlon,maxlon,minzoom,maxzoom from render_queue')
62 id,minlat,maxlat,minlon,maxlon,minzoom,maxzoom=rec
64 command = 'map='+map+ \
65 ' z='+str(minzoom)+'-'+str(maxzoom)+ \
66 ' lat='+str(minlat)+','+str(maxlat)+ \
67 ' lon='+str(minlon)+','+str(maxlon)
70 command = 'tirex-batch -n 0 '+command
72 command = 'tirex-batch -n 0 '+command+' -f not-exists'
76 if system(command)==0:
78 dcur.execute('delete from render_queue where id=?',(id,))
83 from optparse import OptionParser
85 parser = OptionParser()
86 parser.add_option("-d", "--data", dest="directory",
87 help="Data directory", metavar="DIR")
88 parser.add_option("-m", "--map", dest="map",
89 help="Map name", metavar="MAP")
90 parser.add_option("-z", "--zoom", dest="zoom",
91 help="Maximal zoom (forced), used with coordinates pairs (minlat minlon maxlat maxlon) or filename in arguments", metavar="MAP")
92 parser.add_option("-f", "--force", dest="force",
93 help="Force tile regeneration (on/off), default off")
94 (options, args) = parser.parse_args()
98 db=options.directory+'/gpx.db'
101 force=(options.force=='on')
106 print "Rendering file: "+filename+"\n"
107 queue_render(db,filename)
109 minlat,minlon,maxlat,maxlon=args
110 queue_tiles(db,float(minlat),float(minlon),float(maxlat),float(maxlon),int(zoom))
113 process_queue(db,map,force)
115 if __name__ == "__main__":