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)
40 print maxzoom,':',minx,'-',maxx,'/',miny,'-',maxy
41 if (maxx-minx>16) or (maxy-miny>12) or (maxzoom==16):
50 print minlat,minlon,maxlat,maxlon,minzoom,maxzoom
51 ins.execute('insert into render_queue(minlat,maxlat,minlon,maxlon,minzoom,maxzoom) values(?,?,?,?,?,?)',(minlat,maxlat,minlon,maxlon,minzoom,maxzoom))
55 def process_queue(db,map,force=False,backend="renderd"):
59 conn = sqlite3.connect(db)
61 cur.execute('select id,minlat,maxlat,minlon,maxlon,minzoom,maxzoom from render_queue')
66 id,minlat,maxlat,minlon,maxlon,minzoom,maxzoom=rec
68 if backend == "tirex" or backend == "default":
70 command = 'map='+map+ \
71 ' z='+str(minzoom)+'-'+str(maxzoom)+ \
72 ' lat='+str(minlat)+','+str(maxlat)+ \
73 ' lon='+str(minlon)+','+str(maxlon)
76 command = 'tirex-batch -n 0 --prio=50 '+command
78 command = 'tirex-batch -n 0 --prio=50 '+command+' -f not-exists'
82 if system(command)==0:
84 dcur.execute('delete from render_queue where id=?',(id,))
87 elif backend == "renderd":
89 print minlat,minlon,maxlat,maxlon
91 for zoom in range(minzoom,maxzoom+1):
92 minx,miny=deg2num(minlat,minlon,zoom)
93 maxx,maxy=deg2num(maxlat,maxlon,zoom)
105 print zoom,minx,miny,maxx,maxy
107 maps = map.split(',')
109 for map_name in maps:
111 command = 'render_list -a -m '+map+ \
112 ' -z '+str(zoom)+' -Z '+str(zoom)+ \
113 ' -x '+str(minx)+' -X '+str(maxx)+ \
114 ' -y '+str(miny)+' -Y '+str(maxy)
116 command = command+ ' --force'
120 if system(command)<>0:
124 dcur.execute('delete from render_queue where id=?',(id,))
129 from optparse import OptionParser
131 parser = OptionParser()
132 parser.add_option("-d", "--data", dest="directory",
133 help="Data directory", metavar="DIR")
134 parser.add_option("-m", "--map", dest="map",
135 help="Map name", metavar="MAP")
136 parser.add_option("-z", "--zoom", dest="zoom",
137 help="Maximal zoom (forced), used with coordinates pairs (minlat minlon maxlat maxlon) or filename in arguments", metavar="MAP")
138 parser.add_option("-f", "--force", dest="force",
139 help="Force tile regeneration (on/off), default off")
140 parser.add_option("-r", "--renderer", dest="renderer",
141 help="Rendering backend: tirex or renderd")
142 (options, args) = parser.parse_args()
144 db=options.directory+'/gpx.db'
149 force=(options.force=='on')
152 renderer=options.renderer
154 print "Using default rendering backend..."
159 print "Rendering file: "+filename+"\n"
160 queue_render(db,filename)
162 minlat,minlon,maxlat,maxlon=args
163 print "Rendering region "+minlat+'..'+maxlat+' / '+minlon+'..'+maxlon+"\n"
164 queue_tiles(db,float(minlat),float(minlon),float(maxlat),float(maxlon),int(zoom))
167 print "Processing map(s) "+map+"\n"
168 process_queue(db,map,force,renderer)
170 if __name__ == "__main__":