From: kdiluca Date: Tue, 21 Jun 2016 20:51:16 +0000 (-0400) Subject: Adds inital route support X-Git-Tag: submit/tizen_3.0/20161108.012559~58 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=5ec7942b26149de4a7abf5184b1a76f899940224;p=platform%2Fcore%2Flocation%2Fmaps-plugin-mapzen.git Adds inital route support Change-Id: I1e2c487b4743a37afe566c226ef4c1556248ef8c --- diff --git a/src/mapzen/mapzen_jsonparser.cpp b/src/mapzen/mapzen_jsonparser.cpp index 34cb807..b692ae3 100644 --- a/src/mapzen/mapzen_jsonparser.cpp +++ b/src/mapzen/mapzen_jsonparser.cpp @@ -17,11 +17,15 @@ #include #include -#include "rapidjson/document.h" + #include "mapzen_jsonparser.hpp" +#include "rapidjson/document.h" + +extern "C" { #include "mapzen_queue.h" #include "mapzen_debug.h" #include "mapzen_util.h" +} #include #include @@ -352,7 +356,6 @@ void post_curl_response(char *response, int size, mapzen_resp_type type, void *u responseData->error = __convert_status(status); responseData->addressDetails = NULL; } - mapzen_push_to_queue(type, (gpointer)responseData); } diff --git a/src/mapzen/mapzen_restcurl.c b/src/mapzen/mapzen_restcurl.c index 9530345..a8d8928 100644 --- a/src/mapzen/mapzen_restcurl.c +++ b/src/mapzen/mapzen_restcurl.c @@ -323,7 +323,7 @@ static int __progress_info(void *p, double dltotal, double dlnow, double ultotal return __xferinfo(p, (curl_off_t)dltotal, (curl_off_t)dlnow, (curl_off_t)ultotal, (curl_off_t)ulnow); } -int add_handle(char *url, mapzen_req_type type, void *user_data) +int add_handle(const char *url, mapzen_req_type type, void *user_data) { if (!url || !user_data) return -1; diff --git a/src/mapzen/mapzen_restcurl.h b/src/mapzen/mapzen_restcurl.h index 897993d..fd7e343 100644 --- a/src/mapzen/mapzen_restcurl.h +++ b/src/mapzen/mapzen_restcurl.h @@ -27,7 +27,7 @@ int deinit_curl(); int get_num_running_requests(); -int add_handle(char *url, mapzen_req_type type, void *user_data); +int add_handle(const char *url, mapzen_req_type type, void *user_data); void cancel_request(int request_id); diff --git a/src/mapzen/mapzen_route.cpp b/src/mapzen/mapzen_route.cpp index 1f72746..a7a3381 100644 --- a/src/mapzen/mapzen_route.cpp +++ b/src/mapzen/mapzen_route.cpp @@ -17,6 +17,7 @@ #include #include #include +#include #include "mapzen_route.hpp" #include "mapzen_types.h" #include "mapzen_server_private.h" @@ -25,99 +26,147 @@ #include "mapzen_restcurl.h" #include "mapzen_util.h" -#define ROUTE_URL "https://valhalla.mapzen.com/route?api_key=%s&json=" +#include "rapidjson/rapidjson.h" +#include "rapidjson/allocators.h" +#include "rapidjson/document.h" +#include "rapidjson/stringbuffer.h" +#include "rapidjson/writer.h" +#include -int query_route(gchar *maps_key, coords_s startPoint, coords_s endPoint, route_type type, route_feature_avoids avoids, route_driving_style style, GList *waypoints, gpointer user_data) -{ - char url[1024] = ""; - char tmpStr[512]; - - if (maps_key != NULL) - snprintf(tmpStr, sizeof(tmpStr), ROUTE_URL, maps_key); - else - snprintf(tmpStr, sizeof(tmpStr), ROUTE_URL, "null"); +#ifdef __cplusplus +extern "C" { +#endif - STRCPY(url, tmpStr); +#define ROUTE_URL "https://valhalla.mapzen.com/route?api_key=%s&json=%s" - /*STRCAT(url, '"directions_options":{"units":"mi"}'); Keeping default as miles and conversion will be done later - if (costing == COSTING_AUTO) - STRCAT(url, '"costing":"auto"'); - else if (costing == COSTING_AUTO_SHORTER) - STRCAT(url, '"costing":"auto_shorter"'); - else if (costing == COSTING_PEDESTRIAN) - STRCAT(url, '"costing":"pedestrian"'); - else if (costing == COSTING_MULTIMODAL) - STRCAT(url, '"costing":"multimodal"'); - else if (costing == COSTING_BICYCLE) - STRCAT(url, '"costing":"bicycle"'); +std::string url_encode(const std::string& unencoded) { + char* encoded = curl_escape(unencoded.c_str(), static_cast(unencoded.size())); + dlog_print(DLOG_DEBUG, "URL:*","json after url encoding: " + *encoded); + std::string encoded_str(encoded); + curl_free(encoded); + return encoded_str; + } - if (avoids) { - STRCAT(url, '"costing_options":{' + costing + ':{'); - - if (avoids == PENALTY_LIMITED_ACCESS) { - snprintf(tmpStr, sizeof(tmpStr), ""); - STRCAT(url, ""); - } - else if (avoids == PENALTY_TOLL_ROADS) { - snprintf(tmpStr, sizeof(tmpStr), ',"toll_booth_penalty":"1"'); - STRCAT(url, tmpStr); - } - else if (avoids == PENALTY_USE_FERRY){ - snprintf(tmpStr, sizeof(tmpStr), ',"use_ferry":"0"'); - STRCAT(url, tmpStr); - } - else if (avoids == PENALTY_USE_UNPAVED) { - snprintf(tmpStr, sizeof(tmpStr), ""); - STRCAT(url, tmpStr); - } - else if (avoids == PENALTY_SEASONAL_CLOSURE) { - snprintf(tmpStr, sizeof(tmpStr), ""); - STRCAT(url, tmpStr); - } - else if (avoids == PENALTY_COUNTRY_CROSSING) { - snprintf(tmpStr, sizeof(tmpStr), ',"country_crossing_penalty":"1"'); - STRCAT(url, tmpStr); - } - STRCAT(url, '}}'); - } +int query_route(gchar *maps_key, coords_s startPoint, coords_s endPoint, route_type type, route_feature_avoids avoids, route_driving_style style, GList *waypoints, gpointer user_data) +{ + rapidjson::Document document; + rapidjson::Document::AllocatorType& allocator = document.GetAllocator(); - if (style == DRIVING_STYLE_NORMAL) - STRCAT(url, ""); - else if (style == DRIVING_STYLE_CAUTIOUS) - STRCAT(url, ""); - else if (style == DRIVING_STYLE_AGGRESSIVE) - STRCAT(url, ""); + rapidjson::Value& locations = document.AddMember(rapidjson::Value("locations", allocator).Move(), + rapidjson::Value(rapidjson::kArrayType).Move(), allocator); + rapidjson::Value& location = locations.AddMember(rapidjson::Value("", allocator).Move(), + rapidjson::Value(rapidjson::kObjectType).Move(), allocator); int length = g_list_length(waypoints); if (length != 0) { int index = 0; GList *waypoints_list = NULL; waypoints_list = g_list_first(waypoints); - while (waypoints_list) { - coords_s *data = (coords_s *) waypoints_list->data; - STRCAT(url, '"locations":[{"'); - - for (i = 0; i < sizeof(data); i++) { - snprintf(tmpStr, sizeof(tmpStr), '"lat":%f,"lon":%f},', data[i].latitude, data[i].longitude); - STRCAT(url, tmpStr); + for (unsigned i = 0; i < sizeof(data); i++) { + location.AddMember("lat", data[i].latitude, allocator); + location.AddMember("lon", data[i].longitude, allocator); } - STRCAT(url, ']'); - waypoints_list = g_list_next(waypoints_list); index++; } } else { - snprintf(tmpStr, sizeof(tmpStr), '"locations":[{"lat":%f,"lon":%f},{"lat":%f,"lon":%f}]', startPoint.latitude, startPoint.longitude, endPoint.latitude, endPoint.longitude); - STRCAT(url, tmpStr); + location.AddMember("lat", startPoint.latitude, allocator); + location.AddMember("lon", startPoint.longitude, allocator); + location.AddMember("lat", endPoint.latitude, allocator); + location.AddMember("lon", endPoint.longitude, allocator); + } + + rapidjson::Value& costing = document.AddMember(rapidjson::Value("costing", allocator).Move(), + rapidjson::Value(rapidjson::kStringType).Move(), allocator); + switch (type) { + case COSTING_AUTO: + costing.SetString("auto"); + break; + case COSTING_AUTO_SHORTER: + costing.SetString("auto_shorter"); + break; + case COSTING_PEDESTRIAN: + costing.SetString("pedestrian"); + break; + case COSTING_MULTIMODAL: + costing.SetString("multimodal"); + break; + case COSTING_BICYCLE: + costing.SetString("bicycle"); + break; + default: + costing.SetString("auto"); + } + + rapidjson::Value& directions_options = document.AddMember(rapidjson::Value("directions_options", allocator).Move(), + rapidjson::Value(rapidjson::kObjectType).Move(), allocator); + directions_options.AddMember("unit","mi",allocator); + + if (avoids) { + rapidjson::Value& costing_options = document.AddMember(rapidjson::Value("costing_options", allocator).Move(), + rapidjson::Value(rapidjson::kObjectType).Move(), allocator); + + switch (avoids) { + case PENALTY_NONE: + //TODO + break; + case PENALTY_LIMITED_ACCESS: + //TODO + break; + case PENALTY_TOLL_ROADS: + costing_options.AddMember("toll_booth_penalty","1", allocator); + break; + case PENALTY_USE_FERRY: + costing_options.AddMember("use_ferry","0", allocator); + break; + case PENALTY_USE_UNPAVED: + //TODO + break; + case PENALTY_SEASONAL_CLOSURE: + //TODO + break; + case PENALTY_COUNTRY_CROSSING: + costing_options.AddMember("country_crossing_penalty","1", allocator); + break; + } + } + + /*rapidjson::Value& drive_style = document.AddMember(rapidjson::Value("drive_style", allocator).Move(), + * rapidjson::Value(rapidjson::kStringType).Move(), allocator); + switch (style) { + case DRIVING_STYLE_NORMAL: + //TODO + break; + case DRIVING_STYLE_CAUTIOUS: + //TODO + break; + case DRIVING_STYLE_AGGRESSIVE: + //TODO + break; }*/ - //TODO: url encode (curl_escape) the json part of the request + //Stringify JSON + rapidjson::StringBuffer buffer; + rapidjson::Writer writer(buffer); + document.Accept(writer); + const char* json = buffer.GetString(); - add_handle(url, REQ_TYPE_ROUTE, user_data); + //we think that this is already being encoded somewhere else + //std::string encoded_json = url_encode(json); + char routeRequest[1024]; + if (maps_key != NULL) + snprintf(routeRequest, sizeof(routeRequest), ROUTE_URL, maps_key, json); + else + snprintf(routeRequest, sizeof(routeRequest), ROUTE_URL, "null", json); + + add_handle(routeRequest, REQ_TYPE_ROUTE, user_data); return 0; } +#ifdef __cplusplus +} +#endif diff --git a/src/mapzen/mapzen_route.hpp b/src/mapzen/mapzen_route.hpp index 2e9ec0f..3af7b77 100644 --- a/src/mapzen/mapzen_route.hpp +++ b/src/mapzen/mapzen_route.hpp @@ -20,6 +20,14 @@ #include "mapzen_api.h" #include "mapzen_types.h" +#ifdef __cplusplus +extern "C" { +#endif + int query_route(gchar *maps_key, coords_s startPoint, coords_s endPoint, route_type type, route_feature_avoids avoids, route_driving_style style, GList *waypoints, gpointer user_data); +#ifdef __cplusplus +} +#endif + #endif /* _MAPZEN_ROUTE_H_ */