Example #1
0
    def gtfsdb_to_headway_edges( self, maxtrips=None ):

        # load headways
        if self.reporter: self.reporter.write( "Loading headways trips to graph...\n" )
        for trip_id, start_time, end_time, headway_secs in self.gtfsdb.execute( "SELECT * FROM frequencies" ):
            service_id = list(self.gtfsdb.execute( "SELECT service_id FROM trips WHERE trip_id=?", (trip_id,) ))[0][0]
            service_id = service_id.encode('utf-8')
            
            hb = HeadwayBoard( service_id, self.sc, self.tz, 0, trip_id.encode('utf-8'), start_time, end_time, headway_secs )
            ha = HeadwayAlight( service_id, self.sc, self.tz, 0, trip_id.encode('utf-8'), start_time, end_time, headway_secs )
            
            stoptimes = list(self.gtfsdb.execute( "SELECT * FROM stop_times WHERE trip_id=? ORDER BY stop_sequence", (trip_id,)) )
            
            #add board edges
            for trip_id, arrival_time, departure_time, stop_id, stop_sequence, stop_dist_traveled in stoptimes[:-1]:
                yield ( "sta-%s"%stop_id, "hwv-%s-%s-%s"%(self.agency_namespace,stop_id, trip_id), hb )
                
            #add alight edges
            for trip_id, arrival_time, departure_time, stop_id, stop_sequence, stop_dist_traveled in stoptimes[1:]:
                yield ( "hwv-%s-%s-%s"%(self.agency_namespace,stop_id, trip_id), "sta-%s"%stop_id, ha )
            
            #add crossing edges
            for (trip_id1, arrival_time1, departure_time1, stop_id1, stop_sequence1, stop_dist_traveled1), (trip_id2, arrival_time2, departure_time2, stop_id2, stop_sequence2,stop_dist_traveled2) in cons(stoptimes):
                cr = Crossing()
                cr.add_crossing_time( trip_id1, (arrival_time2-departure_time1) )
                yield ( "hwv-%s-%s-%s"%(self.agency_namespace,stop_id1, trip_id1), "hwv-%s-%s-%s"%(self.agency_namespace,stop_id2, trip_id2), cr )
Example #2
0
    def bundle_to_boardalight_edges(self, bundle, service_id):
        """takes a bundle and yields a bunch of edges"""

        stop_time_bundles = bundle.stop_time_bundles(service_id)

        n_trips = len(bundle.trip_ids)

        # If there's less than two stations on this trip bundle, the trip bundle doesn't actually span two places
        if len(stop_time_bundles) < 2:
            return

        # If there are no stop_times in a bundle on this service day, there is nothing to load
        if n_trips == 0:
            return

        if self.reporter:
            self.reporter.write(
                "inserting %d trips with %d stop_time bundles on service_id '%s'\n"
                % (len(
                    stop_time_bundles[0]), len(stop_time_bundles), service_id))

        #add board edges
        for i, stop_time_bundle in enumerate(stop_time_bundles[:-1]):

            trip_id, arrival_time, departure_time, stop_id, stop_sequence, stop_dist_traveled = stop_time_bundle[
                0]

            if arrival_time != departure_time:
                patternstop_vx_name = "psv-%s-%03d-%03d-%s-depart" % (
                    self.agency_namespace, bundle.pattern.pattern_id, i,
                    service_id)

                # construct the board/alight/dwell triangle for this patternstop
                patternstop_arrival_vx_name = "psv-%s-%03d-%03d-%s-arrive" % (
                    self.agency_namespace, bundle.pattern.pattern_id, i,
                    service_id)

                dwell_crossing = Crossing()
                for trip_id, arrival_time, departure_time, stop_id, stop_sequence, stop_dist_traveled in stop_time_bundle:
                    dwell_crossing.add_crossing_time(
                        trip_id, departure_time - arrival_time)

                yield (patternstop_arrival_vx_name, patternstop_vx_name,
                       dwell_crossing)

            else:
                patternstop_vx_name = "psv-%s-%03d-%03d-%s" % (
                    self.agency_namespace, bundle.pattern.pattern_id, i,
                    service_id)

            b = TripBoard(service_id, self.sc, self.tz, 0)
            for trip_id, arrival_time, departure_time, stop_id, stop_sequence, stop_dist_traveled in stop_time_bundle:
                b.add_boarding(trip_id, departure_time, stop_sequence)

            yield ("sta-%s" % stop_id, patternstop_vx_name, b)

        #add alight edges
        for i, stop_time_bundle in enumerate(stop_time_bundles[1:]):

            trip_id, arrival_time, departure_time, stop_id, stop_sequence, stop_dist_traveled = stop_time_bundle[
                0]

            if arrival_time != departure_time:
                patternstop_vx_name = "psv-%s-%03d-%03d-%s-arrive" % (
                    self.agency_namespace, bundle.pattern.pattern_id, i + 1,
                    service_id)
            else:
                patternstop_vx_name = "psv-%s-%03d-%03d-%s" % (
                    self.agency_namespace, bundle.pattern.pattern_id, i + 1,
                    service_id)

            al = TripAlight(service_id, self.sc, self.tz, 0)
            for trip_id, arrival_time, departure_time, stop_id, stop_sequence, stop_dist_traveled in stop_time_bundle:
                al.add_alighting(trip_id.encode('ascii'), arrival_time,
                                 stop_sequence)

            yield (patternstop_vx_name, "sta-%s" % stop_id, al)

        # add crossing edges
        for i, (from_stop_time_bundle,
                to_stop_time_bundle) in enumerate(cons(stop_time_bundles)):

            trip_id, from_arrival_time, from_departure_time, stop_id, stop_sequence, stop_dist_traveled = from_stop_time_bundle[
                0]
            trip_id, to_arrival_time, to_departure_time, stop_id, stop_sequence, stop_dist_traveled = to_stop_time_bundle[
                0]

            if from_arrival_time != from_departure_time:
                from_patternstop_vx_name = "psv-%s-%03d-%03d-%s-depart" % (
                    self.agency_namespace, bundle.pattern.pattern_id, i,
                    service_id)
            else:
                from_patternstop_vx_name = "psv-%s-%03d-%03d-%s" % (
                    self.agency_namespace, bundle.pattern.pattern_id, i,
                    service_id)

            if to_arrival_time != to_departure_time:
                to_patternstop_vx_name = "psv-%s-%03d-%03d-%s-arrive" % (
                    self.agency_namespace, bundle.pattern.pattern_id, i + 1,
                    service_id)
            else:
                to_patternstop_vx_name = "psv-%s-%03d-%03d-%s" % (
                    self.agency_namespace, bundle.pattern.pattern_id, i + 1,
                    service_id)

            crossing = Crossing()
            for i in range(len(from_stop_time_bundle)):
                trip_id, from_arrival_time, from_departure_time, stop_id, stop_sequence, stop_dist_traveled = from_stop_time_bundle[
                    i]
                trip_id, to_arrival_time, to_departure_time, stop_id, stop_sequence, stop_dist_traveled = to_stop_time_bundle[
                    i]
                crossing.add_crossing_time(
                    trip_id, (to_arrival_time - from_departure_time))

            yield (from_patternstop_vx_name, to_patternstop_vx_name, crossing)
Example #3
0
    def bundle_to_boardalight_edges(self, bundle, service_id):
        """takes a bundle and yields a bunch of edges"""
        
        stop_time_bundles = bundle.stop_time_bundles(service_id)
        
        n_trips = len(bundle.trip_ids)
            
        # If there's less than two stations on this trip bundle, the trip bundle doesn't actually span two places
        if len(stop_time_bundles)<2:
            return
            
        # If there are no stop_times in a bundle on this service day, there is nothing to load
        if n_trips==0:
            return
            
        if self.reporter: self.reporter.write( "inserting %d trips with %d stop_time bundles on service_id '%s'\n"%(len(stop_time_bundles[0]),len(stop_time_bundles),service_id) )

        #add board edges
        for i, stop_time_bundle in enumerate(stop_time_bundles[:-1]):
            
            trip_id, arrival_time, departure_time, stop_id, stop_sequence, stop_dist_traveled = stop_time_bundle[0]
            
            if arrival_time != departure_time:
                patternstop_vx_name = "psv-%s-%03d-%03d-%s-depart"%(self.agency_namespace,bundle.pattern.pattern_id,i,service_id)
                
                # construct the board/alight/dwell triangle for this patternstop
                patternstop_arrival_vx_name = "psv-%s-%03d-%03d-%s-arrive"%(self.agency_namespace,bundle.pattern.pattern_id,i,service_id)
                
                dwell_crossing = Crossing()
                for trip_id, arrival_time, departure_time, stop_id, stop_sequence, stop_dist_traveled in stop_time_bundle:
                    dwell_crossing.add_crossing_time( trip_id, departure_time-arrival_time )
                
                yield (patternstop_arrival_vx_name, 
                           patternstop_vx_name,
                           dwell_crossing)
                
            else:
                patternstop_vx_name = "psv-%s-%03d-%03d-%s"%(self.agency_namespace,bundle.pattern.pattern_id,i,service_id)
            
            b = TripBoard(service_id, self.sc, self.tz, 0)
            for trip_id, arrival_time, departure_time, stop_id, stop_sequence, stop_dist_traveled in stop_time_bundle:
                b.add_boarding( trip_id, departure_time, stop_sequence )
                
            yield ( "sta-%s"%stop_id, patternstop_vx_name, b )
            
        #add alight edges
        for i, stop_time_bundle in enumerate(stop_time_bundles[1:]):

            trip_id, arrival_time, departure_time, stop_id, stop_sequence, stop_dist_traveled = stop_time_bundle[0]
            
            if arrival_time != departure_time:
                patternstop_vx_name = "psv-%s-%03d-%03d-%s-arrive"%(self.agency_namespace,bundle.pattern.pattern_id,i+1,service_id)
            else:
                patternstop_vx_name = "psv-%s-%03d-%03d-%s"%(self.agency_namespace,bundle.pattern.pattern_id,i+1,service_id)
            
            al = TripAlight(service_id, self.sc, self.tz, 0)
            for trip_id, arrival_time, departure_time, stop_id, stop_sequence, stop_dist_traveled in stop_time_bundle:
                al.add_alighting( trip_id.encode('ascii'), arrival_time, stop_sequence )
                
            yield ( patternstop_vx_name, "sta-%s"%stop_id, al )
        
        # add crossing edges
        for i, (from_stop_time_bundle, to_stop_time_bundle) in enumerate(cons(stop_time_bundles)):
            
            trip_id, from_arrival_time, from_departure_time, stop_id, stop_sequence, stop_dist_traveled = from_stop_time_bundle[0]
            trip_id, to_arrival_time, to_departure_time, stop_id, stop_sequence, stop_dist_traveled = to_stop_time_bundle[0]
            
            if from_arrival_time!=from_departure_time:
                from_patternstop_vx_name = "psv-%s-%03d-%03d-%s-depart"%(self.agency_namespace,bundle.pattern.pattern_id,i,service_id)
            else:
                from_patternstop_vx_name = "psv-%s-%03d-%03d-%s"%(self.agency_namespace,bundle.pattern.pattern_id,i,service_id)
                
            if to_arrival_time!=to_departure_time:
                to_patternstop_vx_name = "psv-%s-%03d-%03d-%s-arrive"%(self.agency_namespace,bundle.pattern.pattern_id,i+1,service_id)
            else:
                to_patternstop_vx_name = "psv-%s-%03d-%03d-%s"%(self.agency_namespace,bundle.pattern.pattern_id,i+1,service_id)
            
            crossing = Crossing()
            for i in range( len( from_stop_time_bundle ) ):
                trip_id, from_arrival_time, from_departure_time, stop_id, stop_sequence, stop_dist_traveled = from_stop_time_bundle[i]
                trip_id, to_arrival_time, to_departure_time, stop_id, stop_sequence, stop_dist_traveled = to_stop_time_bundle[i]
                crossing.add_crossing_time( trip_id, (to_arrival_time-from_departure_time) )
            
            yield ( from_patternstop_vx_name, 
                    to_patternstop_vx_name, 
                    crossing )
Example #4
0
    def test_hello_world(self):
        g = Graph()

        g.add_vertex("Seattle")
        g.add_vertex("Portland")

        g.add_edge("Seattle", "Portland", Street("I-5 south", 5000))
        g.add_edge("Portland", "Seattle", Street("I-5 north", 5500))

        spt = g.shortest_path_tree("Seattle", "Portland",
                                   State(g.numagencies, 0), WalkOptions())

        assert spt.get_vertex(
            "Seattle").outgoing[0].payload.name == "I-5 south"

        g.add_vertex("Portland-busstop")
        g.add_vertex("Seattle-busstop")

        g.add_edge("Seattle", "Seattle-busstop", Link())
        g.add_edge("Seattle-busstop", "Seattle", Link())
        g.add_edge("Portland", "Portland-busstop", Link())
        g.add_edge("Portland-busstop", "Portland", Link())

        spt = g.shortest_path_tree("Seattle", "Seattle-busstop",
                                   State(g.numagencies, 0), WalkOptions())
        assert spt.get_vertex(
            "Seattle-busstop").incoming[0].payload.__class__ == Link
        spt.destroy()

        spt = g.shortest_path_tree("Seattle-busstop", "Portland",
                                   State(g.numagencies, 0), WalkOptions())
        assert spt.get_vertex(
            "Portland").incoming[0].payload.__class__ == Street
        spt.destroy()

        sc = ServiceCalendar()
        sc.add_period(0, 86400, ["WKDY", "SAT"])
        tz = Timezone()
        tz.add_period(TimezonePeriod(0, 86400, 0))

        g.add_vertex("Portland-busstop-onbus")
        g.add_vertex("Seattle-busstop-onbus")

        tb = TripBoard("WKDY", sc, tz, 0)
        tb.add_boarding("A", 10, 0)
        tb.add_boarding("B", 15, 0)
        tb.add_boarding("C", 400, 0)

        cr = Crossing()

        al = TripAlight("WKDY", sc, tz, 0)
        al.add_alighting("A", 10 + 20, 0)
        al.add_alighting("B", 15 + 20, 0)
        al.add_alighting("C", 400 + 20, 0)

        g.add_edge("Seattle-busstop", "Seattle-busstop-onbus", tb)
        g.add_edge("Seattle-busstop-onbus", "Portland-busstop-onbus", cr)
        g.add_edge("Portland-busstop-onbus", "Portland-busstop", al)

        spt = g.shortest_path_tree("Seattle", "Portland",
                                   State(g.numagencies, 0), WalkOptions())

        assert spt.get_vertex("Portland").incoming[0].from_v.incoming[
            0].from_v.incoming[0].from_v.incoming[0].from_v.incoming[
                0].from_v.label == "Seattle"

        spt = g.shortest_path_tree("Seattle", "Portland",
                                   State(g.numagencies, 0), WalkOptions())
        vertices, edges = spt.path("Portland")

        assert [v.label for v in vertices] == [
            'Seattle', 'Seattle-busstop', "Seattle-busstop-onbus",
            "Portland-busstop-onbus", 'Portland-busstop', 'Portland'
        ]
        assert [e.payload.__class__ for e in edges
                ] == [Link, TripBoard, Crossing, TripAlight, Link]

        spt.destroy()
        g.destroy()