Adapts Nearest plugin to new API
This commit is contained in:
		
							parent
							
								
									9704ca7a8b
								
							
						
					
					
						commit
						d989714007
					
				| @ -2,12 +2,9 @@ | ||||
| #define NEAREST_HPP | ||||
| 
 | ||||
| #include "engine/plugins/plugin_base.hpp" | ||||
| #include "engine/phantom_node.hpp" | ||||
| #include "util/integer_range.hpp" | ||||
| #include "engine/api/nearest_parameters.hpp" | ||||
| #include "osrm/json_container.hpp" | ||||
| 
 | ||||
| #include <string> | ||||
| 
 | ||||
| namespace osrm | ||||
| { | ||||
| namespace engine | ||||
| @ -15,90 +12,12 @@ namespace engine | ||||
| namespace plugins | ||||
| { | ||||
| 
 | ||||
| /*
 | ||||
|  * This Plugin locates the nearest point on a street in the road network for a given coordinate. | ||||
|  */ | ||||
| 
 | ||||
| class NearestPlugin final : public BasePlugin | ||||
| { | ||||
|   public: | ||||
|     explicit NearestPlugin(DataFacadeT *facade) : facade(facade), descriptor_string("nearest") {} | ||||
|     explicit NearestPlugin(datafacade::BaseDataFacade &facade); | ||||
| 
 | ||||
|     const std::string GetDescriptor() const override final { return descriptor_string; } | ||||
| 
 | ||||
|     Status HandleRequest(const RouteParameters &route_parameters, | ||||
|                          util::json::Object &json_result) override final | ||||
|     { | ||||
|         // check number of parameters
 | ||||
|         if (route_parameters.coordinates.empty() || !route_parameters.coordinates.front().IsValid()) | ||||
|         { | ||||
|             return Status::Error; | ||||
|         } | ||||
| 
 | ||||
|         const auto &input_bearings = route_parameters.bearings; | ||||
|         if (input_bearings.size() > 0 && | ||||
|             route_parameters.coordinates.size() != input_bearings.size()) | ||||
|         { | ||||
|             json_result.values["status_message"] = | ||||
|                 "Number of bearings does not match number of coordinates"; | ||||
|             return Status::Error; | ||||
|         } | ||||
| 
 | ||||
|         auto number_of_results = static_cast<std::size_t>(route_parameters.num_results); | ||||
|         const int bearing = input_bearings.size() > 0 ? input_bearings.front().first : 0; | ||||
|         const int range = | ||||
|             input_bearings.size() > 0 | ||||
|                 ? (input_bearings.front().second ? *input_bearings.front().second : 10) | ||||
|                 : 180; | ||||
|         auto phantom_node_vector = facade->NearestPhantomNodes(route_parameters.coordinates.front(), | ||||
|                                                                number_of_results, bearing, range); | ||||
| 
 | ||||
|         if (phantom_node_vector.empty()) | ||||
|         { | ||||
|             json_result.values["status_message"] = | ||||
|                 std::string("Could not find a matching segments for coordinate"); | ||||
|             return Status::NoSegment; | ||||
|         } | ||||
|         else | ||||
|         { | ||||
|             json_result.values["status_message"] = "Found nearest edge"; | ||||
|             if (number_of_results > 1) | ||||
|             { | ||||
|                 util::json::Array results; | ||||
| 
 | ||||
|                 auto vector_length = phantom_node_vector.size(); | ||||
|                 for (const auto i : | ||||
|                      util::irange<std::size_t>(0, std::min(number_of_results, vector_length))) | ||||
|                 { | ||||
|                     const auto &node = phantom_node_vector[i].phantom_node; | ||||
|                     util::json::Array json_coordinate; | ||||
|                     util::json::Object result; | ||||
|                     json_coordinate.values.push_back(node.location.lat / COORDINATE_PRECISION); | ||||
|                     json_coordinate.values.push_back(node.location.lon / COORDINATE_PRECISION); | ||||
|                     result.values["mapped coordinate"] = json_coordinate; | ||||
|                     result.values["name"] = facade->get_name_for_id(node.name_id); | ||||
|                     results.values.push_back(result); | ||||
|                 } | ||||
|                 json_result.values["results"] = results; | ||||
|             } | ||||
|             else | ||||
|             { | ||||
|                 util::json::Array json_coordinate; | ||||
|                 json_coordinate.values.push_back( | ||||
|                     phantom_node_vector.front().phantom_node.location.lat / COORDINATE_PRECISION); | ||||
|                 json_coordinate.values.push_back( | ||||
|                     phantom_node_vector.front().phantom_node.location.lon / COORDINATE_PRECISION); | ||||
|                 json_result.values["mapped_coordinate"] = json_coordinate; | ||||
|                 json_result.values["name"] = | ||||
|                     facade->get_name_for_id(phantom_node_vector.front().phantom_node.name_id); | ||||
|             } | ||||
|         } | ||||
|         return Status::Ok; | ||||
|     } | ||||
| 
 | ||||
|   private: | ||||
|     DataFacadeT *facade; | ||||
|     std::string descriptor_string; | ||||
|     Status HandleRequest(const api::NearestParameters ¶ms, util::json::Object &result); | ||||
| }; | ||||
| } | ||||
| } | ||||
|  | ||||
| @ -17,16 +17,16 @@ namespace plugins | ||||
| 
 | ||||
| class TablePlugin final : public BasePlugin | ||||
| { | ||||
|   private: | ||||
|     SearchEngineData heaps; | ||||
|     routing_algorithms::ManyToManyRouting<datafacade::BaseDataFacade> distance_table; | ||||
|     int max_locations_distance_table; | ||||
| 
 | ||||
|   public: | ||||
|     explicit TablePlugin(datafacade::BaseDataFacade &facade, | ||||
|                          const int max_locations_distance_table); | ||||
| 
 | ||||
|     Status HandleRequest(const api::TableParameters ¶ms, util::json::Object &result); | ||||
| 
 | ||||
|   private: | ||||
|     SearchEngineData heaps; | ||||
|     routing_algorithms::ManyToManyRouting<datafacade::BaseDataFacade> distance_table; | ||||
|     int max_locations_distance_table; | ||||
| }; | ||||
| } | ||||
| } | ||||
|  | ||||
| @ -83,7 +83,7 @@ class MapMatching final : public BasicRoutingInterface<DataFacadeT, MapMatching< | ||||
| 
 | ||||
|         const bool use_timestamps = trace_timestamps.size() > 1; | ||||
| 
 | ||||
|         const auto median_sample_time = [&]() | ||||
|         const auto median_sample_time = [&] | ||||
|         { | ||||
|             if (use_timestamps) | ||||
|             { | ||||
| @ -95,7 +95,7 @@ class MapMatching final : public BasicRoutingInterface<DataFacadeT, MapMatching< | ||||
|             } | ||||
|         }(); | ||||
|         const auto max_broken_time = median_sample_time * MAX_BROKEN_STATES; | ||||
|         const auto max_distance_delta = [&]() | ||||
|         const auto max_distance_delta = [&] | ||||
|         { | ||||
|             if (use_timestamps) | ||||
|             { | ||||
|  | ||||
| @ -144,14 +144,30 @@ Engine::~Engine() = default; | ||||
| Engine::Engine(Engine &&) noexcept = default; | ||||
| Engine &Engine::operator=(Engine &&) noexcept = default; | ||||
| 
 | ||||
| Status Engine::Route(const api::RouteParameters &route_parameters, util::json::Object &result) | ||||
| Status Engine::Route(const api::RouteParameters ¶ms, util::json::Object &result) | ||||
| { | ||||
|     return RunQuery(lock, *query_data_facade, route_parameters, *route_plugin, result); | ||||
|     return RunQuery(lock, *query_data_facade, params, *route_plugin, result); | ||||
| } | ||||
| 
 | ||||
| Status Engine::Table(const api::TableParameters &table_parameters, util::json::Object &result) | ||||
| Status Engine::Table(const api::TableParameters ¶ms, util::json::Object &result) | ||||
| { | ||||
|     return RunQuery(lock, *query_data_facade, table_parameters, *table_plugin, result); | ||||
|     return RunQuery(lock, *query_data_facade, params, *table_plugin, result); | ||||
| } | ||||
| 
 | ||||
| Status Engine::Nearest(const api::NearestParameters ¶ms, util::json::Object &result) | ||||
| { | ||||
|     return RunQuery(lock, *query_data_facade, params, *nearest_plugin, result); | ||||
| } | ||||
| 
 | ||||
| //Status Engine::Trip(const api::TripParameters ¶ms, util::json::Object &result)
 | ||||
| //{
 | ||||
| //    return RunQuery(lock, *query_data_facade, params, *trip_plugin, result);
 | ||||
| //}
 | ||||
| //
 | ||||
| //Status Engine::Match(const api::MatchParameters ¶ms, util::json::Object &result)
 | ||||
| //{
 | ||||
| //    return RunQuery(lock, *query_data_facade, params, *match_plugin, result);
 | ||||
| //}
 | ||||
| 
 | ||||
| } // engine ns
 | ||||
| } // osrm ns
 | ||||
|  | ||||
| @ -0,0 +1,87 @@ | ||||
| #include "engine/plugins/nearest.hpp" | ||||
| #include "engine/phantom_node.hpp" | ||||
| #include "util/integer_range.hpp" | ||||
| 
 | ||||
| #include <cstddef> | ||||
| #include <string> | ||||
| 
 | ||||
| #include <boost/assert.hpp> | ||||
| 
 | ||||
| namespace osrm | ||||
| { | ||||
| namespace engine | ||||
| { | ||||
| namespace plugins | ||||
| { | ||||
| 
 | ||||
| NearestPlugin::NearestPlugin(datafacade::BaseDataFacade &facade) : BasePlugin{facade} {} | ||||
| 
 | ||||
| Status NearestPlugin::HandleRequest(const api::NearestParameters ¶ms, | ||||
|                                     util::json::Object &result) | ||||
| { | ||||
|     BOOST_ASSERT(params.IsValid()); | ||||
| 
 | ||||
|     if (!CheckAllCoordinates(params.coordinates)) | ||||
|         return Error("invalid-options", "Coordinates are invalid", result); | ||||
| 
 | ||||
|     if (params.bearings.size() > 0 && params.coordinates.size() != params.bearings.size()) | ||||
|         return Error("invalid-options", "Number of bearings does not match number of coordinates", | ||||
|                      result); | ||||
| 
 | ||||
|     const auto &input_bearings = params.bearings; | ||||
|     auto number_of_results = static_cast<std::size_t>(params.number_of_results); | ||||
| 
 | ||||
|     /* TODO(daniel-j-h): bearing range?
 | ||||
|     const int bearing = input_bearings.size() > 0 ? input_bearings.front().first : 0; | ||||
|     const int range = input_bearings.size() > 0 | ||||
|                           ? (input_bearings.front().second ? *input_bearings.front().second : 10) | ||||
|                           : 180; | ||||
|     auto phantom_node_vector = | ||||
|         facade.NearestPhantomNodes(params.coordinates.front(), number_of_results, bearing, range); | ||||
| 
 | ||||
|     if (phantom_node_vector.empty()) | ||||
|     { | ||||
|         result.values["status_message"] = | ||||
|             std::string("Could not find a matching segments for coordinate"); | ||||
|         return Status::NoSegment; | ||||
|     } | ||||
|     else | ||||
|     { | ||||
|         result.values["status_message"] = "Found nearest edge"; | ||||
|         if (number_of_results > 1) | ||||
|         { | ||||
|             util::json::Array results; | ||||
| 
 | ||||
|             auto vector_length = phantom_node_vector.size(); | ||||
|             for (const auto i : | ||||
|                  util::irange<std::size_t>(0, std::min(number_of_results, vector_length))) | ||||
|             { | ||||
|                 const auto &node = phantom_node_vector[i].phantom_node; | ||||
|                 util::json::Array json_coordinate; | ||||
|                 util::json::Object result; | ||||
|                 json_coordinate.values.push_back(node.location.lat / COORDINATE_PRECISION); | ||||
|                 json_coordinate.values.push_back(node.location.lon / COORDINATE_PRECISION); | ||||
|                 result.values["mapped coordinate"] = json_coordinate; | ||||
|                 result.values["name"] = facade.get_name_for_id(node.name_id); | ||||
|                 results.values.push_back(result); | ||||
|             } | ||||
|             result.values["results"] = results; | ||||
|         } | ||||
|         else | ||||
|         { | ||||
|             util::json::Array json_coordinate; | ||||
|             json_coordinate.values.push_back(phantom_node_vector.front().phantom_node.location.lat / | ||||
|                                              COORDINATE_PRECISION); | ||||
|             json_coordinate.values.push_back(phantom_node_vector.front().phantom_node.location.lon / | ||||
|                                              COORDINATE_PRECISION); | ||||
|             result.values["mapped_coordinate"] = json_coordinate; | ||||
|             result.values["name"] = | ||||
|                 facade.get_name_for_id(phantom_node_vector.front().phantom_node.name_id); | ||||
|         } | ||||
|     } | ||||
|     */ | ||||
|     return Status::Ok; | ||||
| } | ||||
| } | ||||
| } | ||||
| } | ||||
| @ -15,6 +15,8 @@ | ||||
| #include <string> | ||||
| #include <vector> | ||||
| 
 | ||||
| #include <boost/assert.hpp> | ||||
| 
 | ||||
| namespace osrm | ||||
| { | ||||
| namespace engine | ||||
| @ -58,7 +60,7 @@ Status TablePlugin::HandleRequest(const api::TableParameters ¶ms, util::json | ||||
| 
 | ||||
|     auto snapped_phantoms = SnapPhantomNodes(GetPhantomNodes(params)); | ||||
| 
 | ||||
|     const auto result_table = [&]() | ||||
|     const auto result_table = [&] | ||||
|     { | ||||
|         if (params.sources.empty()) | ||||
|         { | ||||
| @ -76,7 +78,7 @@ Status TablePlugin::HandleRequest(const api::TableParameters ¶ms, util::json | ||||
|         return Error("no-table", "No table found", result); | ||||
|     } | ||||
| 
 | ||||
|     api::TableAPI table_api {facade, params}; | ||||
|     api::TableAPI table_api{facade, params}; | ||||
|     table_api.MakeResponse(result_table, snapped_phantoms, result); | ||||
| 
 | ||||
|     return Status::Ok; | ||||
|  | ||||
| @ -113,7 +113,7 @@ int main(int argc, const char *argv[]) try | ||||
|     } | ||||
|     else | ||||
|     { | ||||
|         std::packaged_task<int()> server_task([&]() -> int | ||||
|         std::packaged_task<int()> server_task([&] | ||||
|                                               { | ||||
|                                                   routing_server->Run(); | ||||
|                                                   return 0; | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user