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

176 lines
6.6 KiB
C++
Raw Normal View History

2016-01-28 10:28:44 -05:00
#ifndef ENGINE_API_BASE_API_HPP
#define ENGINE_API_BASE_API_HPP
#include "engine/api/base_parameters.hpp"
#include "engine/api/flatbuffers/fbresult_generated.h"
2016-01-28 10:28:44 -05:00
#include "engine/datafacade/datafacade_base.hpp"
#include "engine/api/json_factory.hpp"
#include "engine/hint.hpp"
#include "util/coordinate_calculation.hpp"
2016-01-28 10:28:44 -05:00
#include <boost/algorithm/string/join.hpp>
2016-01-28 10:28:44 -05:00
#include <boost/assert.hpp>
#include <boost/range/adaptor/transformed.hpp>
#include <boost/range/algorithm/transform.hpp>
2016-01-28 10:28:44 -05:00
#include <boost/range/adaptor/filtered.hpp>
#include <memory>
2016-01-28 10:28:44 -05:00
#include <vector>
namespace osrm
{
namespace engine
{
namespace api
{
static const constexpr char *INTERSECTION_DELIMITER = " / ";
class BaseAPI
2016-01-28 10:28:44 -05:00
{
public:
BaseAPI(const datafacade::BaseDataFacade &facade_, const BaseParameters &parameters_)
2016-01-28 10:28:44 -05:00
: facade(facade_), parameters(parameters_)
{
}
util::json::Array
MakeWaypoints(const std::vector<PhantomNodeCandidates> &waypoint_candidates) const
2016-01-28 10:28:44 -05:00
{
BOOST_ASSERT(parameters.coordinates.size() > 0);
BOOST_ASSERT(parameters.coordinates.size() == waypoint_candidates.size());
2016-01-28 10:28:44 -05:00
util::json::Array waypoints;
waypoints.values.resize(parameters.coordinates.size());
2016-05-27 15:05:04 -04:00
boost::range::transform(
waypoint_candidates,
waypoints.values.begin(),
[this](const PhantomNodeCandidates &candidates) { return MakeWaypoint(candidates); });
2016-01-28 10:28:44 -05:00
return waypoints;
}
// FIXME: gcc 4.9 does not like MakeWaypoints to be protected
// protected:
util::json::Object MakeWaypoint(const PhantomNodeCandidates &candidates) const
2016-01-28 10:28:44 -05:00
{
// TODO: check forward/reverse
const auto toName = [this](const auto &phantom) {
return std::string(facade.GetNameForID(facade.GetNameIndex(phantom.forward_segment_id.id)));
};
const auto noEmpty = [](const auto &name) { return !name.empty(); };
// At an intersection we may have multiple phantom node candidates.
// Combine them to represent the waypoint name.
std::string waypoint_name = boost::algorithm::join(
candidates | boost::adaptors::transformed(toName) | boost::adaptors::filtered(noEmpty),
INTERSECTION_DELIMITER);
const auto &snapped_location = candidatesSnappedLocation(candidates);
const auto &input_location = candidatesInputLocation(candidates);
if (parameters.generate_hints)
{
std::vector<SegmentHint> seg_hints(candidates.size());
std::transform(candidates.begin(),
candidates.end(),
seg_hints.begin(),
[this](const auto &phantom) {
return SegmentHint{phantom, facade.GetCheckSum()};
});
2017-05-11 03:13:59 -04:00
return json::makeWaypoint(
snapped_location,
util::coordinate_calculation::greatCircleDistance(snapped_location, input_location),
waypoint_name,
{std::move(seg_hints)});
}
else
{
2017-05-11 03:13:59 -04:00
return json::makeWaypoint(
snapped_location,
util::coordinate_calculation::greatCircleDistance(snapped_location, input_location),
waypoint_name);
}
2016-01-28 10:28:44 -05:00
}
2019-08-15 04:40:23 -04:00
flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<fbresult::Waypoint>>>
MakeWaypoints(flatbuffers::FlatBufferBuilder *builder,
const std::vector<PhantomNodeCandidates> &waypoint_candidates) const
{
BOOST_ASSERT(parameters.coordinates.size() > 0);
BOOST_ASSERT(parameters.coordinates.size() == waypoint_candidates.size());
std::vector<flatbuffers::Offset<fbresult::Waypoint>> waypoints;
waypoints.resize(parameters.coordinates.size());
std::transform(waypoint_candidates.begin(),
waypoint_candidates.end(),
waypoints.begin(),
[this, builder](const PhantomNodeCandidates &candidates) {
return MakeWaypoint(builder, candidates)->Finish();
2019-08-15 04:40:23 -04:00
});
return builder->CreateVector(waypoints);
}
// FIXME: gcc 4.9 does not like MakeWaypoints to be protected
// protected:
std::unique_ptr<fbresult::WaypointBuilder>
MakeWaypoint(flatbuffers::FlatBufferBuilder *builder,
const PhantomNodeCandidates &candidates) const
{
const auto &snapped_location = candidatesSnappedLocation(candidates);
const auto &input_location = candidatesInputLocation(candidates);
auto location = fbresult::Position(
static_cast<float>(static_cast<double>(util::toFloating(snapped_location.lon))),
static_cast<float>(static_cast<double>(util::toFloating(snapped_location.lat))));
const auto toName = [this](const auto &phantom) {
return std::string(facade.GetNameForID(facade.GetNameIndex(phantom.forward_segment_id.id)));
};
const auto noEmpty = [](const auto &name) { return !name.empty(); };
// At an intersection we may have multiple phantom node candidates.
// Combine them to represent the waypoint name.
std::string waypoint_name = boost::algorithm::join(
candidates | boost::adaptors::transformed(toName) | boost::adaptors::filtered(noEmpty),
INTERSECTION_DELIMITER);
auto name_string = builder->CreateString(waypoint_name);
flatbuffers::Offset<flatbuffers::String> hint_string;
if (parameters.generate_hints)
{
std::vector<SegmentHint> seg_hints(candidates.size());
std::transform(candidates.begin(),
candidates.end(),
seg_hints.begin(),
[this](const auto &phantom) {
return SegmentHint{phantom, facade.GetCheckSum()};
});
Hint hint{std::move(seg_hints)};
hint_string = builder->CreateString(hint.ToBase64());
}
auto waypoint = std::make_unique<fbresult::WaypointBuilder>(*builder);
waypoint->add_location(&location);
waypoint->add_distance(static_cast<float>(
util::coordinate_calculation::greatCircleDistance(snapped_location, input_location)));
waypoint->add_name(name_string);
if (parameters.generate_hints)
{
waypoint->add_hint(hint_string);
}
return waypoint;
}
2016-01-28 10:28:44 -05:00
const datafacade::BaseDataFacade &facade;
const BaseParameters &parameters;
};
} // namespace api
} // namespace engine
} // namespace osrm
2016-01-28 10:28:44 -05:00
#endif