Esempio n. 1
0
    def test_stop_station_multi_feed(self):
        dao = Dao()
        fa = FeedInfo("FA")
        fb = FeedInfo("FB")
        sa = Stop("FA",
                  "S",
                  "Station A",
                  45.0,
                  0.0,
                  location_type=Stop.TYPE_STATION)
        sa1 = Stop("FA", "S1", "Stop A1", 45.0, 0.0, parent_station_id="S")
        sa2 = Stop("FA", "S2", "Stop A2", 45.0, 0.1, parent_station_id="S")
        sa3 = Stop("FA", "S3", "Stop A3", 45.0, 0.2)
        sb = Stop("FB",
                  "S",
                  "Station B",
                  45.0,
                  0.0,
                  location_type=Stop.TYPE_STATION)
        sb1 = Stop("FB", "S1", "Stop B1", 45.0, 0.0, parent_station_id="S")
        sb2 = Stop("FB", "S2", "Stop B2", 45.0, 0.1, parent_station_id="S")
        dao.add_all([fa, fb, sa, sa1, sa2, sa3, sb1, sb2, sb])

        sa = dao.stop("S", feed_id="FA")
        self.assertTrue(sa.stop_name == "Station A")
        self.assertTrue(len(sa.sub_stops) == 2)
        for ssa in sa.sub_stops:
            self.assertTrue(ssa.stop_name.startswith("Stop A"))
            self.assertTrue(ssa.parent_station.stop_name == "Station A")

        sa1 = dao.stop("S1", feed_id="FA")
        self.assertTrue(sa1.stop_name == "Stop A1")
        self.assertTrue(sa1.parent_station.stop_name == "Station A")

        self.assertTrue(len(list(dao.stops())) == 7)
Esempio n. 2
0
    def test_broken(self):
        exception = False
        try:
            clear_mappers()
            dao = Dao("")
            dao.load_gtfs(BROKEN_GTFS, lenient=False)
        except KeyError:
            exception = True
        self.assertTrue(exception)

        clear_mappers()
        dao = Dao("")
        dao.load_gtfs(BROKEN_GTFS, lenient=True)

        # The following are based on BROKEN GTFS content,
        # that is the entities count minus broken ones.
        self.assertTrue(len(dao.routes()) == 4)
        self.assertTrue(len(list(dao.stops())) == 12)
        self.assertTrue(len(dao.calendars()) == 2)
        self.assertTrue(len(list(dao.trips())) == 104)
        self.assertTrue(len(dao.stoptimes()) == 500)
        self.assertTrue(len(dao.fare_attributes()) == 2)
        self.assertTrue(len(dao.fare_rules()) == 4)
        # This stop has missing coordinates in the broken file
        stop00 = dao.stop('FUR_CREEK_RES3')
        self.assertAlmostEqual(stop00.stop_lat, 0.0, 5)
        self.assertAlmostEqual(stop00.stop_lon, 0.0, 5)
Esempio n. 3
0
    def test_zones(self):
        dao = Dao()
        f1 = FeedInfo("")
        z1 = Zone("", "Z1")
        s1 = Stop("", "S1", "Stop 1", 45.0, 0.0)
        s2 = Stop("", "S2", "Stop 2", 45.1, 0.1, zone_id="Z1")
        s3 = Stop("", "S3", "Stop 3", 45.2, 0.2)
        s3.zone = z1
        dao.add_all([f1, z1, s1, s2, s3])
        dao.commit()

        self.assertTrue(len(dao.zones()) == 1)
        z = dao.zone("Z1")
        self.assertTrue(len(z.stops) == 2)
        for stop in z.stops:
            self.assertTrue(stop.zone == z)
        s = dao.stop("S1")
        self.assertTrue(s.zone == None)
        s = dao.stop("S2")
        self.assertTrue(s.zone == z)
        s = dao.stop("S3")
        self.assertTrue(s.zone == z)
Esempio n. 4
0
    def test_clusterizer(self):
        dao = Dao(DAO_URL, sql_logging=SQL_LOG)
        dao.load_gtfs(DUMMY_GTFS)

        # Merge stops closer than 300m together
        sc = SpatialClusterizer(300.0)
        for stop in dao.stops():
            sc.add_point(stop)
        sc.clusterize()

        # for cluster in sc.clusters():
        #    print("---CLUSTER: %d stops" % (len(cluster)))
        #    for stop in cluster:
        #        print("%s %s" % (stop.stop_id, stop.stop_name))

        gare1 = dao.stop("GBSJT")
        gare2 = dao.stop("GBSJ")
        gare3 = dao.stop("GBSJB")
        self.assertTrue(sc.in_same_cluster(gare1, gare2))
        self.assertTrue(sc.in_same_cluster(gare1, gare3))
        self.assertTrue(sc.in_same_cluster(gare2, gare3))

        bq = dao.stop("BQ")
        bq1 = dao.stop("BQA")
        bq2 = dao.stop("BQD")
        self.assertTrue(sc.in_same_cluster(bq, bq1))
        self.assertTrue(sc.in_same_cluster(bq, bq2))

        bs = dao.stop("BS")
        bs1 = dao.stop("BS1")
        bs2 = dao.stop("BS2")
        self.assertTrue(sc.in_same_cluster(bs, bs1))
        self.assertTrue(sc.in_same_cluster(bs, bs2))

        self.assertFalse(sc.in_same_cluster(gare1, bq))
        self.assertFalse(sc.in_same_cluster(gare1, bs))
        self.assertFalse(sc.in_same_cluster(gare3, bs2))

        bjb = dao.stop("BJB")
        self.assertFalse(sc.in_same_cluster(bjb, gare1))
        self.assertFalse(sc.in_same_cluster(bjb, bs))
        self.assertFalse(sc.in_same_cluster(bjb, bq))
Esempio n. 5
0
    def test_transfers(self):
        dao = Dao()
        f1 = FeedInfo("F1")
        s1 = Stop("F1", "S1", "Stop 1", 45.0000, 0.0000)
        s2 = Stop("F1", "S2", "Stop 2", 45.0001, 0.0001)
        s3 = Stop("F1", "S3", "Stop 3", 45.0002, 0.0002)
        t12 = Transfer("F1", "S1", "S2")
        t21 = Transfer("F1", "S2", "S1")
        t23 = Transfer("F1",
                       "S2",
                       "S3",
                       transfer_type=Transfer.TRANSFER_TIMED,
                       min_transfer_time=180)
        t32 = Transfer("F1",
                       "S3",
                       "S2",
                       transfer_type=Transfer.TRANSFER_TIMED,
                       min_transfer_time=120)
        t13 = Transfer("F1", "S1", "S3", transfer_type=Transfer.TRANSFER_NONE)
        a1 = Agency("F1", "A1", "Agency 1", "url1", "Europe/Paris")
        a2 = Agency("F1", "A2", "Agency 2", "url2", "Europe/London")
        r1 = Route("F1", "R1", "A1", Route.TYPE_BUS)
        r2 = Route("F1", "R2", "A2", Route.TYPE_BUS)
        c1 = Calendar("F1", "C1")
        t1 = Trip("F1", "T1", "R1", "C1")
        t2 = Trip("F1", "T2", "R2", "C1")
        st1a = StopTime("F1", "T1", "S1", 0, None, 3600, 0.0)
        st1b = StopTime("F1", "T1", "S2", 1, 3800, None, 100.0)
        st2a = StopTime("F1", "T2", "S1", 0, None, 4600, 0.0)
        st2b = StopTime("F1", "T2", "S3", 1, 4800, None, 100.0)
        dao.add_all([
            f1, s1, s2, s3, t12, t21, t23, t32, t13, a1, a2, r1, r2, c1, t1,
            t2, st1a, st1b, st2a, st2b
        ])

        self.assertTrue(len(dao.transfers()) == 5)

        timed_transfers = dao.transfers(
            fltr=(Transfer.transfer_type == Transfer.TRANSFER_TIMED))
        self.assertTrue(len(timed_transfers) == 2)
        for transfer in timed_transfers:
            self.assertTrue(transfer.transfer_type == Transfer.TRANSFER_TIMED)

        s1_from_transfers = dao.transfers(
            fltr=(dao.transfer_from_stop().stop_name == "Stop 1"))
        self.assertTrue(len(s1_from_transfers) == 2)
        for transfer in s1_from_transfers:
            self.assertTrue(transfer.from_stop.stop_name == "Stop 1")

        s1_fromto_transfers = dao.transfers(
            fltr=((dao.transfer_from_stop().stop_name == "Stop 1")
                  | (dao.transfer_to_stop().stop_name == "Stop 1")))
        self.assertTrue(len(s1_fromto_transfers) == 3)
        for transfer in s1_fromto_transfers:
            self.assertTrue(transfer.from_stop.stop_name == "Stop 1"
                            or transfer.to_stop.stop_name == "Stop 1")

        s1 = dao.stop("S1", feed_id="F1")
        self.assertTrue(len(s1.from_transfers) == 2)
        self.assertTrue(len(s1.to_transfers) == 1)
        for transfer in s1.from_transfers:
            if transfer.to_stop.stop_id == "S2":
                self.assertTrue(
                    transfer.transfer_type == Transfer.TRANSFER_DEFAULT)
            elif transfer.to_stop.stop_id == "S3":
                self.assertTrue(
                    transfer.transfer_type == Transfer.TRANSFER_NONE)

        a1_stops = list(dao.stops(fltr=(Agency.agency_id == 'A1')))
        self.assertTrue(len(a1_stops) == 2)
        self.assertTrue(s1 in a1_stops)
        self.assertTrue(s2 in a1_stops)
Esempio n. 6
0
    def test_trip(self):
        dao = Dao()
        f1 = FeedInfo("F1")
        a1 = Agency("F1",
                    "A1",
                    "Agency 1",
                    agency_url="http://www.agency.fr/",
                    agency_timezone="Europe/Paris")
        r1 = Route("F1",
                   "R1",
                   "A1",
                   3,
                   route_short_name="R1",
                   route_long_name="Route 1")
        c1 = Calendar("F1", "C1")
        c1.dates = [
            d for d in CalendarDate.range(
                CalendarDate.ymd(2016, 1, 1),
                CalendarDate.ymd(2016, 1, 31).next_day())
        ]
        s1 = Stop("F1", "S1", "Stop 1", 45.0, 0.0)
        s2 = Stop("F1", "S2", "Stop 2", 45.1, 0.1)
        s3 = Stop("F1", "S3", "Stop 3", 45.2, 0.2)
        t1 = Trip("F1", "T1", "R1", "C1")
        t1.direction_id = 0
        t11 = StopTime("F1", "T1", "S1", 0, 28800, 28800, 0.0)
        t12 = StopTime("F1", "T1", "S2", 1, 29400, 29400, 0.0)
        t13 = StopTime("F1", "T1", "S3", 2, 30000, 30000, 0.0)
        t2 = Trip("F1", "T2", "R1", "C1")
        t2.direction_id = 1
        # Order is not important for now
        t2.stop_times.append(StopTime(None, None, "S1", 1, 31000, 31000, 0.0))
        t2.stop_times.append(StopTime(None, None, "S2", 0, 30600, 30600, 0.0))

        dao.add_all([f1, a1, r1, c1, s1, s2, s3, t1, t11, t12, t13, t2])
        # Commit is needed to re-order stop times of T2
        dao.commit()

        cal = dao.calendar("C1", feed_id="F1")
        for trip in cal.trips:
            self.assertTrue(trip.calendar.service_id == "C1")
            for stoptime in trip.stop_times:
                self.assertTrue(stoptime.trip.calendar.service_id == "C1")

        stop = dao.stop("S2", feed_id="F1")
        for stoptime in stop.stop_times:
            self.assertTrue(stoptime.stop.stop_id == "S2")
            self.assertTrue(stoptime.trip.trip_id.startswith("T"))

        trip = dao.trip("T1", feed_id="F1")
        self.assertTrue(len(trip.stop_times) == 3)

        trip = dao.trip("T2", feed_id="F1")
        self.assertTrue(len(trip.stop_times) == 2)

        for trip in dao.trips(prefetch_stop_times=True):
            last_stop_seq = -1
            for stoptime in trip.stop_times:
                self.assertTrue(stoptime.stop_sequence > last_stop_seq)
                last_stop_seq = stoptime.stop_sequence

        for trip in dao.trips():
            for stoptime1, stoptime2 in trip.hops():
                self.assertTrue(stoptime1.trip == stoptime2.trip)
                self.assertTrue(stoptime1.stop_sequence +
                                1 == stoptime2.stop_sequence)

        trips = list(dao.trips(fltr=Trip.direction_id == 0))
        self.assertTrue(len(trips) == 1)
        trips = list(dao.trips(fltr=Trip.direction_id == 1))
        self.assertTrue(len(trips) == 1)
Esempio n. 7
0
    def test_gtfs_data(self):
        dao = Dao(DAO_URL, sql_logging=False)
        dao.load_gtfs(DUMMY_GTFS)

        # Check feed
        feed = dao.feed()
        self.assertTrue(feed.feed_id == "")
        self.assertTrue(feed.feed_publisher_name == "Mecatran")
        self.assertTrue(feed.feed_publisher_url == "http://www.mecatran.com/")
        self.assertTrue(feed.feed_contact_email == "*****@*****.**")
        self.assertTrue(feed.feed_lang == "fr")
        self.assertTrue(len(dao.agencies()) == 2)
        self.assertTrue(len(dao.routes()) == 3)
        self.assertTrue(len(feed.agencies) == 2)
        self.assertTrue(len(feed.routes) == 3)

        # Check agencies
        at = dao.agency("AT")
        self.assertTrue(at.agency_name == "Agency Train")
        self.assertTrue(len(at.routes) == 1)
        ab = dao.agency("AB")
        self.assertTrue(ab.agency_name == "Agency Bus")
        self.assertTrue(len(ab.routes) == 2)

        # Check calendars
        week = dao.calendar("WEEK")
        self.assertTrue(len(week.dates) == 253)
        summer = dao.calendar("SUMMER")
        self.assertTrue(len(summer.dates) == 42)
        mon = dao.calendar("MONDAY")
        self.assertTrue(len(mon.dates) == 49)
        sat = dao.calendar("SAT")
        self.assertTrue(len(sat.dates) == 53)
        for date in mon.dates:
            self.assertTrue(date.dow() == 0)
        for date in sat.dates:
            self.assertTrue(date.dow() == 5)
        for date in week.dates:
            self.assertTrue(date.dow() >= 0 and date.dow() <= 4)
        for date in summer.dates:
            self.assertTrue(date >= CalendarDate.ymd(2016, 7, 1)
                            and date <= CalendarDate.ymd(2016, 8, 31))
        empty = dao.calendars(
            func.date(CalendarDate.date) == datetime.date(2016, 5, 1))
        # OR USE: empty = dao.calendars(CalendarDate.date == "2016-05-01")
        self.assertTrue(len(empty) == 0)
        july4 = CalendarDate.ymd(2016, 7, 4)
        summer_mon = dao.calendars(func.date(CalendarDate.date) == july4.date)
        n = 0
        for cal in summer_mon:
            self.assertTrue(july4 in cal.dates)
            n += 1
        self.assertTrue(n == 3)

        # Check stops
        sbq = dao.stop("BQ")
        self.assertAlmostEqual(sbq.stop_lat, 44.844, places=2)
        self.assertAlmostEqual(sbq.stop_lon, -0.573, places=2)
        self.assertTrue(sbq.stop_name == "Bordeaux Quinconces")
        n = 0
        for stop in dao.stops(Stop.stop_name.like("Gare%")):
            self.assertTrue(stop.stop_name.startswith("Gare"))
            n += 1
        self.assertTrue(n == 7)
        n = 0
        for stop in dao.stops(
                fltr=dao.in_area(RectangularArea(44.7, -0.6, 44.9, -0.4))):
            self.assertTrue(stop.stop_lat >= 44.7 and stop.stop_lat <= 44.9
                            and stop.stop_lon >= -0.6
                            and stop.stop_lon <= -0.4)
            n += 1
        self.assertTrue(n == 16)
        for station in dao.stops(Stop.location_type == Stop.TYPE_STATION):
            self.assertTrue(station.location_type == Stop.TYPE_STATION)
            self.assertTrue(len(station.sub_stops) >= 2)
            for stop in station.sub_stops:
                self.assertTrue(stop.parent_station == station)

        # Check zones
        z_inexistant = dao.zone("ZX")
        self.assertTrue(z_inexistant is None)
        z1 = dao.zone("Z1")
        self.assertEqual(16, len(z1.stops))
        z2 = dao.zone("Z2")
        self.assertEqual(4, len(z2.stops))

        # Check transfers
        transfers = dao.transfers()
        self.assertTrue(len(transfers) == 3)
        transfers = dao.transfers(
            fltr=(dao.transfer_from_stop().stop_id == 'GBSJB'))
        self.assertTrue(len(transfers) == 1)
        self.assertTrue(transfers[0].from_stop.stop_id == 'GBSJB')

        # Check routes
        tgv = dao.route("TGVBP")
        self.assertTrue(tgv.agency == at)
        self.assertTrue(tgv.route_type == 2)
        r1 = dao.route("BR")
        self.assertTrue(r1.route_short_name == "R1")
        self.assertTrue(r1.route_long_name == "Bus Red")
        n = 0
        for route in dao.routes(Route.route_type == 3):
            self.assertTrue(route.route_type == 3)
            n += 1
        self.assertTrue(n == 2)

        # Check trip for route
        n = 0
        trips = dao.trips(fltr=Route.route_type == Route.TYPE_BUS)
        for trip in trips:
            self.assertTrue(trip.route.route_type == Route.TYPE_BUS)
            n += 1
        self.assertTrue(n > 20)

        # Check trips on date
        trips = dao.trips(fltr=func.date(CalendarDate.date) == july4.date,
                          prefetch_calendars=True)
        n = 0
        for trip in trips:
            self.assertTrue(july4 in trip.calendar.dates)
            n += 1
        self.assertTrue(n > 30)
Esempio n. 8
0
    def test_complex_queries(self):
        dao = Dao(DAO_URL, sql_logging=SQL_LOG)
        dao.load_gtfs(DUMMY_GTFS)

        # Get the list of departures:
        # 1) from "Porte de Bourgogne"
        # 2) on 4th July
        # 3) between 10:00 and 14:00
        # 4) on route type BUS
        # 5) not the last of trip (only departing)
        porte_bourgogne = dao.stop("BBG")
        july4 = CalendarDate.ymd(2016, 7, 4)
        from_time = gtfstime(10, 00)
        to_time = gtfstime(14, 00)
        departures = dao.stoptimes(
            fltr=(StopTime.stop == porte_bourgogne) &
            (StopTime.departure_time >= from_time) &
            (StopTime.departure_time <= to_time)
            & (Route.route_type == Route.TYPE_BUS) &
            (func.date(CalendarDate.date) == july4.date),
            prefetch_trips=True)

        n = 0
        for dep in departures:
            self.assertTrue(dep.stop == porte_bourgogne)
            self.assertTrue(july4 in dep.trip.calendar.dates)
            self.assertTrue(dep.trip.route.route_type == Route.TYPE_BUS)
            self.assertTrue(dep.departure_time >= from_time
                            and dep.departure_time <= to_time)
            n += 1
        self.assertTrue(n > 10)

        # Plage is a stop that is used only in summer (hence the name!)
        plage = dao.stop("BPG")
        # Get the list of stops used by some route:
        # 1) All-year round
        route_red = dao.route("BR")
        stoplist_all = list(dao.stops(fltr=Trip.route == route_red))
        # 2) Only in january
        from_date = CalendarDate.ymd(2016, 1, 1)
        to_date = CalendarDate.ymd(2016, 1, 31)
        stoplist_jan = list(
            dao.stops(fltr=(Trip.route == route_red)
                      & (func.date(CalendarDate.date) >= from_date.date)
                      & (func.date(CalendarDate.date) <= to_date.date)))
        # Now, make some tests
        self.assertTrue(len(stoplist_all) > 5)
        self.assertTrue(plage in stoplist_all)
        self.assertFalse(plage in stoplist_jan)
        stoplist = list(stoplist_all)
        stoplist.remove(plage)
        self.assertTrue(set(stoplist) == set(stoplist_jan))

        # Get all routes passing by the set of stops
        routes = dao.routes(fltr=or_(StopTime.stop == stop
                                     for stop in stoplist_jan))
        stopset = set()
        for route in routes:
            for trip in route.trips:
                for stoptime in trip.stop_times:
                    stopset.add(stoptime.stop)
        self.assertTrue(set(stoplist_jan).issubset(stopset))