Search in sources :

Example 1 with Lines

use of uk.org.siri.siri_2.AnnotatedStopPointStructure.Lines 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 Lines

use of uk.org.siri.siri_2.AnnotatedStopPointStructure.Lines 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 Lines

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

the class SiriSupportV2 method fillAnnotatedStopPointStructure.

public static boolean fillAnnotatedStopPointStructure(AnnotatedStopPointStructure annotatedStopPoint, StopRouteDirection stopRouteDirection, Map<Filters, String> filters, DetailLevel detailLevel, long currentTime) {
    StopBean stopBean = stopRouteDirection.getStop();
    List<RouteForDirection> routeDirections = stopRouteDirection.getRouteDirections();
    // Set Stop Name
    NaturalLanguageStringStructure stopName = new NaturalLanguageStringStructure();
    stopName.setValue(stopBean.getName());
    // Set Route and Direction
    Lines lines = new Lines();
    for (RouteForDirection routeDirection : routeDirections) {
        String directionId = routeDirection.getDirectionId();
        String routeId = routeDirection.getRouteId();
        LineRefStructure line = new LineRefStructure();
        line.setValue(routeId);
        DirectionRefStructure direction = new DirectionRefStructure();
        direction.setValue(directionId);
        LineDirectionStructure lineDirection = new LineDirectionStructure();
        lineDirection.setDirectionRef(direction);
        lineDirection.setLineRef(line);
        lines.getLineRefOrLineDirection().add(lineDirection);
    }
    // Set Lat and Lon
    BigDecimal stopLat = new BigDecimal(stopBean.getLat());
    BigDecimal stopLon = new BigDecimal(stopBean.getLon());
    LocationStructure location = new LocationStructure();
    location.setLongitude(stopLon.setScale(6, BigDecimal.ROUND_HALF_DOWN));
    location.setLatitude(stopLat.setScale(6, BigDecimal.ROUND_HALF_DOWN));
    // Set StopId
    StopPointRefStructure stopPointRef = new StopPointRefStructure();
    stopPointRef.setValue(stopBean.getId());
    // Details -- minimum
    annotatedStopPoint.getStopName().add(stopName);
    // Details -- normal
    if (detailLevel.equals(DetailLevel.NORMAL) || detailLevel.equals(DetailLevel.FULL)) {
        annotatedStopPoint.setLocation(location);
        annotatedStopPoint.setLines(lines);
        annotatedStopPoint.setMonitored(true);
    }
    annotatedStopPoint.setStopPointRef(stopPointRef);
    return true;
}
Also used : NaturalLanguageStringStructure(uk.org.siri.siri_2.NaturalLanguageStringStructure) LineDirectionStructure(uk.org.siri.siri_2.LineDirectionStructure) StopBean(org.onebusaway.transit_data.model.StopBean) LineRefStructure(uk.org.siri.siri_2.LineRefStructure) RouteForDirection(org.onebusaway.api.actions.siri.model.RouteForDirection) StopPointRefStructure(uk.org.siri.siri_2.StopPointRefStructure) DirectionRefStructure(uk.org.siri.siri_2.DirectionRefStructure) LocationStructure(uk.org.siri.siri_2.LocationStructure) BigDecimal(java.math.BigDecimal) Lines(uk.org.siri.siri_2.AnnotatedStopPointStructure.Lines)

Aggregations

IOException (java.io.IOException)1 BigDecimal (java.math.BigDecimal)1 ArrayList (java.util.ArrayList)1 Calendar (java.util.Calendar)1 GregorianCalendar (java.util.GregorianCalendar)1 HashMap (java.util.HashMap)1 List (java.util.List)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 RouteForDirection (org.onebusaway.api.actions.siri.model.RouteForDirection)1 CoordinateBounds (org.onebusaway.geospatial.model.CoordinateBounds)1 AgencyAndId (org.onebusaway.gtfs.model.AgencyAndId)1 StopBean (org.onebusaway.transit_data.model.StopBean)1 SiriUpcomingServiceExtension (org.onebusaway.transit_data_federation.siri.SiriUpcomingServiceExtension)1 AnnotatedLineStructure (uk.org.siri.siri_2.AnnotatedLineStructure)1 Lines (uk.org.siri.siri_2.AnnotatedStopPointStructure.Lines)1 DirectionRefStructure (uk.org.siri.siri_2.DirectionRefStructure)1 ErrorDescriptionStructure (uk.org.siri.siri_2.ErrorDescriptionStructure)1 ExtensionsStructure (uk.org.siri.siri_2.ExtensionsStructure)1