Finish the nearest plugin

This commit is contained in:
Patrick Niklaus
2016-02-23 00:44:35 +01:00
parent 81319228bd
commit 14c36bc405
9 changed files with 363 additions and 72 deletions
+55
View File
@@ -0,0 +1,55 @@
#ifndef ENGINE_API_NEAREST_API_HPP
#define ENGINE_API_NEAREST_API_HPP
#include "engine/api/base_api.hpp"
#include "engine/api/nearest_parameters.hpp"
#include "engine/api/json_factory.hpp"
#include "engine/phantom_node.hpp"
#include <boost/assert.hpp>
#include <vector>
namespace osrm
{
namespace engine
{
namespace api
{
class NearestAPI final : public BaseAPI
{
public:
NearestAPI(const datafacade::BaseDataFacade &facade_, const NearestParameters &parameters_)
: BaseAPI(facade_, parameters_), parameters(parameters_)
{
}
void MakeResponse(const std::vector<std::vector<PhantomNodeWithDistance>> &phantom_nodes, util::json::Object& response) const
{
BOOST_ASSERT(phantom_nodes.size() == 1);
BOOST_ASSERT(parameters.coordinates.size() == 1);
util::json::Array waypoints;
waypoints.values.resize(phantom_nodes.front().size());
std::transform(phantom_nodes.front().begin(),
phantom_nodes.front().end(), waypoints.values.begin(), [this](const PhantomNodeWithDistance& phantom_with_distance)
{
auto waypoint = MakeWaypoint(parameters.coordinates.front(), phantom_with_distance.phantom_node);
waypoint.values["distance"] = phantom_with_distance.distance;
return waypoint;
});
response.values["code"] = "ok";
response.values["waypoints"] = std::move(waypoints);
}
const NearestParameters &parameters;
};
} // ns api
} // ns engine
} // ns osrm
#endif
+14 -6
View File
@@ -88,7 +88,6 @@ class BaseDataFacade
const float max_distance,
const int bearing,
const int bearing_range) = 0;
virtual std::vector<PhantomNodeWithDistance>
NearestPhantomNodesInRange(const util::FixedPointCoordinate input_coordinate,
const float max_distance) = 0;
@@ -96,21 +95,30 @@ class BaseDataFacade
virtual std::vector<PhantomNodeWithDistance>
NearestPhantomNodes(const util::FixedPointCoordinate input_coordinate,
const unsigned max_results,
const int bearing = 0,
const int bearing_range = 180) = 0;
const double max_distance,
const int bearing,
const int bearing_range) = 0;
virtual std::vector<PhantomNodeWithDistance>
NearestPhantomNodes(const util::FixedPointCoordinate input_coordinate,
const unsigned max_results,
const int bearing,
const int bearing_range) = 0;
virtual std::vector<PhantomNodeWithDistance>
NearestPhantomNodes(const util::FixedPointCoordinate input_coordinate,
const unsigned max_results) = 0;
virtual std::vector<PhantomNodeWithDistance>
NearestPhantomNodes(const util::FixedPointCoordinate input_coordinate,
const unsigned max_results, const double max_distance) = 0;
virtual std::pair<PhantomNode, PhantomNode> NearestPhantomNodeWithAlternativeFromBigComponent(
const util::FixedPointCoordinate input_coordinate) = 0;
virtual std::pair<PhantomNode, PhantomNode> NearestPhantomNodeWithAlternativeFromBigComponent(
const util::FixedPointCoordinate input_coordinate, const double max_distance) = 0;
virtual std::pair<PhantomNode, PhantomNode> NearestPhantomNodeWithAlternativeFromBigComponent(
const util::FixedPointCoordinate input_coordinate,
const double max_distance,
const int bearing,
const int bearing_range) = 0;
virtual std::pair<PhantomNode, PhantomNode> NearestPhantomNodeWithAlternativeFromBigComponent(
const util::FixedPointCoordinate input_coordinate,
const int bearing,
@@ -383,11 +383,38 @@ class InternalDataFacade final : public BaseDataFacade
bearing, bearing_range);
}
std::vector<PhantomNodeWithDistance>
NearestPhantomNodes(const util::FixedPointCoordinate input_coordinate,
const unsigned max_results) override final
{
if (!m_static_rtree.get())
{
LoadRTree();
BOOST_ASSERT(m_geospatial_query.get());
}
return m_geospatial_query->NearestPhantomNodes(input_coordinate, max_results);
}
std::vector<PhantomNodeWithDistance>
NearestPhantomNodes(const util::FixedPointCoordinate input_coordinate,
const unsigned max_results,
const int bearing = 0,
const int bearing_range = 180) override final
const double max_distance) override final
{
if (!m_static_rtree.get())
{
LoadRTree();
BOOST_ASSERT(m_geospatial_query.get());
}
return m_geospatial_query->NearestPhantomNodes(input_coordinate, max_results, max_distance);
}
std::vector<PhantomNodeWithDistance>
NearestPhantomNodes(const util::FixedPointCoordinate input_coordinate,
const unsigned max_results,
const int bearing,
const int bearing_range) override final
{
if (!m_static_rtree.get())
{
@@ -399,6 +426,23 @@ class InternalDataFacade final : public BaseDataFacade
bearing_range);
}
std::vector<PhantomNodeWithDistance>
NearestPhantomNodes(const util::FixedPointCoordinate input_coordinate,
const unsigned max_results,
const double max_distance,
const int bearing,
const int bearing_range) override final
{
if (!m_static_rtree.get())
{
LoadRTree();
BOOST_ASSERT(m_geospatial_query.get());
}
return m_geospatial_query->NearestPhantomNodes(input_coordinate, max_results, max_distance,
bearing, bearing_range);
}
std::pair<PhantomNode, PhantomNode> NearestPhantomNodeWithAlternativeFromBigComponent(
const util::FixedPointCoordinate input_coordinate, const double max_distance) override final
{
@@ -235,7 +235,8 @@ class SharedDataFacade final : public BaseDataFacade
}
data_timestamp_ptr = static_cast<storage::SharedDataTimestamp *>(
storage::makeSharedMemory(storage::CURRENT_REGIONS,
sizeof(storage::SharedDataTimestamp), false, false)->Ptr());
sizeof(storage::SharedDataTimestamp), false, false)
->Ptr());
CURRENT_LAYOUT = storage::LAYOUT_NONE;
CURRENT_DATA = storage::DATA_NONE;
CURRENT_TIMESTAMP = 0;
@@ -306,8 +307,8 @@ class SharedDataFacade final : public BaseDataFacade
LoadNames();
LoadCoreInformation();
util::SimpleLogger().Write()
<< "number of geometries: " << m_coordinate_list->size();
util::SimpleLogger().Write() << "number of geometries: "
<< m_coordinate_list->size();
for (unsigned i = 0; i < m_coordinate_list->size(); ++i)
{
if (!GetCoordinateOfNode(i).IsValid())
@@ -450,11 +451,38 @@ class SharedDataFacade final : public BaseDataFacade
bearing, bearing_range);
}
std::vector<PhantomNodeWithDistance>
NearestPhantomNodes(const util::FixedPointCoordinate input_coordinate,
const unsigned max_results) override final
{
if (!m_static_rtree.get() || CURRENT_TIMESTAMP != m_static_rtree->first)
{
LoadRTree();
BOOST_ASSERT(m_geospatial_query.get());
}
return m_geospatial_query->NearestPhantomNodes(input_coordinate, max_results);
}
std::vector<PhantomNodeWithDistance>
NearestPhantomNodes(const util::FixedPointCoordinate input_coordinate,
const unsigned max_results,
const int bearing = 0,
const int bearing_range = 180) override final
const double max_distance) override final
{
if (!m_static_rtree.get() || CURRENT_TIMESTAMP != m_static_rtree->first)
{
LoadRTree();
BOOST_ASSERT(m_geospatial_query.get());
}
return m_geospatial_query->NearestPhantomNodes(input_coordinate, max_results, max_distance);
}
std::vector<PhantomNodeWithDistance>
NearestPhantomNodes(const util::FixedPointCoordinate input_coordinate,
const unsigned max_results,
const int bearing,
const int bearing_range) override final
{
if (!m_static_rtree.get() || CURRENT_TIMESTAMP != m_static_rtree->first)
{
@@ -466,6 +494,23 @@ class SharedDataFacade final : public BaseDataFacade
bearing_range);
}
std::vector<PhantomNodeWithDistance>
NearestPhantomNodes(const util::FixedPointCoordinate input_coordinate,
const unsigned max_results,
const double max_distance,
const int bearing,
const int bearing_range) override final
{
if (!m_static_rtree.get() || CURRENT_TIMESTAMP != m_static_rtree->first)
{
LoadRTree();
BOOST_ASSERT(m_geospatial_query.get());
}
return m_geospatial_query->NearestPhantomNodes(input_coordinate, max_results, max_distance,
bearing, bearing_range);
}
std::pair<PhantomNode, PhantomNode> NearestPhantomNodeWithAlternativeFromBigComponent(
const util::FixedPointCoordinate input_coordinate) override final
{
@@ -523,7 +568,6 @@ class SharedDataFacade final : public BaseDataFacade
input_coordinate, bearing, bearing_range);
}
unsigned GetCheckSum() const override final { return m_check_sum; }
unsigned GetNameIndexFromEdgeID(const unsigned id) const override final
+63 -2
View File
@@ -82,8 +82,8 @@ template <typename RTreeT, typename DataFacadeT> class GeospatialQuery
std::vector<PhantomNodeWithDistance>
NearestPhantomNodes(const util::FixedPointCoordinate input_coordinate,
const unsigned max_results,
const int bearing = 0,
const int bearing_range = 180)
const int bearing,
const int bearing_range)
{
auto results = rtree.Nearest(input_coordinate,
[this, bearing, bearing_range](const EdgeData &data)
@@ -98,6 +98,67 @@ template <typename RTreeT, typename DataFacadeT> class GeospatialQuery
return MakePhantomNodes(input_coordinate, results);
}
// Returns max_results nearest PhantomNodes in the given bearing range within the maximum distance.
// Does not filter by small/big component!
std::vector<PhantomNodeWithDistance>
NearestPhantomNodes(const util::FixedPointCoordinate input_coordinate,
const unsigned max_results,
const double max_distance,
const int bearing,
const int bearing_range)
{
auto results = rtree.Nearest(input_coordinate,
[this, bearing, bearing_range](const EdgeData &data)
{
return checkSegmentBearing(data, bearing, bearing_range);
},
[max_results, max_distance](const std::size_t num_results, const double min_dist)
{
return num_results >= max_results || min_dist > max_distance;
});
return MakePhantomNodes(input_coordinate, results);
}
// Returns max_results nearest PhantomNodes.
// Does not filter by small/big component!
std::vector<PhantomNodeWithDistance>
NearestPhantomNodes(const util::FixedPointCoordinate input_coordinate,
const unsigned max_results)
{
auto results = rtree.Nearest(input_coordinate,
[](const EdgeData &)
{
return std::make_pair(true, true);
},
[max_results](const std::size_t num_results, const double)
{
return num_results >= max_results;
});
return MakePhantomNodes(input_coordinate, results);
}
// Returns max_results nearest PhantomNodes in the given max distance.
// Does not filter by small/big component!
std::vector<PhantomNodeWithDistance>
NearestPhantomNodes(const util::FixedPointCoordinate input_coordinate,
const unsigned max_results,
const double max_distance)
{
auto results = rtree.Nearest(input_coordinate,
[](const EdgeData &)
{
return std::make_pair(true, true);
},
[max_results, max_distance](const std::size_t num_results, const double min_dist)
{
return num_results >= max_results || min_dist > max_distance;
});
return MakePhantomNodes(input_coordinate, results);
}
// Returns the nearest phantom node. If this phantom node is not from a big component
// a second phantom node is return that is the nearest coordinate in a big component.
std::pair<PhantomNode, PhantomNode> NearestPhantomNodeWithAlternativeFromBigComponent(
+64
View File
@@ -150,6 +150,70 @@ class BasePlugin
return phantom_nodes;
}
std::vector<std::vector<PhantomNodeWithDistance>> GetPhantomNodes(const api::BaseParameters &parameters, unsigned number_of_results)
{
std::vector<std::vector<PhantomNodeWithDistance>> phantom_nodes(parameters.coordinates.size());
const bool use_hints = !parameters.hints.empty();
const bool use_bearings = !parameters.bearings.empty();
const bool use_radiuses = !parameters.radiuses.empty();
BOOST_ASSERT(parameters.IsValid());
for (const auto i : util::irange<std::size_t>(0, parameters.coordinates.size()))
{
if (use_hints && parameters.hints[i] &&
parameters.hints[i]->IsValid(parameters.coordinates[i], facade))
{
phantom_nodes[i].push_back(PhantomNodeWithDistance{
parameters.hints[i]->phantom,
util::coordinate_calculation::haversineDistance(
parameters.coordinates[i], parameters.hints[i]->phantom.location),
});
continue;
}
if (use_bearings && parameters.bearings[i])
{
if (use_radiuses && parameters.radiuses[i])
{
phantom_nodes[i] =
facade.NearestPhantomNodes(
parameters.coordinates[i], number_of_results, *parameters.radiuses[i],
parameters.bearings[i]->bearing, parameters.bearings[i]->range);
}
else
{
phantom_nodes[i] =
facade.NearestPhantomNodes(
parameters.coordinates[i], number_of_results, parameters.bearings[i]->bearing,
parameters.bearings[i]->range);
}
}
else
{
if (use_radiuses && parameters.radiuses[i])
{
phantom_nodes[i] =
facade.NearestPhantomNodes(
parameters.coordinates[i], number_of_results, *parameters.radiuses[i]);
}
else
{
phantom_nodes[i] =
facade.NearestPhantomNodes(
parameters.coordinates[i], number_of_results);
}
}
// we didn't found a fitting node, return error
if (phantom_nodes[i].empty())
{
break;
}
}
return phantom_nodes;
}
std::vector<PhantomNodePair> GetPhantomNodes(const api::BaseParameters &parameters)
{
std::vector<PhantomNodePair> phantom_node_pairs(parameters.coordinates.size());
@@ -26,7 +26,12 @@ struct NearestParametersGrammar final : public BaseParametersGrammar
NearestParametersGrammar() : BaseParametersGrammar(root_rule, parameters)
{
root_rule = "TODO(daniel-j-h)";
const auto set_number = [this](const unsigned number)
{
parameters.number_of_results = number;
};
nearest_rule = (qi::lit("number=") >> qi::uint_)[set_number];
root_rule = *(base_rule | nearest_rule);
}
engine::api::NearestParameters parameters;