/** * Finds all egress paths from to coordinate to end stop and adds routers to egressRouter * * @param request * @param egressRouter */ private void findEgressPaths(ProfileRequest request, Map<LegMode, StreetRouter> egressRouter) { // For egress // TODO: this must be reverse search for (LegMode mode : request.egressModes) { StreetRouter streetRouter = new StreetRouter(transportNetwork.streetLayer); streetRouter.transitStopSearch = true; streetRouter.dominanceVariable = StreetRouter.State.RoutingVariable.DURATION_SECONDS; if (egressUnsupportedModes.contains(mode)) { continue; } // TODO: add support for bike sharing streetRouter.streetMode = StreetMode.valueOf(mode.toString()); streetRouter.profileRequest = request; streetRouter.timeLimitSeconds = request.getTimeLimit(mode); if (streetRouter.setOrigin(request.toLat, request.toLon)) { streetRouter.route(); TIntIntMap stops = streetRouter.getReachedStops(); egressRouter.put(mode, streetRouter); LOG.info("Added {} edgres stops for mode {}", stops.size(), mode); } else { LOG.warn( "MODE:{}, Edge near the origin coordinate wasn't found. Routing didn't start!", mode); } } }
/** * Finds direct paths between from and to coordinates in request and adds them to option * * @param request * @param option */ private void findDirectPaths(ProfileRequest request, ProfileOption option) { // For direct modes for (LegMode mode : request.directModes) { StreetRouter streetRouter = new StreetRouter(transportNetwork.streetLayer); StreetPath streetPath; streetRouter.profileRequest = request; if (mode == LegMode.BICYCLE_RENT) { if (!transportNetwork.streetLayer.bikeSharing) { LOG.warn("Bike sharing trip requested but no bike sharing stations in the streetlayer"); continue; } streetRouter = findBikeRentalPath(request, streetRouter, true); if (streetRouter != null) { StreetRouter.State lastState = streetRouter.getState(request.toLat, request.toLon); if (lastState != null) { streetPath = new StreetPath(lastState, streetRouter, LegMode.BICYCLE_RENT, transportNetwork); } else { LOG.warn( "MODE:{}, Edge near the destination coordinate wasn't found. Routing didn't start!", mode); continue; } } else { LOG.warn("Not found path from cycle to end"); continue; } } else { streetRouter.streetMode = StreetMode.valueOf(mode.toString()); streetRouter.timeLimitSeconds = request.streetTime * 60; if (streetRouter.setOrigin(request.fromLat, request.fromLon)) { if (!streetRouter.setDestination(request.toLat, request.toLon)) { LOG.warn("Direct mode {} destination wasn't found!", mode); continue; } streetRouter.route(); StreetRouter.State lastState = streetRouter.getState(streetRouter.getDestinationSplit()); if (lastState == null) { LOG.warn("Direct mode {} last state wasn't found", mode); continue; } streetPath = new StreetPath(lastState, transportNetwork); } else { LOG.warn("Direct mode {} origin wasn't found!", mode); continue; } } StreetSegment streetSegment = new StreetSegment(streetPath, mode, transportNetwork.streetLayer); option.addDirect(streetSegment, request.getFromTimeDateZD()); } }
/** * Finds access paths from from coordinate in request and adds all routers with paths to * accessRouter map * * @param request * @param accessRouter */ private void findAccessPaths(ProfileRequest request, Map<LegMode, StreetRouter> accessRouter) { // Routes all access modes for (LegMode mode : request.accessModes) { StreetRouter streetRouter = new StreetRouter(transportNetwork.streetLayer); streetRouter.profileRequest = request; if (mode == LegMode.CAR_PARK) { streetRouter = findParkRidePath(request, streetRouter); if (streetRouter != null) { accessRouter.put(LegMode.CAR_PARK, streetRouter); } else { LOG.warn( "MODE:{}, Edge near the origin coordinate wasn't found. Routing didn't start!", mode); } } else if (mode == LegMode.BICYCLE_RENT) { if (!transportNetwork.streetLayer.bikeSharing) { LOG.warn("Bike sharing trip requested but no bike sharing stations in the streetlayer"); continue; } streetRouter = findBikeRentalPath(request, streetRouter, false); if (streetRouter != null) { accessRouter.put(LegMode.BICYCLE_RENT, streetRouter); } else { LOG.warn("Not found path from cycle to end"); } } else { streetRouter.streetMode = StreetMode.valueOf(mode.toString()); // Gets correct maxCar/Bike/Walk time in seconds for access leg based on mode since it // depends on the mode streetRouter.timeLimitSeconds = request.getTimeLimit(mode); streetRouter.transitStopSearch = true; streetRouter.dominanceVariable = StreetRouter.State.RoutingVariable.DURATION_SECONDS; if (streetRouter.setOrigin(request.fromLat, request.fromLon)) { streetRouter.route(); // Searching for access paths accessRouter.put(mode, streetRouter); } else { LOG.warn( "MODE:{}, Edge near the origin coordinate wasn't found. Routing didn't start!", mode); } } } }
// Does point to point routing with data from request public ProfileResponse getPlan(ProfileRequest request) { long startRouting = System.currentTimeMillis(); request.zoneId = transportNetwork.getTimeZone(); // Do the query and return result ProfileResponse profileResponse = new ProfileResponse(); ProfileOption option = new ProfileOption(); findDirectPaths(request, option); option.summary = option.generateSummary(); profileResponse.addOption(option); if (request.hasTransit()) { Map<LegMode, StreetRouter> accessRouter = new HashMap<>(request.accessModes.size()); Map<LegMode, StreetRouter> egressRouter = new HashMap<>(request.egressModes.size()); // This map saves which access mode was used to access specific stop in access mode TIntObjectMap<LegMode> stopModeAccessMap = new TIntObjectHashMap<>(); // This map saves which egress mode was used to access specific stop in egress mode TIntObjectMap<LegMode> stopModeEgressMap = new TIntObjectHashMap<>(); findAccessPaths(request, accessRouter); findEgressPaths(request, egressRouter); // fold access and egress times into single maps TIntIntMap accessTimes = combineMultimodalRoutingAccessTimes(accessRouter, stopModeAccessMap, request); TIntIntMap egressTimes = combineMultimodalRoutingAccessTimes(egressRouter, stopModeEgressMap, request); McRaptorSuboptimalPathProfileRouter router = new McRaptorSuboptimalPathProfileRouter( transportNetwork, request, accessTimes, egressTimes); List<PathWithTimes> usefullpathList = new ArrayList<>(); // getPaths actually returns a set, which is important so that things are deduplicated. // However we need a list // so we can sort it below. usefullpathList.addAll(router.getPaths()); // This sort is necessary only for text debug output so it will be disabled when it is // finished /** * Orders first no transfers then one transfers 2 etc - then orders according to first trip: - * board stop - alight stop - alight time - same for one transfer trip */ usefullpathList.sort( (o1, o2) -> { int c; c = Integer.compare(o1.patterns.length, o2.patterns.length); if (c == 0) { c = Integer.compare(o1.boardStops[0], o2.boardStops[0]); } if (c == 0) { c = Integer.compare(o1.alightStops[0], o2.alightStops[0]); } if (c == 0) { c = Integer.compare(o1.alightTimes[0], o2.alightTimes[0]); } if (c == 0 && o1.patterns.length == 2) { c = Integer.compare(o1.boardStops[1], o2.boardStops[1]); if (c == 0) { c = Integer.compare(o1.alightStops[1], o2.alightStops[1]); } if (c == 0) { c = Integer.compare(o1.alightTimes[1], o2.alightTimes[1]); } } return c; }); LOG.info("Usefull paths:{}", usefullpathList.size()); int seen_paths = 0; int boardStop = -1, alightStop = -1; for (PathWithTimes path : usefullpathList) { profileResponse.addTransitPath( accessRouter, egressRouter, stopModeAccessMap, stopModeEgressMap, path, transportNetwork, request.getFromTimeDateZD()); // LOG.info("Num patterns:{}", path.patterns.length); // ProfileOption transit_option = new ProfileOption(); /*if (path.patterns.length == 1) { continue; }*/ /*if (seen_paths > 20) { break; }*/ if (LOG.isDebugEnabled()) { LOG.debug(" "); for (int i = 0; i < path.patterns.length; i++) { // TransitSegment transitSegment = new TransitSegment(transportNetwork.transitLayer, // path.boardStops[i], path.alightStops[i], path.patterns[i]); if (!(((boardStop == path.boardStops[i] && alightStop == path.alightStops[i])))) { LOG.debug( " BoardStop: {} pattern: {} allightStop: {}", path.boardStops[i], path.patterns[i], path.alightStops[i]); } TripPattern pattern = transportNetwork.transitLayer.tripPatterns.get(path.patterns[i]); if (pattern.routeIndex >= 0) { RouteInfo routeInfo = transportNetwork.transitLayer.routes.get(pattern.routeIndex); LOG.debug( " Pattern:{} on route:{} ({}) with {} stops", path.patterns[i], routeInfo.route_long_name, routeInfo.route_short_name, pattern.stops.length); } LOG.debug( " {}->{} ({}:{})", transportNetwork.transitLayer.stopNames.get(path.boardStops[i]), transportNetwork.transitLayer.stopNames.get(path.alightStops[i]), path.alightTimes[i] / 3600, path.alightTimes[i] % 3600 / 60); // transit_option.addTransit(transitSegment); } boardStop = path.boardStops[0]; alightStop = path.alightStops[0]; } seen_paths++; } profileResponse.generateStreetTransfers(transportNetwork, request); } LOG.info("Returned {} options", profileResponse.getOptions().size()); LOG.info("Took {} ms", System.currentTimeMillis() - startRouting); return profileResponse; }