use of org.opentripplanner.common.model.GenericLocation in project OpenTripPlanner by opentripplanner.
the class GraphIndex method findClosestStopsByWalking.
/* TODO: an almost similar function exists in ProfileRouter, combine these.
* Should these live in a separate class? */
public List<StopAndDistance> findClosestStopsByWalking(double lat, double lon, int radius) {
// Make a normal OTP routing request so we can traverse edges and use GenericAStar
// TODO make a function that builds normal routing requests from profile requests
RoutingRequest rr = new RoutingRequest(TraverseMode.WALK);
rr.from = new GenericLocation(lat, lon);
// FIXME requires destination to be set, not necessary for analyst
rr.to = new GenericLocation(lat, lon);
rr.batch = true;
rr.setRoutingContext(graph);
rr.walkSpeed = 1;
rr.dominanceFunction = new DominanceFunction.LeastWalk();
// RR dateTime defaults to currentTime.
// If elapsed time is not capped, searches are very slow.
rr.worstTime = (rr.dateTime + radius);
AStar astar = new AStar();
rr.setNumItineraries(1);
StopFinderTraverseVisitor visitor = new StopFinderTraverseVisitor();
astar.setTraverseVisitor(visitor);
// timeout in seconds
astar.getShortestPathTree(rr, 1);
// Destroy the routing context, to clean up the temporary edges & vertices
rr.rctx.destroy();
return visitor.stopsFound;
}
use of org.opentripplanner.common.model.GenericLocation in project OpenTripPlanner by opentripplanner.
the class TimeGridWs method getTimeGridPng.
@GET
@Produces({ "image/png" })
public Response getTimeGridPng(@QueryParam("base64") @DefaultValue("false") boolean base64) throws Exception {
/* Fetch the Router for this request using server and routerId fields from superclass. */
Router router = otpServer.getRouter(routerId);
if (precisionMeters < 10)
throw new IllegalArgumentException("Too small precisionMeters: " + precisionMeters);
if (offRoadDistanceMeters < 10)
throw new IllegalArgumentException("Too small offRoadDistanceMeters: " + offRoadDistanceMeters);
// Build the request
RoutingRequest sptRequest = buildRequest();
SampleGridRequest tgRequest = new SampleGridRequest();
tgRequest.maxTimeSec = maxTimeSec;
tgRequest.precisionMeters = precisionMeters;
tgRequest.offRoadDistanceMeters = offRoadDistanceMeters;
if (coordinateOrigin != null)
tgRequest.coordinateOrigin = new GenericLocation(null, coordinateOrigin).getCoordinate();
// Get a sample grid
ZSampleGrid<WTWD> sampleGrid = router.sampleGridRenderer.getSampleGrid(tgRequest, sptRequest);
int cols = sampleGrid.getXMax() - sampleGrid.getXMin() + 1;
int rows = sampleGrid.getYMax() - sampleGrid.getYMin() + 1;
// Hard-coded to RGBA
int channels = 4;
// We force to 8 bits channel depth, some clients won't support more than 8
// (namely, HTML5 canvas...)
ImageInfo imgInfo = new ImageInfo(cols, rows, 8, true, false, false);
/**
* TODO: PNGJ allow for progressive (ie line-by-line) writing. Thus we could theorically
* prevent having to allocate a bit pixel array in the first place, but we would need a
* line-by-line iterator on the sample grid, which is currently not the case.
*/
int[][] rgba = new int[rows][cols * channels];
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PngWriter pw = new PngWriter(baos, imgInfo);
pw.getMetadata().setText(PngChunkTextVar.KEY_Software, "OTPA");
pw.getMetadata().setText(PngChunkTextVar.KEY_Creation_Time, new Date().toString());
pw.getMetadata().setText(PngChunkTextVar.KEY_Description, "Sample grid bitmap");
String gridCornerStr = String.format(Locale.US, "%.8f,%.8f", sampleGrid.getCenter().y + sampleGrid.getYMin() * sampleGrid.getCellSize().y, sampleGrid.getCenter().x + sampleGrid.getXMin() * sampleGrid.getCellSize().x);
String gridCellSzStr = String.format(Locale.US, "%.12f,%.12f", sampleGrid.getCellSize().y, sampleGrid.getCellSize().x);
String offRoadDistStr = String.format(Locale.US, "%d", offRoadDistanceMeters);
PngChunkTEXT gridCornerChunk = new PngChunkTEXT(imgInfo);
gridCornerChunk.setKeyVal(OTPA_GRID_CORNER, gridCornerStr);
pw.getChunksList().queue(gridCornerChunk);
PngChunkTEXT gridCellSzChunk = new PngChunkTEXT(imgInfo);
gridCellSzChunk.setKeyVal(OTPA_GRID_CELL_SIZE, gridCellSzStr);
pw.getChunksList().queue(gridCellSzChunk);
PngChunkTEXT offRoadDistChunk = new PngChunkTEXT(imgInfo);
offRoadDistChunk.setKeyVal(OTPA_OFFROAD_DIST, offRoadDistStr);
pw.getChunksList().queue(offRoadDistChunk);
double unit;
switch(zDataType) {
case TIME:
// 1:1sec, max 18h
unit = 1.0;
break;
case BOARDINGS:
// 1:0.001 boarding, max 65.5
unit = 1000.0;
break;
case WALK_DISTANCE:
// 1:0.1m, max 6.55km
unit = 10.0;
break;
default:
throw new IllegalArgumentException("Unsupported Z DataType.");
}
for (ZSamplePoint<WTWD> p : sampleGrid) {
WTWD z = p.getZ();
int row = p.getY() - sampleGrid.getYMin();
int col = p.getX() - sampleGrid.getXMin();
double zz;
switch(zDataType) {
case TIME:
zz = z.wTime / z.w;
break;
case BOARDINGS:
zz = z.wBoardings / z.w;
break;
case WALK_DISTANCE:
zz = z.wWalkDist / z.w;
break;
default:
throw new IllegalArgumentException("Unsupported Z DataType.");
}
int iz;
if (Double.isInfinite(zz)) {
iz = 65535;
} else {
iz = ImageLineHelper.clampTo_0_65535((int) Math.round(zz * unit));
if (iz == 65535)
// Clamp
iz = 65534;
}
// d is expressed as a percentage of grid size, max 255%.
// Sometimes d will be bigger than 2.55 x grid size,
// but this should not be too much important as we are off-bounds.
int id = ImageLineHelper.clampTo_0_255((int) Math.round(z.d / precisionMeters * 100));
int offset = col * channels;
// z low 8 bits
rgba[row][offset + 0] = (iz & 0xFF);
// z high 8 bits
rgba[row][offset + 1] = (iz >> 8);
// d
rgba[row][offset + 2] = id;
/*
* Keep the alpha channel at 255, otherwise the RGB channel will be downsampled on some
* rendering clients (namely, JS canvas).
*/
rgba[row][offset + 3] = 255;
}
for (int row = 0; row < rgba.length; row++) {
ImageLineInt iline = new ImageLineInt(imgInfo, rgba[row]);
pw.writeRow(iline, row);
}
pw.end();
// Disallow caching on client side
CacheControl cc = new CacheControl();
cc.setNoCache(true);
// Also put the meta-data in the HTML header (easier to read from JS)
byte[] data = baos.toByteArray();
if (base64) {
data = Base64.encodeBase64(data);
}
return Response.ok().cacheControl(cc).entity(data).header(OTPA_GRID_CORNER, gridCornerStr).header(OTPA_GRID_CELL_SIZE, gridCellSzStr).header(OTPA_OFFROAD_DIST, offRoadDistStr).build();
}
use of org.opentripplanner.common.model.GenericLocation in project OpenTripPlanner by opentripplanner.
the class TestIntermediatePlaces method handleRequest.
private void handleRequest(GenericLocation from, GenericLocation to, GenericLocation[] via, String modes, boolean arriveBy) {
RoutingRequest request = new RoutingRequest(modes);
request.setDateTime("2016-04-20", "13:00", timeZone);
request.setArriveBy(arriveBy);
request.from = from;
request.to = to;
for (GenericLocation intermediateLocation : via) {
request.addIntermediatePlace(intermediateLocation);
}
List<GraphPath> pathList = graphPathFinder.graphPathFinderEntryPoint(request);
assertNotNull(pathList);
assertFalse(pathList.isEmpty());
TripPlan plan = GraphPathToTripPlanConverter.generatePlan(pathList, request);
assertLocationIsVeryCloseToPlace(from, plan.from);
assertLocationIsVeryCloseToPlace(to, plan.to);
assertTrue(1 <= plan.itinerary.size());
for (Itinerary itinerary : plan.itinerary) {
validateIntermediatePlacesVisited(itinerary, via);
assertTrue(via.length < itinerary.legs.size());
validateLegsTemporally(request, itinerary);
validateLegsSpatially(plan, itinerary);
if (modes.contains("TRANSIT")) {
assert itinerary.transitTime > 0;
}
}
}
use of org.opentripplanner.common.model.GenericLocation in project OpenTripPlanner by opentripplanner.
the class TestIntermediatePlaces method testTwoIntermediatePlaces.
@Test
@Ignore
public void testTwoIntermediatePlaces() {
GenericLocation fromLocation = new GenericLocation(39.93080, -82.98522);
GenericLocation toLocation = new GenericLocation(39.96383, -82.96291);
GenericLocation[] intermediateLocations = new GenericLocation[2];
intermediateLocations[0] = new GenericLocation(39.92099, -82.95570);
intermediateLocations[1] = new GenericLocation(39.96146, -82.99552);
handleRequest(fromLocation, toLocation, intermediateLocations, "CAR", false);
handleRequest(fromLocation, toLocation, intermediateLocations, "CAR", true);
}
use of org.opentripplanner.common.model.GenericLocation in project OpenTripPlanner by opentripplanner.
the class TestIntermediatePlaces method testTransitTwoIntermediatePlaces.
@Test
public void testTransitTwoIntermediatePlaces() {
GenericLocation fromLocation = new GenericLocation(39.9908, -83.0118);
GenericLocation toLocation = new GenericLocation(39.9998, -83.0198);
GenericLocation[] intermediateLocations = new GenericLocation[2];
intermediateLocations[0] = new GenericLocation(40.0000, -82.900);
intermediateLocations[1] = new GenericLocation(39.9100, -83.100);
handleRequest(fromLocation, toLocation, intermediateLocations, "TRANSIT,WALK", false);
handleRequest(fromLocation, toLocation, intermediateLocations, "TRANSIT,WALK", true);
}
Aggregations