Ejemplo n.º 1
0
    def load_data_source(self, dao: Dao) -> bool:
        self.dao = dao
        self._load_agencies()
        self._load_services()
        self._load_routes()
        dn = DataNormalizer(dao, self.feed_id)
        dn.normalize()
        dao.commit()

        return super().load_data_source(dao)
Ejemplo n.º 2
0
    def test_shapes(self):
        dao = Dao()
        f1 = FeedInfo("")
        a1 = Agency("",
                    "A1",
                    "Agency 1",
                    agency_url="http://www.agency.fr/",
                    agency_timezone="Europe/Paris")
        r1 = Route("",
                   "R1",
                   "A1",
                   3,
                   route_short_name="R1",
                   route_long_name="Route 1")
        c1 = Calendar("", "C1")
        c1.dates = [
            CalendarDate.ymd(2016, 1, 31),
            CalendarDate.ymd(2016, 2, 1)
        ]
        s1 = Stop("", "S1", "Stop 1", 45.0, 0.0)
        s2 = Stop("", "S2", "Stop 2", 45.1, 0.1)
        s3 = Stop("", "S3", "Stop 3", 45.2, 0.2)
        t1 = Trip("", "T1", "R1", "C1")
        t1.stop_times = [
            StopTime(None, None, "S1", 0, 28800, 28800, 0.0),
            StopTime(None, None, "S2", 1, 29400, 29400, 2.0),
            StopTime(None, None, "S3", 2, 30000, 30000, 4.0)
        ]
        t2 = Trip("", "T2", "R1", "C1")
        t2.stop_times = [
            StopTime(None, None, "S2", 0, 30600, 30600, 0.0),
            StopTime(None, None, "S1", 1, 31000, 31000, 1.0)
        ]
        sh1 = Shape("", "Sh1")
        sh1.points = [
            ShapePoint(None, None, 0, 45.00, 0.00, 0.0),
            ShapePoint(None, None, 1, 45.05, 0.10, 1.0),
            ShapePoint(None, None, 2, 45.10, 0.10, 2.0),
            ShapePoint(None, None, 3, 45.15, 0.20, 3.0),
            ShapePoint(None, None, 4, 45.20, 0.20, 4.0)
        ]
        t1.shape = sh1
        dao.add_all([f1, a1, r1, c1, s1, s2, s3, t1, t2, sh1])
        dao.commit()

        t = dao.trip("T1")
        self.assertTrue(t.shape.shape_id == "Sh1")
        self.assertTrue(len(t.shape.points) == 5)
        t = dao.trip("T2")
        self.assertTrue(t.shape == None)
Ejemplo 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)
Ejemplo n.º 4
0
    def test_fares(self):
        dao = Dao()
        f1 = FeedInfo("")
        a1 = Agency("",
                    "A1",
                    "Agency 1",
                    agency_url="http://www.agency.fr/",
                    agency_timezone="Europe/Paris")
        r1 = Route("",
                   "R1",
                   "A1",
                   3,
                   route_short_name="R1",
                   route_long_name="Route 1")
        r2 = Route("",
                   "R2",
                   "A1",
                   3,
                   route_short_name="R2",
                   route_long_name="Route 2")
        z1 = Zone("", "Z1")
        z2 = Zone("", "Z2")
        fare1 = FareAttribute("", "F1", 1.0, "EUR",
                              FareAttribute.PAYMENT_ONBOARD, None, None)
        fare2 = FareAttribute("", "F2", 2.0, "EUR",
                              FareAttribute.PAYMENT_BEFOREBOARDING, 3, 3600)
        rule1 = FareRule("", "F1", route_id="R1")
        rule2 = FareRule("", "F1", origin_id="Z1", destination_id="Z2")
        dao.add_all([f1, a1, r1, r2, z1, z2, fare1, fare2, rule1, rule2])
        dao.commit()

        self.assertTrue(len(dao.fare_attributes()) == 2)
        self.assertTrue(len(dao.fare_rules(fltr=(FareRule.route == r1))) == 1)
        self.assertTrue(len(dao.fare_rules(fltr=(FareRule.route == r2))) == 0)
        self.assertTrue(len(dao.fare_rules(fltr=(FareRule.origin == z1))) == 1)
        fare = dao.fare_attribute("F1")
        self.assertTrue(len(fare.fare_rules) == 2)
        fare = dao.fare_attribute("F2")
        self.assertTrue(len(fare.fare_rules) == 0)

        # Test equivalence and hash on primary keys for rule
        fr1a = FareRule("",
                        "F1",
                        route_id="R",
                        origin_id="ZO",
                        destination_id="ZD",
                        contains_id=None)
        fr1b = FareRule("",
                        "F1",
                        route_id="R",
                        origin_id="ZO",
                        destination_id="ZD",
                        contains_id=None)
        fr2 = FareRule("",
                       "F1",
                       route_id="R",
                       origin_id="ZO",
                       destination_id=None,
                       contains_id="ZD")
        ruleset = set()
        ruleset.add(fr1a)
        ruleset.add(fr2)
        ruleset.add(fr1b)
        self.assertTrue(len(ruleset) == 2)
        self.assertTrue(fr1a == fr1b)
        self.assertTrue(fr1a != fr2)
Ejemplo n.º 5
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)
Ejemplo n.º 6
0
class Exporter:
    def __init__(self, arguments):
        self.logger = logging.getLogger('gtfsexporter')
        self._arguments = arguments

        if arguments['--id'] is None:
            arguments['--id'] = "default"

        database_path = os.path.join(__cwd_path__,
                                     arguments['--id'] + ".sqlite")

        self._dao = Dao(database_path,
                        sql_logging=arguments['--logsql'],
                        schema=arguments['--schema'])

        if arguments['--list']:
            for feed in self._dao.feeds():
                print(feed.feed_id if feed.feed_id != "" else "(default)")

        if arguments['--delete']:
            feed_id = arguments['--id']
            existing_feed = self._dao.feed(feed_id)
            if existing_feed:
                self.logger.warning("Deleting existing feed ID '%s'" % feed_id)
                self._dao.delete_feed(feed_id)
                self._dao.commit()

    @property
    def dao(self):
        return self._dao

    def load(self, provider: DataProvider):
        self.logger.info("Importing data from provided source")
        provider.load_data_source(self._dao)

    def process(self, processor: Processor = None):
        self.logger.info("Processing data from provided source")

        # Here we should use a rule processor to have more flexibility when processing data
        # processor.process(ruleset)

        for route in self._dao.routes():
            print("updating route [%s] setting correct color" %
                  route.route_long_name)

            route.route_text_color = "FFFFFF"

            if route.route_type == Route.TYPE_BUS:
                route.route_color = "195BAD"
            elif route.route_type == Route.TYPE_TRAM:
                route.route_color = "FFAD33"
            elif route.route_type == Route.TYPE_RAIL:
                route.route_color = "FF5B33"
            elif route.route_type == Route.TYPE_CABLECAR:
                route.route_color = "FF8433"
            elif route.route_type == Route.TYPE_SUBWAY:
                route.route_color = "D13333"
            elif route.route_type == Route.TYPE_FERRY:
                route.route_color = "62A9DD"

        self._dao.commit()

    def export(self, bundle=False, out_path: str = __output_path__) -> str:
        self.logger.info(
            f"Generating archive with name gtfs-{self._arguments['--id']}.zip")

        class __Args:
            filter = None

        context = Context(self._dao, __Args(), out_path)

        if bundle:
            w = Writer(context, bundle=f"gtfs-{self._arguments['--id']}.zip")
        else:
            w = Writer(context)

        w.run()

        return out_path