MCPcopy Create free account
hub / github.com/Project-OSRM/osrm-backend / main

Function main

example/example.cpp:21–95  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

19#include <cstdlib>
20
21int main(int argc, const char *argv[])
22{
23 if (argc < 2)
24 {
25 std::cerr << "Usage: " << argv[0] << " data.osrm\n";
26 return EXIT_FAILURE;
27 }
28
29 using namespace osrm;
30
31 // Configure based on a .osrm base path, and no datasets in shared mem from osrm-datastore
32 EngineConfig config;
33
34 config.storage_config = {argv[1]};
35 config.use_shared_memory = false;
36
37 // We support two routing speed up techniques:
38 // - Contraction Hierarchies (CH): requires extract+contract pre-processing
39 // - Multi-Level Dijkstra (MLD): requires extract+partition+customize pre-processing
40 //
41 // config.algorithm = EngineConfig::Algorithm::CH;
42 config.algorithm = EngineConfig::Algorithm::MLD;
43
44 // Routing machine with several services (such as Route, Table, Nearest, Trip, Match)
45 const OSRM osrm{config};
46
47 // The following shows how to use the Route service; configure this service
48 RouteParameters params;
49
50 // Route in monaco
51 params.coordinates.push_back({util::FloatLongitude{7.419758}, util::FloatLatitude{43.731142}});
52 params.coordinates.push_back({util::FloatLongitude{7.419505}, util::FloatLatitude{43.736825}});
53
54 // Response is in JSON format
55 engine::api::ResultT result = json::Object();
56
57 // Execute routing request, this does the heavy lifting
58 const auto status = osrm.Route(params, result);
59
60 auto &json_result = std::get<json::Object>(result);
61 if (status == Status::Ok)
62 {
63 auto &routes = std::get<json::Array>(json_result.values["routes"]);
64
65 // Let's just use the first route
66 auto &route = std::get<json::Object>(routes.values.at(0));
67 const auto distance = std::get<json::Number>(route.values["distance"]).value;
68 const auto duration = std::get<json::Number>(route.values["duration"]).value;
69
70 // Warn users if extract does not contain the default coordinates from above
71 if (distance == 0 || duration == 0)
72 {
73 std::cout << "Note: distance or duration is zero. ";
74 std::cout << "You are probably doing a query outside of the OSM extract.\n\n";
75 }
76
77 std::cout << "Distance: " << distance << " meter\n";
78 std::cout << "Duration: " << duration << " seconds\n";

Callers

nothing calls this directly

Calls 4

ObjectClass · 0.50
push_backMethod · 0.45
RouteMethod · 0.45
atMethod · 0.45

Tested by

no test coverage detected