def do_routing(request, response): # get request parameters from_location = maps.GeoPoint(*[float(x) for x in request.get('from').split(',')]) to_location = maps.GeoPoint(*[float(x) for x in request.get('to').split(',')]) view_mode = request.get('out', 'html') # produce data data = router.get_route(from_location, to_location) # populate the requested view if view_mode == 'json': view.to_json(data, response) else: view.to_html(data, 'routing', request, response)
def get_subway_route(start_location, end_location): route = router.get_route(start_location, end_location) if route == None: return None steps = [] first_subway = True for index, leg in enumerate(route): if leg.transport == 'Subway': if index > 0: if first_subway: to_text = 'Walk to subway station ' + utils.subway_color(leg.steps[0].station.name, leg.line) walk_time = max(int((leg.steps[0].distance + 30) / 60), 3) step = RouteStep(to_text, walk_time, utils.duration_to_string(walk_time), None, steps[-1].end_location, GeoPoint(leg.steps[0].station.lat, leg.steps[0].station.lng), True) else: to_text = 'Change to ' + utils.subway_color('line ' + str(leg.line), leg.line) + ' station ' + utils.subway_color(leg.steps[0].station.name, leg.line) change_time = max(int((leg.steps[0].distance + 30) / 60), 3) step = RouteStep(to_text, change_time, utils.duration_to_string(change_time)) steps.append(step) if first_subway: step = RouteStep('Buy the tokens if needed and enter subway station ' + utils.subway_color(leg.steps[0].station.name, leg.line), 5, utils.duration_to_string(5) + ', ' + utils.price_to_string(27), Transport('Subway', price = 27), GeoPoint(leg.steps[0].station.lat, leg.steps[0].station.lng)) #start location is needed for start walk steps.append(step) first_subway = False if len(leg.steps) > 1: # The change might be the first move => don't add a 0 steps way step = RouteStep() step.direction = 'Subway ' + utils.subway_color('line ' + str(leg.line), leg.line) + ' to ' + utils.subway_color(leg.steps[-1].station.name, leg.line) step.duration = int((sum([x.distance for x in leg.steps[1:]]) + 30) / 60) step.addinfo = utils.duration_to_string(step.duration) + ', ' + str(len(leg.steps) - 1) + ' stops' step.transport = Transport(leg.transport) step.start_location = GeoPoint(leg.steps[0].station.lat, leg.steps[0].station.lng) step.end_location = GeoPoint(leg.steps[-1].station.lat, leg.steps[-1].station.lng) step.has_map = True steps.append(step) else: if index > 0: to_text = 'Walk to ' + str(leg.transport).lower() + ' stop ' + leg.steps[0].station.name walk_time = max(int((leg.steps[0].distance + 30) / 60), 3) step = RouteStep(to_text, walk_time, utils.duration_to_string(walk_time), None, steps[-1].end_location, GeoPoint(leg.steps[0].station.lat, leg.steps[0].station.lng), True) steps.append(step) step = RouteStep() step.direction = leg.transport + ' ' + str(leg.line) + ' to ' + utils.subway_color(leg.steps[-1].station.name, leg.line) step.duration = sum([x.distance for x in leg.steps[1:]]) / 60 step.addinfo = utils.duration_to_string(step.duration) + ', ' + utils.price_to_string(23) step.transport = Transport(leg.transport, price = 23) step.start_location = GeoPoint(leg.steps[0].station.lat, leg.steps[0].station.lng) step.end_location = GeoPoint(leg.steps[-1].station.lat, leg.steps[-1].station.lng) step.has_map = True steps.append(step) start_walk = get_walking_route(start_location, steps[0].start_location) if start_walk.get_duration() > 0: steps.insert(0, start_walk.directions[0]) end_walk = get_walking_route(steps[-1].end_location, end_location) if end_walk.get_duration() > 0: steps.append(end_walk.directions[0]) if start_walk.get_duration() + end_walk.get_duration() < 40: return Route(steps)