Search in sources :

Example 6 with ServiceCalendarDate

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

the class CalendarServiceDataFactoryImpl method getServiceDatesForServiceId.

public Set<ServiceDate> getServiceDatesForServiceId(AgencyAndId serviceId, TimeZone serviceIdTimeZone) {
    Set<ServiceDate> activeDates = new HashSet<ServiceDate>();
    ServiceCalendar c = _dao.getCalendarForServiceId(serviceId);
    if (c != null) {
        addDatesFromCalendar(c, serviceIdTimeZone, activeDates);
    }
    for (ServiceCalendarDate cd : _dao.getCalendarDatesForServiceId(serviceId)) {
        addAndRemoveDatesFromCalendarDate(cd, serviceIdTimeZone, activeDates);
    }
    return activeDates;
}
Also used : ServiceCalendarDate(org.onebusaway.gtfs.model.ServiceCalendarDate) ServiceDate(org.onebusaway.gtfs.model.calendar.ServiceDate) HashSet(java.util.HashSet) ServiceCalendar(org.onebusaway.gtfs.model.ServiceCalendar)

Example 7 with ServiceCalendarDate

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

the class CalendarServiceDataFactoryImpl method addAndRemoveDatesFromCalendarDate.

private void addAndRemoveDatesFromCalendarDate(ServiceCalendarDate calendarDate, TimeZone serviceIdTimeZone, Set<ServiceDate> activeDates) {
    ServiceDate serviceDate = calendarDate.getDate();
    Date targetDate = calendarDate.getDate().getAsDate();
    Calendar c = Calendar.getInstance();
    c.setTime(targetDate);
    switch(calendarDate.getExceptionType()) {
        case ServiceCalendarDate.EXCEPTION_TYPE_ADD:
            addServiceDate(activeDates, serviceDate, serviceIdTimeZone);
            break;
        case ServiceCalendarDate.EXCEPTION_TYPE_REMOVE:
            activeDates.remove(serviceDate);
            break;
        default:
            _log.warn("unknown CalendarDate exception type: " + calendarDate.getExceptionType());
            break;
    }
}
Also used : ServiceDate(org.onebusaway.gtfs.model.calendar.ServiceDate) ServiceCalendar(org.onebusaway.gtfs.model.ServiceCalendar) Calendar(java.util.Calendar) ServiceDate(org.onebusaway.gtfs.model.calendar.ServiceDate) Date(java.util.Date) ServiceCalendarDate(org.onebusaway.gtfs.model.ServiceCalendarDate)

Example 8 with ServiceCalendarDate

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

the class GtfsRelationalDaoImplTest method testBart.

@Test
public void testBart() throws IOException {
    GtfsRelationalDaoImpl dao = new GtfsRelationalDaoImpl();
    GtfsTestData.readGtfs(dao, GtfsTestData.getBartGtfs(), "BART");
    List<String> tripAgencyIds = dao.getTripAgencyIdsReferencingServiceId(new AgencyAndId("BART", "WKDY"));
    assertEquals(1, tripAgencyIds.size());
    assertEquals("BART", tripAgencyIds.get(0));
    Agency agency = dao.getAgencyForId("BART");
    List<Route> routes = dao.getRoutesForAgency(agency);
    assertEquals(10, routes.size());
    agency = dao.getAgencyForId("AirBART");
    routes = dao.getRoutesForAgency(agency);
    assertEquals(1, routes.size());
    Route route = dao.getRouteForId(new AgencyAndId("BART", "01"));
    List<Trip> trips = dao.getTripsForRoute(route);
    assertEquals(225, trips.size());
    Trip trip = dao.getTripForId(new AgencyAndId("BART", "15PB1"));
    List<StopTime> stopTimes = dao.getStopTimesForTrip(trip);
    assertEquals(12, stopTimes.size());
    // Ensure the stopTimes are in stop sequence order
    for (int i = 0; i < stopTimes.size() - 1; i++) assertTrue(stopTimes.get(i).getStopSequence() < stopTimes.get(i + 1).getStopSequence());
    Stop stop = dao.getStopForId(new AgencyAndId("BART", "DBRK"));
    stopTimes = dao.getStopTimesForStop(stop);
    assertEquals(584, stopTimes.size());
    List<ShapePoint> shapePoints = dao.getShapePointsForShapeId(new AgencyAndId("BART", "airbart-dn.csv"));
    assertEquals(50, shapePoints.size());
    for (int i = 0; i < shapePoints.size() - 1; i++) assertTrue(shapePoints.get(i).getSequence() < shapePoints.get(i + 1).getSequence());
    trip = dao.getTripForId(new AgencyAndId("AirBART", "M-FSAT1DN"));
    List<Frequency> frequencies = dao.getFrequenciesForTrip(trip);
    assertEquals(1, frequencies.size());
    Frequency frequency = frequencies.get(0);
    assertEquals(5 * 60 * 60, frequency.getStartTime());
    assertEquals(6 * 60 * 60, frequency.getEndTime());
    assertEquals(trip, frequency.getTrip());
    assertEquals(1200, frequency.getHeadwaySecs());
    ServiceCalendar calendar = dao.getCalendarForServiceId(new AgencyAndId("BART", "WKDY"));
    assertEquals(new ServiceDate(2007, 1, 1), calendar.getStartDate());
    List<ServiceCalendarDate> calendarDates = dao.getCalendarDatesForServiceId(new AgencyAndId("BART", "WKDY"));
    assertEquals(7, calendarDates.size());
}
Also used : ServiceCalendarDate(org.onebusaway.gtfs.model.ServiceCalendarDate) Trip(org.onebusaway.gtfs.model.Trip) Agency(org.onebusaway.gtfs.model.Agency) AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) Stop(org.onebusaway.gtfs.model.Stop) ShapePoint(org.onebusaway.gtfs.model.ShapePoint) ShapePoint(org.onebusaway.gtfs.model.ShapePoint) ServiceDate(org.onebusaway.gtfs.model.calendar.ServiceDate) Frequency(org.onebusaway.gtfs.model.Frequency) Route(org.onebusaway.gtfs.model.Route) StopTime(org.onebusaway.gtfs.model.StopTime) ServiceCalendar(org.onebusaway.gtfs.model.ServiceCalendar) Test(org.junit.Test)

Example 9 with ServiceCalendarDate

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

the class CalendarExtensionStrategy method run.

@Override
public void run(TransformContext context, GtfsMutableRelationalDao dao) {
    CalendarService service = CalendarServiceDataFactoryImpl.createService(dao);
    CalendarSimplicationLibrary simplication = new CalendarSimplicationLibrary();
    for (AgencyAndId serviceId : dao.getAllServiceIds()) {
        ServiceCalendarSummary summary = simplication.getSummaryForServiceDates(service.getServiceDatesForServiceId(serviceId));
        /**
       * If a service id has no active dates at all, we don't extend it.
       */
        if (summary.allServiceDates.isEmpty()) {
            continue;
        }
        ServiceCalendar calendar = dao.getCalendarForServiceId(serviceId);
        if (calendar == null) {
            ServiceDate lastDate = summary.serviceDatesInOrder.get(summary.serviceDatesInOrder.size() - 1);
            if (lastDate.compareTo(inactiveCalendarCutoff) < 0) {
                continue;
            }
            /**
         * We only want days of the week that are in service past our stale
         * calendar cutoff.
         */
            Set<Integer> daysOfTheWeekToUse = getDaysOfTheWeekToUse(summary);
            if (daysOfTheWeekToUse.isEmpty()) {
                continue;
            }
            ServiceDate firstMissingDate = lastDate.next();
            for (ServiceDate serviceDate = firstMissingDate; serviceDate.compareTo(endDate) <= 0; serviceDate = serviceDate.next()) {
                Calendar serviceDateAsCalendar = serviceDate.getAsCalendar(_utcTimeZone);
                // Move the calendar forward to "noon" to mitigate the effects of DST
                // (though the shouldn't be a problem for UTC?)
                serviceDateAsCalendar.add(Calendar.HOUR_OF_DAY, 12);
                int dayOfWeek = serviceDateAsCalendar.get(Calendar.DAY_OF_WEEK);
                if (daysOfTheWeekToUse.contains(dayOfWeek)) {
                    ServiceCalendarDate scd = new ServiceCalendarDate();
                    scd.setDate(serviceDate);
                    scd.setExceptionType(ServiceCalendarDate.EXCEPTION_TYPE_ADD);
                    scd.setServiceId(serviceId);
                    dao.saveEntity(scd);
                }
            }
        } else {
            if (calendar.getEndDate().compareTo(inactiveCalendarCutoff) >= 0) {
                calendar.setEndDate(endDate);
            }
        }
    }
    UpdateLibrary.clearDaoCache(dao);
}
Also used : ServiceCalendarDate(org.onebusaway.gtfs.model.ServiceCalendarDate) ServiceDate(org.onebusaway.gtfs.model.calendar.ServiceDate) AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) ServiceCalendar(org.onebusaway.gtfs.model.ServiceCalendar) Calendar(java.util.Calendar) ServiceCalendarSummary(org.onebusaway.gtfs_transformer.updates.CalendarSimplicationLibrary.ServiceCalendarSummary) CalendarService(org.onebusaway.gtfs.services.calendar.CalendarService) ServiceCalendar(org.onebusaway.gtfs.model.ServiceCalendar)

Example 10 with ServiceCalendarDate

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

the class CalendarSimplicationLibrary method computeSimplifiedCalendar.

public void computeSimplifiedCalendar(AgencyAndId updatedServiceId, ServiceCalendarSummary summary, List<Object> newEntities) {
    List<ServiceDate> serviceDatesInOrder = summary.serviceDatesInOrder;
    Set<Integer> daysOfTheWeekToUse = summary.daysOfTheWeekToUse;
    if (serviceDatesInOrder.isEmpty()) {
        return;
    }
    ServiceDate fromDate = serviceDatesInOrder.get(0);
    ServiceDate toDate = serviceDatesInOrder.get(serviceDatesInOrder.size() - 1);
    boolean useDateRange = summary.maxDayOfWeekCount >= _minNumberOfWeeksForCalendarEntry;
    if (useDateRange) {
        ServiceCalendar sc = createServiceCalendar(updatedServiceId, daysOfTheWeekToUse, fromDate, toDate);
        newEntities.add(sc);
    }
    TimeZone tz = TimeZone.getTimeZone("UTC");
    for (ServiceDate serviceDate = fromDate; serviceDate.compareTo(toDate) <= 0; serviceDate = serviceDate.next()) {
        boolean isActive = summary.allServiceDates.contains(serviceDate);
        Calendar serviceDateAsCalendar = serviceDate.getAsCalendar(tz);
        if (useDateRange) {
            int dayOfWeek = serviceDateAsCalendar.get(Calendar.DAY_OF_WEEK);
            boolean dateRangeIncludesServiceDate = daysOfTheWeekToUse.contains(dayOfWeek);
            if (isActive && !dateRangeIncludesServiceDate) {
                ServiceCalendarDate scd = new ServiceCalendarDate();
                scd.setDate(serviceDate);
                scd.setExceptionType(ServiceCalendarDate.EXCEPTION_TYPE_ADD);
                scd.setServiceId(updatedServiceId);
                newEntities.add(scd);
            }
            if (!isActive && dateRangeIncludesServiceDate) {
                ServiceCalendarDate scd = new ServiceCalendarDate();
                scd.setDate(serviceDate);
                scd.setExceptionType(ServiceCalendarDate.EXCEPTION_TYPE_REMOVE);
                scd.setServiceId(updatedServiceId);
                newEntities.add(scd);
            }
        } else {
            if (isActive) {
                ServiceCalendarDate scd = new ServiceCalendarDate();
                scd.setDate(serviceDate);
                scd.setExceptionType(ServiceCalendarDate.EXCEPTION_TYPE_ADD);
                scd.setServiceId(updatedServiceId);
                newEntities.add(scd);
            }
        }
    }
}
Also used : ServiceCalendarDate(org.onebusaway.gtfs.model.ServiceCalendarDate) ServiceDate(org.onebusaway.gtfs.model.calendar.ServiceDate) TimeZone(java.util.TimeZone) ServiceCalendar(org.onebusaway.gtfs.model.ServiceCalendar) Calendar(java.util.Calendar) ServiceCalendar(org.onebusaway.gtfs.model.ServiceCalendar)

Aggregations

ServiceCalendarDate (org.onebusaway.gtfs.model.ServiceCalendarDate)23 ServiceCalendar (org.onebusaway.gtfs.model.ServiceCalendar)18 AgencyAndId (org.onebusaway.gtfs.model.AgencyAndId)15 ServiceDate (org.onebusaway.gtfs.model.calendar.ServiceDate)15 Test (org.junit.Test)13 Trip (org.onebusaway.gtfs.model.Trip)10 Agency (org.onebusaway.gtfs.model.Agency)8 Route (org.onebusaway.gtfs.model.Route)5 ShapePoint (org.onebusaway.gtfs.model.ShapePoint)5 Stop (org.onebusaway.gtfs.model.Stop)5 GtfsRelationalDao (org.onebusaway.gtfs.services.GtfsRelationalDao)5 FareAttribute (org.onebusaway.gtfs.model.FareAttribute)4 StopTime (org.onebusaway.gtfs.model.StopTime)4 Calendar (java.util.Calendar)3 GtfsRelationalDaoImpl (org.onebusaway.gtfs.impl.GtfsRelationalDaoImpl)3 FareRule (org.onebusaway.gtfs.model.FareRule)3 Frequency (org.onebusaway.gtfs.model.Frequency)3 CalendarService (org.onebusaway.gtfs.services.calendar.CalendarService)3 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2