diff --git a/all/modules/routing/RouteMatchingResult.i b/all/modules/routing/RouteMatchingResult.i index 72ccd8384..ee7aed0b5 100644 --- a/all/modules/routing/RouteMatchingResult.i +++ b/all/modules/routing/RouteMatchingResult.i @@ -29,6 +29,7 @@ %attributeval(carto::RouteMatchingResult, std::vector, Points, getPoints) %attributeval(carto::RouteMatchingResult, std::vector, MatchingEdges, getMatchingEdges) %attributeval(carto::RouteMatchingResult, std::vector, MatchingPoints, getMatchingPoints) +%attributestring(carto::RouteMatchingResult, std::string, RawResult, getRawResult) %std_exceptions(carto::RouteMatchingResult::RouteMatchingResult) !standard_equals(carto::RouteMatchingResult); !custom_tostring(carto::RouteMatchingResult); diff --git a/all/modules/routing/RoutingResult.i b/all/modules/routing/RoutingResult.i index 97c072fee..06353b4b8 100644 --- a/all/modules/routing/RoutingResult.i +++ b/all/modules/routing/RoutingResult.i @@ -29,6 +29,7 @@ %attributeval(carto::RoutingResult, std::vector, Instructions, getInstructions) %attribute(carto::RoutingResult, double, TotalDistance, getTotalDistance) %attribute(carto::RoutingResult, double, TotalTime, getTotalTime) +%attributestring(carto::RoutingResult, std::string, RawResult, getRawResult) %std_exceptions(carto::RoutingResult::RoutingResult) !standard_equals(carto::RoutingResult); !custom_tostring(carto::RoutingResult); diff --git a/all/native/routing/RouteMatchingResult.cpp b/all/native/routing/RouteMatchingResult.cpp index fb285564a..2b64bae74 100644 --- a/all/native/routing/RouteMatchingResult.cpp +++ b/all/native/routing/RouteMatchingResult.cpp @@ -7,8 +7,9 @@ namespace carto { - RouteMatchingResult::RouteMatchingResult(const std::shared_ptr& projection, std::vector matchingPoints, std::vector matchingEdges) : + RouteMatchingResult::RouteMatchingResult(const std::shared_ptr& projection, std::vector matchingPoints, std::vector matchingEdges, std::string rawResult) : _projection(projection), + _rawResult(rawResult), _matchingPoints(std::move(matchingPoints)), _matchingEdges(std::move(matchingEdges)) { @@ -41,6 +42,10 @@ namespace carto { return _matchingPoints; } + const std::string& RouteMatchingResult::getRawResult() const { + return _rawResult; + } + std::string RouteMatchingResult::toString() const { std::stringstream ss; ss << "RouteMatchingResult [matchingPoints=["; diff --git a/all/native/routing/RouteMatchingResult.h b/all/native/routing/RouteMatchingResult.h index 733e8ed0b..451956c03 100644 --- a/all/native/routing/RouteMatchingResult.h +++ b/all/native/routing/RouteMatchingResult.h @@ -30,7 +30,7 @@ namespace carto { * @param matchingPoints The matched points corresponding to the requested points snapped to road network. * @param matchingEdges The matched edges that are referenced through matching points. */ - RouteMatchingResult(const std::shared_ptr& projection, std::vector matchingPoints, std::vector matchingEdges); + RouteMatchingResult(const std::shared_ptr& projection, std::vector matchingPoints, std::vector matchingEdges, std::string rawResult); virtual ~RouteMatchingResult(); /** @@ -53,6 +53,10 @@ namespace carto { * @return The list with details of the matched points. */ const std::vector& getMatchingPoints() const; + /** + * Returns raw result + */ + const std::string& getRawResult() const; /** * Creates a string representation of this result object, useful for logging. @@ -64,6 +68,7 @@ namespace carto { std::shared_ptr _projection; std::vector _matchingPoints; std::vector _matchingEdges; + std::string _rawResult; }; } diff --git a/all/native/routing/RoutingResult.cpp b/all/native/routing/RoutingResult.cpp index 5946a9a43..cb4c6938f 100644 --- a/all/native/routing/RoutingResult.cpp +++ b/all/native/routing/RoutingResult.cpp @@ -11,8 +11,9 @@ namespace carto { - RoutingResult::RoutingResult(const std::shared_ptr& projection, std::vector points, std::vector instructions) : + RoutingResult::RoutingResult(const std::shared_ptr& projection, std::vector points, std::vector instructions, const std::string rawResult) : _projection(projection), + _rawResult(rawResult), _points(std::move(points)), _instructions(std::move(instructions)) { @@ -48,6 +49,10 @@ namespace carto { }); } + const std::string& RoutingResult::getRawResult() const { + return _rawResult; + } + std::string RoutingResult::toString() const { std::stringstream ss; ss << std::setiosflags(std::ios::fixed); diff --git a/all/native/routing/RoutingResult.h b/all/native/routing/RoutingResult.h index 3a40ebe08..795fe6acc 100644 --- a/all/native/routing/RoutingResult.h +++ b/all/native/routing/RoutingResult.h @@ -29,7 +29,7 @@ namespace carto { * @param points The point list defining the routing path. Instructions refer to this list. * @param instructions The turn-by-turn instruction list. */ - RoutingResult(const std::shared_ptr& projection, std::vector points, std::vector instructions); + RoutingResult(const std::shared_ptr& projection, std::vector points, std::vector instructions, const std::string rawResult); virtual ~RoutingResult(); /** @@ -58,6 +58,10 @@ namespace carto { * @return The total duration in seconds. */ double getTotalTime() const; + /** + * Returns raw result + */ + const std::string& getRawResult() const; /** * Creates a string representation of this result object, useful for logging. @@ -69,6 +73,7 @@ namespace carto { std::shared_ptr _projection; std::vector _points; std::vector _instructions; + std::string _rawResult; }; } diff --git a/all/native/routing/SGREOfflineRoutingService.cpp b/all/native/routing/SGREOfflineRoutingService.cpp index 737ab7196..08fa27ecf 100644 --- a/all/native/routing/SGREOfflineRoutingService.cpp +++ b/all/native/routing/SGREOfflineRoutingService.cpp @@ -139,7 +139,7 @@ namespace carto { results.push_back(std::move(result)); } - RoutingResultBuilder resultBuilder(proj); + RoutingResultBuilder resultBuilder(proj, nullptr); for (std::size_t i = 0; i < results.size(); i++) { const sgre::Result& result = results[i]; if (result.getInstructions().empty()) { diff --git a/all/native/routing/utils/OSRMRoutingProxy.cpp b/all/native/routing/utils/OSRMRoutingProxy.cpp index 4b50fe926..e3ec599d2 100644 --- a/all/native/routing/utils/OSRMRoutingProxy.cpp +++ b/all/native/routing/utils/OSRMRoutingProxy.cpp @@ -43,7 +43,7 @@ namespace carto { results.push_back(std::move(result)); } - RoutingResultBuilder resultBuilder(proj); + RoutingResultBuilder resultBuilder(proj, nullptr); for (std::size_t i = 0; i < results.size(); i++) { const osrm::Result& result = results[i]; if (result.getInstructions().empty()) { @@ -116,7 +116,7 @@ namespace carto { throw GenericException("Routing failed", responseDoc["status_message"].GetString()); } - RoutingResultBuilder resultBuilder(proj); + RoutingResultBuilder resultBuilder(proj, json); std::vector wgs84Points = DecodeGeometry(responseDoc["route_geometry"].GetString()); std::vector points; diff --git a/all/native/routing/utils/RoutingResultBuilder.cpp b/all/native/routing/utils/RoutingResultBuilder.cpp index 1bfe5a704..cb5087912 100644 --- a/all/native/routing/utils/RoutingResultBuilder.cpp +++ b/all/native/routing/utils/RoutingResultBuilder.cpp @@ -12,8 +12,9 @@ namespace carto { - RoutingResultBuilder::RoutingResultBuilder(const std::shared_ptr& proj) : + RoutingResultBuilder::RoutingResultBuilder(const std::shared_ptr& proj, const std::string& rawResult) : _projection(proj), + _rawResult(rawResult), _points(), _instructions() { @@ -57,7 +58,7 @@ namespace carto { } instructions.push_back(instrBuilder.buildRoutingInstruction()); } - return std::make_shared(_projection, _points, std::move(instructions)); + return std::make_shared(_projection, _points, std::move(instructions), _rawResult); } float RoutingResultBuilder::calculateTurnAngle(int pointIndex) const { diff --git a/all/native/routing/utils/RoutingResultBuilder.h b/all/native/routing/utils/RoutingResultBuilder.h index 7b62de926..7c1efd694 100644 --- a/all/native/routing/utils/RoutingResultBuilder.h +++ b/all/native/routing/utils/RoutingResultBuilder.h @@ -23,7 +23,7 @@ namespace carto { class RoutingResultBuilder { public: - explicit RoutingResultBuilder(const std::shared_ptr& proj); + explicit RoutingResultBuilder(const std::shared_ptr& proj, const std::string& rawResult); int addPoints(const std::vector& points); @@ -45,6 +45,7 @@ namespace carto { const std::shared_ptr _projection; std::vector _points; std::list _instructions; + std::string _rawResult; }; } diff --git a/all/native/routing/utils/ValhallaRoutingProxy.cpp b/all/native/routing/utils/ValhallaRoutingProxy.cpp index 9942cebb1..b490030b4 100644 --- a/all/native/routing/utils/ValhallaRoutingProxy.cpp +++ b/all/native/routing/utils/ValhallaRoutingProxy.cpp @@ -205,6 +205,7 @@ namespace carto { lokiworker.trace(api); valhalla::thor::thor_worker_t thorworker(configTree, reader); resultString = thorworker.trace_attributes(api); + lokiworker.cleanup(); } catch (const std::exception& ex) { throw GenericException("Exception while matching route", ex.what()); @@ -231,6 +232,9 @@ namespace carto { valhalla::odin::odin_worker_t odinworker(configTree); odinworker.narrate(api); resultString = valhalla::tyr::serializeDirections(api); + lokiworker.cleanup(); + thorworker.cleanup(); + odinworker.cleanup(); } catch (const std::exception& ex) { throw GenericException("Exception while calculating route", ex.what()); @@ -400,47 +404,44 @@ namespace carto { if (!err.empty()) { throw GenericException("Failed to parse result", err); } - if (!result.get("matched_points").is()) { - throw GenericException("No matched_points info in the result"); - } - if (!result.get("edges").is()) { - throw GenericException("No edges info in the result"); - } std::vector matchingPoints; std::vector matchingEdges; try { - for (const picojson::value& matchedPointInfo : result.get("matched_points").get()) { - RouteMatchingPointType::RouteMatchingPointType type = RouteMatchingPointType::ROUTE_MATCHING_POINT_UNMATCHED; - if (matchedPointInfo.get("type").get() == "matched") { - type = RouteMatchingPointType::ROUTE_MATCHING_POINT_MATCHED; - } else if (matchedPointInfo.get("type").get() == "interpolated") { - type = RouteMatchingPointType::ROUTE_MATCHING_POINT_INTERPOLATED; - } + if (result.get("matched_points").is()) { + for (const picojson::value& matchedPointInfo : result.get("matched_points").get()) { + RouteMatchingPointType::RouteMatchingPointType type = RouteMatchingPointType::ROUTE_MATCHING_POINT_UNMATCHED; + if (matchedPointInfo.get("type").get() == "matched") { + type = RouteMatchingPointType::ROUTE_MATCHING_POINT_MATCHED; + } else if (matchedPointInfo.get("type").get() == "interpolated") { + type = RouteMatchingPointType::ROUTE_MATCHING_POINT_INTERPOLATED; + } - double lat = matchedPointInfo.get("lat").get(); - double lon = matchedPointInfo.get("lon").get(); - int edgeIndex = static_cast(matchedPointInfo.get("edge_index").get()); + double lat = matchedPointInfo.get("lat").get(); + double lon = matchedPointInfo.get("lon").get(); + int edgeIndex = static_cast(matchedPointInfo.get("edge_index").get()); - matchingPoints.emplace_back(proj->fromLatLong(lat, lon), type, edgeIndex); + matchingPoints.emplace_back(proj->fromLatLong(lat, lon), type, edgeIndex); + } } - - for (const picojson::value& edgeInfo : result.get("edges").get()) { - std::map attributes; - if (edgeInfo.is()) { - const picojson::object& edgeInfoObject = edgeInfo.get(); - for (auto it = edgeInfoObject.begin(); it != edgeInfoObject.end(); it++) { - attributes[it->first] = Variant::FromPicoJSON(it->second); + if (result.get("edges").is()) { + for (const picojson::value &edgeInfo: result.get("edges").get()) { + std::map attributes; + if (edgeInfo.is()) { + const picojson::object &edgeInfoObject = edgeInfo.get(); + for (auto it = edgeInfoObject.begin(); it != edgeInfoObject.end(); it++) { + attributes[it->first] = Variant::FromPicoJSON(it->second); + } } - } - matchingEdges.emplace_back(attributes); + matchingEdges.emplace_back(attributes); + } } } catch (const std::exception& ex) { throw GenericException("Exception while translating route", ex.what()); } - return std::make_shared(proj, std::move(matchingPoints), std::move(matchingEdges)); + return std::make_shared(proj, std::move(matchingPoints), std::move(matchingEdges), resultString); } std::shared_ptr ValhallaRoutingProxy::ParseRoutingResult(const std::shared_ptr& proj, const std::string& resultString) { @@ -453,7 +454,7 @@ namespace carto { throw GenericException("No trip info in the result"); } - RoutingResultBuilder resultBuilder(proj); + RoutingResultBuilder resultBuilder(proj, resultString); try { for (const picojson::value& legInfo : result.get("trip").get("legs").get()) { std::vector shape = valhalla::midgard::decode >(legInfo.get("shape").get());