diff --git a/connection_scan_algorithm/src/calculator.cpp b/connection_scan_algorithm/src/calculator.cpp index c38f54ba..43c014a9 100644 --- a/connection_scan_algorithm/src/calculator.cpp +++ b/connection_scan_algorithm/src/calculator.cpp @@ -28,15 +28,55 @@ namespace TrRouting std::cerr << "-- forward calculation -- " << algorithmCalculationTime.getDurationMicrosecondsNoStop() - calculationTime << " microseconds\n"; calculationTime = algorithmCalculationTime.getDurationMicrosecondsNoStop(); - - result = forwardJourney(bestArrivalTime, bestEgressStopIndex, bestEgressTravelTime); - - std::cerr << "-- forward journey -- " << algorithmCalculationTime.getDurationMicrosecondsNoStop() - calculationTime << " microseconds\n"; - calculationTime = algorithmCalculationTime.getDurationMicrosecondsNoStop(); - + + if (params.returnAllStopsResult) + { + result = forwardJourney(bestArrivalTime, bestEgressStopIndex, bestEgressTravelTime); + + std::cerr << "-- forward journey -- " << algorithmCalculationTime.getDurationMicrosecondsNoStop() - calculationTime << " microseconds\n"; + calculationTime = algorithmCalculationTime.getDurationMicrosecondsNoStop(); + } + else + { + + if (bestArrivalTime < MAX_INT) + { + departureTimeSeconds = -1; + arrivalTimeSeconds = bestArrivalTime; + + for (auto & egressFootpath : egressFootpaths) // reset stops reverse tentative times with new arrival time: + { + stopsReverseTentativeTime[egressFootpath.first] = arrivalTimeSeconds - egressFootpath.second; + } + + std::tie(bestDepartureTime, bestAccessStopIndex, bestAccessTravelTime) = reverseCalculation(); + + std::cerr << "-- reverse calculation -- " << algorithmCalculationTime.getDurationMicrosecondsNoStop() - calculationTime << " microseconds\n"; + calculationTime = algorithmCalculationTime.getDurationMicrosecondsNoStop(); + + result = reverseJourney(bestDepartureTime, bestAccessStopIndex, bestAccessTravelTime); + + std::cerr << "-- reverse journey -- " << algorithmCalculationTime.getDurationMicrosecondsNoStop() - calculationTime << " microseconds\n"; + calculationTime = algorithmCalculationTime.getDurationMicrosecondsNoStop(); + + } + else + { + + result = forwardJourney(bestArrivalTime, bestEgressStopIndex, bestEgressTravelTime); + + std::cerr << "-- forward journey -- " << algorithmCalculationTime.getDurationMicrosecondsNoStop() - calculationTime << " microseconds\n"; + calculationTime = algorithmCalculationTime.getDurationMicrosecondsNoStop(); + + } + + } } else if (arrivalTimeSeconds > -1) { + + std::fill(tripsUsable.begin(), tripsUsable.end(), 1); // we need to make all trips usable when not coming from forward result because reverse calculation, by default, checks for usableTrips == 1 + std::tie(bestDepartureTime, bestAccessStopIndex, bestAccessTravelTime) = reverseCalculation(); std::cerr << "-- reverse calculation -- " << algorithmCalculationTime.getDurationMicrosecondsNoStop() - calculationTime << " microseconds\n"; @@ -49,15 +89,6 @@ namespace TrRouting } - //if (!params.returnAllStopsResult && bestArrivalTime < MAX_INT && bestEgressStopIndex != -1) - //{ - // arrivalTimeSeconds = bestArrivalTime; - // - // - // Calculator::reverseCalculation(); - // - //} - return result; } diff --git a/connection_scan_algorithm/src/forward_calculation.cpp b/connection_scan_algorithm/src/forward_calculation.cpp index a1efd64b..94b96d46 100644 --- a/connection_scan_algorithm/src/forward_calculation.cpp +++ b/connection_scan_algorithm/src/forward_calculation.cpp @@ -130,7 +130,7 @@ namespace TrRouting { egressTravelTime = stopsEgressTravelTime[egressFootpath.first]; egressStopArrivalTime = std::get(forwardConnections[egressExitConnection]) + egressTravelTime; - std::cerr << stops[egressFootpath.first].name << ": " << egressTravelTime << " - " << Toolbox::convertSecondsToFormattedTime(egressStopArrivalTime) << std::endl; + //std::cerr << stops[egressFootpath.first].name << ": " << egressTravelTime << " - " << Toolbox::convertSecondsToFormattedTime(egressStopArrivalTime) << std::endl; if (egressStopArrivalTime >= 0 && egressStopArrivalTime < MAX_INT && egressStopArrivalTime < bestArrivalTime) { bestArrivalTime = egressStopArrivalTime; @@ -149,4 +149,4 @@ namespace TrRouting } -} \ No newline at end of file +} diff --git a/connection_scan_algorithm/src/forward_journey.cpp b/connection_scan_algorithm/src/forward_journey.cpp index 6e66ac15..71f9ee5d 100644 --- a/connection_scan_algorithm/src/forward_journey.cpp +++ b/connection_scan_algorithm/src/forward_journey.cpp @@ -35,6 +35,9 @@ namespace TrRouting std::tuple emptyJourneyStep {-1,-1,-1,-1,-1,-1}; std::tuple journeyStepEnterConnection; // connection tuple: departureStopIndex, arrivalStopIndex, departureTimeSeconds, arrivalTimeSeconds, tripIndex, canBoard, canUnboard, sequenceinTrip std::tuple journeyStepExitConnection; + std::vector routeIds; + std::vector routeTypeIds; + std::vector agencyIds; std::vector> legs; // tuple: tripId, routeId, routePathId, boarding sequence, unboarding sequence nlohmann::json stepJson = {}; nlohmann::json stopJson = {}; @@ -58,6 +61,8 @@ namespace TrRouting legs.clear(); journey.clear(); + routeIds.clear(); + routeTypeIds.clear(); totalInVehicleTime = 0; transferArrivalTime = -1; firstDepartureTime = -1; totalWalkingTime = 0; transferReadyTime = -1; minimizedDepartureTime = -1; @@ -123,6 +128,9 @@ namespace TrRouting totalInVehicleTime += inVehicleTime; totalWaitingTime += waitingTime; numberOfTransfers += 1; + routeIds.push_back(journeyStepRoute.id); + routeTypeIds.push_back(journeyStepRoute.routeTypeId); + agencyIds.push_back(journeyStepRoute.agencyId); legs.push_back(std::make_tuple(journeyStepTrip.id, journeyStepTrip.routeId, journeyStepTrip.routePathId, boardingSequence, unboardingSequence)); if (i == 1) // first leg @@ -254,16 +262,19 @@ namespace TrRouting if (params.returnAllStopsResult) { - arrivalTime = stopsTentativeTime[resultingStopIndex] - params.minWaitingTimeSeconds; - if (arrivalTime - departureTimeSeconds <= params.maxTotalTravelTimeSeconds) + if (std::get<0>(forwardEgressJourneys[resultingStopIndex]) != -1) { - reachableStopsCount++; - stopJson = {}; - stopJson["id"] = stops[resultingStopIndex].id; - stopJson["arrivalTime"] = Toolbox::convertSecondsToFormattedTime(arrivalTime); - stopJson["totalTravelTimeSeconds"] = arrivalTime - departureTimeSeconds; - stopJson["numberOfTransfers"] = numberOfTransfers; - json["stops"].push_back(stopJson); + arrivalTime = std::get(forwardConnections[std::get<1>(forwardEgressJourneys[resultingStopIndex])]); + if (arrivalTime - departureTimeSeconds <= params.maxTotalTravelTimeSeconds) + { + reachableStopsCount++; + stopJson = {}; + stopJson["id"] = stops[resultingStopIndex].id; + stopJson["arrivalTime"] = Toolbox::convertSecondsToFormattedTime(arrivalTime); + stopJson["totalTravelTimeSeconds"] = arrivalTime - departureTimeSeconds; + stopJson["numberOfTransfers"] = numberOfTransfers; + json["stops"].push_back(stopJson); + } } } else if (!params.returnAllStopsResult) @@ -304,12 +315,23 @@ namespace TrRouting json["minimumWaitingTimeBeforeEachBoardingMinutes"] = Toolbox::convertSecondsToMinutes(params.minWaitingTimeSeconds); json["minimumWaitingTimeBeforeEachBoardingSeconds"] = params.minWaitingTimeSeconds; - result.travelTimeSeconds = arrivalTime - departureTimeSeconds; - result.arrivalTimeSeconds = arrivalTime; - result.departureTimeSeconds = departureTimeSeconds; - result.numberOfTransfers = numberOfTransfers; - result.legs = legs; - result.status = "success"; + result.travelTimeSeconds = arrivalTime - departureTimeSeconds; + result.arrivalTimeSeconds = arrivalTime; + result.departureTimeSeconds = departureTimeSeconds; + result.numberOfTransfers = numberOfTransfers; + result.inVehicleTravelTimeSeconds = totalInVehicleTime; + result.transferTravelTimeSeconds = totalTransferWalkingTime; + result.waitingTimeSeconds = totalWaitingTime; + result.accessTravelTimeSeconds = accessWalkingTime; + result.egressTravelTimeSeconds = egressWalkingTime; + result.transferWaitingTimeSeconds = totalTransferWaitingTime; + result.firstWaitingTimeSeconds = accessWaitingTime; + result.nonTransitTravelTimeSeconds = totalWalkingTime; + result.legs = legs; + result.routeIds = routeIds; + result.routeTypeIds = routeTypeIds; + result.agencyIds = agencyIds; + result.status = "success"; } } @@ -317,13 +339,24 @@ namespace TrRouting } else // no route found { - json["status"] = "no_routing_found"; - json["origin"] = { params.origin.longitude, params.origin.latitude }; - json["destination"] = { params.destination.longitude, params.destination.latitude }; - json["departureTime"] = Toolbox::convertSecondsToFormattedTime(departureTimeSeconds); - json["departureTimeSeconds"] = departureTimeSeconds; - result.status = "no_routing_found"; - result.travelTimeSeconds = -1; + json["status"] = "no_routing_found"; + json["origin"] = { params.origin.longitude, params.origin.latitude }; + json["destination"] = { params.destination.longitude, params.destination.latitude }; + json["departureTime"] = Toolbox::convertSecondsToFormattedTime(departureTimeSeconds); + json["departureTimeSeconds"] = departureTimeSeconds; + result.status = "no_routing_found"; + result.travelTimeSeconds = -1; + result.arrivalTimeSeconds = -1; + result.departureTimeSeconds = departureTimeSeconds; + result.numberOfTransfers = -1; + result.inVehicleTravelTimeSeconds = -1; + result.transferTravelTimeSeconds = -1; + result.waitingTimeSeconds = -1; + result.accessTravelTimeSeconds = -1; + result.egressTravelTimeSeconds = -1; + result.transferWaitingTimeSeconds = -1; + result.firstWaitingTimeSeconds = -1; + result.nonTransitTravelTimeSeconds = -1; } if (params.returnAllStopsResult) @@ -337,4 +370,4 @@ namespace TrRouting } -} \ No newline at end of file +} diff --git a/connection_scan_algorithm/src/resets.cpp b/connection_scan_algorithm/src/resets.cpp index 764720b8..d694aa3b 100644 --- a/connection_scan_algorithm/src/resets.cpp +++ b/connection_scan_algorithm/src/resets.cpp @@ -7,7 +7,7 @@ namespace TrRouting { calculationTime = algorithmCalculationTime.getDurationMicrosecondsNoStop(); - + std::fill(stopsTentativeTime.begin(), stopsTentativeTime.end(), MAX_INT); std::fill(stopsReverseTentativeTime.begin(), stopsReverseTentativeTime.end(), -1); //std::fill(stopsD.begin(), stopsD.end(), MAX_INT); @@ -20,18 +20,18 @@ namespace TrRouting std::fill(tripsExitConnectionTransferTravelTime.begin(), tripsExitConnectionTransferTravelTime.end(), MAX_INT); //std::fill(tripsReverseTime.begin(), tripsReverseTime.end(), MAX_INT); std::fill(tripsEnabled.begin(), tripsEnabled.end(), 1); - std::fill(tripsUsable.begin(), tripsUsable.end(), 0); + std::fill(tripsUsable.begin(), tripsUsable.end(), -1); std::fill(forwardJourneys.begin(), forwardJourneys.end(), std::make_tuple(-1,-1,-1,-1,-1,-1)); std::fill(forwardEgressJourneys.begin(), forwardEgressJourneys.end(), std::make_tuple(-1,-1,-1,-1,-1,-1)); std::fill(reverseJourneys.begin(), reverseJourneys.end(), std::make_tuple(-1,-1,-1,-1,-1,-1)); std::fill(reverseAccessJourneys.begin(), reverseAccessJourneys.end(), std::make_tuple(-1,-1,-1,-1,-1,-1)); - + accessFootpaths.clear(); egressFootpaths.clear(); departureTimeSeconds = -1; arrivalTimeSeconds = -1; - + if(params.odTrip != NULL) { departureTimeSeconds = params.odTrip->departureTimeSeconds; diff --git a/connection_scan_algorithm/src/reverse_calculation.cpp b/connection_scan_algorithm/src/reverse_calculation.cpp index 21b095a7..1e166a5f 100644 --- a/connection_scan_algorithm/src/reverse_calculation.cpp +++ b/connection_scan_algorithm/src/reverse_calculation.cpp @@ -40,11 +40,13 @@ namespace TrRouting // ignore connections before departure time + minimum access travel time: if (std::get(connection) <= arrivalTimeSeconds - minEgressTravelTime) { + tripIndex = std::get(connection); // enabled trips only here: - if (/*tripsUsable[tripIndex] != -1 && */tripsEnabled[tripIndex] != -1) + if (tripsUsable[tripIndex] == 1 && tripsEnabled[tripIndex] != -1) { + connectionArrivalTime = std::get(connection); // no need to parse next connections if already reached destination from all egress stops, except if max travel time is set, so we can get a reverse profile in the next loop calculation: @@ -56,6 +58,8 @@ namespace TrRouting stopArrivalIndex = std::get(connection); stopArrivalTentativeTime = stopsReverseTentativeTime[stopArrivalIndex]; + //std::cerr << "stopArrivalTentativeTime: " << stopArrivalTentativeTime << " connectionArrivalTime: " << connectionArrivalTime << " tripExitConnectionIndex: " << tripExitConnectionIndex << std::endl; + // reachable connections only here: if (tripExitConnectionIndex != -1 || stopArrivalTentativeTime >= connectionArrivalTime) { @@ -108,7 +112,10 @@ namespace TrRouting } i++; } + + std::cerr << "-- " << reachableConnectionsCount << " reverse connections parsed on " << connectionsCount << std::endl; + int accessStopDepartureTime {-1}; int accessEnterConnection {-1}; int accessTravelTime {-1}; @@ -123,7 +130,7 @@ namespace TrRouting { accessTravelTime = stopsAccessTravelTime[accessFootpath.first]; accessStopDepartureTime = std::get(reverseConnections[accessEnterConnection]) - accessTravelTime - params.minWaitingTimeSeconds; - std::cerr << stops[accessFootpath.first].name << ": " << accessTravelTime << " t: " << trips[std::get(reverseConnections[accessEnterConnection])].id << " - " << Toolbox::convertSecondsToFormattedTime(accessStopDepartureTime) << std::endl; + //std::cerr << stops[accessFootpath.first].name << ": " << accessTravelTime << " t: " << trips[std::get(reverseConnections[accessEnterConnection])].id << " - " << Toolbox::convertSecondsToFormattedTime(accessStopDepartureTime) << std::endl; if (accessStopDepartureTime >= 0 && accessStopDepartureTime < MAX_INT && accessStopDepartureTime > bestDepartureTime) { bestDepartureTime = accessStopDepartureTime; @@ -143,4 +150,4 @@ namespace TrRouting } -} \ No newline at end of file +} diff --git a/connection_scan_algorithm/src/reverse_journey.cpp b/connection_scan_algorithm/src/reverse_journey.cpp index b48bc746..fc15541c 100644 --- a/connection_scan_algorithm/src/reverse_journey.cpp +++ b/connection_scan_algorithm/src/reverse_journey.cpp @@ -12,9 +12,6 @@ namespace TrRouting int i {0}; int reachableStopsCount {0}; bool foundRoute {false}; - - - //return result; std::vector resultingStops; if (params.returnAllStopsResult) @@ -38,6 +35,9 @@ namespace TrRouting std::tuple emptyJourneyStep {-1,-1,-1,-1,-1,-1}; std::tuple journeyStepEnterConnection; // connection tuple: departureStopIndex, arrivalStopIndex, departureTimeSeconds, arrivalTimeSeconds, tripIndex, canBoard, canUnboard, sequenceinTrip std::tuple journeyStepExitConnection; + std::vector routeIds; + std::vector routeTypeIds; + std::vector agencyIds; std::vector> legs; // tuple: tripId, routeId, routePathId, boarding sequence, unboarding sequence nlohmann::json stepJson = {}; nlohmann::json stopJson = {}; @@ -47,31 +47,33 @@ namespace TrRouting Trip journeyStepTrip; Route journeyStepRoute; - int totalInVehicleTime { 0}; int transferArrivalTime {-1}; int firstDepartureTime {-1}; - int totalWalkingTime { 0}; int transferReadyTime {-1}; int numberOfTransfers {-1}; - int totalWaitingTime { 0}; int departureTime {-1}; int boardingSequence {-1}; - int totalTransferWalkingTime { 0}; int arrivalTime {-1}; int unboardingSequence {-1}; - int totalTransferWaitingTime { 0}; int inVehicleTime {-1}; int bestEgressStopIndex {-1}; - int journeyStepTravelTime {-1}; int accessWalkingTime {-1}; - int transferTime {-1}; int egressWalkingTime {-1}; - int waitingTime {-1}; int accessWaitingTime {-1}; + int totalInVehicleTime { 0}; int transferArrivalTime {-1}; int firstDepartureTime {-1}; + int totalWalkingTime { 0}; int transferReadyTime {-1}; int numberOfTransfers {-1}; + int totalWaitingTime { 0}; int departureTime {-1}; int boardingSequence {-1}; + int totalTransferWalkingTime { 0}; int arrivalTime {-1}; int unboardingSequence {-1}; + int totalTransferWaitingTime { 0}; int inVehicleTime {-1}; int bestEgressStopIndex {-1}; + int journeyStepTravelTime {-1}; int accessWalkingTime {-1}; + int transferTime {-1}; int egressWalkingTime {-1}; + int waitingTime {-1}; int accessWaitingTime {-1}; for (auto & resultingStopIndex : resultingStops) { legs.clear(); journey.clear(); + routeIds.clear(); + routeTypeIds.clear(); - totalInVehicleTime = 0; transferArrivalTime = -1; firstDepartureTime = -1; - totalWalkingTime = 0; transferReadyTime = -1; numberOfTransfers = -1; - totalWaitingTime = 0; departureTime = -1; boardingSequence = -1; - totalTransferWalkingTime = 0; arrivalTime = -1; unboardingSequence = -1; - totalTransferWaitingTime = 0; inVehicleTime = -1; bestEgressStopIndex = -1; - journeyStepTravelTime = -1; accessWalkingTime = -1; - transferTime = -1; egressWalkingTime = -1; - waitingTime = -1; accessWaitingTime = -1; + totalInVehicleTime = 0; transferArrivalTime = -1; firstDepartureTime = -1; + totalWalkingTime = 0; transferReadyTime = -1; numberOfTransfers = -1; + totalWaitingTime = 0; departureTime = -1; boardingSequence = -1; + totalTransferWalkingTime = 0; arrivalTime = -1; unboardingSequence = -1; + totalTransferWaitingTime = 0; inVehicleTime = -1; bestEgressStopIndex = -1; + journeyStepTravelTime = -1; accessWalkingTime = -1; + transferTime = -1; egressWalkingTime = -1; + waitingTime = -1; accessWaitingTime = -1; - std::cerr << stops[resultingStopIndex].name << " : " << std::get<0>(reverseAccessJourneys[resultingStopIndex]) << " tt: " << std::get<4>(reverseAccessJourneys[resultingStopIndex]) << std::endl; + //std::cerr << stops[resultingStopIndex].name << " : " << std::get<0>(reverseAccessJourneys[resultingStopIndex]) << " tt: " << std::get<4>(reverseAccessJourneys[resultingStopIndex]) << std::endl; // recreate journey: resultingStopJourneyStep = reverseAccessJourneys[resultingStopIndex]; @@ -89,11 +91,12 @@ namespace TrRouting } journey.push_back(resultingStopJourneyStep); bestEgressStopIndex = std::get(reverseConnections[std::get<1>(resultingStopJourneyStep)]); - std::cerr << stops[std::get(reverseConnections[std::get<0>(resultingStopJourneyStep)])].name << " tt: " << std::get<4>(resultingStopJourneyStep) << " > " << stops[std::get(reverseConnections[std::get<1>(resultingStopJourneyStep)])].name << std::endl; + //std::cerr << stops[std::get(reverseConnections[std::get<0>(resultingStopJourneyStep)])].name << " tt: " << std::get<4>(resultingStopJourneyStep) << " > " << stops[std::get(reverseConnections[std::get<1>(resultingStopJourneyStep)])].name << std::endl; resultingStopJourneyStep = reverseJourneys[bestEgressStopIndex]; i++; } + if (!params.returnAllStopsResult) { json["steps"] = nlohmann::json::array(); @@ -129,6 +132,9 @@ namespace TrRouting totalInVehicleTime += inVehicleTime; totalWaitingTime += waitingTime; numberOfTransfers += 1; + routeIds.push_back(journeyStepRoute.id); + routeTypeIds.push_back(journeyStepRoute.routeTypeId); + agencyIds.push_back(journeyStepRoute.agencyId); legs.push_back(std::make_tuple(journeyStepTrip.id, journeyStepTrip.routeId, journeyStepTrip.routePathId, boardingSequence, unboardingSequence)); if (i == 1) // first leg @@ -258,16 +264,19 @@ namespace TrRouting if (params.returnAllStopsResult) { - arrivalTime = stopsTentativeTime[resultingStopIndex] - params.minWaitingTimeSeconds; - if (arrivalTime - departureTimeSeconds <= params.maxTotalTravelTimeSeconds) + if (std::get<0>(reverseAccessJourneys[resultingStopIndex]) != -1) { - reachableStopsCount++; - stopJson = {}; - stopJson["id"] = stops[resultingStopIndex].id; - stopJson["arrivalTime"] = Toolbox::convertSecondsToFormattedTime(arrivalTime); - stopJson["totalTravelTimeSeconds"] = arrivalTime - departureTimeSeconds; - stopJson["numberOfTransfers"] = numberOfTransfers; - json["stops"].push_back(stopJson); + departureTime = std::get(reverseConnections[std::get<0>(reverseAccessJourneys[resultingStopIndex])]) - params.minWaitingTimeSeconds; + if (arrivalTimeSeconds - departureTime <= params.maxTotalTravelTimeSeconds) + { + reachableStopsCount++; + stopJson = {}; + stopJson["id"] = stops[resultingStopIndex].id; + stopJson["departureTime"] = Toolbox::convertSecondsToFormattedTime(departureTime); + stopJson["totalTravelTimeSeconds"] = arrivalTimeSeconds - departureTime; + stopJson["numberOfTransfers"] = numberOfTransfers; + json["stops"].push_back(stopJson); + } } } else if (!params.returnAllStopsResult) @@ -303,12 +312,23 @@ namespace TrRouting json["totalWaitingTimeMinutes"] = Toolbox::convertSecondsToMinutes(totalWaitingTime); json["totalWaitingTimeSeconds"] = totalWaitingTime; - result.travelTimeSeconds = arrivalTime - bestDepartureTime; - result.arrivalTimeSeconds = arrivalTime; - result.departureTimeSeconds = bestDepartureTime; - result.numberOfTransfers = numberOfTransfers; - result.legs = legs; - result.status = "success"; + result.travelTimeSeconds = arrivalTime - bestDepartureTime; + result.arrivalTimeSeconds = arrivalTime; + result.departureTimeSeconds = bestDepartureTime; + result.numberOfTransfers = numberOfTransfers; + result.inVehicleTravelTimeSeconds = totalInVehicleTime; + result.transferTravelTimeSeconds = totalTransferWalkingTime; + result.waitingTimeSeconds = totalWaitingTime; + result.accessTravelTimeSeconds = accessWalkingTime; + result.egressTravelTimeSeconds = egressWalkingTime; + result.transferWaitingTimeSeconds = totalTransferWaitingTime; + result.firstWaitingTimeSeconds = accessWaitingTime; + result.nonTransitTravelTimeSeconds = totalWalkingTime; + result.legs = legs; + result.routeIds = routeIds; + result.routeTypeIds = routeTypeIds; + result.agencyIds = agencyIds; + result.status = "success"; } } @@ -316,13 +336,23 @@ namespace TrRouting } else // no route found { - json["status"] = "no_routing_found"; - json["origin"] = { params.origin.longitude, params.origin.latitude }; - json["destination"] = { params.destination.longitude, params.destination.latitude }; - json["departureTime"] = Toolbox::convertSecondsToFormattedTime(departureTimeSeconds); - json["departureTimeSeconds"] = departureTimeSeconds; - result.status = "no_routing_found"; - result.travelTimeSeconds = -1; + json["status"] = "no_routing_found"; + json["origin"] = { params.origin.longitude, params.origin.latitude }; + json["destination"] = { params.destination.longitude, params.destination.latitude }; + json["arrivalTime"] = Toolbox::convertSecondsToFormattedTime(arrivalTimeSeconds); + json["arrivalTimeSeconds"] = arrivalTimeSeconds; + result.status = "no_routing_found"; + result.travelTimeSeconds = -1; + result.arrivalTimeSeconds = arrivalTimeSeconds; + result.numberOfTransfers = -1; + result.inVehicleTravelTimeSeconds = -1; + result.transferTravelTimeSeconds = -1; + result.waitingTimeSeconds = -1; + result.accessTravelTimeSeconds = -1; + result.egressTravelTimeSeconds = -1; + result.transferWaitingTimeSeconds = -1; + result.firstWaitingTimeSeconds = -1; + result.nonTransitTravelTimeSeconds = -1; } if (params.returnAllStopsResult) @@ -335,4 +365,4 @@ namespace TrRouting return result; } -} \ No newline at end of file +} diff --git a/connection_scan_algorithm/src/transit_routing_http_server.cpp b/connection_scan_algorithm/src/transit_routing_http_server.cpp index 88112ead..7f812bd6 100644 --- a/connection_scan_algorithm/src/transit_routing_http_server.cpp +++ b/connection_scan_algorithm/src/transit_routing_http_server.cpp @@ -668,7 +668,14 @@ int main(int argc, char** argv) { int legBoardingSequence; int legUnboardingSequence; - resultStr += "{\n \"odTrips\": [\n"; + nlohmann::json json; + nlohmann::json odTripJson; + nlohmann::json routesOdTripsCountJson; + nlohmann::json routePathsOdTripsProfilesJson; + nlohmann::json routePathsOdTripsProfilesSequenceJson; + //std::vector routePathsOdTripsProfilesOdTripIds; + + json["odTrips"] = nlohmann::json::array(); int i = 0; for (auto & odTrip : calculator.odTrips) { @@ -694,7 +701,7 @@ int main(int argc, char** argv) { } } - if (attributesMatches && atLeastOneCompatiblePeriod) + if (attributesMatches && (atLeastOneCompatiblePeriod || odTripsPeriods.size() == 0)) { std::cout << "od trip id " << odTrip.id << " (" << (i+1) << "/" << odTripsCount << ")" << std::endl;// << " dts: " << odTrip.departureTimeSeconds << " atLeastOneCompatiblePeriod: " << (atLeastOneCompatiblePeriod ? "true " : "false ") << "attributesMatches: " << (attributesMatches ? "true " : "false ") << std::endl; @@ -756,17 +763,34 @@ int main(int argc, char** argv) { } } } - resultStr += " {\"id\":" + std::to_string(odTrip.id) + ", " - "\"status\": \"" + routingResult.status + "\", " - "\"ageGroup\": \"" + odTrip.ageGroup + "\", " - "\"gender\": \"" + odTrip.gender + "\", " - "\"occupation\": \"" + odTrip.occupation + "\", " - "\"activity\": \"" + odTrip.activity + "\", " - "\"mode\": \"" + odTrip.mode + "\", " - "\"expansionFactor\": " + std::to_string(odTrip.expansionFactor) + ", " - "\"travelTimeSeconds\": " + std::to_string(routingResult.travelTimeSeconds) + ", " - "\"walkingTravelTimeSeconds\": " + std::to_string(odTrip.walkingTravelTimeSeconds) + ", " - "\"numberOfTransfers\": " + std::to_string(routingResult.numberOfTransfers) + "},\n"; + + odTripJson = {}; + odTripJson["id"] = odTrip.id; + odTripJson["status"] = routingResult.status; + odTripJson["ageGroup"] = odTrip.ageGroup; + odTripJson["gender"] = odTrip.gender; + odTripJson["occupation"] = odTrip.occupation; + odTripJson["activity"] = odTrip.activity; + odTripJson["mode"] = odTrip.mode; + odTripJson["expansionFactor"] = odTrip.expansionFactor; + odTripJson["travelTimeSeconds"] = routingResult.travelTimeSeconds; + odTripJson["onlyWalkingTravelTimeSeconds"] = odTrip.walkingTravelTimeSeconds; + odTripJson["departureTimeSeconds"] = routingResult.departureTimeSeconds; + odTripJson["arrivalTimeSeconds"] = routingResult.arrivalTimeSeconds; + odTripJson["numberOfTransfers"] = routingResult.numberOfTransfers; + odTripJson["inVehicleTravelTimeSeconds"] = routingResult.inVehicleTravelTimeSeconds; + odTripJson["transferTravelTimeSeconds"] = routingResult.transferTravelTimeSeconds; + odTripJson["waitingTimeSeconds"] = routingResult.waitingTimeSeconds; + odTripJson["accessTravelTimeSeconds"] = routingResult.accessTravelTimeSeconds; + odTripJson["egressTravelTimeSeconds"] = routingResult.egressTravelTimeSeconds; + odTripJson["transferWaitingTimeSeconds"] = routingResult.transferWaitingTimeSeconds; + odTripJson["firstWaitingTimeSeconds"] = routingResult.firstWaitingTimeSeconds; + odTripJson["nonTransitTravelTimeSeconds"] = routingResult.nonTransitTravelTimeSeconds; + odTripJson["routeIds"] = routingResult.routeIds; + odTripJson["routeTypeIds"] = routingResult.routeTypeIds; + odTripJson["agencyIds"] = routingResult.agencyIds; + json["odTrips"].push_back(odTripJson); + } } i++; @@ -775,51 +799,31 @@ int main(int argc, char** argv) { break; } } - - if (atLeastOneOdTrip) - { - resultStr.pop_back(); - } - - resultStr.pop_back(); - resultStr += "\n ],\n \"routesOdTripsCount\": {\n"; + routesOdTripsCountJson = {}; for (auto & routeCount : routesOdTripsCount) { - resultStr += " \"" + std::to_string(routeCount.first) + "\": " + std::to_string(routeCount.second) + ",\n"; + routesOdTripsCountJson[std::to_string(routeCount.first)] = routeCount.second; } - if (routesOdTripsCount.size() > 0) - { - resultStr.pop_back(); - resultStr.pop_back(); - } - resultStr += "\n },\n \"routePathsOdTripsProfiles\": {\n"; + json["routesOdTripsCount"] = routesOdTripsCountJson; + + routePathsOdTripsProfilesJson = {}; for (auto & routePathProfile : routePathsLegsProfile) { - resultStr += " \"" + std::to_string(routePathProfile.first) + "\": {"; - + routePathsOdTripsProfilesSequenceJson = {}; for (auto & sequenceProfile : routePathProfile.second) { - resultStr += " \"" + std::to_string(sequenceProfile.first) + "\": { \"demand\": " + std::to_string(std::get<0>(sequenceProfile.second)) + ", \"odTripIds\": ["; - - for (auto & odTripId : std::get<1>(sequenceProfile.second)) - { - resultStr += std::to_string(odTripId) + ","; - } - resultStr.pop_back(); - resultStr += "]},"; - } - if (routePathProfile.second.size() > 0) - { - resultStr.pop_back(); + //routePathsOdTripsProfilesOdTripIds.clear(); + //for (auto & odTripId : std::get<1>(sequenceProfile.second)) + //{ + // routePathsOdTripsProfilesOdTripIds.push_back() + //} + routePathsOdTripsProfilesSequenceJson[std::to_string(sequenceProfile.first)] = {{"demand", std::get<0>(sequenceProfile.second)}, {"odTripIds", std::get<1>(sequenceProfile.second)}}; } - resultStr += "},\n"; - } - if (routePathsLegsProfile.size() > 0) - { - resultStr.pop_back(); - resultStr.pop_back(); + routePathsOdTripsProfilesJson[std::to_string(routePathProfile.first)] = routePathsOdTripsProfilesSequenceJson; } - resultStr += "\n }\n}"; + json["routePathsOdTripsProfiles"] = routePathsOdTripsProfilesJson; + + resultStr = json.dump(2); } else diff --git a/include/routing_result.hpp b/include/routing_result.hpp index 73c2fc33..dedeb21e 100644 --- a/include/routing_result.hpp +++ b/include/routing_result.hpp @@ -11,9 +11,20 @@ namespace TrRouting int arrivalTimeSeconds; int departureTimeSeconds; int numberOfTransfers; + int inVehicleTravelTimeSeconds; + int transferTravelTimeSeconds; + int waitingTimeSeconds; + int accessTravelTimeSeconds; + int egressTravelTimeSeconds; + int transferWaitingTimeSeconds; + int firstWaitingTimeSeconds; + int nonTransitTravelTimeSeconds; int calculationTimeMilliseconds; std::string status; std::string json; + std::vector routeIds; + std::vector routeTypeIds; + std::vector agencyIds; std::vector> legs; // tuple: tripId, routeId, routePathId, boarding sequence, unboarding sequence };