def runTest(self): self.trip.AddStopTime(self.stop1, arrival_time="5:11:00", departure_time="5:12:00") self.trip.AddStopTime(self.stop2, arrival_time="5:15:00", departure_time="5:16:00") # Add some more stop times and test GetEndTime does the correct thing self.assertEqual( transitfeed.FormatSecondsSinceMidnight(self.trip.GetStartTime()), "05:11:00") self.assertEqual( transitfeed.FormatSecondsSinceMidnight(self.trip.GetEndTime()), "05:16:00") self.trip.AddStopTime(self.stop1, stop_time="05:20:00") self.assertEqual( transitfeed.FormatSecondsSinceMidnight(self.trip.GetEndTime()), "05:20:00") self.trip.AddStopTime(self.stop2, stop_time="05:22:00") self.assertEqual( transitfeed.FormatSecondsSinceMidnight(self.trip.GetEndTime()), "05:22:00")
def runTest(self): schedule = transitfeed.Schedule( problem_reporter=util.ExceptionProblemReporterNoExpiration()) schedule.NewDefaultAgency(agency_name="Test Agency", agency_url="http://example.com", agency_timezone="America/Los_Angeles") route = schedule.AddRoute(short_name="54C", long_name="Polish Hill", route_type=3) service_period = schedule.GetDefaultServicePeriod() service_period.SetDateHasService("20070101") trip = route.AddTrip(schedule, 'via Polish Hill') stop1 = schedule.AddStop(36.425288, -117.133162, "Demo Stop 1") stop2 = schedule.AddStop(36.424288, -117.133142, "Demo Stop 2") trip.AddStopTime(stop1, arrival_time="5:11:00", departure_time="5:12:00") trip.AddStopTime(stop2, arrival_time="5:15:00", departure_time="5:16:00") # Add some more stop times and test GetEndTime does the correct thing self.assertEqual( transitfeed.FormatSecondsSinceMidnight(trip.GetStartTime()), "05:11:00") self.assertEqual( transitfeed.FormatSecondsSinceMidnight(trip.GetEndTime()), "05:16:00") trip.AddStopTime(stop1, stop_time="05:20:00") self.assertEqual( transitfeed.FormatSecondsSinceMidnight(trip.GetEndTime()), "05:20:00") trip.AddStopTime(stop2, stop_time="05:22:00") self.assertEqual( transitfeed.FormatSecondsSinceMidnight(trip.GetEndTime()), "05:22:00") self.assertEqual(len(trip.GetStopTimesTuples()), 4) self.assertEqual(trip.GetStopTimesTuples()[0], (trip.trip_id, "05:11:00", "05:12:00", stop1.stop_id, 1, '', '', '', '')) self.assertEqual(trip.GetStopTimesTuples()[3], (trip.trip_id, "05:22:00", "05:22:00", stop2.stop_id, 4, '', '', '', ''))
def testGetFrequencyStartTimes(self): start_times = self.trip1.GetFrequencyStartTimes() self.assertEqual( ["16:00:00", "16:30:00", "17:00:00", "17:30:00", "18:00:00", "18:45:00", "19:30:00"], [transitfeed.FormatSecondsSinceMidnight(secs) for secs in start_times]) # GetHeadwayStartTimes is deprecated, but should still return the same # result as GetFrequencyStartTimes self.assertEqual(start_times, self.trip1.GetFrequencyStartTimes())
def interpolateTimes(self, speed=20, commit=False, n_timepoints=5): logger.debug("Interpolating stop times for trip_id: %s", self.trip_id) interval = len(self.stopsSequence)/n_timepoints or 1 for i, item in enumerate(self.stopsSequence): stop = item.Stop stopSeq = item.StopSeq if i % interval == 0 or i + 1 == len(self.stopsSequence): dist_traveled = float(stopSeq.shape_dist_traveled) stop_time_secs = int(3600*dist_traveled/(speed)) stop_time = transitfeed.FormatSecondsSinceMidnight(stop_time_secs) else: stop_time = None stopSeq.stop_time = stop_time self.db.session.merge(stopSeq) self.db.session.commit()
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)
def _DrawTrips(self, triplist, colpar=""): """Generates svg polylines for each transit trip. Args: # Class Trip is defined in transitfeed.py [Trip, Trip, ...] Returns: # A string containing a polyline tag for each trip ' <polyline class="T" stroke="#336633" points="433,0 ...' """ stations = [] if not self._stations and triplist: self._stations = self._CalculateYLines(self._TravelTimes(triplist)) if not self._stations: self._AddWarning("Failed to use traveltimes for graph") self._stations = self._CalculateYLines(self._Uniform(triplist)) if not self._stations: self._AddWarning("Failed to calculate station distances") return stations = self._stations tmpstrs = [] servlist = [] for t in triplist: if not colpar: if t.service_id not in servlist: servlist.append(t.service_id) shade = int( servlist.index(t.service_id) * (200 / len(servlist)) + 55) color = "#00%s00" % hex(shade)[2:4] else: color = colpar start_offsets = [0] first_stop = t.GetTimeStops()[0] for j, freq_offset in enumerate(start_offsets): if j > 0 and not colpar: color = "purple" scriptcall = 'onmouseover="LineClick(\'%s\',\'Trip %s starting %s\')"' % ( t.trip_id, t.trip_id, transitfeed.FormatSecondsSinceMidnight(t.GetStartTime())) tmpstrhead = '<polyline class="T" id="%s" stroke="%s" %s points="' % \ (str(t.trip_id),color, scriptcall) tmpstrs.append(tmpstrhead) for i, s in enumerate(t.GetTimeStops()): arr_t = s[0] dep_t = s[1] if arr_t is None or dep_t is None: continue arr_x = int( arr_t / 3600.0 * self._hour_grid) - self._hour_grid * self._offset dep_x = int( dep_t / 3600.0 * self._hour_grid) - self._hour_grid * self._offset tmpstrs.append("%s,%s " % (int(arr_x + 20), int(stations[i] + 20))) tmpstrs.append("%s,%s " % (int(dep_x + 20), int(stations[i] + 20))) tmpstrs.append('" />') return "".join(tmpstrs)