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

Feature/ors http routing backend #215

Merged
50 changes: 30 additions & 20 deletions src/routing/orshttp_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,31 +26,37 @@ OrsHttpWrapper::OrsHttpWrapper(const std::string& profile, const Server& server)
std::string OrsHttpWrapper::build_query(const std::vector<Location>& locations,
std::string service,
std::string extra_args = "") const {
// Building query for ORS
std::string query = "GET /v2/" + service;

query += "?profile=" + _profile;

// Adding locations.
if (service == "routes") {
query += "&coordinates=";
std::string body = "{\"";
if (service == "directions") {
body += "coordinates";
} else {
query += "&locations=";
body += "locations";
}
body += "\":[";
for (auto const& location : locations) {
query += std::to_string(location.lon()) + "," +
std::to_string(location.lat()) + "|";
body += "[" + std::to_string(location.lon()) + "," +
std::to_string(location.lat()) + "],";
}
query.pop_back(); // Remove trailing '|'.

body.pop_back(); // Remove trailing ','.
body += "]";
if (!extra_args.empty()) {
query += "&" + extra_args;
body += "," + extra_args;
}
body += "}";

// Building query for ORS

std::string query = "POST /ors/v2/" + service + "/" + _profile;


query += " HTTP/1.1\r\n";
jcoupey marked this conversation as resolved.
Show resolved Hide resolved
query += "Host: " + _server.host + "\r\n";
query += "Accept: */*\r\n";
query += "Connection: close\r\n\r\n";
query += "Content-Type: application/json\r\n";
query += "Content-Length: " + std::to_string(body.size()) + "\r\n";
query += "Host: " + _server.host + ":" + _server.port + "\r\n";
query += "Connection: close\r\n";
query += "\r\n" + body;

return query;
}
Expand Down Expand Up @@ -95,11 +101,12 @@ std::string OrsHttpWrapper::send_then_receive(std::string query) const {
Matrix<Cost>
OrsHttpWrapper::get_matrix(const std::vector<Location>& locs) const {
std::string query = this->build_query(locs, "matrix");

std::string response = this->send_then_receive(query);

// Removing headers.
std::string json_content = response.substr(response.find("{"));
size_t json_start = response.find("{");
jcoupey marked this conversation as resolved.
Show resolved Hide resolved
size_t json_length = response.rfind("}") - json_start + 1;
std::string json_content = response.substr(json_start, json_length);

// Expected matrix size.
std::size_t m_size = locs.size();
Expand All @@ -116,6 +123,7 @@ OrsHttpWrapper::get_matrix(const std::vector<Location>& locs) const {
"ORS matrix:" +
std::string(infos["error"]["message"].GetString()));
}

assert(infos["durations"].Size() == m_size);

// Build matrix while checking for unfound routes to avoid
Expand Down Expand Up @@ -154,14 +162,16 @@ void OrsHttpWrapper::add_route_info(Route& route) const {
ordered_locations.push_back(step.location);
}

std::string extra_args = "geometry_simplify=false&continue_straight=false";
std::string extra_args = "\"geometry_simplify\":\"false\",\"continue_straight\":\"false\"";

std::string query =
this->build_query(ordered_locations, "routes", extra_args);
this->build_query(ordered_locations, "directions", extra_args);
std::string response = this->send_then_receive(query);

// Removing headers
std::string json_content = response.substr(response.find("{"));
size_t json_start = response.find("{");
size_t json_length = response.rfind("}") - json_start + 1;
jcoupey marked this conversation as resolved.
Show resolved Hide resolved
std::string json_content = response.substr(json_start, json_length);

// Checking everything is fine in the response.
rapidjson::Document infos;
Expand Down