Search in sources :

Example 1 with AnnotatedStopPointStructure

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

the class StopPointsV2Action 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(STOP_POINTS_DETAIL_LEVEL);
    // 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 (bounds == null) {
        if (routeIds.size() > 0) {
            useLineRefOnly = true;
        } else {
            boundsErrorString += ERROR_REQUIRED_PARAMS;
        }
    }
    // Setup Filters
    Map<Filters, String> filters = new HashMap<Filters, String>();
    filters.put(Filters.DIRECTION_REF, directionId);
    filters.put(Filters.UPCOMING_SCHEDULED_SERVICE, hasUpcomingScheduledService);
    // Annotated Stop Points
    List<AnnotatedStopPointStructure> stopPoints = new ArrayList<AnnotatedStopPointStructure>();
    Map<Boolean, List<AnnotatedStopPointStructure>> stopPointsMap;
    // Error Handler
    Exception error = null;
    if ((bounds == null && !useLineRefOnly) || (_request.getParameter(LINE_REF) != null && routeIds.size() == 0) || !validBoundDistance) {
        String errorString = (boundsErrorString + " " + routeIdsErrorString).trim();
        error = new Exception(errorString);
    } else {
        if (useLineRefOnly) {
            stopPointsMap = _realtimeService.getAnnotatedStopPointStructures(agencyIds, routeIds, detailLevel, responseTimestamp, filters);
        } else {
            stopPointsMap = _realtimeService.getAnnotatedStopPointStructures(bounds, agencyIds, routeIds, detailLevel, responseTimestamp, filters);
        }
        for (Map.Entry<Boolean, List<AnnotatedStopPointStructure>> entry : stopPointsMap.entrySet()) {
            if (entry.getValue().size() > 0)
                upcomingServiceAllStops = entry.getKey();
            stopPoints.addAll(entry.getValue());
        }
    }
    _response = generateSiriResponse(stopPoints, upcomingServiceAllStops, error, responseTimestamp);
    try {
        this._servletResponse.getWriter().write(getStopPoints());
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}
Also used : AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) HashMap(java.util.HashMap) AnnotatedStopPointStructure(uk.org.siri.siri_2.AnnotatedStopPointStructure) ArrayList(java.util.ArrayList) 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 AnnotatedStopPointStructure

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

the class StopPointsV2Action method generateSiriResponse.

private Siri generateSiriResponse(List<AnnotatedStopPointStructure> stopPoints, Boolean hasUpcomingScheduledService, Exception error, long responseTimestamp) {
    StopPointsDeliveryStructure stopPointsDelivery = new StopPointsDeliveryStructure();
    stopPointsDelivery.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);
        stopPointsDelivery.setErrorCondition(errorConditionStructure);
    } else {
        Calendar gregorianCalendar = new GregorianCalendar();
        gregorianCalendar.setTimeInMillis(responseTimestamp);
        gregorianCalendar.add(Calendar.MINUTE, 1);
        stopPointsDelivery.setValidUntil(DateUtil.toXmlGregorianCalendar(gregorianCalendar.getTimeInMillis()));
        stopPointsDelivery.getAnnotatedStopPointRef().addAll(stopPoints);
        if (hasUpcomingScheduledService != null) {
            // siri extensions
            ExtensionsStructure upcomingServiceExtensions = new ExtensionsStructure();
            SiriUpcomingServiceExtension upcomingService = new SiriUpcomingServiceExtension();
            upcomingService.setUpcomingScheduledService(hasUpcomingScheduledService);
            upcomingServiceExtensions.setAny(upcomingService);
            stopPointsDelivery.setExtensions(upcomingServiceExtensions);
        }
        stopPointsDelivery.setResponseTimestamp(DateUtil.toXmlGregorianCalendar(responseTimestamp));
    // TODO - LCARABALLO Do I still need serviceAlertsHelper?
    /*
       * _serviceAlertsHelper.addSituationExchangeToSiriForStops(
       * serviceDelivery, visits, _nycTransitDataService, stopIds);
       * _serviceAlertsHelper.addGlobalServiceAlertsToServiceDelivery(
       * serviceDelivery, _realtimeService);
       */
    }
    Siri siri = new Siri();
    siri.setStopPointsDelivery(stopPointsDelivery);
    return siri;
}
Also used : OtherErrorStructure(uk.org.siri.siri_2.OtherErrorStructure) ServiceDeliveryErrorConditionStructure(uk.org.siri.siri_2.ServiceDeliveryErrorConditionStructure) StopPointsDeliveryStructure(uk.org.siri.siri_2.StopPointsDeliveryStructure) 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 AnnotatedStopPointStructure

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

the class RealtimeServiceV2Impl method getAnnotatedStopPointStructures.

@Override
public Map<Boolean, List<AnnotatedStopPointStructure>> getAnnotatedStopPointStructures(List<String> agencyIds, List<AgencyAndId> routeIds, DetailLevel detailLevel, long currentTime, Map<Filters, String> filters) {
    // Cache stops by route so we don't need to call the transit data service repeatedly for the same route
    Map<String, StopsForRouteBean> stopsForRouteCache = new HashMap<String, StopsForRouteBean>();
    // Store processed StopBean as AnnotatedStopPointStructure
    List<AnnotatedStopPointStructure> annotatedStopPoints = new ArrayList<AnnotatedStopPointStructure>();
    // AnnotatedStopPointStructures List with hasUpcomingScheduledService
    Map<Boolean, List<AnnotatedStopPointStructure>> output = new HashMap<Boolean, List<AnnotatedStopPointStructure>>();
    String upcomingScheduledService = filters.get(Filters.UPCOMING_SCHEDULED_SERVICE);
    Boolean upcomingServiceAllStops = true;
    if (upcomingScheduledService != null && upcomingScheduledService.trim().equalsIgnoreCase("false")) {
        upcomingServiceAllStops = false;
    }
    for (AgencyAndId aid : routeIds) {
        String routeId = AgencyAndId.convertToString(aid);
        StopsForRouteBean stopsForLineRef = _transitDataService.getStopsForRoute(routeId);
        processAnnotatedStopPoints(agencyIds, routeIds, stopsForLineRef.getStops(), annotatedStopPoints, filters, stopsForRouteCache, detailLevel, currentTime);
    }
    output.put(upcomingServiceAllStops, annotatedStopPoints);
    return output;
}
Also used : AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) HashMap(java.util.HashMap) AnnotatedStopPointStructure(uk.org.siri.siri_2.AnnotatedStopPointStructure) ArrayList(java.util.ArrayList) StopsForRouteBean(org.onebusaway.transit_data.model.StopsForRouteBean) List(java.util.List) ArrayList(java.util.ArrayList)

Example 4 with AnnotatedStopPointStructure

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

the class RealtimeServiceV2Impl method processAnnotatedStopPoints.

private void processAnnotatedStopPoints(List<String> agencyIds, List<AgencyAndId> routeIds, List<StopBean> stopBeans, List<AnnotatedStopPointStructure> annotatedStopPoints, Map<Filters, String> filters, Map<String, StopsForRouteBean> stopsForRouteCache, DetailLevel detailLevel, long currentTime) {
    for (StopBean stopBean : stopBeans) {
        List<StopsForRouteBean> stopsForRouteList = new ArrayList<StopsForRouteBean>();
        boolean filterByLineRef = (routeIds != null && routeIds.size() > 0) ? true : false;
        boolean containsLineRef = false;
        // Get a list of all the routes for the stop
        for (RouteBean route : stopBean.getRoutes()) {
            // Filter By AgencyID
            if (route.getAgency() == null || !agencyIds.contains(route.getAgency().getId()))
                continue;
            // Add list of stops retreived from route to cache
            StopsForRouteBean stopsForRoute = stopsForRouteCache.get(route.getId());
            if (stopsForRoute == null) {
                stopsForRoute = _transitDataService.getStopsForRoute(route.getId());
                stopsForRouteCache.put(route.getId(), stopsForRoute);
            }
            if (stopsForRoute != null)
                stopsForRouteList.add(stopsForRoute);
            if (filterByLineRef && routeIds.contains(AgencyAndIdLibrary.convertFromString(route.getId())))
                containsLineRef = true;
        }
        // Filter By LineRef
        if (filterByLineRef && !containsLineRef)
            continue;
        // Get Stops with List of Routes, Direction, and Upcoming Service Info
        StopRouteDirection stopRouteDirection = getStopRouteDirection(stopBean, stopsForRouteList, filters);
        // Skip if No Route Directions Found
        if (stopRouteDirection == null)
            continue;
        // Used to filter stops that don't have any routes that match hasUpcomingScheduledStop
        if (stopRouteDirection.getRouteDirections() == null || stopRouteDirection.getRouteDirections().size() == 0)
            continue;
        AnnotatedStopPointStructure annotatedStopPoint = new AnnotatedStopPointStructure();
        boolean isValid = SiriSupportV2.fillAnnotatedStopPointStructure(annotatedStopPoint, stopRouteDirection, filters, detailLevel, currentTime);
        if (isValid)
            annotatedStopPoints.add(annotatedStopPoint);
    }
}
Also used : RouteBean(org.onebusaway.transit_data.model.RouteBean) StopsForRouteBean(org.onebusaway.transit_data.model.StopsForRouteBean) StopRouteDirection(org.onebusaway.api.actions.siri.model.StopRouteDirection) AnnotatedStopPointStructure(uk.org.siri.siri_2.AnnotatedStopPointStructure) ArrayList(java.util.ArrayList) StopBean(org.onebusaway.transit_data.model.StopBean) StopsForRouteBean(org.onebusaway.transit_data.model.StopsForRouteBean)

Example 5 with AnnotatedStopPointStructure

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

the class RealtimeServiceTest method testStopPointsByBounds.

@Test
public void testStopPointsByBounds() throws Exception {
    // MOCKS
    // Coordinate Bounds
    CoordinateBounds bounds = new CoordinateBounds(Double.parseDouble("47.612813"), Double.parseDouble("-122.339662"), Double.parseDouble("47.608813"), Double.parseDouble("-122.337662"));
    // Stops For Bounds
    StopsBean stopsBean = new StopsBean();
    stopsBean.setStops(stops);
    when(transitDataService.getRouteForId("1_100194")).thenReturn(routeBean);
    when(transitDataService.getStops(any(SearchQueryBean.class))).thenReturn(stopsBean);
    when(transitDataService.getStopsForRoute("1_100194")).thenReturn(stopsForRouteBean);
    when(transitDataService.stopHasUpcomingScheduledService(anyString(), anyLong(), anyString(), anyString(), anyString())).thenReturn(true);
    // EXPECTED
    LineDirectionStructure lds = new LineDirectionStructure();
    DirectionRefStructure drs = new DirectionRefStructure();
    LineRefStructure lrs = new LineRefStructure();
    lds.setDirectionRef(drs);
    lds.setLineRef(lrs);
    drs.setValue("0");
    lrs.setValue("1_100194");
    LocationStructure ls = new LocationStructure();
    BigDecimal lat = new BigDecimal(47.610813);
    BigDecimal lon = new BigDecimal(-122.338662);
    ls.setLatitude(lat.setScale(6, BigDecimal.ROUND_HALF_DOWN));
    ls.setLongitude(lon.setScale(6, BigDecimal.ROUND_HALF_DOWN));
    NaturalLanguageStringStructure stopName = new NaturalLanguageStringStructure();
    stopName.setValue("3rd Ave & Pine St");
    List<NaturalLanguageStringStructure> stopNames = new ArrayList<NaturalLanguageStringStructure>();
    stopNames.add(stopName);
    StopPointRefStructure stopPointRef = new StopPointRefStructure();
    stopPointRef.setValue("1_430");
    Boolean monitored = true;
    AnnotatedStopPointStructure mockStopPoint = new AnnotatedStopPointStructure();
    mockStopPoint.setLines(new AnnotatedStopPointStructure.Lines());
    mockStopPoint.getLines().getLineRefOrLineDirection().add(lds);
    mockStopPoint.setLocation(ls);
    mockStopPoint.getStopName().add(stopName);
    mockStopPoint.setStopPointRef(stopPointRef);
    mockStopPoint.setMonitored(monitored);
    // REALTIME ARGUMENTS
    // Agency Ids
    List<String> agencyIds = new ArrayList<String>();
    String agencyId = "1";
    agencyIds.add(agencyId);
    // Route Ids
    List<AgencyAndId> routeIds = new ArrayList<AgencyAndId>();
    AgencyAndId routeId = AgencyAndIdLibrary.convertFromString("1_100194");
    routeIds.add(routeId);
    // Detail Level
    DetailLevel detailLevel = DetailLevel.NORMAL;
    // Time
    long time = System.currentTimeMillis();
    // Filters
    Map<Filters, String> filters = new HashMap<Filters, String>();
    Map<Boolean, List<AnnotatedStopPointStructure>> actualResult = realtimeService.getAnnotatedStopPointStructures(bounds, agencyIds, routeIds, detailLevel, time, filters);
    AnnotatedStopPointStructure actualStopPoint = actualResult.get(true).get(0);
    assertTrue(isEqual(mockStopPoint, actualStopPoint));
}
Also used : NaturalLanguageStringStructure(uk.org.siri.siri_2.NaturalLanguageStringStructure) LineDirectionStructure(uk.org.siri.siri_2.LineDirectionStructure) AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Matchers.anyString(org.mockito.Matchers.anyString) LocationStructure(uk.org.siri.siri_2.LocationStructure) Filters(org.onebusaway.api.actions.siri.impl.SiriSupportV2.Filters) SearchQueryBean(org.onebusaway.transit_data.model.SearchQueryBean) ArrayList(java.util.ArrayList) List(java.util.List) StopPointRefStructure(uk.org.siri.siri_2.StopPointRefStructure) AnnotatedStopPointStructure(uk.org.siri.siri_2.AnnotatedStopPointStructure) LineRefStructure(uk.org.siri.siri_2.LineRefStructure) BigDecimal(java.math.BigDecimal) DetailLevel(org.onebusaway.api.actions.siri.model.DetailLevel) DirectionRefStructure(uk.org.siri.siri_2.DirectionRefStructure) CoordinateBounds(org.onebusaway.geospatial.model.CoordinateBounds) StopsBean(org.onebusaway.transit_data.model.StopsBean) Test(org.junit.Test)

Aggregations

ArrayList (java.util.ArrayList)7 HashMap (java.util.HashMap)6 List (java.util.List)6 AgencyAndId (org.onebusaway.gtfs.model.AgencyAndId)6 AnnotatedStopPointStructure (uk.org.siri.siri_2.AnnotatedStopPointStructure)6 BigDecimal (java.math.BigDecimal)4 Filters (org.onebusaway.api.actions.siri.impl.SiriSupportV2.Filters)4 DetailLevel (org.onebusaway.api.actions.siri.model.DetailLevel)4 StopsForRouteBean (org.onebusaway.transit_data.model.StopsForRouteBean)4 DirectionRefStructure (uk.org.siri.siri_2.DirectionRefStructure)4 LineDirectionStructure (uk.org.siri.siri_2.LineDirectionStructure)4 LineRefStructure (uk.org.siri.siri_2.LineRefStructure)4 LocationStructure (uk.org.siri.siri_2.LocationStructure)4 NaturalLanguageStringStructure (uk.org.siri.siri_2.NaturalLanguageStringStructure)4 StopPointRefStructure (uk.org.siri.siri_2.StopPointRefStructure)4 Matchers.anyString (org.mockito.Matchers.anyString)3 CoordinateBounds (org.onebusaway.geospatial.model.CoordinateBounds)3 RouteBean (org.onebusaway.transit_data.model.RouteBean)3 StopBean (org.onebusaway.transit_data.model.StopBean)3 IOException (java.io.IOException)2