Search in sources :

Example 1 with StopShort

use of org.opentripplanner.index.model.StopShort in project OpenTripPlanner by opentripplanner.

the class IndexAPI method getStopsInRadius.

/**
 * Return a list of all stops within a circle around the given coordinate.
 */
@GET
@Path("/stops")
public Response getStopsInRadius(@QueryParam("minLat") Double minLat, @QueryParam("minLon") Double minLon, @QueryParam("maxLat") Double maxLat, @QueryParam("maxLon") Double maxLon, @QueryParam("lat") Double lat, @QueryParam("lon") Double lon, @QueryParam("radius") Double radius) {
    /* When no parameters are supplied, return all stops. */
    if (uriInfo.getQueryParameters().isEmpty()) {
        Collection<Stop> stops = index.stopForId.values();
        return Response.status(Status.OK).entity(StopShort.list(stops)).build();
    }
    /* If any of the circle parameters are specified, expect a circle not a box. */
    boolean expectCircle = (lat != null || lon != null || radius != null);
    if (expectCircle) {
        if (lat == null || lon == null || radius == null || radius < 0) {
            return Response.status(Status.BAD_REQUEST).entity(MSG_400).build();
        }
        if (radius > MAX_STOP_SEARCH_RADIUS) {
            radius = MAX_STOP_SEARCH_RADIUS;
        }
        List<StopShort> stops = Lists.newArrayList();
        Coordinate coord = new Coordinate(lon, lat);
        for (TransitStop stopVertex : streetIndex.getNearbyTransitStops(new Coordinate(lon, lat), radius)) {
            double distance = SphericalDistanceLibrary.fastDistance(stopVertex.getCoordinate(), coord);
            if (distance < radius) {
                stops.add(new StopShort(stopVertex.getStop(), (int) distance));
            }
        }
        return Response.status(Status.OK).entity(stops).build();
    } else {
        /* We're not circle mode, we must be in box mode. */
        if (minLat == null || minLon == null || maxLat == null || maxLon == null) {
            return Response.status(Status.BAD_REQUEST).entity(MSG_400).build();
        }
        if (maxLat <= minLat || maxLon <= minLon) {
            return Response.status(Status.BAD_REQUEST).entity(MSG_400).build();
        }
        List<StopShort> stops = Lists.newArrayList();
        Envelope envelope = new Envelope(new Coordinate(minLon, minLat), new Coordinate(maxLon, maxLat));
        for (TransitStop stopVertex : streetIndex.getTransitStopForEnvelope(envelope)) {
            stops.add(new StopShort(stopVertex.getStop()));
        }
        return Response.status(Status.OK).entity(stops).build();
    }
}
Also used : TransitStop(org.opentripplanner.routing.vertextype.TransitStop) TransitStop(org.opentripplanner.routing.vertextype.TransitStop) Stop(org.onebusaway.gtfs.model.Stop) Coordinate(com.vividsolutions.jts.geom.Coordinate) StopShort(org.opentripplanner.index.model.StopShort) Envelope(com.vividsolutions.jts.geom.Envelope) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Aggregations

Coordinate (com.vividsolutions.jts.geom.Coordinate)1 Envelope (com.vividsolutions.jts.geom.Envelope)1 GET (javax.ws.rs.GET)1 Path (javax.ws.rs.Path)1 Stop (org.onebusaway.gtfs.model.Stop)1 StopShort (org.opentripplanner.index.model.StopShort)1 TransitStop (org.opentripplanner.routing.vertextype.TransitStop)1