예제 #1
0
 def _print_walk_path(walk_path, route_info):
     ret_string = ""
     
     # find distances between first and last point from last point on previous walk path
     first_pt_dist = vincenty(_get_lat_lon(walk_path.points[0])[0], _get_lat_lon(walk_path.points[0])[1], walk_path.lastlat, walk_path.lastlon)
     last_pt_dist = vincenty(_get_lat_lon(walk_path.points[-1])[0], _get_lat_lon(walk_path.points[-1])[1], walk_path.lastlat, walk_path.lastlon)
     
     # if the first point is closer to the last point on the previous walk path, reverse the order of the points
     if (first_pt_dist > last_pt_dist):
         walk_path.points.reverse()
     
     # determine current walk path's bearing
     walk_path.bearing = _path_bearing(walk_path.points)
     
     # if this is the first edge in the route
     if (route_info.first_edge):
         lat, lon = _get_lat_lon(walk_path.points[0])
         ret_string += _print_orig_path(route_info, lat, lon)
         route_info.first_edge = False
     
     # build return string
     ret_string += '<street name="' + walk_path.name + '" length="' + str(int(round(walk_path.length))) + '" bearing="' + str(int(round(walk_path.bearing))) + '" dep_time="' + walk_path.dep_time + '" arr_time="' + walk_path.arr_time + '">'
     ret_string += '<points>'
     
     for point in walk_path.points:
         ret_string += '<point lat="' + point[point.index(' ')+1:] + '" lon="' + point[0:point.index(' ')] + '" />'
     
     ret_string += '</points>'
     ret_string += '</street>'
     
     return ret_string
예제 #2
0
 def _print_dest_path(route_info, fromlat, fromlon):
     
     # find distance between last point and destination point
     path_length = vincenty(fromlat, fromlon, route_info.destlat, route_info.destlon)
     
     # determine path bearing
     path_bearing = _path_bearing([str(fromlon) + ' ' + str(fromlat), str(route_info.destlon) + ' ' + str(route_info.destlat)])
     
     # build destination string
     ret_string = '<end length="' + str(int(round(path_length))) + '" bearing="' + str(int(round(path_bearing))) + '" dep_time="' + str(route_info.actual_arr_time - route_info.arr_time_diff) + '" arr_time="' + str(route_info.actual_arr_time) + '">'
     ret_string += '<points>'
     ret_string += '<point lat="' + str(fromlat) + '" lon="' + str(fromlon) + '" />'
     ret_string += '<point lat="' + str(route_info.destlat) + '" lon="' + str(route_info.destlon) + '" />'
     ret_string += '</points>'
     ret_string += '</end>'
     
     return ret_string
예제 #3
0
 def _print_orig_path(route_info, tolat, tolon):
     
     # find distance between origin point and first point
     path_length = vincenty(route_info.origlat, route_info.origlon, tolat, tolon)
     
     # determine path bearing
     path_bearing = _path_bearing([str(route_info.origlon) + ' ' + str(route_info.origlat), str(tolon) + ' ' + str(tolat)])
     
     # build origin string
     ret_string = '<start length="' + str(int(round(path_length))) + '" bearing="' + str(int(round(path_bearing))) + '" dep_time="' + str(route_info.actual_dep_time) + '" arr_time="' + str(route_info.actual_dep_time + route_info.dep_time_diff) + '">'
     ret_string += '<points>'
     ret_string += '<point lat="' + str(route_info.origlat) + '" lon="' + str(route_info.origlon) + '" />'
     ret_string += '<point lat="' + str(tolat) + '" lon="' + str(tolon) + '" />'
     ret_string += '</points>'
     ret_string += '</start>'
     
     return ret_string
예제 #4
0
    def path_xml(self, origlon, origlat, destlon, destlat, dep_time=0, arr_time=0, max_results=1, timezone="", transfer_penalty=60, walking_speed=1.0, walking_reluctance=1.0, max_walk=10000, walking_overage=0.1, seqno=0, street_mode="walk", transit_mode="Both", less_walking="False", udid="", version="2.0", two_way_routing="True"):
        
        if (two_way_routing == "False"):
            self.two_way_routing = False
        
        # set hard bounds on max_results [0,25]
        if (max_results < 0):
            max_results = 0
        elif (max_results > 25):
            max_results = 25
        
        sys.stderr.write("[xml_path_entry_point," + str(time.time()) + "] \"origlon=" + str(origlon) + "&origlat=" + str(origlat) + "&destlon=" + str(destlon) + "&destlat=" + str(destlat) + "&dep_time=" + str(dep_time) + "&arr_time=" + str(arr_time) + "&timezone=" + str(timezone) + "&transfer_penalty=" + str(transfer_penalty) + "&walking_speed=" + str(walking_speed) + "&walking_reluctance=" + str(walking_reluctance) + "&max_walk=" + str(max_walk) + "&walking_overage=" + str(walking_overage) + "&seqno=" + str(seqno) + "&street_mode=\"" + str(street_mode) + "\"&less_walking=\"" + str(less_walking) + "\"&udid=\"" + str(udid) + "\"&version=" + str(version) + "\"\n")
        
        if (origlon <= ChicagoMap.bounding_lon_left or origlon >= ChicagoMap.bounding_lon_right or 
            origlat <= ChicagoMap.bounding_lat_bottom or origlat >= ChicagoMap.bounding_lat_top or 
            destlon <= ChicagoMap.bounding_lon_left or destlon >= ChicagoMap.bounding_lon_right or 
            destlat <= ChicagoMap.bounding_lat_bottom or destlat >= ChicagoMap.bounding_lat_top):
            
            sys.stderr.write("[exit_outside_bounding_box," + str(time.time()) + "]\n")
            raise RoutingException
            #return '<?xml version="1.0"?><routes></routes>'
        
        # initialize spt to 'None' object
        spt = None
        
        # initialize walk options object
        wo = WalkOptions()
        
        # create database connections
        pgosmdb_conn = self.pgosmdb.create_pgosmdb_connection()
        pggtfsdb_conn = self.pggtfsdb.create_pggtfsdb_connection()
        
        try:
            # if the departure time is not specified, set it
            if (dep_time == 0):
                dep_time = int(time.time())
            
            # if the timezone is not specified, default to "America/Chicago"
            if (timezone == ""):
                timezone = "America/Chicago"
            
            # get origin and destination nodes from osm map
            sys.stderr.write("[get_osm_vertex_from_coords," + str(time.time()) + "]\n")
            orig_osm, orig_osm_dist = self.pgosmdb.get_osm_vertex_from_coords(pgosmdb_conn, origlon, origlat)
            dest_osm, dest_osm_dist = self.pgosmdb.get_osm_vertex_from_coords(pgosmdb_conn, destlon, destlat)
            
            #print "\nOrigin OSM: " + str(orig_osm) + " (" + str(orig_osm_dist) + ")"
            #print "Destination OSM: " + str(dest_osm) + " (" + str(dest_osm_dist) + ")\n"
            
            # get origin and destination nodes from gtfs database
            sys.stderr.write("[get_station_vertex_from_coords," + str(time.time()) + "]\n")
            orig_sta, orig_sta_dist = self.pggtfsdb.get_station_vertex_from_coords(pggtfsdb_conn, origlon, origlat)
            dest_sta, dest_sta_dist = self.pggtfsdb.get_station_vertex_from_coords(pggtfsdb_conn, destlon, destlat)
            
            #print "Origin STA: " + str(orig_sta) + " (" + str(orig_sta_dist) + ")"
            #print "Destination STA: " + str(dest_sta) + " (" + str(dest_sta_dist) + ")\n"
            
            # get coordinates for origin node
            if (orig_osm_dist < orig_sta_dist):
                origin = orig_osm
                sys.stderr.write("[get_coords_for_osm_vertex," + str(time.time()) + "]\n")
                orig_node_lat, orig_node_lon = self.pgosmdb.get_coords_for_osm_vertex(pgosmdb_conn, origin)
            else:
                origin = orig_sta
                sys.stderr.write("[get_coords_for_station_vertex," + str(time.time()) + "]\n")
                orig_node_lat, orig_node_lon = self.pggtfsdb.get_coords_for_station_vertex(pggtfsdb_conn, origin)
                
            # get coordinates for destination node
            if (dest_osm_dist < dest_sta_dist):
                dest = dest_osm
                sys.stderr.write("[get_coords_for_osm_vertex," + str(time.time()) + "]\n")
                dest_node_lat, dest_node_lon = self.pgosmdb.get_coords_for_osm_vertex(pgosmdb_conn, dest)
            else:
                dest = dest_sta
                sys.stderr.write("[get_coords_for_station_vertex," + str(time.time()) + "]\n")
                dest_node_lat, dest_node_lon = self.pggtfsdb.get_coords_for_station_vertex(pggtfsdb_conn, dest)
            
            #print "Origin: " + str(origin)
            #print "Destination: " + str(dest) + "\n"
            
            #print "Origin coords: " + str(orig_node_lat) + ", " + str(orig_node_lon)
            #print "Destination coords: " + str(dest_node_lat) + ", " + str(dest_node_lon)
            
            # determine distance from actual origin/destination to osm nodes
            orig_distance = vincenty(float(origlat), float(origlon), orig_node_lat, orig_node_lon)
            dest_distance = vincenty(dest_node_lat, dest_node_lon, float(destlat), float(destlon))
            
            #print "Origin distance: " + str(orig_distance)
            #print "Destination distance: " + str(dest_distance)
            
            # calculate time to origin and destination nodes (seconds)
            time_to_orig = int(round(float( float(orig_distance) / float(walking_speed) )))
            time_to_dest = int(round(float( float(dest_distance) / float(walking_speed) )))
            
            #print "Origin time: " + str(time_to_orig)
            #print "Destination time: " + str(time_to_dest)
            
            # adjust departure time by time needed to reach origin node
            dep_time = (dep_time + time_to_orig)
            
            # adjust arrival time by time needed to reach destination node
            if (arr_time != 0):
                arr_time = (arr_time - time_to_dest)
            
            #print "Adjusted departure time: " + str(dep_time)
            
            # set walk options
            wo.transfer_penalty=transfer_penalty
            wo.walking_speed=walking_speed
            wo.walking_reluctance=walking_reluctance
            wo.max_walk=max_walk
            wo.walking_overage=walking_overage
            
            # check for wheelchair street_mode
            if (street_mode == "wheelchair"):
                wo.with_wheelchair = int(True)
                wo.transfer_penalty = 180
            else:
                wo.with_wheelchair = int(False)
            
            # check for bike street_mode
            if (street_mode == "bike"):
                wo.transfer_penalty = 120
            
            # check for transit_mode
            if (transit_mode == "Both"):
                wo.transit_types = int(14)
            elif (transit_mode == "Bus"):
                wo.transit_types = int(8)
            elif (transit_mode == "Rail"):
                wo.transit_types = int(6)
            elif (transit_mode == "None"):
                wo.transit_types = int(0)
            
            # check for less_walking flag
            if (less_walking == "True"):
                wo.walking_reluctance *= 10.0
            
            # create RouteInfo object
            route_info = RouteInfo()
            route_info.origlat = origlat
            route_info.origlon = origlon
            route_info.dep_time_diff = time_to_orig
            route_info.destlat = destlat
            route_info.destlon = destlon
            route_info.arr_time_diff = time_to_dest
            route_info.street_mode = street_mode
            
            yield "--multipart-path_xml-boundary1234\n";
            
			# loop to create multiple responses
            for q in range(max_results):
                if (spt is not None):
                    spt.destroy_no_hash()
                route_info.first_edge = True
                route_info.last_edge = False
                
                # initialize return string
                ret_string = 'Content-Type: text/xml\n\n<?xml version="1.0"?><routes>'
                
                if (arr_time == 0):
                    (spt, edges, vertices) = self.shortest_path(origin,dest,dep_time,wo)
                else:
                    (spt, edges, vertices) = self.shortest_path_retro(origin,dest,arr_time,wo)
                
                # if there are no edges or vertices (i.e., there is no path found)
                if ((edges is None) or (vertices is None)): raise RoutingException
                # create WalkPath object
                walk_path = WalkPath()
                walk_path.lastlat = origlat
                walk_path.lastlon = origlon
                walk_path.timezone = timezone
                
                # string to store returned route
                curr_route = ""
                
                route_info.actual_dep_time = vertices[0].payload.time - time_to_orig
                route_info.actual_arr_time = vertices[-1].payload.time + time_to_dest
                
                # iterate through all edges in the route
                sys.stderr.write("[edges_loop," + str(time.time()) + "]\n")
                
                # determine the number of edges
                edges_len = len(edges)            
                for i in range(edges_len):
                    
                    if (i == (edges_len-1)):
                        route_info.last_edge = True
                    elif (i == (edges_len-2) and edges[i+1].payload.__class__ == graphserver.core.Link):
                        route_info.last_edge = True
                    elif (i == (edges_len-3) and edges[i+1].payload.__class__ == graphserver.core.Link and edges[i+2].payload.__class__ == graphserver.core.Link):
                        route_info.last_edge = True
                    
                    edgetype = edges[i].payload.__class__
                    if edgetype in self.event_dispatch:
                        (new_event, walk_path, route_info) = self.event_dispatch[ edges[i].payload.__class__ ]( vertices[i], edges[i], vertices[i+1], walk_path, route_info, pgosmdb_conn, pggtfsdb_conn)
                        curr_route += new_event
                
                ret_string += '<route dep_time="' + str(route_info.actual_dep_time) + '" req_dep_time="' + str(dep_time - time_to_orig) + '" arr_time="' + str(route_info.actual_arr_time) + '" req_arr_time="' + str(arr_time) + '" origlat="' + str(origlat) + '" origlon="' + str(origlon) + '" destlat="' + str(destlat) + '" destlon="' + str(destlon) + '" timezone="' + timezone + '" total_time="' + str(route_info.actual_arr_time - route_info.actual_dep_time) + '" total_walk_distance="' + str(int(round(walk_path.total_distance)) + int(round(orig_distance)) + int(round(dest_distance))) + '" walking_speed="' + str(walking_speed) + '" seqno="' + str(seqno) + '" version="' + str(version) + '">' + curr_route + '</route>'
                
                # close return string
                if q == max_results:                    
                    ret_string += '</routes>\n\n--multipart-path_xml-boundary1234--\n\n'
                else:
                    ret_string += '</routes>\n\n--multipart-path_xml-boundary1234\n'

                sys.stderr.write("[xml_path_exit_point," + str(time.time()) + "]\n")
                
                # return routes xml
                yield xstr(str(ret_string))
                
                if arr_time == 0: 
                    dep_time = route_info.actual_dep_time + time_to_orig + 60
                else:
                    arr_time = route_info.actual_arr_time - time_to_dest - 60
                            
    
        except RoutingException:
            if (spt is not None):
                spt.destroy_no_hash()
            
            yield '\n\n--multipart-path_xml-boundary1234\nContent-Type: text/xml\n\n<?xml version="1.0"?><routes></routes>--multipart-path_xml-boundary1234--\n\n '
            
        finally:
            #yield '\n\n--multipart-path_xml-boundary1234--\n\n'
            
            # close database connections
            self.pgosmdb.close_pgosmdb_connection(pgosmdb_conn)
            self.pggtfsdb.close_pggtfsdb_connection(pggtfsdb_conn)
            
            # destroy WalkOptions object
            wo.destroy()
            
            # destroy shortest path tree
            if (spt is not None):
                spt.destroy_no_hash()