From 8f8fe5021fcd57346e5f5566043f556646d93816 Mon Sep 17 00:00:00 2001 From: srimal_n Date: Fri, 1 May 2020 18:38:13 +0530 Subject: [PATCH] Added trip service --- libosrmc/osrmc.cc | 130 ++++++++++++++++++++++++++++++++++++++++++++++ libosrmc/osrmc.h | 21 ++++++++ 2 files changed, 151 insertions(+) diff --git a/libosrmc/osrmc.cc b/libosrmc/osrmc.cc index c61d9fc..7e40d96 100644 --- a/libosrmc/osrmc.cc +++ b/libosrmc/osrmc.cc @@ -12,6 +12,7 @@ #include #include #include +#include #include #include @@ -112,6 +113,32 @@ void osrmc_params_add_coordinate_with(osrmc_params_t params, float longitude, fl osrmc_error_from_exception(e, error); } +/* Route service */ +osrmc_route_annotations_t osrmc_route_annotations_construct(osrmc_error_t* error) try { + auto* out = new osrm::RouteParameters::AnnotationsType{osrm::RouteParameters::AnnotationsType::Duration}; + return reinterpret_cast(out); +} catch (const std::exception& e) { + osrmc_error_from_exception(e, error); + return nullptr; +} + +void osrmc_route_annotations_destruct(osrmc_route_annotations_t annotations) { + delete reinterpret_cast(annotations); +} + +void osrmc_route_annotations_enable_distance(osrmc_route_annotations_t annotations, bool enable, osrmc_error_t* error) try { + using AnnotationsType = osrm::RouteParameters::AnnotationsType; + auto* annotations_typed = reinterpret_cast(annotations); + + if (enable) { + *annotations_typed |= AnnotationsType::Distance; + } else { + *annotations_typed = static_cast(static_cast(*annotations_typed) ^ static_cast(AnnotationsType::Distance)); + } +} catch (const std::exception& e) { + osrmc_error_from_exception(e, error); +} + osrmc_route_params_t osrmc_route_params_construct(osrmc_error_t* error) try { auto* out = new osrm::RouteParameters; @@ -135,6 +162,14 @@ void osrmc_route_params_add_alternatives(osrmc_route_params_t params, int on) { params_typed->alternatives = on; } +void osrmc_route_params_set_annotations(osrmc_route_params_t params, osrmc_route_annotations_t annotations, osrmc_error_t* error) try { + auto* params_typed = reinterpret_cast(params); + auto* annotations_typed = reinterpret_cast(annotations); + params_typed->annotations_type = *annotations_typed; +} catch (const std::exception& e) { + osrmc_error_from_exception(e, error); +} + osrmc_route_response_t osrmc_route(osrmc_osrm_t osrm, osrmc_route_params_t params, osrmc_error_t* error) try { auto* osrm_typed = reinterpret_cast(osrm); auto* params_typed = reinterpret_cast(params); @@ -376,3 +411,98 @@ void osrmc_match_params_add_timestamp(osrmc_match_params_t params, unsigned time } catch (const std::exception& e) { osrmc_error_from_exception(e, error); } + + +/* Trip service */ +osrmc_trip_params_t osrmc_trip_params_construct(osrmc_error_t* error) try { + osrm::TripParameters* out = new osrm::TripParameters; + return reinterpret_cast(out); +} catch (const std::exception& e) { + osrmc_error_from_exception(e, error); + return nullptr; +} + +void osrmc_trip_params_destruct(osrmc_trip_params_t params) { + delete reinterpret_cast(params); +} + +void osrmc_trip_params_add_source(osrmc_trip_params_t params, bool first, osrmc_error_t* error) try { + auto* params_typed = reinterpret_cast(params); + + if (first) + params_typed->source = osrm::TripParameters::SourceType::First; + +} catch (const std::exception& e) { + osrmc_error_from_exception(e, error); +} + +void osrmc_trip_params_add_destination(osrmc_trip_params_t params, bool last, osrmc_error_t* error) try { + auto* params_typed = reinterpret_cast(params); + if (last) + params_typed->destination = osrm::TripParameters::DestinationType::Last; +} catch (const std::exception& e) { + osrmc_error_from_exception(e, error); +} + +void osrmc_trip_params_add_roundtrip(osrmc_trip_params_t params, bool on, osrmc_error_t* error) try { + auto* params_typed = reinterpret_cast(params); + params_typed->roundtrip = on; +} catch (const std::exception& e) { + osrmc_error_from_exception(e, error); +} + +void osrmc_trip_params_set_annotations(osrmc_trip_params_t params, osrmc_route_annotations_t annotations, osrmc_error_t* error) try { + auto* params_typed = reinterpret_cast(params); + auto* annotations_typed = reinterpret_cast(annotations); + params_typed->annotations_type = *annotations_typed; +} catch (const std::exception& e) { + osrmc_error_from_exception(e, error); +} + +osrmc_trip_response_t osrmc_trip(osrmc_osrm_t osrm, osrmc_trip_params_t params, osrmc_error_t* error) try { + auto* osrm_typed = reinterpret_cast(osrm); + auto* params_typed = reinterpret_cast(params); + + auto* out = new osrm::json::Object; + const auto status = osrm_typed->Trip(*params_typed, *out); + + if (status == osrm::Status::Ok) + return reinterpret_cast(out); + + osrmc_error_from_json(*out, error); + return nullptr; +} catch (const std::exception& e) { + osrmc_error_from_exception(e, error); + return nullptr; +} + +float osrmc_trip_response_distance(osrmc_trip_response_t response, osrmc_error_t* error) try { + auto* response_typed = reinterpret_cast(response); + + auto& routes = response_typed->values["trips"].get(); + auto& route = routes.values.at(0).get(); + + const auto distance = route.values["distance"].get().value; + return distance; +} catch (const std::exception& e) { + osrmc_error_from_exception(e, error); + return INFINITY; +} + +float osrmc_trip_response_duration(osrmc_trip_response_t response, osrmc_error_t* error) try { + auto* response_typed = reinterpret_cast(response); + + auto& routes = response_typed->values["trips"].get(); + auto& route = routes.values.at(0).get(); + + const auto duration = route.values["duration"].get().value; + return duration; +} catch (const std::exception& e) { + osrmc_error_from_exception(e, error); + return INFINITY; +} + + +void osrmc_trip_response_destruct(osrmc_trip_response_t response) { + delete reinterpret_cast(response); +} diff --git a/libosrmc/osrmc.h b/libosrmc/osrmc.h index d873033..070df0e 100644 --- a/libosrmc/osrmc.h +++ b/libosrmc/osrmc.h @@ -145,15 +145,18 @@ typedef struct osrmc_params* osrmc_params_t; /* Service-specific parameters */ typedef struct osrmc_route_params* osrmc_route_params_t; +typedef struct osrmc_route_annotations* osrmc_route_annotations_t; typedef struct osrmc_table_params* osrmc_table_params_t; typedef struct osrmc_table_annotations* osrmc_table_annotations_t; typedef struct osrmc_nearest_params* osrmc_nearest_params_t; typedef struct osrmc_match_params* osrmc_match_params_t; +typedef struct osrmc_trip_params* osrmc_trip_params_t; /* Service-specific responses */ typedef struct osrmc_route_response* osrmc_route_response_t; typedef struct osrmc_table_response* osrmc_table_response_t; +typedef struct osrmc_trip_response* osrmc_trip_response_t; /* Service-specific callbacks */ @@ -182,11 +185,15 @@ OSRMC_API void osrmc_params_add_coordinate_with(osrmc_params_t params, float lon int bearing, int range, osrmc_error_t* error); /* Route service */ +OSRMC_API osrmc_route_annotations_t osrmc_route_annotations_construct(osrmc_error_t* error); +OSRMC_API void osrmc_route_annotations_destruct(osrmc_route_annotations_t annotations); +OSRMC_API void osrmc_route_annotations_enable_distance(osrmc_route_annotations_t annotations, bool enable, osrmc_error_t* error); OSRMC_API osrmc_route_params_t osrmc_route_params_construct(osrmc_error_t* error); OSRMC_API void osrmc_route_params_destruct(osrmc_route_params_t params); OSRMC_API void osrmc_route_params_add_steps(osrmc_route_params_t params, int on); OSRMC_API void osrmc_route_params_add_alternatives(osrmc_route_params_t params, int on); +OSRMC_API void osrmc_route_params_set_annotations(osrmc_route_params_t params, osrmc_route_annotations_t annotations, osrmc_error_t* error); OSRMC_API osrmc_route_response_t osrmc_route(osrmc_osrm_t osrm, osrmc_route_params_t params, osrmc_error_t* error); OSRMC_API void osrmc_route_with(osrmc_osrm_t osrm, osrmc_route_params_t params, osrmc_waypoint_handler_t handler, @@ -229,6 +236,20 @@ OSRMC_API osrmc_match_params_t osrmc_match_params_construct(osrmc_error_t* error OSRMC_API void osrmc_match_params_destruct(osrmc_match_params_t params); OSRMC_API void osrmc_match_params_add_timestamp(osrmc_match_params_t params, unsigned timestamp, osrmc_error_t* error); + +/* Trip service */ + +OSRMC_API osrmc_trip_params_t osrmc_trip_params_construct(osrmc_error_t* error); +OSRMC_API void osrmc_trip_params_destruct(osrmc_trip_params_t params); +OSRMC_API void osrmc_trip_params_add_source(osrmc_trip_params_t params, bool first, osrmc_error_t* error); +OSRMC_API void osrmc_trip_params_add_destination(osrmc_trip_params_t params, bool last, osrmc_error_t* error); +OSRMC_API void osrmc_trip_params_add_roundtrip(osrmc_trip_params_t params, bool on, osrmc_error_t* error); +OSRMC_API void osrmc_trip_params_set_annotations(osrmc_trip_params_t params, osrmc_route_annotations_t annotations, osrmc_error_t* error); +OSRMC_API osrmc_trip_response_t osrmc_trip(osrmc_osrm_t osrm, osrmc_trip_params_t params, osrmc_error_t* error); +OSRMC_API float osrmc_trip_response_distance(osrmc_trip_response_t response, osrmc_error_t* error); +OSRMC_API float osrmc_trip_response_duration(osrmc_trip_response_t response, osrmc_error_t* error); +OSRMC_API void osrmc_trip_response_destruct(osrmc_trip_response_t response); + #ifdef __cplusplus } #endif