예제 #1
0
def trip(request, agency_id, route_id, trip_id):
    """
    Browse the information of a trip
    """

    trip_loader = TripLoader(trip_id)
    trip_loader.load()

    trip_entity = trip_loader.get_trip()
    agency_entity = trip_loader.get_agency()
    route_entity = trip_loader.get_route()

    trip_entity = trEntity(trip_entity, request)
    agency_entity = trEntity(agency_entity, request)
    route_entity = trEntity(route_entity, request)
    stop_list = [trEntity(stop, request) for stop in trip_loader.get_stop_list()]

    faretrip_list = []
    for faretrip in trip_loader.get_faretrip_list():
        entity = trEntity(faretrip, request)
        entity["fare_range"] = None
        if entity["max_fare"] > 0:
            if entity["max_fare"] != entity["min_fare"]:
                entity["fare_range"] = "$%0.1f - $%0.1f" % (entity["min_fare"], entity["max_fare"])
            else:
                entity["fare_range"] = "$%0.1f" % (entity["min_fare"])

        faretrip_list.append(entity)

    pathbar = Pathbar(agency=(agency_entity, route_entity, trip_entity))

    return render_to_response(
        request,
        "gogogo/transit/trip.html",
        {
            "page_title": trip_entity["headsign"],
            "pathbar": pathbar,
            "object_type": "trip",
            "agency": agency_entity,
            "route": route_entity,
            "trip": trip_entity,
            "stop_list": stop_list,
            "faretrip_list": faretrip_list,
            "faretrip_kind": "faretrip",
        },
    )
예제 #2
0
    def load(self):
        """
       Load from database or bigtable
        """
        agency_loader = AgencyLoader(self.id)
        agency_loader.load()

        trip_id_list = agency_loader.get_trip_id_list()

        trip_loader_list = []
        stop_table = {}

        for trip_id in trip_id_list:
            trip_loader = TripLoader(trip_id)
            trip_loader.load(stop_table=stop_table)
            trip_loader_list.append(trip_loader)

            prev_node = None

            for stop in trip_loader.get_stop_list():
                node = self.search_node(stop["id"])
                if node == None:
                    node = Node(name=stop["id"])
                    self.add_node(node)

                if prev_node:
                    arc = Arc(weight=1)
                    arc.link(prev_node, node)
                    self.add_arc(arc)

                prev_node = node

        agency = agency_loader.get_agency()
        key = db.Key.from_path(Agency.kind(), str(agency["id"]))
        query = Transfer.all().filter("agency = ", key)

        stop_a_property = getattr(Transfer, "stop_a")
        stop_b_property = getattr(Transfer, "stop_b")

        for transfer in query:
            stop_a = stop_a_property.get_value_for_datastore(transfer)
            stop_b = stop_b_property.get_value_for_datastore(transfer)

            node_a = self.search_node(stop_a.id_or_name())
            if node_a == None:
                node_a = Node(name=stop_a.id_or_name())
                self.add_node(node_a)

            node_b = self.search_node(stop_b.id_or_name())
            if node_b == None:
                node_b = Node(name=stop_b.id_or_name())
                self.add_node(node_b)

            arc = Arc(weight=1)
            arc.link(node_a, node_b)
            self.add_arc(arc)
            # logging.info("%s => %s" %(arc.src.name , arc.dest.name))

            arc = Arc(weight=1)
            arc.link(node_b, node_a)
            self.add_arc(arc)
예제 #3
0
    def load(self):
        """
       Load graph from bigtable
        """

        route_list = []
        agency_list = []
        cluster_list = []
        trip_list = []

        query = Agency.all().filter("no_service = ", False)

        for agency in query:
            agency_list.append(agency)

        # Load all the trip by using TripLoader
        trip_loader_list = []
        stop_table = {}
        limit = 1000
        entities = Trip.all(keys_only=True).fetch(limit)
        while entities:
            for key in entities:
                loader = TripLoader(key.id_or_name())
                loader.load(stop_table=stop_table)

                stop_list = loader.get_stop_list()
                for stop in stop_list:
                    stop_table[stop["id"]] = stop

                agency = loader.get_agency()

                if agency["free_transfer"] == False:  # Ignore agency with free transfer service
                    trip_loader_list.append(loader)

            entities = Trip.all(keys_only=True).filter("__key__ >", entities[-1]).fetch(limit)

        # Load all cluster
        query = Cluster.all()
        for cluster in query:
            cluster_list.append(cluster)
            cluster_name = cluster.key().id_or_name()

            node = Node(name=cluster.key().id_or_name())

            # Cluster to ID matching
            self.cluster_id[cluster.key().id_or_name()] = self.add_node(node)
            # logging.info(cluster.key().id_or_name())

            for key in cluster.members:
                name = key.id_or_name()
                if name in stop_table:
                    self.stop_to_cluster[name] = cluster_name

        # Process agency with "free_transfer"
        for agency in agency_list:
            if agency.free_transfer:
                logging.info("Processing " + agency.key().id_or_name())
                query = db.GqlQuery(
                    "SELECT __key__ from gogogo_farestop WHERE agency = :1 and default = True limit 1", agency
                )
                key = query.get()
                if key == None:
                    continue
                farestop_loader = FareStopLoader(key.id_or_name())
                farestop_loader.load()
                farestop = farestop_loader.get_farestop()

                pair_list = farestop_loader.get_pair_list()

                for pair in pair_list:
                    try:
                        a = self.get_node_by_stop(pair["from"])
                        b = self.get_node_by_stop(pair["to"])

                        arc = TransitArc(agency=agency.key().id_or_name(), weight=pair["fare"])
                        arc.link(a, b)
                        self.add_arc(arc)
                    except KeyError, e:
                        logging.error(str(e) + "not found")