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();
}
}
Aggregations