Beispiel #1
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)
Beispiel #2
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)
Beispiel #3
0
    def test_areas(self):
        dao = Dao()
        f1 = FeedInfo("F1")
        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)
        dao.add_all([f1, s1, s2, s3])

        # Rectangular area
        stops = list(dao.stops(fltr=dao.in_area(RectangularArea(0, 0, 1, 1))))
        self.assertTrue(len(stops) == 0)
        stops = list(
            dao.stops(fltr=dao.in_area(RectangularArea(-90, -180, 90, 180))))
        self.assertTrue(len(stops) == 3)
        stops = list(
            dao.stops(
                fltr=dao.in_area(RectangularArea(45.05, 0.05, 45.15, 0.15))))
        self.assertTrue(len(stops) == 1)
        self.assertTrue(stops[0].stop_id == 'S2')
Beispiel #4
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)
Beispiel #5
0
    def __process_stops_and_times(self, trip, timepoint, waypoints):
        filtered_waypoints = [x for x in waypoints if x['name']]
        for waypoint_idx, waypoint in enumerate(filtered_waypoints):
            stop = Stop(self.feed_id,
                        str(waypoint['stationID']),
                        waypoint['name'],
                        waypoint['lat'],
                        waypoint['lng'])

            if stop.stop_id not in self.stops:
                self.stops.add(stop.stop_id)
                self.dao.add(stop)

            distance_traveled = math.floor(waypoint['total'])

            first_departure_time = 0
            if waypoint_idx == 0:
                departure_time = self.__convert_tsm(timepoint)
                stop_time = StopTime(feed_id=self.feed_id,
                                     trip_id=trip.trip_id,
                                     stop_id=stop.stop_id,
                                     stop_sequence=0,
                                     departure_time=departure_time,
                                     arrival_time=departure_time,
                                     shape_dist_traveled=distance_traveled,
                                     timepoint=1)
                first_departure_time = departure_time
            elif waypoint_idx == (len(filtered_waypoints) - 1):
                delta_time = self.__process_time_for_distance(waypoint['total'])
                end_time = int(delta_time + first_departure_time)
                stop_time = StopTime(feed_id=self.feed_id,
                                     trip_id=trip.trip_id,
                                     stop_id=stop.stop_id,
                                     stop_sequence=waypoint_idx,
                                     departure_time=end_time,
                                     arrival_time=end_time,
                                     shape_dist_traveled=distance_traveled,
                                     timepoint=1)
            else:
                stop_time = StopTime(feed_id=self.feed_id,
                                     trip_id=trip.trip_id,
                                     stop_id=stop.stop_id,
                                     stop_sequence=waypoint_idx,
                                     departure_time=None,
                                     arrival_time=None,
                                     shape_dist_traveled=distance_traveled,
                                     interpolated=True,
                                     timepoint=0)

            trip.stop_times.append(stop_time)
Beispiel #6
0
    def _make_stop(self, stop_label, **kwargs):
        stop_id = sanitize(stop_label)
        if stop_id not in self.stop_map:
            if "latitude" not in kwargs or "longitude" not in kwargs:
                logger.error(f"Mising GPS position for stop: {stop_label}")
            lat = kwargs.get("latitude", DEFAULT_LATITUDE)
            lng = kwargs.get("longitude", DEFAULT_LONGITUDE)
            self.dao.add(Stop(self.feed_id, stop_id, stop_label, lat, lng))
            self.stop_map[stop_id] = {"id": stop_id, "label": stop_label}

        elif stop_label != self.stop_map[stop_id]["label"]:
            existing_label = self.stop_map[stop_id]["label"]
            logger.error(
                f"Stop label consistency issue: {stop_label} != {existing_label}"
            )

        return stop_id
Beispiel #7
0
 def import_stop(stop, stoptype, zone_ids, item_ids, station_ids=None):
     zone_id = stop.get('zone_id')
     if zone_id and zone_id not in zone_ids:
         # Lazy-creation of zone
         zone = Zone(feed_id, zone_id)
         zone_ids.add(zone_id)
         dao.add(zone)
     stop['location_type'] = _toint(stop.get('location_type'),
                                    Stop.TYPE_STOP)
     if stop['location_type'] != stoptype:
         return 0
     stop['wheelchair_boarding'] = _toint(stop.get('wheelchair_boarding'),
                                          Stop.WHEELCHAIR_UNKNOWN)
     lat = _tofloat(stop.get('stop_lat'), None)
     lon = _tofloat(stop.get('stop_lon'), None)
     if lat is None or lon is None:
         if lenient:
             logger.error("Missing lat/lon for '%s', set to default (0,0)" %
                          (stop, ))
             if lat is None:
                 lat = 0
             if lon is None:
                 lon = 0
         else:
             raise ValueError("Missing mandatory lat/lon for '%s'." %
                              (stop, ))
     stop['stop_lat'] = lat
     stop['stop_lon'] = lon
     # This field has been renamed for consistency
     parent_id = stop.get('parent_station')
     stop['parent_station_id'] = parent_id if parent_id else None
     if parent_id and station_ids and parent_id not in station_ids:
         if lenient:
             logger.error(
                 "Parent station ID '%s' in '%s' is invalid, resetting." %
                 (parent_id, stop))
             stop['parent_station_id'] = None
         else:
             raise KeyError("Parent station ID '%s' in '%s' is invalid." %
                            (parent_id, stop))
     stop.pop('parent_station', None)
     stop2 = Stop(feed_id, **stop)
     dao.add(stop2)
     item_ids.add(stop2.stop_id)
     return 1
Beispiel #8
0
    def _load_routes(self):
        self._clear_trips()

        stops = set()
        route_data = self.line_request()
        logger.info(f"Total lines to process \t\t\t{len(route_data['lines'])}")
        for line_nb, line in enumerate(route_data["lines"]):
            logger.info(
                f"\tprocessing line {line['name']} \t\t\t [{line_nb + 1}/{len(route_data['lines'])}]"
            )
            r = Route(
                self.feed_id, line['id'], line['organization']['id'],
                self._parse_route_type(line['type']), **{
                    "route_color": line['color'],
                    "route_text_color": "000000",
                    "route_short_name": line['name']
                })

            # fetch both directions
            for direction in [0, 1]:
                trip_data = self.line_detail_request(r.route_id, direction)
                r.route_long_name = f"{trip_data['direction_name_tur']} - {trip_data['direction_name_retur']}"

                trips = []
                shape_id = f"shp{r.agency_id}_{r.route_id}_{direction}"

                shape_count = self.dao.session.query(Shape).filter(
                    Shape.shape_id == shape_id).count()
                shape_points_count = self.dao.session.query(ShapePoint).filter(
                    ShapePoint.shape_id == shape_id).count()

                if shape_count == 0 or shape_points_count == 0:
                    shape_points = polyline.decode(trip_data['segment_path'])
                    logger.debug("processing shape")
                    shp = Shape(self.feed_id,
                                f"shp{r.agency_id}_{r.route_id}_{direction}")
                    self.dao.add(shp)

                    for shp_point_index, shape_point in enumerate(
                            shape_points):
                        self.dao.add(
                            ShapePoint(self.feed_id, shp.shape_id,
                                       shp_point_index, shape_point[0],
                                       shape_point[1], -999999))
                else:
                    shp = self.dao.session.query(Shape).get(
                        [self.feed_id, shape_id])

                logger.debug(
                    f"total stops to process {len(trip_data['stops'])}")
                for stop_index, stop in enumerate(trip_data['stops']):
                    logger.debug(
                        f" - processing stop {stop_index + 1} of {len(trip_data['stops'])}"
                    )
                    s = Stop(self.feed_id, stop['id'], stop['name'],
                             stop['lat'], stop['lng'])
                    if s.stop_id not in stops:
                        stops.add(s.stop_id)
                        self.dao.update(s)

                    result = self._process_route_stop(r, s, shp, direction,
                                                      stop_index, trips)

            self.dao.update(r)
            self.dao.flush()
Beispiel #9
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)
Beispiel #10
0
    def test_stop_station(self):
        dao = Dao()
        f1 = FeedInfo("F1")
        sa = Stop("F1", "SA", "Station A", 45.0000, 0.0000)
        sa1 = Stop("F1", "SA1", "Stop A1", 45.0001, 0.0001)
        sa1.parent_station_id = 'SA'
        sb = Stop("F1", "SB", "Station B", 45.0002, 0.0002)
        sb1 = Stop("F1", "SB1", "Stop B1", 45.0003, 0.0003)
        sb1.parent_station_id = 'SB'
        sb2 = Stop("F1", "SB2", "Stop B2", 45.0002, 0.0003)
        sb2.parent_station_id = 'SB'
        a1 = Agency("F1", "A1", "Agency 1", "url1", "Europe/Paris")
        r1 = Route("F1", "R1", "A1", Route.TYPE_BUS)
        c1 = Calendar("F1", "C1")
        t1 = Trip("F1", "T1", "R1", "C1")
        st1a = StopTime("F1", "T1", "SA1", 0, None, 3600, 0.0)
        st1b = StopTime("F1", "T1", "SB1", 1, 3800, None, 100.0)
        dao.add_all([f1, sa, sa1, sb, sb1, sb2, a1, r1, c1, t1, st1a, st1b])

        stops = list(dao.stops(fltr=(Agency.agency_id == 'A1')))
        self.assertTrue(len(stops) == 2)
        self.assertTrue(sa1 in stops)
        self.assertTrue(sb1 in stops)

        stops = list(dao.stops(fltr=(Stop.parent_station_id == 'SA')))
        self.assertTrue(len(stops) == 1)
        self.assertTrue(sa1 in stops)

        stops = list(dao.stops(fltr=(Stop.parent_station_id == 'SB')))
        self.assertTrue(len(stops) == 2)
        self.assertTrue(sb1 in stops)
        self.assertTrue(sb2 in stops)
Beispiel #11
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)
Beispiel #12
0
 def test_same_station(self):
     s1a = Stop('F1', 'Sa', 'StopA', 45, 0)
     s1b = Stop('F1', 'Sb', 'StopB', 45, 0.1)
     s1 = Stop('F1', 'S', 'Stop', 45, 0.05, location_type=Stop.TYPE_STATION)
     s1a.parent_station_id = 'S'
     s1b.parent_station_id = 'S'
     self.assertTrue(s1a.in_same_station(s1b))
     self.assertTrue(s1b.in_same_station(s1a))
     self.assertTrue(s1.in_same_station(s1a))
     self.assertTrue(s1a.in_same_station(s1))
     s1c = Stop('F2', 'Sb', 'StopB', 45, 0.1)
     s1c.parent_station_id = 'S'
     self.assertFalse(s1c.in_same_station(s1b))
     self.assertFalse(s1c.in_same_station(s1a))
     self.assertFalse(s1a.in_same_station(s1c))