6 from pprint import pprint
8 def queue_render(db,filename,forced_max_zoom=None):
10 conn = sqlite3.connect(db)
11 conn.text_factory = str
14 cur.execute("select minlat,minlon,maxlat,maxlon from tracks where filename=?" , (filename,))
15 minlat,minlon,maxlat,maxlon=cur.fetchone()
16 queue_tiles(db,minlat,minlon,maxlat,maxlon,forced_max_zoom)
18 def deg2num(lat_deg, lon_deg, zoom):
19 lat_rad = math.radians(lat_deg)
21 xtile = int((lon_deg + 180.0) / 360.0 * n)
22 ytile = int((1.0 - math.log(math.tan(lat_rad) + (1 / math.cos(lat_rad))) / math.pi) / 2.0 * n)
25 def queue_tiles(db,minlat,minlon,maxlat,maxlon,forced_max_zoom=None):
27 conn = sqlite3.connect(db)
29 # определяем примерный стартовый зум
34 maxzoom=forced_max_zoom
38 minx,miny=deg2num(minlat,minlon,maxzoom)
39 maxx,maxy=deg2num(maxlat,maxlon,maxzoom)
41 print(maxzoom,':',minx,'-',maxx,'/',miny,'-',maxy)
42 if (maxx-minx>16) or (maxy-miny>12) or (maxzoom==16):
51 print(minlat,minlon,maxlat,maxlon,minzoom,maxzoom)
52 ins.execute('insert into render_queue(minlat,maxlat,minlon,maxlon,minzoom,maxzoom) values(?,?,?,?,?,?)',(minlat,maxlat,minlon,maxlon,minzoom,maxzoom))
56 def process_queue(db,map,force=False,backend="renderd"):
60 conn = sqlite3.connect(db)
62 cur.execute('select id,minlat,maxlat,minlon,maxlon,minzoom,maxzoom from render_queue')
67 id,minlat,maxlat,minlon,maxlon,minzoom,maxzoom=rec
69 if backend == "tirex" or backend == "default":
71 command = 'map='+map+ \
72 ' z='+str(minzoom)+'-'+str(maxzoom)+ \
73 ' lat='+str(minlat)+','+str(maxlat)+ \
74 ' lon='+str(minlon)+','+str(maxlon)
77 command = 'tirex-batch -n 0 --prio=50 '+command
79 command = 'tirex-batch -n 0 --prio=50 '+command+' -f not-exists'
83 if system(command)==0:
85 dcur.execute('delete from render_queue where id=?',(id,))
88 elif backend == "renderd":
90 print(minlat,minlon,maxlat,maxlon)
92 for zoom in range(minzoom,maxzoom+1):
93 minx,miny=deg2num(minlat,minlon,zoom)
94 maxx,maxy=deg2num(maxlat,maxlon,zoom)
106 print(zoom,minx,miny,maxx,maxy)
108 maps = map.split(',')
110 for map_name in maps:
112 command = 'render_list -a -m '+map+ \
113 ' -z '+str(zoom)+' -Z '+str(zoom)+ \
114 ' -x '+str(minx)+' -X '+str(maxx)+ \
115 ' -y '+str(miny)+' -Y '+str(maxy)
117 command = command+ ' --force'
121 if not system(command) == 0:
125 dcur.execute('delete from render_queue where id=?',(id,))
130 from optparse import OptionParser
132 parser = OptionParser()
133 parser.add_option("-d", "--data", dest="directory",
134 help="Data directory", metavar="DIR")
135 parser.add_option("-m", "--map", dest="map",
136 help="Map name", metavar="MAP")
137 parser.add_option("-z", "--zoom", dest="zoom",
138 help="Maximal zoom (forced), used with coordinates pairs (minlat minlon maxlat maxlon) or filename in arguments", metavar="MAP")
139 parser.add_option("-f", "--force", dest="force",
140 help="Force tile regeneration (on/off), default off")
141 parser.add_option("-r", "--renderer", dest="renderer",
142 help="Rendering backend: tirex or renderd")
143 (options, args) = parser.parse_args()
145 db=options.directory+'/gpx.db'
150 force=(options.force=='on')
153 renderer=options.renderer
155 print("Using default rendering backend...")
160 print("Rendering file: "+filename+"\n")
161 queue_render(db,filename)
163 minlat,minlon,maxlat,maxlon=args
164 print("Rendering region "+minlat+'..'+maxlat+' / '+minlon+'..'+maxlon+"\n")
165 queue_tiles(db,float(minlat),float(minlon),float(maxlat),float(maxlon),int(zoom))
168 print("Processing map(s) "+map+"\n")
169 process_queue(db,map,force,renderer)
171 if __name__ == "__main__":