Search in sources :

Example 66 with Trip

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

the class TransformFactoryTest method testReplaceIdInUpdateComplex.

@Test
public void testReplaceIdInUpdateComplex() throws IOException, TransformSpecificationException {
    _factory.addModificationsFromString("{'op':'update', " + "'match':{'file':'trips.txt'}, " + "'update':{'trip_id': 's/^([^_]*)_([0-9]*).*/$2/'}}");
    GtfsTransformStrategy transform = _transformer.getLastTransform();
    TransformContext context = new TransformContext();
    context.setDefaultAgencyId("2");
    GtfsMutableRelationalDao dao = new GtfsRelationalDaoImpl();
    Trip trip = new Trip();
    trip.setId(new AgencyAndId("2", "1234-this-text-to-remove"));
    dao.saveEntity(trip);
    transform.run(context, dao);
    assertEquals("1234", trip.getId().getId());
}
Also used : GtfsMutableRelationalDao(org.onebusaway.gtfs.services.GtfsMutableRelationalDao) Trip(org.onebusaway.gtfs.model.Trip) AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) TransformContext(org.onebusaway.gtfs_transformer.services.TransformContext) GtfsTransformStrategy(org.onebusaway.gtfs_transformer.services.GtfsTransformStrategy) GtfsRelationalDaoImpl(org.onebusaway.gtfs.impl.GtfsRelationalDaoImpl) Test(org.junit.Test)

Example 67 with Trip

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

the class TransformFactoryTest method testPathInUpdate.

@Test
public void testPathInUpdate() throws IOException, TransformSpecificationException {
    _factory.addModificationsFromString("{'op':'update', " + "'match':{'file':'trips.txt'}, " + "'update':{'trip_headsign': 'path(route.longName)'}}");
    GtfsTransformStrategy transform = _transformer.getLastTransform();
    TransformContext context = new TransformContext();
    GtfsMutableRelationalDao dao = new GtfsRelationalDaoImpl();
    Route route = new Route();
    route.setLongName("long cat");
    Trip trip = new Trip();
    trip.setId(new AgencyAndId("1", "1"));
    trip.setRoute(route);
    dao.saveEntity(trip);
    transform.run(context, dao);
    assertEquals("long cat", trip.getTripHeadsign());
}
Also used : GtfsMutableRelationalDao(org.onebusaway.gtfs.services.GtfsMutableRelationalDao) Trip(org.onebusaway.gtfs.model.Trip) AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) TransformContext(org.onebusaway.gtfs_transformer.services.TransformContext) GtfsTransformStrategy(org.onebusaway.gtfs_transformer.services.GtfsTransformStrategy) GtfsRelationalDaoImpl(org.onebusaway.gtfs.impl.GtfsRelationalDaoImpl) Route(org.onebusaway.gtfs.model.Route) Test(org.junit.Test)

Example 68 with Trip

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

the class DeduplicateServiceIdsStrategy method run.

@Override
public void run(TransformContext context, GtfsMutableRelationalDao dao) {
    CalendarService service = CalendarServiceDataFactoryImpl.createService(dao);
    Map<Set<ServiceDate>, List<AgencyAndId>> serviceIdsByServiceDates = new FactoryMap<Set<ServiceDate>, List<AgencyAndId>>(new ArrayList<AgencyAndId>());
    for (AgencyAndId serviceId : dao.getAllServiceIds()) {
        Set<ServiceDate> serviceDates = service.getServiceDatesForServiceId(serviceId);
        serviceIdsByServiceDates.get(serviceDates).add(serviceId);
    }
    Map<AgencyAndId, AgencyAndId> serviceIdMapping = new HashMap<AgencyAndId, AgencyAndId>();
    for (List<AgencyAndId> serviceIds : serviceIdsByServiceDates.values()) {
        Collections.sort(serviceIds);
        if (serviceIds.size() == 1) {
            continue;
        }
        AgencyAndId target = serviceIds.get(0);
        for (int i = 1; i < serviceIds.size(); ++i) {
            AgencyAndId source = serviceIds.get(i);
            serviceIdMapping.put(source, target);
        }
    }
    for (Trip trip : dao.getAllTrips()) {
        AgencyAndId mappedServiceId = serviceIdMapping.get(trip.getServiceId());
        if (mappedServiceId != null) {
            trip.setServiceId(mappedServiceId);
        }
    }
    for (AgencyAndId serviceId : serviceIdMapping.keySet()) {
        ServiceCalendar serviceCalendar = dao.getCalendarForServiceId(serviceId);
        if (serviceCalendar != null) {
            dao.removeEntity(serviceCalendar);
        }
        for (ServiceCalendarDate date : dao.getCalendarDatesForServiceId(serviceId)) {
            dao.removeEntity(date);
        }
    }
    _log.info("removed {} duplicate service ids", serviceIdMapping.size());
    UpdateLibrary.clearDaoCache(dao);
}
Also used : FactoryMap(org.onebusaway.collections.FactoryMap) ServiceCalendarDate(org.onebusaway.gtfs.model.ServiceCalendarDate) Trip(org.onebusaway.gtfs.model.Trip) Set(java.util.Set) AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) HashMap(java.util.HashMap) CalendarService(org.onebusaway.gtfs.services.calendar.CalendarService) ServiceDate(org.onebusaway.gtfs.model.calendar.ServiceDate) ArrayList(java.util.ArrayList) List(java.util.List) ServiceCalendar(org.onebusaway.gtfs.model.ServiceCalendar)

Example 69 with Trip

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

the class DeduplicateTripsStrategy method run.

@Override
public void run(TransformContext context, GtfsMutableRelationalDao dao) {
    Map<String, List<Trip>> tripsByCommonId = new FactoryMap<String, List<Trip>>(new ArrayList<Trip>());
    int total = 0;
    int badIds = 0;
    for (Trip trip : dao.getAllTrips()) {
        AgencyAndId aid = trip.getId();
        String id = aid.getId();
        int index = id.indexOf('_');
        if (index != -1) {
            String commonId = id.substring(index + 1);
            tripsByCommonId.get(commonId).add(trip);
        } else {
            badIds++;
        }
    }
    _log.info("trips=" + total + " badIds=" + badIds);
    int weird = 0;
    int pairs = 0;
    int patternMismatch = 0;
    int propertyMismatch = 0;
    for (List<Trip> trips : tripsByCommonId.values()) {
        if (trips.size() == 1)
            continue;
        if (trips.size() != 2) {
            System.out.println("weird: " + trips);
            weird++;
            continue;
        }
        pairs++;
        Collections.sort(trips, _tripComparator);
        Trip tripA = trips.get(0);
        Trip tripB = trips.get(1);
        List<StopTime> stopTimesA = dao.getStopTimesForTrip(tripA);
        List<StopTime> stopTimesB = dao.getStopTimesForTrip(tripB);
        StopSequencePattern patternA = StopSequencePattern.getPatternForStopTimes(stopTimesA);
        StopSequencePattern patternB = StopSequencePattern.getPatternForStopTimes(stopTimesB);
        if (!patternA.equals(patternB)) {
            System.out.println("  pattern: " + tripA.getId() + " " + tripB.getId());
            patternMismatch++;
            continue;
        }
        String property = areTripsEquivalent(tripA, tripB);
        if (property != null) {
            System.out.println("  property: " + tripA.getId() + " " + tripB.getId() + " " + property);
            propertyMismatch++;
            continue;
        }
    }
    _log.info("weird=" + weird + " pairs=" + pairs + " patternMismatch=" + patternMismatch + " propertyMismatch=" + propertyMismatch);
}
Also used : FactoryMap(org.onebusaway.collections.FactoryMap) Trip(org.onebusaway.gtfs.model.Trip) AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) ArrayList(java.util.ArrayList) List(java.util.List) StopTime(org.onebusaway.gtfs.model.StopTime)

Example 70 with Trip

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

the class DeduplicateRoutesStrategy method run.

@Override
public void run(TransformContext context, GtfsMutableRelationalDao dao) {
    Map<AgencyAndId, List<Route>> routesById = new FactoryMap<AgencyAndId, List<Route>>(new ArrayList<Route>());
    for (Route route : dao.getAllRoutes()) {
        AgencyAndId aid = route.getId();
        String id = aid.getId();
        int index = id.indexOf('_');
        if (index == -1)
            continue;
        String commonId = id.substring(index + 1);
        AgencyAndId commonIdFull = new AgencyAndId(aid.getAgencyId(), commonId);
        routesById.get(commonIdFull).add(route);
    }
    for (Map.Entry<AgencyAndId, List<Route>> entry : routesById.entrySet()) {
        AgencyAndId routeId = entry.getKey();
        List<Route> routes = entry.getValue();
        if (routes.size() == 1)
            continue;
        // Remove the route with the old id
        Route route = routes.get(0);
        dao.removeEntity(route);
        // Add the route with the new id
        route.setId(routeId);
        dao.saveEntity(route);
        for (int i = 1; i < routes.size(); i++) {
            Route duplicateRoute = routes.get(i);
            dao.removeEntity(duplicateRoute);
            List<Trip> trips = dao.getTripsForRoute(duplicateRoute);
            for (Trip trip : trips) trip.setRoute(route);
        }
    }
    UpdateLibrary.clearDaoCache(dao);
}
Also used : FactoryMap(org.onebusaway.collections.FactoryMap) Trip(org.onebusaway.gtfs.model.Trip) AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map) FactoryMap(org.onebusaway.collections.FactoryMap) Route(org.onebusaway.gtfs.model.Route)

Aggregations

Trip (org.onebusaway.gtfs.model.Trip)166 AgencyAndId (org.onebusaway.gtfs.model.AgencyAndId)89 Test (org.junit.Test)56 StopTime (org.onebusaway.gtfs.model.StopTime)52 Route (org.onebusaway.gtfs.model.Route)51 Stop (org.onebusaway.gtfs.model.Stop)40 ArrayList (java.util.ArrayList)34 List (java.util.List)23 GtfsMutableRelationalDao (org.onebusaway.gtfs.services.GtfsMutableRelationalDao)21 TripPattern (org.opentripplanner.routing.edgetype.TripPattern)20 Agency (org.onebusaway.gtfs.model.Agency)19 TransitStop (org.opentripplanner.routing.vertextype.TransitStop)19 HashMap (java.util.HashMap)14 ServiceCalendar (org.onebusaway.gtfs.model.ServiceCalendar)13 ServiceDate (org.onebusaway.gtfs.model.calendar.ServiceDate)13 HashSet (java.util.HashSet)12 Vertex (org.opentripplanner.routing.graph.Vertex)12 FactoryMap (org.onebusaway.collections.FactoryMap)10 ServiceCalendarDate (org.onebusaway.gtfs.model.ServiceCalendarDate)10 ShapePoint (org.onebusaway.gtfs.model.ShapePoint)10