Adapts example/example.cpp to new osrm api
This commit is contained in:
		
							parent
							
								
									283926dbc9
								
							
						
					
					
						commit
						c32900091c
					
				| @ -15,55 +15,85 @@ | |||||||
| #include <utility> | #include <utility> | ||||||
| #include <iostream> | #include <iostream> | ||||||
| #include <exception> | #include <exception> | ||||||
|  | 
 | ||||||
| #include <cstdlib> | #include <cstdlib> | ||||||
| 
 | 
 | ||||||
| int main(int argc, const char *argv[]) try | int main(int argc, const char *argv[]) try | ||||||
| { | { | ||||||
|     if (argc < 2) |     if (argc < 2) | ||||||
|     { |     { | ||||||
|         std::cerr << "Error: Not enough arguments." << std::endl |         std::cerr << "Usage: " << argv[0] << " data.osrm\n"; | ||||||
|                   << "Run " << argv[0] << " data.osrm" << std::endl; |  | ||||||
|         return EXIT_FAILURE; |         return EXIT_FAILURE; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     osrm::EngineConfig engine_config; |     // Path to .osrm base file
 | ||||||
|     std::string base_path(argv[1]); |     std::string base{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; |  | ||||||
| 
 | 
 | ||||||
|     osrm::OSRM routing_machine(engine_config); |     // Configure routing machine
 | ||||||
|  |     osrm::EngineConfig config; | ||||||
| 
 | 
 | ||||||
|     osrm::RouteParameters route_parameters; |     // TODO(daniel-j-h): this is ugly, provide easier way for users
 | ||||||
|     // route is in Monaco
 |     config.server_paths["ramindex"] = base + ".ramIndex"; | ||||||
|     route_parameters.service = "viaroute"; |     config.server_paths["fileindex"] = base + ".fileIndex"; | ||||||
|     route_parameters.AddCoordinate(43.731142, 7.419758); |     config.server_paths["hsgrdata"] = base + ".hsgr"; | ||||||
|     route_parameters.AddCoordinate(43.736825, 7.419505); |     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; |     // Routing machine with several services (such as Route, Table, Nearest, Trip, Match)
 | ||||||
|     const int result_code = routing_machine.RunQuery(route_parameters, json_result); |     osrm::OSRM osrm{config}; | ||||||
|     std::cout << "result code: " << result_code << std::endl; | 
 | ||||||
|     // 2xx code
 |     // The following shows how to use the Route service; configure this service
 | ||||||
|     if (result_code / 100 == 2) |     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 &routes = result.values["routes"].get<osrm::json::Array>(); | ||||||
|         auto &summary = json_result.values["route_summary"].get<osrm::json::Object>(); | 
 | ||||||
|         auto duration = summary.values["total_time"].get<osrm::json::Number>().value; |         // Let's just use the first route
 | ||||||
|         auto distance = summary.values["total_distance"].get<osrm::json::Number>().value; |         auto &route = routes.values.at(0).get<osrm::json::Object>(); | ||||||
|         std::cout << "duration: " << duration << std::endl; |         const auto distance = route.values["distance"].get<osrm::json::Number>().value; | ||||||
|         std::cout << "distance: " << distance << std::endl; |         const auto duration = route.values["duration"].get<osrm::json::Number>().value; | ||||||
|     } | 
 | ||||||
|     return EXIT_SUCCESS; |         // Warn users if extract does not contain the default Berlin coordinates from above
 | ||||||
| } |         if (distance == 0 or duration == 0) | ||||||
| catch (const std::exception ¤t_exception) |  | ||||||
|         { |         { | ||||||
|     std::cout << "exception: " << current_exception.what(); |             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; | ||||||
|  |     } | ||||||
|  | } | ||||||
|  | catch (const std::exception &e) | ||||||
|  | { | ||||||
|  |     std::cerr << "Error: " << e.what() << std::endl; | ||||||
|     return EXIT_FAILURE; |     return EXIT_FAILURE; | ||||||
| } | } | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user