Search in sources :

Example 46 with GtfsMutableRelationalDao

use of org.onebusaway.gtfs.services.GtfsMutableRelationalDao in project onebusaway-gtfs-modules by OneBusAway.

the class MergeStopIdsFromControlStrategy method run.

@Override
public void run(TransformContext context, GtfsMutableRelationalDao dao) {
    GtfsMutableRelationalDao reference = (GtfsMutableRelationalDao) context.getReferenceReader().getEntityStore();
    List<String> controlLines = new InputLibrary().readList((String) context.getParameter("controlFile"));
    int matched = 0;
    int unmatched = 0;
    for (String controlLine : controlLines) {
        String[] controlArray = controlLine.split(",");
        if (controlArray == null || controlArray.length < 2) {
            _log.info("bad control line {}", controlLine);
            continue;
        }
        String gtfsId = controlArray[LOCATION_NAME_INDEX];
        String atisId = controlArray[ATIS_ID_INDEX];
        Stop gtfsStop = reference.getStopForId(new AgencyAndId(getReferenceAgencyId(reference), gtfsId));
        if (gtfsStop == null) {
            _log.info("missing reference stop {} for agency {}", gtfsId, getReferenceAgencyId(reference));
            unmatched++;
            continue;
        }
        Stop atisStop = dao.getStopForId(new AgencyAndId(getDaoAgencyId(dao), atisId));
        if (atisStop == null) {
            _log.info("missing atis stop {} for agency {}", atisId, getDaoAgencyId(dao));
            unmatched++;
            continue;
        }
        matched++;
        atisStop.setName(gtfsStop.getName());
        atisStop.setDirection(gtfsStop.getDirection());
        atisStop.setId(gtfsStop.getId());
    }
    _log.info("Complete with {} matched and {} unmatched", matched, unmatched);
}
Also used : GtfsMutableRelationalDao(org.onebusaway.gtfs.services.GtfsMutableRelationalDao) AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) Stop(org.onebusaway.gtfs.model.Stop)

Example 47 with GtfsMutableRelationalDao

use of org.onebusaway.gtfs.services.GtfsMutableRelationalDao in project onebusaway-gtfs-modules by OneBusAway.

the class CountAndTestSubway method run.

@Override
public void run(TransformContext context, GtfsMutableRelationalDao dao) {
    GtfsMutableRelationalDao reference = (GtfsMutableRelationalDao) context.getReferenceReader().getEntityStore();
    String agency = dao.getAllAgencies().iterator().next().getId();
    String name = dao.getAllAgencies().iterator().next().getName();
    HashMap<String, Route> referenceRoutes = new HashMap<>();
    for (Route route : reference.getAllRoutes()) {
        referenceRoutes.put(route.getId().getId(), route);
    }
    HashMap<String, Trip> referenceTrips = new HashMap<>();
    for (Trip trip : reference.getAllTrips()) {
        referenceTrips.put(trip.getId().getId(), trip);
    }
    HashMap<String, Stop> referenceStops = new HashMap<>();
    for (Stop stop : reference.getAllStops()) {
        referenceStops.put(stop.getId().getId(), stop);
    }
    int matches = 0;
    for (Route route : dao.getAllRoutes()) {
        if (referenceRoutes.containsKey(route.getId().getId())) {
            matches++;
        } else {
            _log.info("ATIS route {} doesn't have match in reference", route.getId().getId());
        }
    }
    _log.info("ATIS Routes: {}, References: {}, ATIS match to reference: {}", dao.getAllRoutes().size(), reference.getAllRoutes().size(), matches);
    int countSt = 0;
    int countCd = 0;
    int countNoSt = 0;
    int countNoCd = 0;
    int countNoHs = 0;
    int curSerTrips = 0;
    AgencyAndId serviceAgencyAndId = new AgencyAndId();
    matches = 0;
    for (Trip trip : dao.getAllTrips()) {
        if (referenceTrips.containsKey(trip.getId().getId())) {
            matches++;
        }
        if (dao.getStopTimesForTrip(trip).size() == 0) {
            countNoSt++;
        } else {
            countSt++;
        }
        serviceAgencyAndId = trip.getServiceId();
        if (dao.getCalendarDatesForServiceId(serviceAgencyAndId).size() == 0) {
            countNoCd++;
        } else {
            countCd++;
        }
        if (trip.getTripHeadsign() == null) {
            countNoHs++;
        }
        // check for current service
        for (ServiceCalendarDate calDate : dao.getCalendarDatesForServiceId(trip.getServiceId())) {
            Date date = constructDate(calDate.getDate());
            Date today = removeTime(new Date());
            if (calDate.getExceptionType() == 1 && date.equals(today)) {
                curSerTrips++;
                break;
            }
        }
    }
    _log.info("ATIS Trips: {}, Reference: {}, match: {}, Current Service: {}", dao.getAllTrips().size(), reference.getAllTrips().size(), matches, curSerTrips);
    _log.info("Total stop times {}, Trips w/ st: {}, Trips w/out st: {}", dao.getAllStopTimes().size(), countSt, countNoSt);
    _log.info("Total calendar dates {}, Trips w/cd {}, Trips w/out cd: {}", dao.getAllCalendarDates().size(), countCd, countNoCd);
    _log.info("Total trips w/out headsign: {}", countNoHs);
    matches = 0;
    for (Stop stop : dao.getAllStops()) {
        if (referenceStops.containsKey(stop.getId().getId())) {
            matches++;
        }
    }
    _log.info("ATIS Stops: {}, Reference: {}, ATIS match to reference: {}", dao.getAllStops().size(), reference.getAllStops().size(), matches);
    ExternalServices es = new ExternalServicesBridgeFactory().getExternalServices();
    String feed = CloudContextService.getLikelyFeedName(dao);
    es.publishMetric(CloudContextService.getNamespace(), "SubwayTripsInServiceToday", "feed", feed, curSerTrips);
    if (countNoHs > 0) {
        _log.error("There are trips with no headsign");
    }
    es.publishMetric(CloudContextService.getNamespace(), "TripsWithoutHeadsigns", "feed", feed, countNoHs);
    HashSet<String> ids = new HashSet<String>();
    for (Stop stop : dao.getAllStops()) {
        // check for duplicate stop ids.
        if (ids.contains(stop.getId().getId())) {
            _log.error("Duplicate stop ids! Agency {} stop id {}", agency, stop.getId().getId());
            es.publishMultiDimensionalMetric(CloudContextService.getNamespace(), "DuplicateStopIds", new String[] { "feed", "stopId" }, new String[] { feed, stop.getId().toString() }, 1);
            throw new IllegalStateException("There are duplicate stop ids!");
        } else {
            ids.add(stop.getId().getId());
        }
    }
}
Also used : GtfsMutableRelationalDao(org.onebusaway.gtfs.services.GtfsMutableRelationalDao) ExternalServicesBridgeFactory(org.onebusaway.cloud.api.ExternalServicesBridgeFactory) HashMap(java.util.HashMap) ServiceDate(org.onebusaway.gtfs.model.calendar.ServiceDate) Date(java.util.Date) ExternalServices(org.onebusaway.cloud.api.ExternalServices) HashSet(java.util.HashSet)

Example 48 with GtfsMutableRelationalDao

use of org.onebusaway.gtfs.services.GtfsMutableRelationalDao in project onebusaway-gtfs-modules by OneBusAway.

the class MergeRouteAndRemoveShuttles method run.

@Override
public void run(TransformContext context, GtfsMutableRelationalDao dao) {
    GtfsMutableRelationalDao reference = (GtfsMutableRelationalDao) context.getReferenceReader().getEntityStore();
    RemoveEntityLibrary removeEntityLibrary = new RemoveEntityLibrary();
    HashMap<String, Route> referenceRoutes = new HashMap<>();
    for (Route route : reference.getAllRoutes()) {
        referenceRoutes.put(route.getId().getId(), route);
    }
    ArrayList<Route> routesToRemove = new ArrayList<Route>();
    for (Route route : dao.getAllRoutes()) {
        String identifier = route.getId().getId();
        // remove the shuttle routes.  They all appear to be -SS
        if (identifier.contains("-SS")) {
            _log.info("Removing route: " + identifier);
            routesToRemove.add(route);
        } else if (identifier.length() > 2) {
            identifier = identifier.substring(0, 2);
        }
        Route refRoute = referenceRoutes.get(identifier);
        if (refRoute != null) {
            route.setShortName(refRoute.getShortName());
            route.setLongName(refRoute.getLongName());
            route.setType(refRoute.getType());
            route.setDesc(refRoute.getDesc());
            route.setUrl(refRoute.getUrl());
            route.setColor(refRoute.getColor());
            route.setTextColor(refRoute.getTextColor());
            route.setId(refRoute.getId());
        }
    }
    for (Route route : routesToRemove) {
        removeEntityLibrary.removeRoute(dao, route);
    }
}
Also used : GtfsMutableRelationalDao(org.onebusaway.gtfs.services.GtfsMutableRelationalDao) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Route(org.onebusaway.gtfs.model.Route)

Example 49 with GtfsMutableRelationalDao

use of org.onebusaway.gtfs.services.GtfsMutableRelationalDao in project onebusaway-gtfs-modules by OneBusAway.

the class MergeStopIdsFromReferenceStrategy method run.

@Override
public void run(TransformContext context, GtfsMutableRelationalDao dao) {
    GtfsMutableRelationalDao reference = (GtfsMutableRelationalDao) context.getReferenceReader().getEntityStore();
    List<String> ignoreMatches = readList((String) context.getParameter("ignoreStops"));
    int matched = 0;
    int unmatched = 0;
    HashMap<String, Stop> referenceStopNamesToStops = new HashMap<>();
    for (Stop stop : reference.getAllStops()) {
        referenceStopNamesToStops.put(stop.getName(), stop);
    }
    for (Stop stop : dao.getAllStops()) {
        Stop referenceStop = referenceStopNamesToStops.get(stop.getName());
        if (referenceStop == null && stop.getName().contains("-")) {
            referenceStop = referenceStopNamesToStops.get(swap(stop.getName()));
        }
        // If Park, try Pk
        if (referenceStop == null && stop.getName().contains("Park")) {
            referenceStop = referenceStopNamesToStops.get(stop.getName().replaceAll("Park", "Pk"));
        }
        // try Pk and swap
        if (referenceStop == null && stop.getName().contains("-")) {
            referenceStop = referenceStopNamesToStops.get(swap(stop.getName().replaceAll("Park", "Pk")));
        }
        // If Pk, try Park
        if (referenceStop == null && stop.getName().contains("Pk")) {
            referenceStop = referenceStopNamesToStops.get(stop.getName().replaceAll("Pk", "Park"));
        }
        // try Pk and swap
        if (referenceStop == null && stop.getName().contains("-")) {
            referenceStop = referenceStopNamesToStops.get(swap(stop.getName().replaceAll("Pk", "Park")));
        }
        if (referenceStop != null) {
            stop.setId(referenceStop.getId());
            matched++;
        } else {
            unmatched++;
            if (!ignoreMatches.contains(stop.getName())) {
                _log.error("unmatched stop |{}|", stop.getName());
            }
        }
    }
    _log.info("Stops replaced with {} matched and {} remaining", matched, unmatched);
}
Also used : GtfsMutableRelationalDao(org.onebusaway.gtfs.services.GtfsMutableRelationalDao) Stop(org.onebusaway.gtfs.model.Stop) HashMap(java.util.HashMap)

Example 50 with GtfsMutableRelationalDao

use of org.onebusaway.gtfs.services.GtfsMutableRelationalDao in project onebusaway-gtfs-modules by OneBusAway.

the class UpdateTripHeadsignExcludeNonreference method run.

@Override
public void run(TransformContext context, GtfsMutableRelationalDao dao) {
    GtfsMutableRelationalDao reference = (GtfsMutableRelationalDao) context.getReferenceReader().getEntityStore();
    int update = 0;
    int fallback = 0;
    int noChange = 0;
    int shuttle = 0;
    for (Trip trip : dao.getAllTrips()) {
        List<StopTime> stopTimes = dao.getStopTimesForTrip(trip);
        if (stopTimes != null && stopTimes.size() > 0) {
            StopLocation lastStop = stopTimes.get(stopTimes.size() - 1).getStop();
            Stop referenceStop = reference.getStopForId(lastStop.getId());
            if (trip.getTripHeadsign() == null || referenceStop != null) {
                String tripHeadSign = stopTimes.get(stopTimes.size() - 1).getStop().getName();
                if (tripHeadSign != null) {
                    trip.setTripHeadsign(tripHeadSign);
                    update++;
                } else {
                    fallbackSetHeadsign(trip);
                    fallback++;
                }
            } else {
                noChange++;
                if (trip.getTripHeadsign().contains("SHUTTLE")) {
                    shuttle++;
                }
            }
        } else {
            fallbackSetHeadsign(trip);
            fallback++;
        }
    }
    _log.info("trip headsign update:{} fallback: {} no change: {} shuttle: {}", update, fallback, noChange, shuttle);
}
Also used : GtfsMutableRelationalDao(org.onebusaway.gtfs.services.GtfsMutableRelationalDao) Trip(org.onebusaway.gtfs.model.Trip) Stop(org.onebusaway.gtfs.model.Stop) StopLocation(org.onebusaway.gtfs.model.StopLocation) StopTime(org.onebusaway.gtfs.model.StopTime)

Aggregations

GtfsMutableRelationalDao (org.onebusaway.gtfs.services.GtfsMutableRelationalDao)57 AgencyAndId (org.onebusaway.gtfs.model.AgencyAndId)25 Test (org.junit.Test)24 Trip (org.onebusaway.gtfs.model.Trip)22 ServiceDate (org.onebusaway.gtfs.model.calendar.ServiceDate)14 Stop (org.onebusaway.gtfs.model.Stop)11 StopTime (org.onebusaway.gtfs.model.StopTime)11 TransformContext (org.onebusaway.gtfs_transformer.services.TransformContext)11 HashMap (java.util.HashMap)10 ArrayList (java.util.ArrayList)9 CalendarService (org.onebusaway.gtfs.services.calendar.CalendarService)9 ExternalServices (org.onebusaway.cloud.api.ExternalServices)8 ExternalServicesBridgeFactory (org.onebusaway.cloud.api.ExternalServicesBridgeFactory)8 GtfsRelationalDaoImpl (org.onebusaway.gtfs.impl.GtfsRelationalDaoImpl)7 GtfsRelationalDao (org.onebusaway.gtfs.services.GtfsRelationalDao)7 GtfsTransformStrategy (org.onebusaway.gtfs_transformer.services.GtfsTransformStrategy)7 Route (org.onebusaway.gtfs.model.Route)6 ServiceCalendar (org.onebusaway.gtfs.model.ServiceCalendar)6 GtfsReader (org.onebusaway.gtfs.serialization.GtfsReader)5 HashSet (java.util.HashSet)4