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

115 lines
3.8 KiB
C++
Raw Permalink Normal View History

2016-01-28 10:28:44 -05:00
#ifndef ENGINE_RESPONSE_OBJECTS_HPP_
#define ENGINE_RESPONSE_OBJECTS_HPP_
#include "extractor/travel_mode.hpp"
#include "guidance/turn_instruction.hpp"
2016-05-27 15:05:04 -04:00
#include "engine/guidance/leg_geometry.hpp"
#include "engine/guidance/route.hpp"
#include "engine/guidance/route_leg.hpp"
#include "engine/guidance/route_step.hpp"
#include "engine/guidance/step_maneuver.hpp"
2016-05-27 15:05:04 -04:00
#include "engine/polyline_compressor.hpp"
2016-01-28 10:28:44 -05:00
#include "util/coordinate.hpp"
#include "util/json_container.hpp"
#include <optional>
2016-01-28 10:28:44 -05:00
#include <algorithm>
2016-04-12 09:00:08 -04:00
#include <iterator>
#include <string>
2016-01-28 10:28:44 -05:00
#include <vector>
namespace osrm::engine
2016-01-28 10:28:44 -05:00
{
struct Hint;
namespace api::json
2016-01-28 10:28:44 -05:00
{
namespace detail
{
// Check whether to include a modifier in the result of the API
inline bool isValidModifier(const guidance::StepManeuver maneuver)
{
return (maneuver.waypoint_type == guidance::WaypointType::None ||
maneuver.instruction.direction_modifier != osrm::guidance::DirectionModifier::UTurn);
}
inline bool hasValidLanes(const guidance::IntermediateIntersection &intersection)
{
return intersection.lanes.lanes_in_turn > 0;
}
util::json::Value coordinateToLonLat(const util::Coordinate &coordinate);
2016-01-28 10:28:44 -05:00
/**
* Ensures that a bearing value is a whole number, and clamped to the range 0-359
*/
inline double roundAndClampBearing(double bearing) { return std::fmod(std::round(bearing), 360); }
2016-01-28 10:28:44 -05:00
} // namespace detail
2016-11-11 08:09:04 -05:00
template <unsigned POLYLINE_PRECISION, typename ForwardIter>
util::json::String makePolyline(ForwardIter begin, ForwardIter end)
2016-01-28 10:28:44 -05:00
{
return {encodePolyline<POLYLINE_PRECISION>(begin, end)};
2016-01-28 10:28:44 -05:00
}
template <typename ForwardIter>
util::json::Object makeGeoJSONGeometry(ForwardIter begin, ForwardIter end)
2016-01-28 10:28:44 -05:00
{
2016-04-20 14:51:45 -04:00
auto num_coordinates = std::distance(begin, end);
BOOST_ASSERT(num_coordinates != 0);
2016-01-28 10:28:44 -05:00
util::json::Object geojson;
geojson.values["type"] = "LineString";
util::json::Array coordinates;
2016-04-20 14:51:45 -04:00
if (num_coordinates > 1)
{
coordinates.values.reserve(num_coordinates);
auto into = std::back_inserter(coordinates.values);
std::transform(begin, end, into, &detail::coordinateToLonLat);
}
2016-04-20 14:51:45 -04:00
else if (num_coordinates > 0)
{
// For a single location we create a [location, location] LineString
// instead of a single Point making the GeoJSON output consistent.
coordinates.values.reserve(2);
auto location = detail::coordinateToLonLat(*begin);
coordinates.values.push_back(location);
coordinates.values.push_back(location);
}
geojson.values["coordinates"] = util::json::Value{std::move(coordinates)};
2016-01-28 10:28:44 -05:00
return geojson;
}
util::json::Object makeStepManeuver(const guidance::StepManeuver &maneuver);
util::json::Object makeRouteStep(guidance::RouteStep step, util::json::Value geometry);
2016-01-28 10:28:44 -05:00
util::json::Object makeRoute(const guidance::Route &route,
util::json::Array legs,
std::optional<util::json::Value> geometry,
const char *weight_name);
2016-01-28 10:28:44 -05:00
// Creates a Waypoint without Hint, see the Hint overload below
util::json::Object
makeWaypoint(const util::Coordinate &location, const double &distance, std::string name);
// Creates a Waypoint with Hint, see the overload above when Hint is not needed
util::json::Object makeWaypoint(const util::Coordinate &location,
const double &distance,
std::string name,
const Hint &hint);
2016-01-28 10:28:44 -05:00
util::json::Object makeRouteLeg(guidance::RouteLeg leg, util::json::Array steps);
2016-01-28 10:28:44 -05:00
util::json::Array makeRouteLegs(std::vector<guidance::RouteLeg> legs,
std::vector<util::json::Value> step_geometries,
std::vector<util::json::Object> annotations);
2022-12-20 12:00:11 -05:00
} // namespace api::json
} // namespace osrm::engine
2016-01-28 10:28:44 -05:00
#endif // ENGINE_GUIDANCE_API_RESPONSE_GENERATOR_HPP_