Fix table plugin
This commit is contained in:
		
							parent
							
								
									d73600387b
								
							
						
					
					
						commit
						207f1d7732
					
				| @ -31,7 +31,7 @@ struct TableParameters; | ||||
| namespace plugins | ||||
| { | ||||
| class ViaRoutePlugin; | ||||
| class DistanceTablePlugin; | ||||
| class TablePlugin; | ||||
| } | ||||
| namespace datafacade | ||||
| { | ||||
| @ -41,19 +41,23 @@ class BaseDataFacade; | ||||
| class Engine final | ||||
| { | ||||
|   public: | ||||
|     Engine(EngineConfig &config); | ||||
|     // Needs to be public
 | ||||
|     struct EngineLock; | ||||
| 
 | ||||
|     explicit Engine(EngineConfig &config); | ||||
|     Engine(const Engine &) = delete; | ||||
|     Engine &operator=(const Engine &) = delete; | ||||
|     // Needed because we have unique_ptr of incomplete types
 | ||||
|     ~Engine(); | ||||
| 
 | ||||
|     Status Route(const api::RouteParameters ¶meters, util::json::Object &result); | ||||
|     Status Table(const api::TableParameters ¶meters, util::json::Object &result); | ||||
| 
 | ||||
|   private: | ||||
|     struct EngineLock; | ||||
|     std::unique_ptr<EngineLock> lock; | ||||
| 
 | ||||
|     std::unique_ptr<plugins::ViaRoutePlugin> route_plugin; | ||||
|     std::unique_ptr<plugins::DistanceTablePlugin> table_plugin; | ||||
|     std::unique_ptr<plugins::TablePlugin> table_plugin; | ||||
| 
 | ||||
|     std::unique_ptr<datafacade::BaseDataFacade> query_data_facade; | ||||
| }; | ||||
|  | ||||
| @ -1,196 +0,0 @@ | ||||
| #ifndef DISTANCE_TABLE_HPP | ||||
| #define DISTANCE_TABLE_HPP | ||||
| 
 | ||||
| #include "engine/plugins/plugin_base.hpp" | ||||
| 
 | ||||
| #include "engine/api/table_parameters.hpp" | ||||
| #include "engine/object_encoder.hpp" | ||||
| #include "engine/routing_algorithms/many_to_many.hpp" | ||||
| #include "engine/search_engine_data.hpp" | ||||
| #include "util/make_unique.hpp" | ||||
| #include "util/string_util.hpp" | ||||
| #include "osrm/json_container.hpp" | ||||
| 
 | ||||
| #include <cstdlib> | ||||
| 
 | ||||
| #include <algorithm> | ||||
| #include <memory> | ||||
| #include <string> | ||||
| #include <vector> | ||||
| 
 | ||||
| namespace osrm | ||||
| { | ||||
| namespace engine | ||||
| { | ||||
| namespace plugins | ||||
| { | ||||
| 
 | ||||
| class DistanceTablePlugin final : public BasePlugin | ||||
| { | ||||
|   private: | ||||
|     SearchEngineData heaps; | ||||
|     routing_algorithms::ManyToManyRouting<datafacade::BaseDataFacade> distance_table; | ||||
|     int max_locations_distance_table; | ||||
| 
 | ||||
|   public: | ||||
|     explicit DistanceTablePlugin(datafacade::BaseDataFacade *facade, | ||||
|                                  const int max_locations_distance_table) | ||||
|         : BasePlugin{*facade}, distance_table(facade, heaps), | ||||
|           max_locations_distance_table(max_locations_distance_table) | ||||
|     { | ||||
|     } | ||||
| 
 | ||||
|     Status HandleRequest(const api::TableParameters ¶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); | ||||
| 
 | ||||
|         if (max_locations_distance_table > 0 && | ||||
|             (params.sources.size() * params.destinations.size() > | ||||
|              max_locations_distance_table * max_locations_distance_table)) | ||||
|           return Error("invalid-options", "Number of entries " + std::to_string(params.sources.size() * params.destinations.size()) + | ||||
|                 " is higher than current maximum (" + | ||||
|                 std::to_string(max_locations_distance_table * max_locations_distance_table) + ")", result); | ||||
| 
 | ||||
|         const bool checksum_OK = (params.check_sum == BasePlugin::facade.GetCheckSum()); | ||||
| 
 | ||||
|         std::vector<PhantomNodePair> phantom_node_source_vector(params.sources.size()); | ||||
|         std::vector<PhantomNodePair> phantom_node_target_vector(params.destinations.size()); | ||||
|         auto phantom_node_source_out_iter = phantom_node_source_vector.begin(); | ||||
|         auto phantom_node_target_out_iter = phantom_node_target_vector.begin(); | ||||
|         for (const auto i : util::irange<std::size_t>(0u, params.coordinates.size())) | ||||
|         { | ||||
|             if (checksum_OK && i < params.hints.size() && !params.hints[i].empty()) | ||||
|             { | ||||
|                 PhantomNode current_phantom_node; | ||||
|                 ObjectEncoder::DecodeFromBase64(params.hints[i], current_phantom_node); | ||||
|                 if (current_phantom_node.IsValid(BasePlugin::facade.GetNumberOfNodes())) | ||||
|                 { | ||||
|                     if (params.is_source[i]) | ||||
|                     { | ||||
|                         *phantom_node_source_out_iter = | ||||
|                             std::make_pair(current_phantom_node, current_phantom_node); | ||||
|                         if (params.is_destination[i]) | ||||
|                         { | ||||
|                             *phantom_node_target_out_iter = *phantom_node_source_out_iter; | ||||
|                             phantom_node_target_out_iter++; | ||||
|                         } | ||||
|                         phantom_node_source_out_iter++; | ||||
|                     } | ||||
|                     else | ||||
|                     { | ||||
|                         BOOST_ASSERT(params.is_destination[i] && !params.is_source[i]); | ||||
|                         *phantom_node_target_out_iter = | ||||
|                             std::make_pair(current_phantom_node, current_phantom_node); | ||||
|                         phantom_node_target_out_iter++; | ||||
|                     } | ||||
|                     continue; | ||||
|                 } | ||||
|             } | ||||
|             const int bearing = params.bearings.size() > 0 ? params.bearings[i].first : 0; | ||||
|             const int range = params.bearings.size() > 0 | ||||
|                                   ? (params.bearings[i].second ? *param_bearings[i].second : 10) | ||||
|                                   : 180; | ||||
|             if (params.is_source[i]) | ||||
|             { | ||||
|                 *phantom_node_source_out_iter = | ||||
|                     BasePlugin::facade.NearestPhantomNodeWithAlternativeFromBigComponent( | ||||
|                         params.coordinates[i], bearing, range); | ||||
|                 // we didn't found a fitting node, return error
 | ||||
|                 if (!phantom_node_source_out_iter->first.IsValid( | ||||
|                         BasePlugin::facade.GetNumberOfNodes())) | ||||
|                 { | ||||
|                     result.values["status_message"] = | ||||
|                         std::string("Could not find a matching segment for coordinate ") + | ||||
|                         std::to_string(i); | ||||
|                     return Status::NoSegment; | ||||
|                 } | ||||
| 
 | ||||
|                 if (params.is_destination[i]) | ||||
|                 { | ||||
|                     *phantom_node_target_out_iter = *phantom_node_source_out_iter; | ||||
|                     phantom_node_target_out_iter++; | ||||
|                 } | ||||
|                 phantom_node_source_out_iter++; | ||||
|             } | ||||
|             else | ||||
|             { | ||||
|                 BOOST_ASSERT(params.is_destination[i] && !params.is_source[i]); | ||||
| 
 | ||||
|                 *phantom_node_target_out_iter = | ||||
|                     BasePlugin::facade.NearestPhantomNodeWithAlternativeFromBigComponent( | ||||
|                         params.coordinates[i], bearing, range); | ||||
|                 // we didn't found a fitting node, return error
 | ||||
|                 if (!phantom_node_target_out_iter->first.IsValid( | ||||
|                         BasePlugin::facade.GetNumberOfNodes())) | ||||
|                 { | ||||
|                     result.values["status_message"] = | ||||
|                         std::string("Could not find a matching segment for coordinate ") + | ||||
|                         std::to_string(i); | ||||
|                     return Status::NoSegment; | ||||
|                 } | ||||
|                 phantom_node_target_out_iter++; | ||||
|             } | ||||
|         } | ||||
| 
 | ||||
|         BOOST_ASSERT((phantom_node_source_out_iter - phantom_node_source_vector.begin()) == | ||||
|                      params.sources.size()); | ||||
|         BOOST_ASSERT((phantom_node_target_out_iter - phantom_node_target_vector.begin()) == | ||||
|                      params.destinations.size()); | ||||
| 
 | ||||
|         // FIXME we should clear phantom_node_source_vector and phantom_node_target_vector after
 | ||||
|         // this
 | ||||
|         auto snapped_source_phantoms = snapPhantomNodes(phantom_node_source_vector); | ||||
|         auto snapped_target_phantoms = snapPhantomNodes(phantom_node_target_vector); | ||||
| 
 | ||||
|         auto result_table = distance_table(snapped_source_phantoms, snapped_target_phantoms); | ||||
| 
 | ||||
|         if (!result_table) | ||||
|         { | ||||
|             result.values["status_message"] = "No distance table found"; | ||||
|             return Status::EmptyResult; | ||||
|         } | ||||
| 
 | ||||
|         util::json::Array matrix_json_array; | ||||
|         for (const auto row : util::irange<std::size_t>(0, params.sources.size())) | ||||
|         { | ||||
|             util::json::Array json_row; | ||||
|             auto row_begin_iterator = result_table->begin() + (row * params.destinations.size()); | ||||
|             auto row_end_iterator = | ||||
|                 result_table->begin() + ((row + 1) * params.destinations.size()); | ||||
|             json_row.values.insert(json_row.values.end(), row_begin_iterator, row_end_iterator); | ||||
|             matrix_json_array.values.push_back(json_row); | ||||
|         } | ||||
|         result.values["distance_table"] = matrix_json_array; | ||||
| 
 | ||||
|         util::json::Array target_coord_json_array; | ||||
|         for (const auto &phantom : snapped_target_phantoms) | ||||
|         { | ||||
|             util::json::Array json_coord; | ||||
|             json_coord.values.push_back(phantom.location.lat / COORDINATE_PRECISION); | ||||
|             json_coord.values.push_back(phantom.location.lon / COORDINATE_PRECISION); | ||||
|             target_coord_json_array.values.push_back(json_coord); | ||||
|         } | ||||
|         result.values["destination_coordinates"] = target_coord_json_array; | ||||
|         util::json::Array source_coord_json_array; | ||||
|         for (const auto &phantom : snapped_source_phantoms) | ||||
|         { | ||||
|             util::json::Array json_coord; | ||||
|             json_coord.values.push_back(phantom.location.lat / COORDINATE_PRECISION); | ||||
|             json_coord.values.push_back(phantom.location.lon / COORDINATE_PRECISION); | ||||
|             source_coord_json_array.values.push_back(json_coord); | ||||
|         } | ||||
|         result.values["source_coordinates"] = source_coord_json_array; | ||||
|         return Status::Ok; | ||||
|     } | ||||
| }; | ||||
| } | ||||
| } | ||||
| } | ||||
| 
 | ||||
| #endif // DISTANCE_TABLE_HPP
 | ||||
							
								
								
									
										35
									
								
								include/engine/plugins/table.hpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										35
									
								
								include/engine/plugins/table.hpp
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,35 @@ | ||||
| #ifndef TABLE_HPP | ||||
| #define TABLE_HPP | ||||
| 
 | ||||
| #include "engine/plugins/plugin_base.hpp" | ||||
| 
 | ||||
| #include "engine/api/table_parameters.hpp" | ||||
| #include "engine/routing_algorithms/many_to_many.hpp" | ||||
| #include "engine/search_engine_data.hpp" | ||||
| #include "util/json_container.hpp" | ||||
| 
 | ||||
| namespace osrm | ||||
| { | ||||
| namespace engine | ||||
| { | ||||
| 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); | ||||
| }; | ||||
| } | ||||
| } | ||||
| } | ||||
| 
 | ||||
| #endif // TABLE_HPP
 | ||||
| @ -44,15 +44,14 @@ class ManyToManyRouting final | ||||
|     { | ||||
|     } | ||||
| 
 | ||||
|     std::shared_ptr<std::vector<EdgeWeight>> | ||||
|     operator()(const std::vector<PhantomNode> &phantom_sources_array, | ||||
|                const std::vector<PhantomNode> &phantom_targets_array) const | ||||
|     // semmetric version
 | ||||
|     std::vector<EdgeWeight> operator()(const std::vector<PhantomNode> &phantom_nodes) const | ||||
|     { | ||||
|         const auto number_of_sources = phantom_sources_array.size(); | ||||
|         const auto number_of_targets = phantom_targets_array.size(); | ||||
|         std::shared_ptr<std::vector<EdgeWeight>> result_table = | ||||
|             std::make_shared<std::vector<EdgeWeight>>(number_of_targets * number_of_sources, | ||||
|                                                       std::numeric_limits<EdgeWeight>::max()); | ||||
|         const auto number_of_sources = phantom_nodes.size(); | ||||
|         const auto number_of_targets = phantom_nodes.size(); | ||||
|         const auto number_of_entries = number_of_sources * number_of_targets; | ||||
|         std::vector<EdgeWeight> result_table(number_of_entries, | ||||
|                                              std::numeric_limits<EdgeWeight>::max()); | ||||
| 
 | ||||
|         engine_working_data.InitializeOrClearFirstThreadLocalStorage( | ||||
|             super::facade->GetNumberOfNodes()); | ||||
| @ -61,8 +60,8 @@ class ManyToManyRouting final | ||||
| 
 | ||||
|         SearchSpaceWithBuckets search_space_with_buckets; | ||||
| 
 | ||||
|         unsigned target_id = 0; | ||||
|         for (const auto &phantom : phantom_targets_array) | ||||
|         unsigned column_idx = 0; | ||||
|         for (const auto &phantom : phantom_nodes) | ||||
|         { | ||||
|             query_heap.Clear(); | ||||
|             // insert target(s) at distance 0
 | ||||
| @ -81,14 +80,14 @@ class ManyToManyRouting final | ||||
|             // explore search space
 | ||||
|             while (!query_heap.Empty()) | ||||
|             { | ||||
|                 BackwardRoutingStep(target_id, query_heap, search_space_with_buckets); | ||||
|                 BackwardRoutingStep(column_idx, query_heap, search_space_with_buckets); | ||||
|             } | ||||
|             ++target_id; | ||||
|             ++column_idx; | ||||
|         } | ||||
| 
 | ||||
|         // for each source do forward search
 | ||||
|         unsigned source_id = 0; | ||||
|         for (const auto &phantom : phantom_sources_array) | ||||
|         unsigned row_idx = 0; | ||||
|         for (const auto &phantom : phantom_nodes) | ||||
|         { | ||||
|             query_heap.Clear(); | ||||
|             // insert target(s) at distance 0
 | ||||
| @ -107,21 +106,95 @@ class ManyToManyRouting final | ||||
|             // explore search space
 | ||||
|             while (!query_heap.Empty()) | ||||
|             { | ||||
|                 ForwardRoutingStep(source_id, number_of_targets, query_heap, | ||||
|                 ForwardRoutingStep(row_idx, number_of_targets, query_heap, | ||||
|                                    search_space_with_buckets, result_table); | ||||
|             } | ||||
| 
 | ||||
|             ++source_id; | ||||
|             ++row_idx; | ||||
|         } | ||||
|         // BOOST_ASSERT(source_id == target_id);
 | ||||
| 
 | ||||
|         return result_table; | ||||
|     } | ||||
| 
 | ||||
|     void ForwardRoutingStep(const unsigned source_id, | ||||
|     // asymmetric version
 | ||||
|     std::vector<EdgeWeight> operator()(const std::vector<PhantomNode> &phantom_nodes, | ||||
|                                        const std::vector<std::size_t> &source_indices, | ||||
|                                        const std::vector<std::size_t> &target_indices) const | ||||
|     { | ||||
|         const auto number_of_sources = source_indices.size(); | ||||
|         const auto number_of_targets = target_indices.size(); | ||||
|         const auto number_of_entries = number_of_sources * number_of_targets; | ||||
|         std::vector<EdgeWeight> result_table(number_of_entries, | ||||
|                                              std::numeric_limits<EdgeWeight>::max()); | ||||
| 
 | ||||
|         engine_working_data.InitializeOrClearFirstThreadLocalStorage( | ||||
|             super::facade->GetNumberOfNodes()); | ||||
| 
 | ||||
|         QueryHeap &query_heap = *(engine_working_data.forward_heap_1); | ||||
| 
 | ||||
|         SearchSpaceWithBuckets search_space_with_buckets; | ||||
| 
 | ||||
|         unsigned column_idx = 0; | ||||
|         for (const auto index : target_indices) | ||||
|         { | ||||
|             const auto &phantom = phantom_nodes[index]; | ||||
|             query_heap.Clear(); | ||||
|             // insert target(s) at distance 0
 | ||||
| 
 | ||||
|             if (SPECIAL_NODEID != phantom.forward_node_id) | ||||
|             { | ||||
|                 query_heap.Insert(phantom.forward_node_id, phantom.GetForwardWeightPlusOffset(), | ||||
|                                   phantom.forward_node_id); | ||||
|             } | ||||
|             if (SPECIAL_NODEID != phantom.reverse_node_id) | ||||
|             { | ||||
|                 query_heap.Insert(phantom.reverse_node_id, phantom.GetReverseWeightPlusOffset(), | ||||
|                                   phantom.reverse_node_id); | ||||
|             } | ||||
| 
 | ||||
|             // explore search space
 | ||||
|             while (!query_heap.Empty()) | ||||
|             { | ||||
|                 BackwardRoutingStep(column_idx, query_heap, search_space_with_buckets); | ||||
|             } | ||||
|             ++column_idx; | ||||
|         } | ||||
| 
 | ||||
|         // for each source do forward search
 | ||||
|         unsigned row_idx = 0; | ||||
|         for (const auto index : source_indices) | ||||
|         { | ||||
|             const auto &phantom = phantom_nodes[index]; | ||||
|             query_heap.Clear(); | ||||
|             // insert target(s) at distance 0
 | ||||
| 
 | ||||
|             if (SPECIAL_NODEID != phantom.forward_node_id) | ||||
|             { | ||||
|                 query_heap.Insert(phantom.forward_node_id, -phantom.GetForwardWeightPlusOffset(), | ||||
|                                   phantom.forward_node_id); | ||||
|             } | ||||
|             if (SPECIAL_NODEID != phantom.reverse_node_id) | ||||
|             { | ||||
|                 query_heap.Insert(phantom.reverse_node_id, -phantom.GetReverseWeightPlusOffset(), | ||||
|                                   phantom.reverse_node_id); | ||||
|             } | ||||
| 
 | ||||
|             // explore search space
 | ||||
|             while (!query_heap.Empty()) | ||||
|             { | ||||
|                 ForwardRoutingStep(row_idx, number_of_targets, query_heap, | ||||
|                                    search_space_with_buckets, result_table); | ||||
|             } | ||||
|             ++row_idx; | ||||
|         } | ||||
| 
 | ||||
|         return result_table; | ||||
|     } | ||||
| 
 | ||||
|     void ForwardRoutingStep(const unsigned row_idx, | ||||
|                             const unsigned number_of_targets, | ||||
|                             QueryHeap &query_heap, | ||||
|                             const SearchSpaceWithBuckets &search_space_with_buckets, | ||||
|                             std::shared_ptr<std::vector<EdgeWeight>> result_table) const | ||||
|                             std::vector<EdgeWeight> &result_table) const | ||||
|     { | ||||
|         const NodeID node = query_heap.DeleteMin(); | ||||
|         const int source_distance = query_heap.GetKey(node); | ||||
| @ -135,9 +208,9 @@ class ManyToManyRouting final | ||||
|             for (const NodeBucket ¤t_bucket : bucket_list) | ||||
|             { | ||||
|                 // get target id from bucket entry
 | ||||
|                 const unsigned target_id = current_bucket.target_id; | ||||
|                 const unsigned column_idx = current_bucket.target_id; | ||||
|                 const int target_distance = current_bucket.distance; | ||||
|                 auto ¤t_distance = (*result_table)[source_id * number_of_targets + target_id]; | ||||
|                 auto ¤t_distance = result_table[row_idx * number_of_targets + column_idx]; | ||||
|                 // check if new distance is better
 | ||||
|                 const EdgeWeight new_distance = source_distance + target_distance; | ||||
|                 if (new_distance < 0) | ||||
| @ -151,7 +224,7 @@ class ManyToManyRouting final | ||||
|                 } | ||||
|                 else if (new_distance < current_distance) | ||||
|                 { | ||||
|                     (*result_table)[source_id * number_of_targets + target_id] = new_distance; | ||||
|                     result_table[row_idx * number_of_targets + column_idx] = new_distance; | ||||
|                 } | ||||
|             } | ||||
|         } | ||||
| @ -162,7 +235,7 @@ class ManyToManyRouting final | ||||
|         RelaxOutgoingEdges<true>(node, source_distance, query_heap); | ||||
|     } | ||||
| 
 | ||||
|     void BackwardRoutingStep(const unsigned target_id, | ||||
|     void BackwardRoutingStep(const unsigned column_idx, | ||||
|                              QueryHeap &query_heap, | ||||
|                              SearchSpaceWithBuckets &search_space_with_buckets) const | ||||
|     { | ||||
| @ -170,7 +243,7 @@ class ManyToManyRouting final | ||||
|         const int target_distance = query_heap.GetKey(node); | ||||
| 
 | ||||
|         // store settled nodes in search space bucket
 | ||||
|         search_space_with_buckets[node].emplace_back(target_id, target_distance); | ||||
|         search_space_with_buckets[node].emplace_back(column_idx, target_distance); | ||||
| 
 | ||||
|         if (StallAtNode<false>(node, target_distance, query_heap)) | ||||
|         { | ||||
|  | ||||
| @ -3,7 +3,7 @@ | ||||
| #include "engine/api/route_parameters.hpp" | ||||
| #include "engine/status.hpp" | ||||
| 
 | ||||
| #include "engine/plugins/distance_table.hpp" | ||||
| #include "engine/plugins/table.hpp" | ||||
| //#include "engine/plugins/hello_world.hpp"
 | ||||
| //#include "engine/plugins/nearest.hpp"
 | ||||
| //#include "engine/plugins/timestamp.hpp"
 | ||||
| @ -119,6 +119,7 @@ namespace osrm | ||||
| { | ||||
| namespace engine | ||||
| { | ||||
| 
 | ||||
| Engine::Engine(EngineConfig &config) | ||||
| { | ||||
|     if (config.use_shared_memory) | ||||
| @ -134,11 +135,23 @@ Engine::Engine(EngineConfig &config) | ||||
| 
 | ||||
|     route_plugin = util::make_unique<plugins::ViaRoutePlugin>(*query_data_facade, | ||||
|                                                               config.max_locations_viaroute); | ||||
|     table_plugin = util::make_unique<plugins::TablePlugin>(*query_data_facade, | ||||
|                                                               config.max_locations_distance_table); | ||||
| } | ||||
| 
 | ||||
| // make sure we deallocate the unique ptr at a position where we know the size of the plugins
 | ||||
| Engine::~Engine() | ||||
| { | ||||
| } | ||||
| 
 | ||||
| Status Engine::Route(const api::RouteParameters &route_parameters, util::json::Object &result) | ||||
| { | ||||
|     return RunQuery(lock, *query_data_facade, route_parameters, *route_plugin, result); | ||||
| } | ||||
| 
 | ||||
| Status Engine::Table(const api::TableParameters &table_parameters, util::json::Object &result) | ||||
| { | ||||
|     return RunQuery(lock, *query_data_facade, table_parameters, *table_plugin, result); | ||||
| } | ||||
| } // engine ns
 | ||||
| } // osrm ns
 | ||||
|  | ||||
							
								
								
									
										142
									
								
								src/engine/plugins/table.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										142
									
								
								src/engine/plugins/table.cpp
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,142 @@ | ||||
| #include "engine/plugins/table.hpp" | ||||
| 
 | ||||
| #include "engine/api/table_parameters.hpp" | ||||
| #include "engine/object_encoder.hpp" | ||||
| #include "engine/routing_algorithms/many_to_many.hpp" | ||||
| #include "engine/search_engine_data.hpp" | ||||
| #include "util/string_util.hpp" | ||||
| #include "util/json_container.hpp" | ||||
| 
 | ||||
| #include <cstdlib> | ||||
| 
 | ||||
| #include <algorithm> | ||||
| #include <memory> | ||||
| #include <string> | ||||
| #include <vector> | ||||
| 
 | ||||
| namespace osrm | ||||
| { | ||||
| namespace engine | ||||
| { | ||||
| namespace plugins | ||||
| { | ||||
| 
 | ||||
| TablePlugin::TablePlugin(datafacade::BaseDataFacade &facade, const int max_locations_distance_table) | ||||
|     : BasePlugin{facade}, distance_table(&facade, heaps), | ||||
|       max_locations_distance_table(max_locations_distance_table) | ||||
| { | ||||
| } | ||||
| 
 | ||||
| Status TablePlugin::HandleRequest(const api::TableParameters ¶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); | ||||
|     } | ||||
| 
 | ||||
|     if (max_locations_distance_table > 0 && | ||||
|         (params.sources.size() * params.destinations.size() > | ||||
|          static_cast<std::size_t>(max_locations_distance_table * max_locations_distance_table))) | ||||
|     { | ||||
|         return Error( | ||||
|             "invalid-options", | ||||
|             "Number of entries " + | ||||
|                 std::to_string(params.sources.size() * params.destinations.size()) + | ||||
|                 " is higher than current maximum (" + | ||||
|                 std::to_string(max_locations_distance_table * max_locations_distance_table) + ")", | ||||
|             result); | ||||
|     } | ||||
| 
 | ||||
|     auto snapped_phantoms = SnapPhantomNodes(GetPhantomNodes(params)); | ||||
| 
 | ||||
|     const auto result_table = [&]() | ||||
|     { | ||||
|         if (params.sources.empty()) | ||||
|         { | ||||
|             BOOST_ASSERT(params.destinations.empty()); | ||||
|             return distance_table(snapped_phantoms); | ||||
|         } | ||||
|         else | ||||
|         { | ||||
|             return distance_table(snapped_phantoms, params.sources, params.destinations); | ||||
|         } | ||||
|     }(); | ||||
| 
 | ||||
|     if (result_table.empty()) | ||||
|     { | ||||
|         return Error("no-table", "No table found", result); | ||||
|     } | ||||
| 
 | ||||
|     util::json::Array matrix_json_array; | ||||
|     for (const auto row : util::irange<std::size_t>(0, params.sources.size())) | ||||
|     { | ||||
|         util::json::Array json_row; | ||||
|         auto row_begin_iterator = result_table.begin() + (row * params.destinations.size()); | ||||
|         auto row_end_iterator = result_table.begin() + ((row + 1) * params.destinations.size()); | ||||
|         json_row.values.insert(json_row.values.end(), row_begin_iterator, row_end_iterator); | ||||
|         matrix_json_array.values.push_back(std::move(json_row)); | ||||
|     } | ||||
|     result.values["distance_table"] = matrix_json_array; | ||||
| 
 | ||||
|     // symmetric case
 | ||||
|     if (params.sources.empty()) | ||||
|     { | ||||
|         BOOST_ASSERT(params.destinations.empty()); | ||||
|         util::json::Array target_coord_json_array; | ||||
|         for (const auto &phantom : snapped_phantoms) | ||||
|         { | ||||
|             util::json::Array json_coord; | ||||
|             json_coord.values.push_back(phantom.location.lat / COORDINATE_PRECISION); | ||||
|             json_coord.values.push_back(phantom.location.lon / COORDINATE_PRECISION); | ||||
|             target_coord_json_array.values.push_back(std::move(json_coord)); | ||||
|         } | ||||
|         result.values["destination_coordinates"] = std::move(target_coord_json_array); | ||||
|         util::json::Array source_coord_json_array; | ||||
|         for (const auto &phantom : snapped_phantoms) | ||||
|         { | ||||
|             util::json::Array json_coord; | ||||
|             json_coord.values.push_back(phantom.location.lat / COORDINATE_PRECISION); | ||||
|             json_coord.values.push_back(phantom.location.lon / COORDINATE_PRECISION); | ||||
|             source_coord_json_array.values.push_back(std::move(json_coord)); | ||||
|         } | ||||
|         result.values["source_coordinates"] = std::move(source_coord_json_array); | ||||
|     } | ||||
|     // asymmetric case
 | ||||
|     else | ||||
|     { | ||||
|         BOOST_ASSERT(!params.destinations.empty()); | ||||
| 
 | ||||
|         util::json::Array target_coord_json_array; | ||||
|         for (const auto index : params.sources) | ||||
|         { | ||||
|             const auto &phantom = snapped_phantoms[index]; | ||||
|             util::json::Array json_coord; | ||||
|             json_coord.values.push_back(phantom.location.lat / COORDINATE_PRECISION); | ||||
|             json_coord.values.push_back(phantom.location.lon / COORDINATE_PRECISION); | ||||
|             target_coord_json_array.values.push_back(std::move(json_coord)); | ||||
|         } | ||||
|         result.values["destination_coordinates"] = std::move(target_coord_json_array); | ||||
|         util::json::Array source_coord_json_array; | ||||
|         for (const auto index : params.sources) | ||||
|         { | ||||
|             const auto &phantom = snapped_phantoms[index]; | ||||
|             util::json::Array json_coord; | ||||
|             json_coord.values.push_back(phantom.location.lat / COORDINATE_PRECISION); | ||||
|             json_coord.values.push_back(phantom.location.lon / COORDINATE_PRECISION); | ||||
|             source_coord_json_array.values.push_back(std::move(json_coord)); | ||||
|         } | ||||
|         result.values["source_coordinates"] = std::move(source_coord_json_array); | ||||
|     } | ||||
|     return Status::Ok; | ||||
| } | ||||
| } | ||||
| } | ||||
| } | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user