def GET(self): if 'mode' not in web.input(): return render.app(timemode='now',time=datetime.today().strftime("%H:%M")) tvars = dict(web.input()) tvars['error'] = None fromplace = getattr(web.input(),'from') toplace = web.input().to if not fromplace or not toplace: tvars['error'] = 'Please enter an address or landmark for From and To' return render.app(**tvars) from_result,fromgeo = geocoder.geocode(fromplace) if from_result != 'OK': tvars['error'] = 'Unable to find address for %s' % fromplace return render.app(**tvars) tvars['fromgeo'] = fromgeo to_result,togeo = geocoder.geocode(toplace) if to_result != 'OK': tvars['error'] = 'Unable to find address for %s' % toplace return render.app(**tvars) tvars['togeo'] = togeo timemode = web.input().get('timemode') if timemode == 'now': result = otp.plan(fromgeo[0:2],togeo[0:2],web.input().mode) else: try: time = dateparser.parse_time(web.input().time) except ValueError: tvars['error'] = "Invalid time format" return render.app(**tvars) result = otp.plan(fromgeo[0:2],togeo[0:2],web.input().mode,time,timemode) if 'plan' in result: tvars['result'] = result else: # no itinerary found - rare but possible tvars['error'] = result['error']['msg'] return render.app(**tvars)
def directions(dirfrom,dirto,mode,at,atmode): if not mode: mode = 'ANY' from_result,from_place = geocode(dirfrom) if from_result != 'OK': return error_for_geocode(from_result,dirfrom) to_result,to_place = geocode(dirto) if to_result != 'OK': return error_for_geocode(to_result,dirto) date = None datemode = None if at: date = dateparser.parse_time(at) if atmode == 'arrive': datemode = 'arrive' else: datemode = 'depart' print "date chosen %s,mode=%s" % (date,datemode) plan = otp.plan(from_place[0:2],to_place[0:2],mode.upper(),date,datemode) return plan_instructions(plan)
import otp import json n = 1 def dump(result): global n f = open("test/data/plan-%d.json" % n,'w') json.dump(result,f,sort_keys=True,indent=2) f.close() n = n+1 if __name__ == '__main__': # Freret St loc1 = (29.934984,-90.109348) # French Quarter loc2 = (29.950138,-90.061519) # Fairgrounds loc3 = (29.991776,-90.083256) dump(otp.plan(loc1,loc2,'ANY')) dump(otp.plan(loc1,loc3,'ANY')) dump(otp.plan(loc2,loc2,'ANY')) dump(otp.plan(loc3,loc2,'BIKE')) dump(otp.plan(loc2,loc1,'BUS')) dump(otp.plan(loc2,loc3,'WALK'))
def test_plan(self): print otp.plan((29.976132,-90.080115),(29.949958,-90.109984),'BIKE',None)