def zoom(self): print "zoom called" #user=meta.Session.query(User).filter( # User.user==tripuser()).one() if request.params['zoom'] == 'auto': if session.get('showarea', '') != '': zoom = 13 minx = 1e30 maxx = -1e30 miny = 1e30 maxy = -1e30 for vert in mapper.parse_lfv_area(session.get('showarea')): merc = mapper.latlon2merc(mapper.from_str(vert), zoom) minx = min(minx, merc[0]) miny = min(miny, merc[1]) maxx = max(maxx, merc[0]) maxy = max(maxy, merc[1]) if maxy < -1e29: self.set_pos_zoom( (59, 18), 6, ) else: size = max(maxx - minx, maxy - miny) if (maxx == minx and maxy == miny): zoom = 10 else: nominal_size = 400 while zoom >= 0 and size > nominal_size: zoom -= 1 size /= 2.0 pos = (int(0.5 * (maxx + minx)), int(0.5 * (maxy + miny))) latlon = mapper.merc2latlon(pos, 13) self.set_pos_zoom(latlon, zoom) elif session.get('showtrack', None) != None: strack = session.get('showtrack') zoom = 13 minx, miny = mapper.latlon2merc(strack.bb1, 13) maxx, maxy = mapper.latlon2merc(strack.bb2, 13) pos = (int(0.5 * (maxx + minx)), int(0.5 * (maxy + miny))) latlon = mapper.merc2latlon(pos, 13) print "AutoZooming to pos", latlon size = max(maxx - minx, maxy - miny, 1) nominal_size = 400 while zoom >= 0 and size > nominal_size: zoom -= 1 size /= 2.0 self.set_pos_zoom(latlon, zoom) else: #mapper.parse_lfv_area() self.set_pos_zoom((59, 18), 6) print "Autozoom zooming to level %d at %s" % (session['zoom'], session['last_pos']) else: zoomlevel = float(request.params['zoom']) if zoomlevel < 0: zoomlevel = 0 pos = mapper.merc2latlon( tuple([int(x) for x in request.params['center'].split(",")]), zoomlevel) if session.get('mapvariant', None) == 'elev': if zoomlevel > 8: zoomlevel = 8 else: if zoomlevel > 13: zoomlevel = 13 print "Zoomlevel: %s" % (zoomlevel, ) print "Pos:", pos self.set_pos_zoom(pos, zoomlevel) redirect(h.url_for(controller='mapview', action="index"))
def get_notam_objs(kind=None): notamupdates = meta.Session.query(NotamUpdate).filter( NotamUpdate.disappearnotam == sa.null()).all() obstacles = [] others = [] spaces = [] areas = [] for u in notamupdates: text = u.text.strip() if text.count("W52355N0234942E"): text = text.replace("W52355N0234942E", "652355N0234942E") coordgroups = [] for line in text.split("\n"): dig = False for char in line: if char.isdigit(): dig = True if dig == False: if len(coordgroups) and coordgroups[-1] != "": coordgroups.append("") else: if len(coordgroups) == 0: coordgroups = [""] coordgroups[-1] += line + "\n" if (kind == None or kind == "notamarea"): for radius, unit, lat, lon in chain( re.findall( r"RADIUS\s*(?:OF)?\s*(\d+)\s*(NM|M)\s*(?:CENT[ERD]+|FR?O?M)?\s*(?:ON)?\s*(?:AT)?\s*(\d+[NS])\s*(\d+[EW])", text), re.findall( r"(\d+)\s*(NM|M)\s*RADIUS\s*(?:CENT[ERD]+)?\s*(?:ON|AT|FROM)?\s*(\d+[NS])\s*(\d+[EW])", text), re.findall( r"(\d+)\s*(NM|M)\s*RADIUS.*?[^0-9](\d+[NS])\s*(\d+[EW])", text, re.DOTALL)): try: radius = float(radius) if unit == "M": radius = radius / 1852.0 else: assert unit == "NM" centre = mapper.parse_coords(lat, lon) coords = mapper.create_circle(centre, radius) areas.append( dict(points=coords, kind="notamarea", name=text, type="notamarea", notam_ordinal=u.appearnotam, notam_line=u.appearline, notam=text)) except Exception, cause: print "Invalid notam coords: %s,%s" % (lat, lon) for coordgroup in coordgroups: try: coords = list(mapper.parse_lfv_area(coordgroup, False)) except Exception, cause: print "Parsing,", coordgroup print "Exception parsing lfv area from notam:%s" % (cause, ) coords = [] if len(coords) == 0: continue if text.count("OBST") and (kind == None or kind == "obstacle"): elevs = re.findall(r"ELEV\s*(\d+)\s*FT", text) elevs = [int(x) for x in elevs if x.isdigit()] if len(elevs) != 0: elev = max(elevs) for coord in coords: obstacles.append( dict(pos=coord, elev=elev, elevf=mapper.parse_elev(elev), kind='notam', notam_ordinal=u.appearnotam, notam_line=u.appearline, name=text.split("\n")[0], notam=text)) continue couldbearea = True if len(coords) <= 2: couldbearea = False if text.count("PSN") >= len(coords) - 2: couldbearea = False if couldbearea == False and (kind == None or kind == "notam"): for coord in coords: others.append( dict(pos=coord, kind='notam', name=text, notam_ordinal=u.appearnotam, notam_line=u.appearline, notam=text)) if couldbearea == True and (kind == None or kind == "notamarea"): if len(coords) > 2: if text.startswith("AREA: "): continue #These aren't real notams, they're area-specifications for all other notams... make this better some day. areas.append( dict(points=coords, kind="notamarea", name=text, type="notamarea", notam_ordinal=u.appearnotam, notam_line=u.appearline, notam=text))
def get_notam_objs(kind=None): notamupdates=meta.Session.query(NotamUpdate).filter( NotamUpdate.disappearnotam==sa.null()).all() obstacles=[] others=[] spaces=[] areas=[] for u in notamupdates: text=u.text.strip() if text.count("W52355N0234942E"): text=text.replace("W52355N0234942E","652355N0234942E") coordgroups=[] for line in text.split("\n"): dig=False for char in line: if char.isdigit(): dig=True if dig==False: if len(coordgroups) and coordgroups[-1]!="": coordgroups.append("") else: if len(coordgroups)==0: coordgroups=[""] coordgroups[-1]+=line+"\n" if (kind==None or kind=="notamarea"): for radius,unit,lat,lon in chain( re.findall(r"RADIUS\s*(?:OF)?\s*(\d+)\s*(NM|M)\s*(?:CENT[ERD]+|FR?O?M)?\s*(?:ON)?\s*(?:AT)?\s*(\d+[NS])\s*(\d+[EW])",text), re.findall(r"(\d+)\s*(NM|M)\s*RADIUS\s*(?:CENT[ERD]+)?\s*(?:ON|AT|FROM)?\s*(\d+[NS])\s*(\d+[EW])",text), re.findall(r"(\d+)\s*(NM|M)\s*RADIUS.*?[^0-9](\d+[NS])\s*(\d+[EW])",text,re.DOTALL) ): try: radius=float(radius) if unit=="M": radius=radius/1852.0 else: assert unit=="NM" centre=mapper.parse_coords(lat,lon) coords=mapper.create_circle(centre,radius) areas.append(dict( points=coords, kind="notamarea", name=text, type="notamarea", notam_ordinal=u.appearnotam, notam_line=u.appearline, notam=text)) except Exception,cause: print "Invalid notam coords: %s,%s"%(lat,lon) for coordgroup in coordgroups: try: coords=list(mapper.parse_lfv_area(coordgroup,False)) except Exception,cause: print "Parsing,",coordgroup print "Exception parsing lfv area from notam:%s"%(cause,) coords=[] if len(coords)==0: continue if text.count("OBST") and (kind==None or kind=="obstacle"): elevs=re.findall(r"ELEV\s*(\d+)\s*FT",text) elevs=[int(x) for x in elevs if x.isdigit()] if len(elevs)!=0: elev=max(elevs) for coord in coords: obstacles.append(dict( pos=coord, elev=elev, elevf=mapper.parse_elev(elev), kind='notam', notam_ordinal=u.appearnotam, notam_line=u.appearline, name=text.split("\n")[0], notam=text)) continue couldbearea=True if len(coords)<=2: couldbearea=False if text.count("PSN")>=len(coords)-2: couldbearea=False if couldbearea==False and (kind==None or kind=="notam"): for coord in coords: others.append(dict( pos=coord, kind='notam', name=text, notam_ordinal=u.appearnotam, notam_line=u.appearline, notam=text)) if couldbearea==True and (kind==None or kind=="notamarea"): if len(coords)>2: if text.startswith("AREA: "): continue #These aren't real notams, they're area-specifications for all other notams... make this better some day. areas.append(dict( points=coords, kind="notamarea", name=text, type="notamarea", notam_ordinal=u.appearnotam, notam_line=u.appearline, notam=text))
def get(self): # Return a rendered template #return render('/maptile.mako') # or, return a response my = int(request.params.get('mercy')) mx = int(request.params.get('mercx')) zoomlevel = int(request.params.get('zoom')) #print request.params #print "dynid: ",request.params.get('dynamic_id','None') if 'showairspaces' in request.params and request.params[ 'showairspaces']: variant = "airspace" else: variant = "plain" variant = request.params.get('mapvariant', variant) #merc_limx1,merc_limy1,merc_limx2,merc_limy2=maptilereader.merc_limits(zoomlevel) #if mx>merc_limx2 or my>merc_limy2 or mx<merc_limx1 or my<merc_limy1: # if variant=='airspace': # variant="plain" neededit = False if session.get('showarea', '') != '': neededit = True if session.get('showtrack', None) != None: neededit = True mtime = request.params.get('mtime', None) user = session.get('user', None) generate_on_the_fly = False if user and userdata.have_any_for(user): generate_on_the_fly = True #print "get: %d,%d,%d (showair:%s, neededit: %s)"%(mx,my,zoomlevel,airspaces,neededit) if generate_on_the_fly: only_user = False if variant == 'plain': only_user = True print "Only:", only_user im = generate_big_tile((256, 256), mx, my, zoomlevel, osmdraw=True, tma=True, return_format="cairo", user=user, only_user=only_user) tilemeta = dict(status="ok") else: #print "Getting %s,%s,%s,%d,%d"%(mx,my,zoomlevel,mx%256,my%256) rawtile, tilemeta = maptilereader.gettile(variant, zoomlevel, mx, my, mtime) if not neededit: response.headers['Pragma'] = '' response.headers['Content-Type'] = 'image/png' if tilemeta['status'] != "ok": response.headers['Cache-Control'] = 'max-age=30' else: response.headers['Cache-Control'] = 'max-age=3600' return rawtile io = StringIO.StringIO(rawtile) io.seek(0) im = cairo.ImageSurface.create_from_png(io) ctx = cairo.Context(im) if session.get('showarea', '') != '': #print "Showarea rendering active",zoomlevel wp = [] #print session.get('showarea','') for vert in mapper.parse_lfv_area(session.get('showarea')): mercx, mercy = mapper.latlon2merc(mapper.from_str(vert), zoomlevel) wp.append((mercx - mx, mercy - my)) #print "wp:",wp if len(wp) > 0: ctx.new_path() ctx.set_line_width(2.0) if len(wp) == 1: w, = wp ctx.arc(w[0], w[1], 8, 0, 2 * math.pi) ctx.set_source(cairo.SolidPattern(0.0, 0.0, 1.0, 0.25)) ctx.fill_preserve() ctx.set_source(cairo.SolidPattern(0.0, 0.0, 1.0, 1)) ctx.stroke() elif len(wp) == 2: ctx.set_source(cairo.SolidPattern(0.0, 0.0, 1.0, 1)) ctx.new_path() ctx.move_to(*wp[0]) ctx.line_to(*wp[1]) ctx.stroke() ctx.arc(wp[0][0], wp[0][1], 8, 0, 2 * math.pi) ctx.stroke() ctx.new_path() ctx.arc(wp[1][0], wp[1][1], 8, 0, 2 * math.pi) ctx.stroke() else: for w in wp: ctx.line_to(*w) ctx.close_path() ctx.set_source(cairo.SolidPattern(0.0, 0.0, 1.0, 0.25)) ctx.fill_preserve() ctx.set_source(cairo.SolidPattern(0.0, 0.0, 1.0, 1)) ctx.stroke() ctx.set_source(cairo.SolidPattern(0.0, 0.0, 0.0, 1.0)) for idx, w in enumerate(wp): ctx.move_to(*w) ctx.show_text("#%d" % (idx, )) if session.get('showtrack', None) != None: print "Showtrack rendering active" track = session.get('showtrack') ctx.new_path() ctx.set_line_width(2.0) ctx.set_source(cairo.SolidPattern(0.0, 0.0, 1.0, 1)) #lastmecc print "%d points" % (len(track.points)) for p, height, dtim in track.points: merc = mapper.latlon2merc(p, zoomlevel) p = ((merc[0] - mx, merc[1] - my)) ctx.line_to(*p) ctx.stroke() buf = StringIO.StringIO() im.write_to_png(buf) png = buf.getvalue() """ im=Image.open(path) draw=ImageDraw.Draw(im) if session.get('showarea','')!='': wp=[] print session.get('showarea','') for vert in mapper.parse_lfv_area(session.get('showarea')): mercx,mercy=mapper.latlon2merc(mapper.from_str(vert),zoomlevel) wp.append((mercx-mx,mercy-my)) print "wp:",wp if len(wp)>0: if len(wp)==1: draw.ellipse((wp[0][0]-5,wp[0][1]-5,wp[0][0]+5,wp[0][1]+5),fill="#ff0000") elif len(wp)==2: draw.line(wp,fill="#ff0000",) else: draw.polygon(wp,fill="#ff0000",) buf= StringIO.StringIO() im.save(buf, format= 'PNG') png=buf.getvalue() """ #print "Corners:",get_map_corners(pixelsize=(width,height),center=pos,lolat=lower,hilat=upper) response.headers['Pragma'] = '' if generate_on_the_fly: response.headers['Cache-Control'] = 'max-age=10' elif tilemeta['status'] != "ok": response.headers['Cache-Control'] = 'max-age=30' else: response.headers['Cache-Control'] = 'max-age=3600' response.headers['Content-Type'] = 'image/png' return png
def get(self): # Return a rendered template #return render('/maptile.mako') # or, return a response my=int(request.params.get('mercy')) mx=int(request.params.get('mercx')) zoomlevel=int(request.params.get('zoom')) #print request.params #print "dynid: ",request.params.get('dynamic_id','None') if 'showairspaces' in request.params and request.params['showairspaces']: variant="airspace" else: variant="plain" variant=request.params.get('mapvariant',variant) #merc_limx1,merc_limy1,merc_limx2,merc_limy2=maptilereader.merc_limits(zoomlevel) #if mx>merc_limx2 or my>merc_limy2 or mx<merc_limx1 or my<merc_limy1: # if variant=='airspace': # variant="plain" neededit=False if session.get('showarea','')!='': neededit=True if session.get('showtrack',None)!=None: neededit=True mtime=request.params.get('mtime',None) user=session.get('user',None) generate_on_the_fly=False if user and userdata.have_any_for(user): generate_on_the_fly=True #print "get: %d,%d,%d (showair:%s, neededit: %s)"%(mx,my,zoomlevel,airspaces,neededit) if generate_on_the_fly: only_user=False if variant=='plain': only_user=True print "Only:",only_user im=generate_big_tile((256,256),mx,my,zoomlevel,osmdraw=True,tma=True,return_format="cairo",user=user,only_user=only_user) tilemeta=dict(status="ok") else: #print "Getting %s,%s,%s,%d,%d"%(mx,my,zoomlevel,mx%256,my%256) rawtile,tilemeta=maptilereader.gettile(variant,zoomlevel,mx,my,mtime) if not neededit: response.headers['Pragma'] = '' response.headers['Content-Type'] = 'image/png' if tilemeta['status']!="ok": response.headers['Cache-Control'] = 'max-age=30' else: response.headers['Cache-Control'] = 'max-age=3600' return rawtile io=StringIO.StringIO(rawtile) io.seek(0) im=cairo.ImageSurface.create_from_png(io) ctx=cairo.Context(im) if session.get('showarea','')!='': #print "Showarea rendering active",zoomlevel wp=[] #print session.get('showarea','') for vert in mapper.parse_lfv_area(session.get('showarea')): mercx,mercy=mapper.latlon2merc(mapper.from_str(vert),zoomlevel) wp.append((mercx-mx,mercy-my)) #print "wp:",wp if len(wp)>0: ctx.new_path() ctx.set_line_width(2.0) if len(wp)==1: w,=wp ctx.arc(w[0],w[1],8,0,2*math.pi) ctx.set_source(cairo.SolidPattern(0.0,0.0,1.0,0.25)) ctx.fill_preserve() ctx.set_source(cairo.SolidPattern(0.0,0.0,1.0,1)) ctx.stroke() elif len(wp)==2: ctx.set_source(cairo.SolidPattern(0.0,0.0,1.0,1)) ctx.new_path() ctx.move_to(*wp[0]) ctx.line_to(*wp[1]) ctx.stroke() ctx.arc(wp[0][0],wp[0][1],8,0,2*math.pi) ctx.stroke() ctx.new_path() ctx.arc(wp[1][0],wp[1][1],8,0,2*math.pi) ctx.stroke() else: for w in wp: ctx.line_to(*w) ctx.close_path() ctx.set_source(cairo.SolidPattern(0.0,0.0,1.0,0.25)) ctx.fill_preserve() ctx.set_source(cairo.SolidPattern(0.0,0.0,1.0,1)) ctx.stroke() ctx.set_source(cairo.SolidPattern(0.0,0.0,0.0,1.0)) for idx,w in enumerate(wp): ctx.move_to(*w) ctx.show_text("#%d"%(idx,)) if session.get('showtrack',None)!=None: print "Showtrack rendering active" track=session.get('showtrack') ctx.new_path() ctx.set_line_width(2.0) ctx.set_source(cairo.SolidPattern(0.0,0.0,1.0,1)) #lastmecc print "%d points"%(len(track.points)) for p,height,dtim in track.points: merc=mapper.latlon2merc(p,zoomlevel) p=((merc[0]-mx,merc[1]-my)) ctx.line_to(*p) ctx.stroke() buf= StringIO.StringIO() im.write_to_png(buf) png=buf.getvalue() """ im=Image.open(path) draw=ImageDraw.Draw(im) if session.get('showarea','')!='': wp=[] print session.get('showarea','') for vert in mapper.parse_lfv_area(session.get('showarea')): mercx,mercy=mapper.latlon2merc(mapper.from_str(vert),zoomlevel) wp.append((mercx-mx,mercy-my)) print "wp:",wp if len(wp)>0: if len(wp)==1: draw.ellipse((wp[0][0]-5,wp[0][1]-5,wp[0][0]+5,wp[0][1]+5),fill="#ff0000") elif len(wp)==2: draw.line(wp,fill="#ff0000",) else: draw.polygon(wp,fill="#ff0000",) buf= StringIO.StringIO() im.save(buf, format= 'PNG') png=buf.getvalue() """ #print "Corners:",get_map_corners(pixelsize=(width,height),center=pos,lolat=lower,hilat=upper) response.headers['Pragma'] = '' if generate_on_the_fly: response.headers['Cache-Control'] = 'max-age=10' elif tilemeta['status']!="ok": response.headers['Cache-Control'] = 'max-age=30' else: response.headers['Cache-Control'] = 'max-age=3600' response.headers['Content-Type'] = 'image/png' return png
def zoom(self): print "zoom called" #user=meta.Session.query(User).filter( # User.user==tripuser()).one() if request.params['zoom']=='auto': if session.get('showarea','')!='': zoom=13 minx=1e30 maxx=-1e30 miny=1e30 maxy=-1e30 for vert in mapper.parse_lfv_area(session.get('showarea')): merc=mapper.latlon2merc(mapper.from_str(vert),zoom) minx=min(minx,merc[0]) miny=min(miny,merc[1]) maxx=max(maxx,merc[0]) maxy=max(maxy,merc[1]) if maxy<-1e29: self.set_pos_zoom((59,18),6,) else: size=max(maxx-minx,maxy-miny) if (maxx==minx and maxy==miny): zoom=10 else: nominal_size=400 while zoom>=0 and size>nominal_size: zoom-=1 size/=2.0 pos=(int(0.5*(maxx+minx)),int(0.5*(maxy+miny))) latlon=mapper.merc2latlon(pos,13) self.set_pos_zoom(latlon,zoom) elif session.get('showtrack',None)!=None: strack=session.get('showtrack') zoom=13 minx,miny=mapper.latlon2merc(strack.bb1,13) maxx,maxy=mapper.latlon2merc(strack.bb2,13) pos=(int(0.5*(maxx+minx)),int(0.5*(maxy+miny))) latlon=mapper.merc2latlon(pos,13) print "AutoZooming to pos",latlon size=max(maxx-minx,maxy-miny,1) nominal_size=400 while zoom>=0 and size>nominal_size: zoom-=1 size/=2.0 self.set_pos_zoom(latlon,zoom) else: #mapper.parse_lfv_area() self.set_pos_zoom((59,18),6) print "Autozoom zooming to level %d at %s"%(session['zoom'],session['last_pos']) else: zoomlevel=float(request.params['zoom']) if zoomlevel<0: zoomlevel=0 pos=mapper.merc2latlon(tuple([int(x) for x in request.params['center'].split(",")]),zoomlevel) if session.get('mapvariant',None)=='elev': if zoomlevel>8: zoomlevel=8 else: if zoomlevel>13: zoomlevel=13 print "Zoomlevel: %s"%(zoomlevel,) print "Pos:",pos self.set_pos_zoom(pos,zoomlevel) redirect(h.url_for(controller='mapview',action="index"))