Search in sources :

Example 81 with ServiceDate

use of org.onebusaway.gtfs.model.calendar.ServiceDate in project onebusaway-gtfs-modules by OneBusAway.

the class ShiftNegativeStopTimesUpdateStrategyTest method test.

@Test
public void test() throws IOException {
    _gtfs.putAgencies(1);
    _gtfs.putStops(3);
    _gtfs.putRoutes(1);
    _gtfs.putCalendars(1, "start_date=20120903", "end_date=20121016", "mask=1111100");
    _gtfs.putTrips(1, "r0", "sid0");
    _gtfs.putLines("stop_times.txt", "trip_id,stop_id,stop_sequence,arrival_time,departure_time", "t0,s0,0,-01:00:00,-01:05:00", "t0,s1,1,-01:30:00,-01:30:00", "t0,s2,2,00:30:00,00:30:00");
    GtfsMutableRelationalDao dao = _gtfs.read();
    _strategy.run(new TransformContext(), dao);
    Trip trip = dao.getTripForId(_gtfs.id("t0"));
    assertEquals("sid0 -1", trip.getServiceId().getId());
    List<StopTime> stopTimes = dao.getStopTimesForTrip(trip);
    assertEquals(3, stopTimes.size());
    {
        StopTime stopTime = stopTimes.get(0);
        assertEquals(stopTime.getArrivalTime(), StopTimeFieldMappingFactory.getStringAsSeconds("23:00:00"));
        assertEquals(stopTime.getDepartureTime(), StopTimeFieldMappingFactory.getStringAsSeconds("23:05:00"));
    }
    {
        StopTime stopTime = stopTimes.get(1);
        assertEquals(stopTime.getArrivalTime(), StopTimeFieldMappingFactory.getStringAsSeconds("23:30:00"));
        assertEquals(stopTime.getDepartureTime(), StopTimeFieldMappingFactory.getStringAsSeconds("23:30:00"));
    }
    {
        StopTime stopTime = stopTimes.get(2);
        assertEquals(stopTime.getArrivalTime(), StopTimeFieldMappingFactory.getStringAsSeconds("24:30:00"));
        assertEquals(stopTime.getDepartureTime(), StopTimeFieldMappingFactory.getStringAsSeconds("24:30:00"));
    }
    ServiceCalendar c = dao.getCalendarForServiceId(trip.getServiceId());
    assertEquals(c.getStartDate(), new ServiceDate(2012, 9, 2));
    assertEquals(c.getEndDate(), new ServiceDate(2012, 10, 15));
    assertEquals(1, c.getMonday());
    assertEquals(1, c.getTuesday());
    assertEquals(1, c.getWednesday());
    assertEquals(1, c.getThursday());
    assertEquals(0, c.getFriday());
    assertEquals(0, c.getSaturday());
    assertEquals(1, c.getSunday());
}
Also used : GtfsMutableRelationalDao(org.onebusaway.gtfs.services.GtfsMutableRelationalDao) Trip(org.onebusaway.gtfs.model.Trip) ServiceDate(org.onebusaway.gtfs.model.calendar.ServiceDate) TransformContext(org.onebusaway.gtfs_transformer.services.TransformContext) StopTime(org.onebusaway.gtfs.model.StopTime) ServiceCalendar(org.onebusaway.gtfs.model.ServiceCalendar) Test(org.junit.Test)

Example 82 with ServiceDate

use of org.onebusaway.gtfs.model.calendar.ServiceDate in project onebusaway-gtfs-modules by OneBusAway.

the class CalendarServiceDataFactoryImpl method createData.

@Override
public CalendarServiceData createData() {
    CalendarServiceData data = new CalendarServiceData();
    setTimeZonesForAgencies(data);
    List<AgencyAndId> serviceIds = _dao.getAllServiceIds();
    int index = 0;
    for (AgencyAndId serviceId : serviceIds) {
        index++;
        _log.info("serviceId=" + serviceId + " (" + index + "/" + serviceIds.size() + ")");
        TimeZone serviceIdTimeZone = data.getTimeZoneForAgencyId(serviceId.getAgencyId());
        if (serviceIdTimeZone == null) {
            serviceIdTimeZone = TimeZone.getDefault();
        }
        Set<ServiceDate> activeDates = getServiceDatesForServiceId(serviceId, serviceIdTimeZone);
        List<ServiceDate> serviceDates = new ArrayList<ServiceDate>(activeDates);
        Collections.sort(serviceDates);
        data.putServiceDatesForServiceId(serviceId, serviceDates);
        List<String> tripAgencyIds = _dao.getTripAgencyIdsReferencingServiceId(serviceId);
        Set<TimeZone> timeZones = new HashSet<TimeZone>();
        for (String tripAgencyId : tripAgencyIds) {
            TimeZone timeZone = data.getTimeZoneForAgencyId(tripAgencyId);
            if (timeZone == null) {
                timeZone = serviceIdTimeZone;
            }
            timeZones.add(timeZone);
        }
        for (TimeZone timeZone : timeZones) {
            List<Date> dates = new ArrayList<Date>(serviceDates.size());
            for (ServiceDate serviceDate : serviceDates) try {
                if (timeZone == null)
                    timeZone = TimeZone.getDefault();
                dates.add(serviceDate.getAsDate(timeZone));
            } catch (Exception any) {
                _log.error(" calendar load failed for " + serviceDate + " for serviceId " + serviceId);
            }
            LocalizedServiceId id = new LocalizedServiceId(serviceId, timeZone);
            data.putDatesForLocalizedServiceId(id, dates);
        }
    }
    return data;
}
Also used : AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) ArrayList(java.util.ArrayList) ServiceDate(org.onebusaway.gtfs.model.calendar.ServiceDate) Date(java.util.Date) ServiceCalendarDate(org.onebusaway.gtfs.model.ServiceCalendarDate) CalendarServiceData(org.onebusaway.gtfs.model.calendar.CalendarServiceData) TimeZone(java.util.TimeZone) ServiceDate(org.onebusaway.gtfs.model.calendar.ServiceDate) LocalizedServiceId(org.onebusaway.gtfs.model.calendar.LocalizedServiceId) HashSet(java.util.HashSet)

Example 83 with ServiceDate

use of org.onebusaway.gtfs.model.calendar.ServiceDate in project onebusaway-gtfs-modules by OneBusAway.

the class CalendarServiceImpl method search.

private int search(List<Date> serviceDates, ServiceIdOp op, int indexFrom, int indexTo, Date key) {
    if (indexTo == indexFrom)
        return indexFrom;
    int index = (indexFrom + indexTo) / 2;
    Date serviceDate = op.getServiceDate(serviceDates, index);
    int rc = op.compare(key, serviceDate);
    if (rc == 0)
        return index;
    if (rc < 0)
        return search(serviceDates, op, indexFrom, index, key);
    else
        return search(serviceDates, op, index + 1, indexTo, key);
}
Also used : ServiceDate(org.onebusaway.gtfs.model.calendar.ServiceDate) Date(java.util.Date)

Example 84 with ServiceDate

use of org.onebusaway.gtfs.model.calendar.ServiceDate in project onebusaway-gtfs-modules by OneBusAway.

the class CalendarServiceImpl method getServiceDates.

private List<Date> getServiceDates(CalendarServiceData allData, LocalizedServiceId serviceId, ServiceInterval interval, ServiceIdOp op, Date from, Date to, boolean includeNextDateIfNeeded) {
    List<Date> serviceDates = allData.getDatesForLocalizedServiceId(serviceId);
    List<Date> resultsForServiceId = new ArrayList<Date>();
    Date nextDate = null;
    if (serviceDates == null)
        return resultsForServiceId;
    Date target = op.shiftTime(interval, from);
    int index = search(serviceDates, op, 0, serviceDates.size(), target);
    if (index == serviceDates.size())
        index--;
    while (0 <= index) {
        Date serviceDate = op.getServiceDate(serviceDates, index);
        int rc = op.compareInterval(interval, serviceDate, from, to);
        if (rc > 0) {
            nextDate = serviceDate;
        } else if (rc == 0) {
            resultsForServiceId.add(serviceDate);
        } else if (rc < 0) {
            break;
        }
        index--;
    }
    if (includeNextDateIfNeeded && resultsForServiceId.isEmpty() && nextDate != null)
        resultsForServiceId.add(nextDate);
    return resultsForServiceId;
}
Also used : ArrayList(java.util.ArrayList) ServiceDate(org.onebusaway.gtfs.model.calendar.ServiceDate) Date(java.util.Date)

Example 85 with ServiceDate

use of org.onebusaway.gtfs.model.calendar.ServiceDate in project onebusaway-gtfs-modules by OneBusAway.

the class CalendarServiceImpl method getServiceDatesForServiceId.

@Override
public Set<ServiceDate> getServiceDatesForServiceId(AgencyAndId serviceId) {
    Set<ServiceDate> dates = new HashSet<ServiceDate>();
    CalendarServiceData allData = getData();
    List<ServiceDate> serviceDates = allData.getServiceDatesForServiceId(serviceId);
    if (serviceDates != null)
        dates.addAll(serviceDates);
    return dates;
}
Also used : CalendarServiceData(org.onebusaway.gtfs.model.calendar.CalendarServiceData) ServiceDate(org.onebusaway.gtfs.model.calendar.ServiceDate) HashSet(java.util.HashSet)

Aggregations

ServiceDate (org.onebusaway.gtfs.model.calendar.ServiceDate)134 Test (org.junit.Test)49 Date (java.util.Date)46 AgencyAndId (org.onebusaway.gtfs.model.AgencyAndId)45 ArrayList (java.util.ArrayList)23 ServiceCalendar (org.onebusaway.gtfs.model.ServiceCalendar)23 Calendar (java.util.Calendar)22 ServiceCalendarDate (org.onebusaway.gtfs.model.ServiceCalendarDate)22 HashSet (java.util.HashSet)17 ServiceIdActivation (org.onebusaway.transit_data_federation.services.transit_graph.ServiceIdActivation)14 CalendarServiceData (org.onebusaway.gtfs.model.calendar.CalendarServiceData)13 CalendarService (org.onebusaway.gtfs.services.calendar.CalendarService)13 Set (java.util.Set)11 Trip (org.onebusaway.gtfs.model.Trip)11 TimeZone (java.util.TimeZone)10 GtfsMutableRelationalDao (org.onebusaway.gtfs.services.GtfsMutableRelationalDao)10 TripUpdate (com.google.transit.realtime.GtfsRealtime.TripUpdate)9 LocalizedServiceId (org.onebusaway.gtfs.model.calendar.LocalizedServiceId)9 Agency (org.onebusaway.gtfs.model.Agency)8 TripDescriptor (com.google.transit.realtime.GtfsRealtime.TripDescriptor)7