Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow calculateRoute and matchRoute to return rawResult #508

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions all/modules/routing/RouteMatchingResult.i
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
%attributeval(carto::RouteMatchingResult, std::vector<carto::MapPos>, Points, getPoints)
%attributeval(carto::RouteMatchingResult, std::vector<carto::RouteMatchingEdge>, MatchingEdges, getMatchingEdges)
%attributeval(carto::RouteMatchingResult, std::vector<carto::RouteMatchingPoint>, MatchingPoints, getMatchingPoints)
%attributestring(carto::RouteMatchingResult, std::string, RawResult, getRawResult)
%std_exceptions(carto::RouteMatchingResult::RouteMatchingResult)
!standard_equals(carto::RouteMatchingResult);
!custom_tostring(carto::RouteMatchingResult);
Expand Down
1 change: 1 addition & 0 deletions all/modules/routing/RoutingResult.i
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
%attributeval(carto::RoutingResult, std::vector<carto::RoutingInstruction>, 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);
Expand Down
7 changes: 6 additions & 1 deletion all/native/routing/RouteMatchingResult.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@

namespace carto {

RouteMatchingResult::RouteMatchingResult(const std::shared_ptr<Projection>& projection, std::vector<RouteMatchingPoint> matchingPoints, std::vector<RouteMatchingEdge> matchingEdges) :
RouteMatchingResult::RouteMatchingResult(const std::shared_ptr<Projection>& projection, std::vector<RouteMatchingPoint> matchingPoints, std::vector<RouteMatchingEdge> matchingEdges, std::string rawResult) :
_projection(projection),
_rawResult(rawResult),
_matchingPoints(std::move(matchingPoints)),
_matchingEdges(std::move(matchingEdges))
{
Expand Down Expand Up @@ -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=[";
Expand Down
7 changes: 6 additions & 1 deletion all/native/routing/RouteMatchingResult.h
Original file line number Diff line number Diff line change
Expand Up @@ -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>& projection, std::vector<RouteMatchingPoint> matchingPoints, std::vector<RouteMatchingEdge> matchingEdges);
RouteMatchingResult(const std::shared_ptr<Projection>& projection, std::vector<RouteMatchingPoint> matchingPoints, std::vector<RouteMatchingEdge> matchingEdges, std::string rawResult);
virtual ~RouteMatchingResult();

/**
Expand All @@ -53,6 +53,10 @@ namespace carto {
* @return The list with details of the matched points.
*/
const std::vector<RouteMatchingPoint>& getMatchingPoints() const;
/**
* Returns raw result
*/
const std::string& getRawResult() const;

/**
* Creates a string representation of this result object, useful for logging.
Expand All @@ -64,6 +68,7 @@ namespace carto {
std::shared_ptr<Projection> _projection;
std::vector<RouteMatchingPoint> _matchingPoints;
std::vector<RouteMatchingEdge> _matchingEdges;
std::string _rawResult;
};

}
Expand Down
7 changes: 6 additions & 1 deletion all/native/routing/RoutingResult.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@

namespace carto {

RoutingResult::RoutingResult(const std::shared_ptr<Projection>& projection, std::vector<MapPos> points, std::vector<RoutingInstruction> instructions) :
RoutingResult::RoutingResult(const std::shared_ptr<Projection>& projection, std::vector<MapPos> points, std::vector<RoutingInstruction> instructions, const std::string rawResult) :
_projection(projection),
_rawResult(rawResult),
_points(std::move(points)),
_instructions(std::move(instructions))
{
Expand Down Expand Up @@ -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);
Expand Down
7 changes: 6 additions & 1 deletion all/native/routing/RoutingResult.h
Original file line number Diff line number Diff line change
Expand Up @@ -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>& projection, std::vector<MapPos> points, std::vector<RoutingInstruction> instructions);
RoutingResult(const std::shared_ptr<Projection>& projection, std::vector<MapPos> points, std::vector<RoutingInstruction> instructions, const std::string rawResult);
virtual ~RoutingResult();

/**
Expand Down Expand Up @@ -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.
Expand All @@ -69,6 +73,7 @@ namespace carto {
std::shared_ptr<Projection> _projection;
std::vector<MapPos> _points;
std::vector<RoutingInstruction> _instructions;
std::string _rawResult;
};

}
Expand Down
2 changes: 1 addition & 1 deletion all/native/routing/SGREOfflineRoutingService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
4 changes: 2 additions & 2 deletions all/native/routing/utils/OSRMRoutingProxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down Expand Up @@ -116,7 +116,7 @@ namespace carto {
throw GenericException("Routing failed", responseDoc["status_message"].GetString());
}

RoutingResultBuilder resultBuilder(proj);
RoutingResultBuilder resultBuilder(proj, json);

std::vector<MapPos> wgs84Points = DecodeGeometry(responseDoc["route_geometry"].GetString());
std::vector<MapPos> points;
Expand Down
5 changes: 3 additions & 2 deletions all/native/routing/utils/RoutingResultBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@

namespace carto {

RoutingResultBuilder::RoutingResultBuilder(const std::shared_ptr<Projection>& proj) :
RoutingResultBuilder::RoutingResultBuilder(const std::shared_ptr<Projection>& proj, const std::string& rawResult) :
_projection(proj),
_rawResult(rawResult),
_points(),
_instructions()
{
Expand Down Expand Up @@ -57,7 +58,7 @@ namespace carto {
}
instructions.push_back(instrBuilder.buildRoutingInstruction());
}
return std::make_shared<RoutingResult>(_projection, _points, std::move(instructions));
return std::make_shared<RoutingResult>(_projection, _points, std::move(instructions), _rawResult);
}

float RoutingResultBuilder::calculateTurnAngle(int pointIndex) const {
Expand Down
3 changes: 2 additions & 1 deletion all/native/routing/utils/RoutingResultBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace carto {

class RoutingResultBuilder {
public:
explicit RoutingResultBuilder(const std::shared_ptr<Projection>& proj);
explicit RoutingResultBuilder(const std::shared_ptr<Projection>& proj, const std::string& rawResult);

int addPoints(const std::vector<MapPos>& points);

Expand All @@ -45,6 +45,7 @@ namespace carto {
const std::shared_ptr<Projection> _projection;
std::vector<MapPos> _points;
std::list<RoutingInstructionBuilder> _instructions;
std::string _rawResult;
};

}
Expand Down
57 changes: 29 additions & 28 deletions all/native/routing/utils/ValhallaRoutingProxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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());
Expand Down Expand Up @@ -400,47 +404,44 @@ namespace carto {
if (!err.empty()) {
throw GenericException("Failed to parse result", err);
}
if (!result.get("matched_points").is<picojson::array>()) {
throw GenericException("No matched_points info in the result");
}
if (!result.get("edges").is<picojson::array>()) {
throw GenericException("No edges info in the result");
}

std::vector<RouteMatchingPoint> matchingPoints;
std::vector<RouteMatchingEdge> matchingEdges;
try {
for (const picojson::value& matchedPointInfo : result.get("matched_points").get<picojson::array>()) {
RouteMatchingPointType::RouteMatchingPointType type = RouteMatchingPointType::ROUTE_MATCHING_POINT_UNMATCHED;
if (matchedPointInfo.get("type").get<std::string>() == "matched") {
type = RouteMatchingPointType::ROUTE_MATCHING_POINT_MATCHED;
} else if (matchedPointInfo.get("type").get<std::string>() == "interpolated") {
type = RouteMatchingPointType::ROUTE_MATCHING_POINT_INTERPOLATED;
}
if (result.get("matched_points").is<picojson::array>()) {
for (const picojson::value& matchedPointInfo : result.get("matched_points").get<picojson::array>()) {
RouteMatchingPointType::RouteMatchingPointType type = RouteMatchingPointType::ROUTE_MATCHING_POINT_UNMATCHED;
if (matchedPointInfo.get("type").get<std::string>() == "matched") {
type = RouteMatchingPointType::ROUTE_MATCHING_POINT_MATCHED;
} else if (matchedPointInfo.get("type").get<std::string>() == "interpolated") {
type = RouteMatchingPointType::ROUTE_MATCHING_POINT_INTERPOLATED;
}

double lat = matchedPointInfo.get("lat").get<double>();
double lon = matchedPointInfo.get("lon").get<double>();
int edgeIndex = static_cast<int>(matchedPointInfo.get("edge_index").get<std::int64_t>());
double lat = matchedPointInfo.get("lat").get<double>();
double lon = matchedPointInfo.get("lon").get<double>();
int edgeIndex = static_cast<int>(matchedPointInfo.get("edge_index").get<std::int64_t>());

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<picojson::array>()) {
std::map<std::string, Variant> attributes;
if (edgeInfo.is<picojson::object>()) {
const picojson::object& edgeInfoObject = edgeInfo.get<picojson::object>();
for (auto it = edgeInfoObject.begin(); it != edgeInfoObject.end(); it++) {
attributes[it->first] = Variant::FromPicoJSON(it->second);
if (result.get("edges").is<picojson::array>()) {
for (const picojson::value &edgeInfo: result.get("edges").get<picojson::array>()) {
std::map<std::string, Variant> attributes;
if (edgeInfo.is<picojson::object>()) {
const picojson::object &edgeInfoObject = edgeInfo.get<picojson::object>();
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<RouteMatchingResult>(proj, std::move(matchingPoints), std::move(matchingEdges));
return std::make_shared<RouteMatchingResult>(proj, std::move(matchingPoints), std::move(matchingEdges), resultString);
}

std::shared_ptr<RoutingResult> ValhallaRoutingProxy::ParseRoutingResult(const std::shared_ptr<Projection>& proj, const std::string& resultString) {
Expand All @@ -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<picojson::array>()) {
std::vector<valhalla::midgard::PointLL> shape = valhalla::midgard::decode<std::vector<valhalla::midgard::PointLL> >(legInfo.get("shape").get<std::string>());
Expand Down