Beispiel #1
0
def AddRouteToSchedule(schedule, table):
  if len(table) >= 2:
    r = schedule.AddRoute(short_name=table[0][0], long_name=table[0][1], route_type='Bus')
    for trip in table[2:]:
      if len(trip) > len(table[1]):
        print("ignoring %s" % trip[len(table[1]):])
        trip = trip[0:len(table[1])]
      t = r.AddTrip(schedule, headsign='My headsign')
      trip_stops = []  # Build a list of (time, stopname) tuples
      for i in range(0, len(trip)):
        if re.search(r'\S', trip[i]):
          trip_stops.append( (transitfeed.TimeToSecondsSinceMidnight(trip[i]), table[1][i]) )
      trip_stops.sort()  # Sort by time
      for (time, stopname) in trip_stops:
        t.AddStopTime(stop=stops[stopname.lower()], arrival_secs=time,
                      departure_secs=time)
Beispiel #2
0
    def add_trips_by_day(self, feed, line, service, route, horarios, day):

        # check if we even have service
        if horarios is None or len(horarios) == 0:
            return

        if isinstance(route, Line):
            # recurse into "Ida" and "Volta" routes
            for sub_route in route.get_itineraries():
                sub_route.duration = route.duration
                self.add_trips_by_day(feed, line, service, sub_route, horarios, day)
            return

        # have at least two stops
        if len(route.stops) < 2:
            sys.stderr.write("Skipping Route, has no stops: " + str(route) + "\n")
            return

        # check if we have a match for the first stop
        key = self.match_first_stops(route, horarios.keys())

        if key is None:
            # Do not print debug output here, because already done in route.match_first_stops()
            return

        if route.route_id == DEBUG_ROUTE:
            print "\n\n\n" + str(route)
            print day + " - " + key

        # get shape id
        shape_id = str(route.route_id)
        try:
            feed.GetShape(shape_id)
        except KeyError:
            shape = transitfeed.Shape(shape_id)
            for point in route.shape:
                shape.AddPoint(lat=float(point["lat"]), lon=float(point["lon"]))
            feed.AddShapeObject(shape)

        if len(horarios) > 1 and route.line is None:
            sys.stderr.write(
                "Route should have a master: [" + route.route_id + "] " + str(
                    route.osm_url) + "\n")

        for time_group in horarios[key]:
            for time_point in time_group:
                # parse first departure time
                start_time = datetime.strptime(time_point[0], "%H:%M")
                start_time = str(start_time.time())

                # calculate last arrival time for GTFS
                start_sec = transitfeed.TimeToSecondsSinceMidnight(start_time)
                factor = 1
                if len(horarios) > 1 and route.line is None:
                    # since this route has only one instead of two trips, double the duration
                    factor = 2
                end_sec = start_sec + route.duration.seconds * factor
                end_time = transitfeed.FormatSecondsSinceMidnight(end_sec)

                # TODO handle options
                # opts = time_point[1]

                trip = line.AddTrip(feed, headsign=route.name, service_period=service)
                # add empty attributes to make navitia happy
                trip.block_id = ""
                trip.wheelchair_accessible = ""
                trip.bikes_allowed = ""
                trip.shape_id = shape_id
                trip.direction_id = ""
                if route.route_id == DEBUG_ROUTE:
                    print "ADD TRIP " + str(trip.trip_id) + ":"
                self.add_trip_stops(feed, trip, route, start_time, end_time)

                # interpolate times, because Navitia can not handle this itself
                Helper.interpolate_stop_times(trip)