Search in sources :

Example 1 with Counter

use of org.onebusaway.collections.Counter in project onebusaway-application-modules by camsys.

the class StopWithArrivalsAndDeparturesBeanServiceImpl method getArrivalsAndDeparturesForStopIds.

public StopsWithArrivalsAndDeparturesBean getArrivalsAndDeparturesForStopIds(Set<AgencyAndId> ids, ArrivalsAndDeparturesQueryBean query) throws NoSuchStopServiceException {
    List<StopBean> stops = new ArrayList<StopBean>();
    List<ArrivalAndDepartureBean> allArrivalsAndDepartures = new ArrayList<ArrivalAndDepartureBean>();
    Set<AgencyAndId> allNearbyStopIds = new HashSet<AgencyAndId>();
    Map<String, ServiceAlertBean> situationsById = new HashMap<String, ServiceAlertBean>();
    Counter<TimeZone> timeZones = new Counter<TimeZone>();
    for (AgencyAndId id : ids) {
        StopBean stopBean = _stopBeanService.getStopForId(id);
        stops.add(stopBean);
        List<ArrivalAndDepartureBean> arrivalsAndDepartures = _arrivalsAndDeparturesBeanService.getArrivalsAndDeparturesByStopId(id, query);
        allArrivalsAndDepartures.addAll(arrivalsAndDepartures);
        List<AgencyAndId> nearbyStopIds = _nearbyStopsBeanService.getNearbyStops(stopBean, 100);
        allNearbyStopIds.addAll(nearbyStopIds);
        TimeZone timeZone = _agencyService.getTimeZoneForAgencyId(id.getAgencyId());
        timeZones.increment(timeZone);
        List<ServiceAlertBean> situations = _serviceAlertsBeanService.getServiceAlertsForStopId(query.getTime(), id);
        for (ServiceAlertBean situation : situations) situationsById.put(situation.getId(), situation);
    }
    allNearbyStopIds.removeAll(ids);
    List<StopBean> nearbyStops = new ArrayList<StopBean>();
    for (AgencyAndId id : allNearbyStopIds) {
        StopBean stop = _stopBeanService.getStopForId(id);
        nearbyStops.add(stop);
    }
    TimeZone timeZone = timeZones.getMax();
    if (timeZone == null)
        timeZone = TimeZone.getDefault();
    StopsWithArrivalsAndDeparturesBean result = new StopsWithArrivalsAndDeparturesBean();
    result.setStops(stops);
    result.setArrivalsAndDepartures(allArrivalsAndDepartures);
    result.setNearbyStops(nearbyStops);
    result.setSituations(new ArrayList<ServiceAlertBean>(situationsById.values()));
    result.setTimeZone(timeZone.getID());
    return result;
}
Also used : AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) StopsWithArrivalsAndDeparturesBean(org.onebusaway.transit_data.model.StopsWithArrivalsAndDeparturesBean) ArrivalAndDepartureBean(org.onebusaway.transit_data.model.ArrivalAndDepartureBean) TimeZone(java.util.TimeZone) Counter(org.onebusaway.collections.Counter) StopBean(org.onebusaway.transit_data.model.StopBean) ServiceAlertBean(org.onebusaway.transit_data.model.service_alerts.ServiceAlertBean) HashSet(java.util.HashSet)

Example 2 with Counter

use of org.onebusaway.collections.Counter in project onebusaway-application-modules by camsys.

the class StopSequenceCollectionServiceImpl method constructCollections.

/**
 * @param route
 * @param sequenceStats
 * @param sequencesByGroupId
 * @return
 */
private List<StopSequenceCollection> constructCollections(Map<StopSequence, PatternStats> sequenceStats, Map<String, List<StopSequence>> sequencesByGroupId) {
    computeContinuations(sequenceStats, sequencesByGroupId);
    Set<String> allNames = new HashSet<String>();
    Map<String, String> directionToName = new HashMap<String, String>();
    Map<String, Segment> segments = new HashMap<String, Segment>();
    for (Map.Entry<String, List<StopSequence>> entry : sequencesByGroupId.entrySet()) {
        String direction = entry.getKey();
        List<StopSequence> sequences = entry.getValue();
        Max<StopSequence> maxTripCount = new Max<StopSequence>();
        Counter<String> names = new Counter<String>();
        for (StopSequence sequence : sequences) {
            maxTripCount.add(sequence.getTripCount(), sequence);
            for (BlockTripEntry blockTrip : sequence.getTrips()) {
                TripEntry trip = blockTrip.getTrip();
                TripNarrative tripNarrative = _narrativeService.getTripForId(trip.getId());
                String headsign = tripNarrative.getTripHeadsign();
                if (headsign != null && headsign.length() > 0)
                    names.increment(headsign);
            }
        }
        String dName = names.getMax();
        RecursiveStats rs = new RecursiveStats();
        rs.maxTripCount = (long) maxTripCount.getMaxValue();
        exploreStopSequences(rs, sequenceStats, sequences, "");
        allNames.add(dName);
        directionToName.put(direction, dName);
        segments.put(direction, rs.longestSegment.getMaxElement());
    }
    if (allNames.size() < directionToName.size()) {
        for (Map.Entry<String, String> entry : directionToName.entrySet()) {
            String direction = entry.getKey();
            String name = entry.getValue();
            direction = direction.charAt(0) + direction.substring(1).toLowerCase();
            entry.setValue(name + " - " + direction);
        }
    }
    List<StopSequenceCollection> blocks = new ArrayList<StopSequenceCollection>();
    for (Map.Entry<String, String> entry : directionToName.entrySet()) {
        String direction = entry.getKey();
        String name = entry.getValue();
        List<StopSequence> patterns = sequencesByGroupId.get(direction);
        Segment segment = segments.get(direction);
        // System.out.println("  " + direction + " => " + name);
        StopSequenceCollection block = new StopSequenceCollection();
        if (segment.fromLat == 0.0)
            throw new IllegalStateException("what?");
        StopSequenceCollectionKey key = new StopSequenceCollectionKey(null, direction);
        block.setId(key);
        block.setPublicId(direction);
        block.setDescription(name);
        block.setStopSequences(patterns);
        block.setStartLat(segment.fromLat);
        block.setStartLon(segment.fromLon);
        block.setEndLat(segment.toLat);
        block.setEndLon(segment.toLon);
        blocks.add(block);
    }
    return blocks;
}
Also used : HashMap(java.util.HashMap) Max(org.onebusaway.collections.Max) BlockTripEntry(org.onebusaway.transit_data_federation.services.transit_graph.BlockTripEntry) ArrayList(java.util.ArrayList) StopSequence(org.onebusaway.transit_data_federation.model.StopSequence) Counter(org.onebusaway.collections.Counter) ArrayList(java.util.ArrayList) List(java.util.List) StopSequenceCollection(org.onebusaway.transit_data_federation.model.StopSequenceCollection) HashSet(java.util.HashSet) TripEntry(org.onebusaway.transit_data_federation.services.transit_graph.TripEntry) BlockTripEntry(org.onebusaway.transit_data_federation.services.transit_graph.BlockTripEntry) StopSequenceCollectionKey(org.onebusaway.transit_data_federation.model.StopSequenceCollectionKey) HashMap(java.util.HashMap) Map(java.util.Map) FactoryMap(org.onebusaway.collections.FactoryMap) TripNarrative(org.onebusaway.transit_data_federation.model.narrative.TripNarrative)

Example 3 with Counter

use of org.onebusaway.collections.Counter in project onebusaway-application-modules by camsys.

the class GenerateNarrativesTask method generateRouteNarratives.

public void generateRouteNarratives(NarrativeProviderImpl provider) {
    for (RouteCollectionEntry routeCollectionEntry : _transitGraphDao.getAllRouteCollections()) {
        List<Route> routes = new ArrayList<Route>();
        Counter<Route> tripCounts = new Counter<Route>();
        for (RouteEntry routeEntry : routeCollectionEntry.getChildren()) {
            Route route = _gtfsDao.getRouteForId(routeEntry.getId());
            routes.add(route);
            int tripCount = routeEntry.getTrips().size();
            tripCounts.increment(route, tripCount);
        }
        RouteCollectionNarrative.Builder builder = RouteCollectionNarrative.builder();
        setPropertiesOfRouteCollectionFromRoutes(routes, tripCounts, builder);
        provider.setNarrativeForRouteCollectionId(routeCollectionEntry.getId(), builder.create());
    }
}
Also used : RouteEntry(org.onebusaway.transit_data_federation.services.transit_graph.RouteEntry) Counter(org.onebusaway.collections.Counter) DoubleArrayList(cern.colt.list.DoubleArrayList) ArrayList(java.util.ArrayList) RouteCollectionEntry(org.onebusaway.transit_data_federation.services.transit_graph.RouteCollectionEntry) Route(org.onebusaway.gtfs.model.Route) ProjectedPoint(org.onebusaway.transit_data_federation.model.ProjectedPoint) RouteCollectionNarrative(org.onebusaway.transit_data_federation.model.narrative.RouteCollectionNarrative)

Example 4 with Counter

use of org.onebusaway.collections.Counter in project onebusaway-application-modules by camsys.

the class GenerateNarrativesTask method setPropertiesOfRouteCollectionFromRoutes.

/**
 **
 * Private Methods
 ***
 */
private void setPropertiesOfRouteCollectionFromRoutes(List<Route> routes, Counter<Route> tripCounts, RouteCollectionNarrative.Builder target) {
    Counter<String> shortNames = new Counter<String>();
    Counter<String> longNames = new Counter<String>();
    Counter<String> descriptions = new Counter<String>();
    Counter<String> colors = new Counter<String>();
    Counter<String> textColors = new Counter<String>();
    Counter<String> urls = new Counter<String>();
    Counter<Integer> types = new Counter<Integer>();
    for (Route route : routes) {
        int count = tripCounts.getCount(route);
        addValueToCounterIfValid(route.getShortName(), shortNames, count);
        addValueToCounterIfValid(route.getLongName(), longNames, count);
        addValueToCounterIfValid(route.getDesc(), descriptions, count);
        addValueToCounterIfValid(route.getColor(), colors, count);
        addValueToCounterIfValid(route.getTextColor(), textColors, count);
        addValueToCounterIfValid(route.getUrl(), urls, count);
        types.increment(route.getType(), count);
    }
    if (shortNames.size() > 0)
        target.setShortName(deduplicate(shortNames.getMax()));
    if (longNames.size() > 0)
        target.setLongName(deduplicate(longNames.getMax()));
    if (descriptions.size() > 0)
        target.setDescription(deduplicate(descriptions.getMax()));
    if (colors.size() > 0)
        target.setColor(deduplicate(colors.getMax()));
    if (textColors.size() > 0)
        target.setTextColor(deduplicate(textColors.getMax()));
    if (urls.size() > 0)
        target.setUrl(deduplicate(urls.getMax()));
    target.setType(deduplicate(types.getMax()));
}
Also used : Counter(org.onebusaway.collections.Counter) Route(org.onebusaway.gtfs.model.Route) ProjectedPoint(org.onebusaway.transit_data_federation.model.ProjectedPoint)

Example 5 with Counter

use of org.onebusaway.collections.Counter in project onebusaway-application-modules by camsys.

the class StopScheduleBeanServiceImpl method getCalendarForStop.

@Cacheable
public StopCalendarDaysBean getCalendarForStop(AgencyAndId stopId) {
    TimeZone timeZone = _agencyService.getTimeZoneForAgencyId(stopId.getAgencyId());
    StopEntry stopEntry = _graph.getStopEntryForId(stopId);
    Set<ServiceIdActivation> serviceIds = new HashSet<ServiceIdActivation>();
    for (BlockStopTimeIndex index : _blockIndexService.getStopTimeIndicesForStop(stopEntry)) serviceIds.add(index.getServiceIds());
    for (FrequencyBlockStopTimeIndex index : _blockIndexService.getFrequencyStopTimeIndicesForStop(stopEntry)) serviceIds.add(index.getServiceIds());
    SortedMap<ServiceDate, Set<ServiceIdActivation>> serviceIdsByDate = getServiceIdsByDate(serviceIds);
    Counter<Set<ServiceIdActivation>> counts = new Counter<Set<ServiceIdActivation>>();
    for (Set<ServiceIdActivation> ids : serviceIdsByDate.values()) counts.increment(ids);
    int total = counts.size();
    Map<Set<ServiceIdActivation>, Integer> idsToGroup = new HashMap<Set<ServiceIdActivation>, Integer>();
    for (Set<ServiceIdActivation> ids : counts.getSortedKeys()) idsToGroup.put(ids, total--);
    List<StopCalendarDayBean> beans = new ArrayList<StopCalendarDayBean>(serviceIdsByDate.size());
    for (Map.Entry<ServiceDate, Set<ServiceIdActivation>> entry : serviceIdsByDate.entrySet()) {
        StopCalendarDayBean bean = new StopCalendarDayBean();
        ServiceDate serviceDate = entry.getKey();
        Date date = serviceDate.getAsDate(timeZone);
        bean.setDate(date);
        Integer indexId = idsToGroup.get(entry.getValue());
        bean.setGroup(indexId);
        beans.add(bean);
    }
    return new StopCalendarDaysBean(timeZone.getID(), beans);
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) StopCalendarDaysBean(org.onebusaway.transit_data.model.StopCalendarDaysBean) HashMap(java.util.HashMap) FrequencyBlockStopTimeIndex(org.onebusaway.transit_data_federation.services.blocks.FrequencyBlockStopTimeIndex) ArrayList(java.util.ArrayList) Date(java.util.Date) ServiceDate(org.onebusaway.gtfs.model.calendar.ServiceDate) StopCalendarDayBean(org.onebusaway.transit_data.model.StopCalendarDayBean) TimeZone(java.util.TimeZone) ServiceDate(org.onebusaway.gtfs.model.calendar.ServiceDate) Counter(org.onebusaway.collections.Counter) FrequencyBlockStopTimeIndex(org.onebusaway.transit_data_federation.services.blocks.FrequencyBlockStopTimeIndex) BlockStopTimeIndex(org.onebusaway.transit_data_federation.services.blocks.BlockStopTimeIndex) StopEntry(org.onebusaway.transit_data_federation.services.transit_graph.StopEntry) ServiceIdActivation(org.onebusaway.transit_data_federation.services.transit_graph.ServiceIdActivation) Map(java.util.Map) FactoryMap(org.onebusaway.collections.FactoryMap) SortedMap(java.util.SortedMap) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) HashSet(java.util.HashSet) Cacheable(org.onebusaway.container.cache.Cacheable)

Aggregations

Counter (org.onebusaway.collections.Counter)6 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)3 HashSet (java.util.HashSet)3 Map (java.util.Map)2 TimeZone (java.util.TimeZone)2 FactoryMap (org.onebusaway.collections.FactoryMap)2 Route (org.onebusaway.gtfs.model.Route)2 ServiceDate (org.onebusaway.gtfs.model.calendar.ServiceDate)2 ProjectedPoint (org.onebusaway.transit_data_federation.model.ProjectedPoint)2 DoubleArrayList (cern.colt.list.DoubleArrayList)1 Calendar (java.util.Calendar)1 Date (java.util.Date)1 List (java.util.List)1 Set (java.util.Set)1 SortedMap (java.util.SortedMap)1 TreeMap (java.util.TreeMap)1 Max (org.onebusaway.collections.Max)1 Cacheable (org.onebusaway.container.cache.Cacheable)1 AgencyAndId (org.onebusaway.gtfs.model.AgencyAndId)1