Hook up map matching

This commit is contained in:
Patrick Niklaus 2016-02-22 17:33:31 +01:00
parent 35b098e656
commit b34f9b1795
6 changed files with 143 additions and 27 deletions

View File

@ -41,6 +41,7 @@ class MatchAPI final : public RouteAPI
sub_routes[index].source_traversed_in_reverse,
sub_routes[index].target_traversed_in_reverse);
route.values["confidence"] = sub_matchings[index].confidence;
routes.values.push_back(std::move(route));
}
response.values["tracepoints"] = MakeTracepoints(sub_matchings);
response.values["routes"] = std::move(routes);

View File

@ -14,6 +14,18 @@ namespace api
struct MatchParameters : public RouteParameters
{
MatchParameters()
: RouteParameters(false, false, RouteParameters::GeometriesType::Polyline, RouteParameters::OverviewType::Simplified, {})
{
}
template<typename... Args>
MatchParameters(std::vector<unsigned> timestamps_,
Args... args_)
: RouteParameters{std::forward<Args>(args_)...}, timestamps{std::move(timestamps_)}
{
}
std::vector<unsigned> timestamps;
bool IsValid() const
{

View File

@ -7,6 +7,7 @@
#include <boost/spirit/include/qi_lit.hpp>
#include <boost/spirit/include/qi_uint.hpp>
#include <boost/spirit/include/qi_bool.hpp>
#include <boost/spirit/include/qi_grammar.hpp>
#include <boost/spirit/include/qi_action.hpp>
#include <boost/spirit/include/qi_optional.hpp>
@ -23,16 +24,61 @@ namespace qi = boost::spirit::qi;
struct MatchParametersGrammar final : public BaseParametersGrammar
{
using Iterator = std::string::iterator;
using StepsT = bool;
using TimestampsT = std::vector<unsigned>;
using GeometriesT = engine::api::RouteParameters::GeometriesType;
using OverviewT = engine::api::RouteParameters::OverviewType;
MatchParametersGrammar() : BaseParametersGrammar(root_rule, parameters)
{
root_rule = "TODO(daniel-j-h)";
const auto set_geojson_type = [this]()
{
parameters.geometries = engine::api::RouteParameters::GeometriesType::GeoJSON;
};
const auto set_polyline_type = [this]()
{
parameters.geometries = engine::api::RouteParameters::GeometriesType::Polyline;
};
const auto set_simplified_type = [this]()
{
parameters.overview = engine::api::RouteParameters::OverviewType::Simplified;
};
const auto set_full_type = [this]()
{
parameters.overview = engine::api::RouteParameters::OverviewType::Full;
};
const auto set_false_type = [this]()
{
parameters.overview = engine::api::RouteParameters::OverviewType::False;
};
const auto set_steps = [this](const StepsT steps)
{
parameters.steps = steps;
};
const auto set_timestamps = [this](TimestampsT &timestamps)
{
parameters.timestamps = std::move(timestamps);
};
steps_rule = qi::lit("steps=") >> qi::bool_;
geometries_rule = qi::lit("geometries=geojson")[set_geojson_type] |
qi::lit("geometries=polyline")[set_polyline_type];
overview_rule = qi::lit("overview=simplified")[set_simplified_type] |
qi::lit("overview=full")[set_full_type] |
qi::lit("overview=false")[set_false_type];
timestamps_rule = qi::lit("timestamps=") >> qi::uint_ % ";";
match_rule = steps_rule[set_steps] | geometries_rule |
overview_rule | timestamps_rule[set_timestamps];
root_rule = -((base_rule | match_rule) % '&');
}
engine::api::MatchParameters parameters;
private:
qi::rule<Iterator> root_rule, match_rule;
qi::rule<Iterator> root_rule, match_rule, geometries_rule, overview_rule;
qi::rule<Iterator, TimestampsT()> timestamps_rule;
qi::rule<Iterator, StepsT()> steps_rule;
};
}
}

View File

@ -0,0 +1,31 @@
#include <boost/format.hpp>
namespace osrm
{
namespace server
{
namespace service
{
const constexpr char PARAMETER_SIZE_MISMATCH_MSG[] =
"Number of elements in %1% size %2% does not match coordinate size %3%";
template <typename ParamT>
bool constrainParamSize(const char *msg_template,
const char *name,
const ParamT &param,
const std::size_t target_size,
std::string &help)
{
if (param.size() > 0 && param.size() != target_size)
{
help = (boost::format(msg_template) % name % param.size() % target_size).str();
return true;
}
return false;
}
}
}
}

View File

@ -2,6 +2,7 @@
#include "engine/api/match_parameters.hpp"
#include "server/api/parameters_parser.hpp"
#include "server/service/utils.hpp"
#include "util/json_container.hpp"
@ -13,13 +14,60 @@ namespace server
{
namespace service
{
namespace
{
std::string getWrongOptionHelp(const engine::api::MatchParameters &parameters)
{
std::string help;
const auto coord_size = parameters.coordinates.size();
const bool param_size_mismatch = constrainParamSize(PARAMETER_SIZE_MISMATCH_MSG, "hints",
parameters.hints, coord_size, help) ||
constrainParamSize(PARAMETER_SIZE_MISMATCH_MSG, "bearings",
parameters.bearings, coord_size, help) ||
constrainParamSize(PARAMETER_SIZE_MISMATCH_MSG, "radiuses",
parameters.radiuses, coord_size, help) ||
constrainParamSize(PARAMETER_SIZE_MISMATCH_MSG, "timestamps",
parameters.timestamps, coord_size, help);
if (!param_size_mismatch && parameters.coordinates.size() < 2)
{
help = "Number of coordinates needs to be at least two.";
}
return help;
}
} // anon. ns
engine::Status MatchService::RunQuery(std::vector<util::FixedPointCoordinate> coordinates,
std::string &options,
util::json::Object &result)
{
// TODO(daniel-j-h)
return Status::Error;
auto options_iterator = options.begin();
auto parameters =
api::parseParameters<engine::api::MatchParameters>(options_iterator, options.end());
if (!parameters || options_iterator != options.end())
{
const auto position = std::distance(options.begin(), options_iterator);
result.values["code"] = "invalid-options";
result.values["message"] =
"Options string malformed close to position " + std::to_string(position);
return engine::Status::Error;
}
BOOST_ASSERT(parameters);
parameters->coordinates = std::move(coordinates);
if (!parameters->IsValid())
{
result.values["code"] = "invalid-options";
result.values["message"] = getWrongOptionHelp(*parameters);
return engine::Status::Error;
}
BOOST_ASSERT(parameters->IsValid());
return BaseService::routing_machine.Match(*parameters, result);
}
}
}

View File

@ -1,40 +1,19 @@
#include "server/service/route_service.hpp"
#include "server/service/utils.hpp"
#include "engine/api/route_parameters.hpp"
#include "server/api/parameters_parser.hpp"
#include "util/json_container.hpp"
#include <boost/format.hpp>
namespace osrm
{
namespace server
{
namespace service
{
namespace
{
const constexpr char PARAMETER_SIZE_MISMATCH_MSG[] =
"Number of elements in %1% size %2% does not match coordinate size %3%";
template <typename ParamT>
bool constrainParamSize(const char *msg_template,
const char *name,
const ParamT &param,
const std::size_t target_size,
std::string &help)
{
if (param.size() > 0 && param.size() != target_size)
{
help = (boost::format(msg_template) % name % param.size() % target_size).str();
return true;
}
return false;
}
std::string getWrongOptionHelp(const engine::api::RouteParameters &parameters)
{
std::string help;
@ -63,7 +42,6 @@ engine::Status RouteService::RunQuery(std::vector<util::FixedPointCoordinate> co
std::string &options,
util::json::Object &result)
{
auto options_iterator = options.begin();
auto parameters =
api::parseParameters<engine::api::RouteParameters>(options_iterator, options.end());