Search in sources :

Example 1 with AnnotatedLineStructure

use of uk.org.siri.siri_2.AnnotatedLineStructure in project onebusaway-application-modules by camsys.

the class LinesRequestV2Action method index.

public DefaultHttpHeaders index() throws IOException {
    long responseTimestamp = getTime();
    processGoogleAnalytics();
    _realtimeService.setTime(responseTimestamp);
    boolean useLineRefOnly = false;
    Boolean upcomingServiceAllStops = null;
    CoordinateBounds bounds = null;
    boolean validBoundDistance = true;
    // User Parameters
    String boundingBox = _request.getParameter(BOUNDING_BOX);
    String circle = _request.getParameter(CIRCLE);
    String lineRef = _request.getParameter(LINE_REF);
    String directionId = _request.getParameter(DIRECTION_REF);
    String agencyId = _request.getParameter(OPERATOR_REF);
    String hasUpcomingScheduledService = _request.getParameter(UPCOMING_SCHEDULED_SERVICE);
    String detailLevelParam = _request.getParameter(LINES_DETAIL_LEVEL);
    String includePolylines = _request.getParameter(INCLUDE_POLYLINES);
    // get the detail level parameter or set it to default if not specified
    DetailLevel detailLevel;
    if (DetailLevel.contains(detailLevelParam)) {
        detailLevel = DetailLevel.valueOf(detailLevelParam.toUpperCase());
    } else {
        detailLevel = DetailLevel.NORMAL;
    }
    // Error Strings
    String routeIdsErrorString = "";
    String boundsErrorString = "";
    /*
     * We need to support the user providing no agency id which means 'all
     * agencies'. So, this array will hold a single agency if the user
     * provides it or all agencies if the user provides none. We'll iterate
     * over them later while querying for vehicles and routes
     */
    List<String> agencyIds = processAgencyIds(agencyId);
    List<AgencyAndId> routeIds = new ArrayList<AgencyAndId>();
    routeIdsErrorString = processRouteIds(lineRef, routeIds, agencyIds);
    // Calculate Bounds
    try {
        if (StringUtils.isNotBlank(circle)) {
            bounds = getCircleBounds(circle);
            if (bounds != null && !isValidBoundsDistance(bounds, MAX_BOUNDS_RADIUS)) {
                boundsErrorString += "Provided values exceed allowed search radius of " + MAX_BOUNDS_RADIUS + "m. ";
                validBoundDistance = false;
            }
        } else if (StringUtils.isNotBlank(boundingBox)) {
            bounds = getBoxBounds(boundingBox);
            if (bounds != null && !isValidBoundBoxDistance(bounds, MAX_BOUNDS_DISTANCE)) {
                boundsErrorString += "Provided values exceed allowed search distance of " + MAX_BOUNDS_DISTANCE + "m. ";
                validBoundDistance = false;
            }
        }
    } catch (NumberFormatException nfe) {
        boundsErrorString += ERROR_NON_NUMERIC;
    }
    // Check for case where only LineRef was provided
    if (routeIds.size() > 0) {
        useLineRefOnly = true;
    } else if (bounds == null) {
        boundsErrorString += ERROR_REQUIRED_PARAMS;
    }
    // Setup Filters
    Map<Filters, String> filters = new HashMap<Filters, String>();
    filters.put(Filters.DIRECTION_REF, directionId);
    filters.put(Filters.LINE_REF, lineRef);
    filters.put(Filters.INCLUDE_POLYLINES, includePolylines);
    filters.put(Filters.UPCOMING_SCHEDULED_SERVICE, hasUpcomingScheduledService);
    // Annotated Lines
    List<AnnotatedLineStructure> lines = new ArrayList<AnnotatedLineStructure>();
    Map<Boolean, List<AnnotatedLineStructure>> linesMap;
    // Error Handler
    Exception error = null;
    if ((bounds == null && !useLineRefOnly) || (lineRef != null && routeIds.size() == 0) || !validBoundDistance) {
        String errorString = (boundsErrorString + " " + routeIdsErrorString).trim();
        error = new Exception(errorString);
    } else {
        if (useLineRefOnly) {
            linesMap = _realtimeService.getAnnotatedLineStructures(agencyIds, routeIds, detailLevel, responseTimestamp, filters);
        } else {
            linesMap = _realtimeService.getAnnotatedLineStructures(agencyIds, bounds, detailLevel, responseTimestamp, filters);
        }
        for (Map.Entry<Boolean, List<AnnotatedLineStructure>> entry : linesMap.entrySet()) {
            upcomingServiceAllStops = entry.getKey();
            lines.addAll(entry.getValue());
        }
    }
    _response = generateSiriResponse(lines, upcomingServiceAllStops, error, responseTimestamp);
    try {
        this._servletResponse.getWriter().write(getLines());
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}
Also used : AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) AnnotatedLineStructure(uk.org.siri.siri_2.AnnotatedLineStructure) IOException(java.io.IOException) IOException(java.io.IOException) Filters(org.onebusaway.api.actions.siri.impl.SiriSupportV2.Filters) DetailLevel(org.onebusaway.api.actions.siri.model.DetailLevel) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) CoordinateBounds(org.onebusaway.geospatial.model.CoordinateBounds)

Example 2 with AnnotatedLineStructure

use of uk.org.siri.siri_2.AnnotatedLineStructure in project onebusaway-application-modules by camsys.

the class LinesRequestV2Action method generateSiriResponse.

private Siri generateSiriResponse(List<AnnotatedLineStructure> lines, Boolean hasUpcomingScheduledService, Exception error, long responseTimestamp) {
    LinesDeliveryStructure linesDelivery = new LinesDeliveryStructure();
    linesDelivery.setResponseTimestamp(DateUtil.toXmlGregorianCalendar(responseTimestamp));
    if (error != null) {
        ServiceDeliveryErrorConditionStructure errorConditionStructure = new ServiceDeliveryErrorConditionStructure();
        ErrorDescriptionStructure errorDescriptionStructure = new ErrorDescriptionStructure();
        errorDescriptionStructure.setValue(error.getMessage());
        OtherErrorStructure otherErrorStructure = new OtherErrorStructure();
        otherErrorStructure.setErrorText(error.getMessage());
        errorConditionStructure.setDescription(errorDescriptionStructure);
        errorConditionStructure.setOtherError(otherErrorStructure);
        linesDelivery.setErrorCondition(errorConditionStructure);
    } else {
        Calendar gregorianCalendar = new GregorianCalendar();
        gregorianCalendar.setTimeInMillis(responseTimestamp);
        gregorianCalendar.add(Calendar.MINUTE, 1);
        linesDelivery.setValidUntil(DateUtil.toXmlGregorianCalendar(gregorianCalendar.getTimeInMillis()));
        linesDelivery.getAnnotatedLineRef().addAll(lines);
        if (hasUpcomingScheduledService != null) {
            // siri extensions
            ExtensionsStructure upcomingServiceExtensions = new ExtensionsStructure();
            upcomingServiceExtensions.setAny(hasUpcomingScheduledService);
            SiriUpcomingServiceExtension upcomingService = new SiriUpcomingServiceExtension();
            upcomingService.setUpcomingScheduledService(hasUpcomingScheduledService);
            upcomingServiceExtensions.setAny(upcomingService);
            linesDelivery.setExtensions(upcomingServiceExtensions);
        }
        linesDelivery.setResponseTimestamp(DateUtil.toXmlGregorianCalendar(responseTimestamp));
    }
    Siri siri = new Siri();
    siri.setLinesDelivery(linesDelivery);
    return siri;
}
Also used : OtherErrorStructure(uk.org.siri.siri_2.OtherErrorStructure) ServiceDeliveryErrorConditionStructure(uk.org.siri.siri_2.ServiceDeliveryErrorConditionStructure) LinesDeliveryStructure(uk.org.siri.siri_2.LinesDeliveryStructure) Calendar(java.util.Calendar) GregorianCalendar(java.util.GregorianCalendar) GregorianCalendar(java.util.GregorianCalendar) SiriUpcomingServiceExtension(org.onebusaway.transit_data_federation.siri.SiriUpcomingServiceExtension) Siri(uk.org.siri.siri_2.Siri) ErrorDescriptionStructure(uk.org.siri.siri_2.ErrorDescriptionStructure) ExtensionsStructure(uk.org.siri.siri_2.ExtensionsStructure)

Example 3 with AnnotatedLineStructure

use of uk.org.siri.siri_2.AnnotatedLineStructure in project onebusaway-application-modules by camsys.

the class RealtimeServiceV2Impl method getAnnotatedLineStructures.

@Override
public Map<Boolean, List<AnnotatedLineStructure>> getAnnotatedLineStructures(List<String> agencyIds, List<AgencyAndId> routeIds, DetailLevel detailLevel, long currentTime, Map<Filters, String> filters) {
    // Store processed StopBean as AnnotatedStopPointStructure
    List<AnnotatedLineStructure> annotatedLines = new ArrayList<AnnotatedLineStructure>();
    // AnnotatedStopPointStructures List with hasUpcomingScheduledService
    Map<Boolean, List<AnnotatedLineStructure>> output = new HashMap<Boolean, List<AnnotatedLineStructure>>();
    Boolean upcomingServiceAllStops = null;
    for (AgencyAndId rteId : routeIds) {
        String routeId = AgencyAndId.convertToString(rteId);
        RouteBean routeBean = _transitDataService.getRouteForId(routeId);
        // Filter By AgencyID
        if (routeBean.getAgency() == null || !agencyIds.contains(routeBean.getAgency().getId()))
            continue;
        AnnotatedLineStructure annotatedLineStructure = new AnnotatedLineStructure();
        RouteResult routeResult = getRouteResult(routeBean, filters);
        // Skip Routes with no stops
        if (routeResult.getDirections() == null || routeResult.getDirections().size() == 0)
            continue;
        boolean isValid = SiriSupportV2.fillAnnotatedLineStructure(annotatedLineStructure, routeResult, filters, detailLevel, currentTime);
        if (isValid)
            annotatedLines.add(annotatedLineStructure);
    }
    output.put(upcomingServiceAllStops, annotatedLines);
    return output;
}
Also used : RouteBean(org.onebusaway.transit_data.model.RouteBean) StopsForRouteBean(org.onebusaway.transit_data.model.StopsForRouteBean) AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) HashMap(java.util.HashMap) RouteResult(org.onebusaway.api.actions.siri.model.RouteResult) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) AnnotatedLineStructure(uk.org.siri.siri_2.AnnotatedLineStructure)

Example 4 with AnnotatedLineStructure

use of uk.org.siri.siri_2.AnnotatedLineStructure in project onebusaway-application-modules by camsys.

the class SiriSupportV2 method fillAnnotatedLineStructure.

public static boolean fillAnnotatedLineStructure(AnnotatedLineStructure annotatedLineStructure, RouteResult routeResult, Map<Filters, String> filters, DetailLevel detailLevel, long currentTime) {
    Directions directions = new Directions();
    // Set Line Value
    LineRefStructure line = new LineRefStructure();
    line.setValue(routeResult.getId());
    NaturalLanguageStringStructure lineName = new NaturalLanguageStringStructure();
    lineName.setValue(routeResult.getShortName());
    // DETAIL - minimum: Return only the name and identifier of stops
    // ideally, this would return only stops with scheduled service
    annotatedLineStructure.setLineRef(line);
    annotatedLineStructure.getLineName().add(lineName);
    annotatedLineStructure.setDirections(directions);
    annotatedLineStructure.setMonitored(true);
    // Loop through Direction Ids
    for (RouteDirection direction : routeResult.getDirections()) {
        // Check for existing stops in direction
        if (direction == null | direction.getStops().size() == 0)
            continue;
        String directionId = direction.getDirectionId();
        // Journey patterns - holds stop points for direction
        JourneyPattern pattern = new JourneyPattern();
        JourneyPatterns patterns = new JourneyPatterns();
        // Directions
        DirectionRefStructure dirRefStructure = new DirectionRefStructure();
        dirRefStructure.setValue(directionId);
        RouteDirectionStructure routeDirectionStructure = new RouteDirectionStructure();
        NaturalLanguageStringStructure directionName = new NaturalLanguageStringStructure();
        directionName.setValue(direction.getDestination());
        routeDirectionStructure.getDirectionName().add(directionName);
        directions.getDirection().add(routeDirectionStructure);
        // Destination
        Destinations destinations = new Destinations();
        AnnotatedDestinationStructure annotatedDest = new AnnotatedDestinationStructure();
        DestinationRefStructure destRef = new DestinationRefStructure();
        destRef.setValue(direction.getDestination());
        annotatedDest.setDestinationRef(destRef);
        destinations.getDestination().add(annotatedDest);
        // Stops
        StopsInPattern stopsInPattern = new StopsInPattern();
        List<StopOnRoute> scheduledStops = new ArrayList<StopOnRoute>();
        List<StopOnRoute> allStops = new ArrayList<StopOnRoute>();
        // Categorize by Scheduled and Unscheduled Stops
        for (StopOnRoute stop : direction.getStops()) {
            if (stop.getHasUpcomingScheduledStop() != null && stop.getHasUpcomingScheduledStop())
                scheduledStops.add(stop);
            allStops.add(stop);
        }
        if (detailLevel.equals(DetailLevel.NORMAL)) {
            for (int i = 0; i < scheduledStops.size(); i++) {
                StopOnRoute stop = direction.getStops().get(i);
                BigDecimal stopLat = new BigDecimal(stop.getLatitude());
                BigDecimal stopLon = new BigDecimal(stop.getLongitude());
                LocationStructure location = new LocationStructure();
                location.setLongitude(stopLon.setScale(6, BigDecimal.ROUND_HALF_DOWN));
                location.setLatitude(stopLat.setScale(6, BigDecimal.ROUND_HALF_DOWN));
                StopPointInPatternStructure pointInPattern = new StopPointInPatternStructure();
                pointInPattern.setLocation(location);
                pointInPattern.setOrder(BigInteger.valueOf(i));
                NaturalLanguageStringStructure stopName = new NaturalLanguageStringStructure();
                stopName.setValue(stop.getName());
                pointInPattern.getStopName().add(stopName);
                StopPointRefStructure spr = new StopPointRefStructure();
                spr.setValue(stop.getId());
                stopsInPattern.getStopPointInPattern().add(pointInPattern);
            }
        }
        if (detailLevel.equals(DetailLevel.STOPS) || detailLevel.equals(DetailLevel.FULL)) {
            for (int i = 0; i < allStops.size(); i++) {
                StopOnRoute stop = direction.getStops().get(i);
                Boolean hasUpcomingScheduledService = stop.getHasUpcomingScheduledStop();
                BigDecimal stopLat = new BigDecimal(stop.getLatitude());
                BigDecimal stopLon = new BigDecimal(stop.getLongitude());
                LocationStructure location = new LocationStructure();
                location.setLongitude(stopLon.setScale(6, BigDecimal.ROUND_HALF_DOWN));
                location.setLatitude(stopLat.setScale(6, BigDecimal.ROUND_HALF_DOWN));
                StopPointRefStructure spr = new StopPointRefStructure();
                spr.setValue(stop.getId());
                StopPointInPatternStructure pointInPattern = new StopPointInPatternStructure();
                pointInPattern.setLocation(location);
                pointInPattern.setOrder(BigInteger.valueOf(i));
                NaturalLanguageStringStructure stopName = new NaturalLanguageStringStructure();
                stopName.setValue(stop.getName());
                pointInPattern.getStopName().add(stopName);
                pointInPattern.setStopPointRef(spr);
                stopsInPattern.getStopPointInPattern().add(pointInPattern);
                // HasUpcomingService Extension
                SiriUpcomingServiceExtension upcomingService = new SiriUpcomingServiceExtension();
                upcomingService.setUpcomingScheduledService(hasUpcomingScheduledService);
                ExtensionsStructure upcomingServiceExtensions = new ExtensionsStructure();
                upcomingServiceExtensions.setAny(upcomingService);
                pointInPattern.setExtensions(upcomingServiceExtensions);
            }
        }
        String includePolylineFilter = filters.get(Filters.INCLUDE_POLYLINES);
        if (includePolylineFilter != null && passFilter("true", includePolylineFilter)) {
            // Polyline Extension
            SiriPolyLinesExtension polylines = new SiriPolyLinesExtension();
            for (String polyline : direction.getPolylines()) {
                polylines.getPolylines().add(polyline);
            }
            ExtensionsStructure PolylineExtension = new ExtensionsStructure();
            PolylineExtension.setAny(polylines);
            routeDirectionStructure.setExtensions(PolylineExtension);
        }
        routeDirectionStructure.setJourneyPatterns(patterns);
        pattern.setStopsInPattern(stopsInPattern);
        patterns.getJourneyPattern().add(pattern);
        routeDirectionStructure.setDirectionRef(dirRefStructure);
    }
    return true;
}
Also used : NaturalLanguageStringStructure(uk.org.siri.siri_2.NaturalLanguageStringStructure) DestinationRefStructure(uk.org.siri.siri_2.DestinationRefStructure) Directions(uk.org.siri.siri_2.AnnotatedLineStructure.Directions) Destinations(uk.org.siri.siri_2.AnnotatedLineStructure.Destinations) ArrayList(java.util.ArrayList) LocationStructure(uk.org.siri.siri_2.LocationStructure) ExtensionsStructure(uk.org.siri.siri_2.ExtensionsStructure) JourneyPattern(uk.org.siri.siri_2.RouteDirectionStructure.JourneyPatterns.JourneyPattern) StopPointRefStructure(uk.org.siri.siri_2.StopPointRefStructure) RouteDirection(org.onebusaway.api.actions.siri.model.RouteDirection) StopRouteDirection(org.onebusaway.api.actions.siri.model.StopRouteDirection) AnnotatedDestinationStructure(uk.org.siri.siri_2.AnnotatedDestinationStructure) SiriPolyLinesExtension(org.onebusaway.transit_data_federation.siri.SiriPolyLinesExtension) StopPointInPatternStructure(uk.org.siri.siri_2.StopPointInPatternStructure) JourneyPatterns(uk.org.siri.siri_2.RouteDirectionStructure.JourneyPatterns) LineRefStructure(uk.org.siri.siri_2.LineRefStructure) StopsInPattern(uk.org.siri.siri_2.RouteDirectionStructure.JourneyPatterns.JourneyPattern.StopsInPattern) BigDecimal(java.math.BigDecimal) RouteDirectionStructure(uk.org.siri.siri_2.RouteDirectionStructure) SiriUpcomingServiceExtension(org.onebusaway.transit_data_federation.siri.SiriUpcomingServiceExtension) DirectionRefStructure(uk.org.siri.siri_2.DirectionRefStructure) StopOnRoute(org.onebusaway.api.actions.siri.model.StopOnRoute)

Aggregations

ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)2 List (java.util.List)2 AgencyAndId (org.onebusaway.gtfs.model.AgencyAndId)2 SiriUpcomingServiceExtension (org.onebusaway.transit_data_federation.siri.SiriUpcomingServiceExtension)2 AnnotatedLineStructure (uk.org.siri.siri_2.AnnotatedLineStructure)2 ExtensionsStructure (uk.org.siri.siri_2.ExtensionsStructure)2 IOException (java.io.IOException)1 BigDecimal (java.math.BigDecimal)1 Calendar (java.util.Calendar)1 GregorianCalendar (java.util.GregorianCalendar)1 Map (java.util.Map)1 Filters (org.onebusaway.api.actions.siri.impl.SiriSupportV2.Filters)1 DetailLevel (org.onebusaway.api.actions.siri.model.DetailLevel)1 RouteDirection (org.onebusaway.api.actions.siri.model.RouteDirection)1 RouteResult (org.onebusaway.api.actions.siri.model.RouteResult)1 StopOnRoute (org.onebusaway.api.actions.siri.model.StopOnRoute)1 StopRouteDirection (org.onebusaway.api.actions.siri.model.StopRouteDirection)1 CoordinateBounds (org.onebusaway.geospatial.model.CoordinateBounds)1 RouteBean (org.onebusaway.transit_data.model.RouteBean)1