osrm-backend/include/engine/api/table_api.hpp

135 lines
4.5 KiB
C++
Raw Normal View History

2016-02-15 21:20:22 -05:00
#ifndef ENGINE_API_TABLE_HPP
#define ENGINE_API_TABLE_HPP
#include "engine/api/base_api.hpp"
#include "engine/api/json_factory.hpp"
2016-05-27 15:05:04 -04:00
#include "engine/api/table_parameters.hpp"
2016-02-15 21:20:22 -05:00
#include "engine/datafacade/datafacade_base.hpp"
#include "engine/guidance/assemble_geometry.hpp"
2016-05-27 15:05:04 -04:00
#include "engine/guidance/assemble_leg.hpp"
2016-02-15 21:20:22 -05:00
#include "engine/guidance/assemble_overview.hpp"
2016-05-27 15:05:04 -04:00
#include "engine/guidance/assemble_route.hpp"
2016-02-15 21:20:22 -05:00
#include "engine/guidance/assemble_steps.hpp"
#include "engine/internal_route_result.hpp"
#include "util/integer_range.hpp"
#include <boost/range/algorithm/transform.hpp>
2016-04-12 09:00:08 -04:00
#include <iterator>
2016-02-15 21:20:22 -05:00
namespace osrm
{
namespace engine
{
namespace api
{
class TableAPI final : public BaseAPI
2016-02-15 21:20:22 -05:00
{
public:
TableAPI(const datafacade::BaseDataFacade &facade_, const TableParameters &parameters_)
: BaseAPI(facade_, parameters_), parameters(parameters_)
2016-02-15 21:20:22 -05:00
{
}
virtual void MakeResponse(const std::vector<EdgeWeight> &durations,
const std::vector<PhantomNode> &phantoms,
util::json::Object &response) const
{
auto number_of_sources = parameters.sources.size();
2016-03-03 08:26:13 -05:00
auto number_of_destinations = parameters.destinations.size();
;
2016-02-15 21:20:22 -05:00
// symmetric case
if (parameters.sources.empty())
{
response.values["sources"] = MakeWaypoints(phantoms);
number_of_sources = phantoms.size();
2016-02-15 21:20:22 -05:00
}
else
{
response.values["sources"] = MakeWaypoints(phantoms, parameters.sources);
}
if (parameters.destinations.empty())
{
response.values["destinations"] = MakeWaypoints(phantoms);
number_of_destinations = phantoms.size();
}
else
{
2016-02-15 21:20:22 -05:00
response.values["destinations"] = MakeWaypoints(phantoms, parameters.destinations);
}
2016-03-03 08:26:13 -05:00
response.values["durations"] =
MakeTable(durations, number_of_sources, number_of_destinations);
2016-04-04 08:05:38 -04:00
response.values["code"] = "Ok";
2016-02-15 21:20:22 -05:00
}
protected:
2016-02-15 21:20:22 -05:00
virtual util::json::Array MakeWaypoints(const std::vector<PhantomNode> &phantoms) const
{
util::json::Array json_waypoints;
json_waypoints.values.reserve(phantoms.size());
BOOST_ASSERT(phantoms.size() == parameters.coordinates.size());
2016-05-27 15:05:04 -04:00
boost::range::transform(
phantoms,
std::back_inserter(json_waypoints.values),
[this](const PhantomNode &phantom) { return BaseAPI::MakeWaypoint(phantom); });
2016-02-15 21:20:22 -05:00
return json_waypoints;
}
virtual util::json::Array MakeWaypoints(const std::vector<PhantomNode> &phantoms,
const std::vector<std::size_t> &indices) const
{
util::json::Array json_waypoints;
json_waypoints.values.reserve(indices.size());
2016-05-27 15:05:04 -04:00
boost::range::transform(indices,
std::back_inserter(json_waypoints.values),
[this, phantoms](const std::size_t idx) {
BOOST_ASSERT(idx < phantoms.size());
return BaseAPI::MakeWaypoint(phantoms[idx]);
});
2016-02-15 21:20:22 -05:00
return json_waypoints;
}
virtual util::json::Array MakeTable(const std::vector<EdgeWeight> &values,
std::size_t number_of_rows,
std::size_t number_of_columns) const
{
util::json::Array json_table;
for (const auto row : util::irange<std::size_t>(0UL, number_of_rows))
2016-02-15 21:20:22 -05:00
{
util::json::Array json_row;
auto row_begin_iterator = values.begin() + (row * number_of_columns);
auto row_end_iterator = values.begin() + ((row + 1) * number_of_columns);
json_row.values.resize(number_of_columns);
2016-05-27 15:05:04 -04:00
std::transform(row_begin_iterator,
row_end_iterator,
json_row.values.begin(),
[](const EdgeWeight duration) {
2016-03-03 08:26:13 -05:00
if (duration == INVALID_EDGE_WEIGHT)
{
return util::json::Value(util::json::Null());
}
return util::json::Value(util::json::Number(duration / 10.));
});
2016-02-15 21:20:22 -05:00
json_table.values.push_back(std::move(json_row));
}
return json_table;
}
const TableParameters &parameters;
};
} // ns api
} // ns engine
} // ns osrm
2016-02-15 21:20:22 -05:00
#endif