Adapts example/example.cpp to new osrm api
This commit is contained in:
parent
715ee66b03
commit
e05a45b080
@ -15,55 +15,85 @@
|
||||
#include <utility>
|
||||
#include <iostream>
|
||||
#include <exception>
|
||||
|
||||
#include <cstdlib>
|
||||
|
||||
int main(int argc, const char *argv[]) try
|
||||
{
|
||||
if (argc < 2)
|
||||
{
|
||||
std::cerr << "Error: Not enough arguments." << std::endl
|
||||
<< "Run " << argv[0] << " data.osrm" << std::endl;
|
||||
std::cerr << "Usage: " << argv[0] << " data.osrm\n";
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
osrm::EngineConfig engine_config;
|
||||
std::string base_path(argv[1]);
|
||||
engine_config.server_paths["ramindex"] = base_path + ".ramIndex";
|
||||
engine_config.server_paths["fileindex"] = base_path + ".fileIndex";
|
||||
engine_config.server_paths["hsgrdata"] = base_path + ".hsgr";
|
||||
engine_config.server_paths["nodesdata"] = base_path + ".nodes";
|
||||
engine_config.server_paths["edgesdata"] = base_path + ".edges";
|
||||
engine_config.server_paths["coredata"] = base_path + ".core";
|
||||
engine_config.server_paths["geometries"] = base_path + ".geometry";
|
||||
engine_config.server_paths["timestamp"] = base_path + ".timestamp";
|
||||
engine_config.server_paths["namesdata"] = base_path + ".names";
|
||||
engine_config.use_shared_memory = false;
|
||||
// Path to .osrm base file
|
||||
std::string base{argv[1]};
|
||||
|
||||
osrm::OSRM routing_machine(engine_config);
|
||||
// Configure routing machine
|
||||
osrm::EngineConfig config;
|
||||
|
||||
osrm::RouteParameters route_parameters;
|
||||
// route is in Monaco
|
||||
route_parameters.service = "viaroute";
|
||||
route_parameters.AddCoordinate(43.731142, 7.419758);
|
||||
route_parameters.AddCoordinate(43.736825, 7.419505);
|
||||
// TODO(daniel-j-h): this is ugly, provide easier way for users
|
||||
config.server_paths["ramindex"] = base + ".ramIndex";
|
||||
config.server_paths["fileindex"] = base + ".fileIndex";
|
||||
config.server_paths["hsgrdata"] = base + ".hsgr";
|
||||
config.server_paths["nodesdata"] = base + ".nodes";
|
||||
config.server_paths["edgesdata"] = base + ".edges";
|
||||
config.server_paths["coredata"] = base + ".core";
|
||||
config.server_paths["geometries"] = base + ".geometry";
|
||||
config.server_paths["timestamp"] = base + ".timestamp";
|
||||
config.server_paths["namesdata"] = base + ".names";
|
||||
config.use_shared_memory = false;
|
||||
|
||||
osrm::json::Object json_result;
|
||||
const int result_code = routing_machine.RunQuery(route_parameters, json_result);
|
||||
std::cout << "result code: " << result_code << std::endl;
|
||||
// 2xx code
|
||||
if (result_code / 100 == 2)
|
||||
// Routing machine with several services (such as Route, Table, Nearest, Trip, Match)
|
||||
osrm::OSRM osrm{config};
|
||||
|
||||
// The following shows how to use the Route service; configure this service
|
||||
osrm::RouteParameters params;
|
||||
|
||||
// Route is in Berlin. Latitude, Longitude
|
||||
// TODO(daniel-j-h): use either coordinate_precision or better provide double,double-taking ctor
|
||||
params.coordinates.push_back({static_cast<int>(43.731142 * osrm::COORDINATE_PRECISION),
|
||||
static_cast<int>(7.419758 * osrm::COORDINATE_PRECISION)});
|
||||
params.coordinates.push_back({static_cast<int>(43.736825 * osrm::COORDINATE_PRECISION),
|
||||
static_cast<int>(7.419505 * osrm::COORDINATE_PRECISION)});
|
||||
|
||||
// Response is in JSON format
|
||||
osrm::json::Object result;
|
||||
|
||||
// Execute routing request, this does the heavy lifting
|
||||
const auto status = osrm.Route(params, result);
|
||||
|
||||
if (status == osrm::Status::Ok)
|
||||
{
|
||||
// Extract data out of JSON structure
|
||||
auto &summary = json_result.values["route_summary"].get<osrm::json::Object>();
|
||||
auto duration = summary.values["total_time"].get<osrm::json::Number>().value;
|
||||
auto distance = summary.values["total_distance"].get<osrm::json::Number>().value;
|
||||
std::cout << "duration: " << duration << std::endl;
|
||||
std::cout << "distance: " << distance << std::endl;
|
||||
auto &routes = result.values["routes"].get<osrm::json::Array>();
|
||||
|
||||
// Let's just use the first route
|
||||
auto &route = routes.values.at(0).get<osrm::json::Object>();
|
||||
const auto distance = route.values["distance"].get<osrm::json::Number>().value;
|
||||
const auto duration = route.values["duration"].get<osrm::json::Number>().value;
|
||||
|
||||
// Warn users if extract does not contain the default Berlin coordinates from above
|
||||
if (distance == 0 or duration == 0)
|
||||
{
|
||||
std::cout << "Note: distance or duration is zero. ";
|
||||
std::cout << "You are probably doing a query outside of the OSM extract.\n\n";
|
||||
}
|
||||
|
||||
std::cout << "Distance: " << distance << " meter\n";
|
||||
std::cout << "Duration: " << duration << " seconds\n";
|
||||
}
|
||||
else if (status == osrm::Status::Error)
|
||||
{
|
||||
const auto code = result.values["code"].get<osrm::json::String>().value;
|
||||
const auto message = result.values["message"].get<osrm::json::String>().value;
|
||||
|
||||
std::cout << "Code: " << code << "\n";
|
||||
std::cout << "Message: " << code << "\n";
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
catch (const std::exception ¤t_exception)
|
||||
catch (const std::exception &e)
|
||||
{
|
||||
std::cout << "exception: " << current_exception.what();
|
||||
std::cerr << "Error: " << e.what() << std::endl;
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user