2 * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include "here_utils.h"
18 #include <common/CommunicationError.h>
21 static const double LATITUDE_RANGE = 85.05113;
22 static const double LONGITUDE_RANGE = 180.0;
27 int ConvertToMapsError(int nErr)
31 case HERE_ERROR_NONE: return MAPS_ERROR_NONE;
32 case HERE_ERROR_PERMISSION_DENIED: return MAPS_ERROR_PERMISSION_DENIED;
33 case HERE_ERROR_OUT_OF_MEMORY: return MAPS_ERROR_OUT_OF_MEMORY;
34 case HERE_ERROR_INVALID_PARAMETER: return MAPS_ERROR_INVALID_PARAMETER;
35 case HERE_ERROR_NOT_SUPPORTED: return MAPS_ERROR_NOT_SUPPORTED;
36 case HERE_ERROR_CONNECTION_TIME_OUT: return MAPS_ERROR_CONNECTION_TIME_OUT;
37 case HERE_ERROR_NETWORK_UNREACHABLE: return MAPS_ERROR_NETWORK_UNREACHABLE;
38 case HERE_ERROR_INVALID_OPERATION: return MAPS_ERROR_INVALID_OPERATION;
39 case HERE_ERROR_KEY_NOT_AVAILABLE: return MAPS_ERROR_KEY_NOT_AVAILABLE;
40 case HERE_ERROR_RESOURCE_BUSY: return MAPS_ERROR_RESOURCE_BUSY;
41 case HERE_ERROR_CANCELED: return MAPS_ERROR_CANCELED;
42 case HERE_ERROR_UNKNOWN: return MAPS_ERROR_UNKNOWN;
43 case HERE_ERROR_SERVICE_NOT_AVAILABLE: return MAPS_ERROR_SERVICE_NOT_AVAILABLE;
44 case HERE_ERROR_NOT_FOUND: return MAPS_ERROR_NOT_FOUND;
46 return MAPS_ERROR_UNKNOWN;
49 int ConvertToHereError(int nErr)
53 case MAPS_ERROR_NONE: return HERE_ERROR_NONE;
54 case MAPS_ERROR_PERMISSION_DENIED: return HERE_ERROR_PERMISSION_DENIED;
55 case MAPS_ERROR_OUT_OF_MEMORY: return HERE_ERROR_OUT_OF_MEMORY;
56 case MAPS_ERROR_INVALID_PARAMETER: return HERE_ERROR_INVALID_PARAMETER;
57 case MAPS_ERROR_NOT_SUPPORTED: return HERE_ERROR_NOT_SUPPORTED;
58 case MAPS_ERROR_CONNECTION_TIME_OUT: return HERE_ERROR_CONNECTION_TIME_OUT;
59 case MAPS_ERROR_NETWORK_UNREACHABLE: return HERE_ERROR_NETWORK_UNREACHABLE;
60 case MAPS_ERROR_INVALID_OPERATION: return HERE_ERROR_INVALID_OPERATION;
61 case MAPS_ERROR_KEY_NOT_AVAILABLE: return HERE_ERROR_KEY_NOT_AVAILABLE;
62 case MAPS_ERROR_RESOURCE_BUSY: return HERE_ERROR_RESOURCE_BUSY;
63 case MAPS_ERROR_CANCELED: return HERE_ERROR_CANCELED;
64 case MAPS_ERROR_UNKNOWN: return HERE_ERROR_UNKNOWN;
65 case MAPS_ERROR_SERVICE_NOT_AVAILABLE: return HERE_ERROR_SERVICE_NOT_AVAILABLE;
66 case MAPS_ERROR_NOT_FOUND: return HERE_ERROR_NOT_FOUND;
68 return HERE_ERROR_UNKNOWN;
71 const char* ConverHereErrorToString(int nErr)
75 case HERE_ERROR_NONE: return "No errors";
76 case HERE_ERROR_PERMISSION_DENIED: return "Permission denied";
77 case HERE_ERROR_OUT_OF_MEMORY: return "Out of memory";
78 case HERE_ERROR_INVALID_PARAMETER: return "Invalid Parameter";
79 case HERE_ERROR_NOT_SUPPORTED: return "Not suppoerted";
80 case HERE_ERROR_CONNECTION_TIME_OUT: return "Connection time out";
81 case HERE_ERROR_NETWORK_UNREACHABLE: return "Network unreachable";
82 case HERE_ERROR_INVALID_OPERATION: return "Invalid operation";
83 case HERE_ERROR_KEY_NOT_AVAILABLE: return "Key not available";
84 case HERE_ERROR_RESOURCE_BUSY: return "Resource busy";
85 case HERE_ERROR_CANCELED: return "Canceled";
86 case HERE_ERROR_UNKNOWN: return "Unknown";
87 case HERE_ERROR_SERVICE_NOT_AVAILABLE: return "Service not available";
88 case HERE_ERROR_NOT_FOUND: return "Not found";
93 const char* ConvertMapsErrorToChar(int nErr)
97 case MAPS_ERROR_NONE: return "MAPS_ERROR_NONE";
98 case MAPS_ERROR_PERMISSION_DENIED: return "MAPS_ERROR_PERMISSION_DENIED";
99 case MAPS_ERROR_OUT_OF_MEMORY: return "MAPS_ERROR_OUT_OF_MEMORY";
100 case MAPS_ERROR_INVALID_PARAMETER: return "MAPS_ERROR_INVALID_PARAMETER";
101 case MAPS_ERROR_NOT_SUPPORTED: return "MAPS_ERROR_NOT_SUPPORTED";
102 case MAPS_ERROR_CONNECTION_TIME_OUT: return "MAPS_ERROR_CONNECTION_TIME_OUT";
103 case MAPS_ERROR_NETWORK_UNREACHABLE: return "MAPS_ERROR_NETWORK_UNREACHABLE";
104 case MAPS_ERROR_INVALID_OPERATION: return "MAPS_ERROR_INVALID_OPERATION";
105 case MAPS_ERROR_KEY_NOT_AVAILABLE: return "MAPS_ERROR_KEY_NOT_AVAILABLE";
106 case MAPS_ERROR_RESOURCE_BUSY: return "MAPS_ERROR_RESOURCE_BUSY";
107 case MAPS_ERROR_CANCELED: return "MAPS_ERROR_CANCELED";
108 case MAPS_ERROR_UNKNOWN: return "MAPS_ERROR_UNKNOWN";
109 case MAPS_ERROR_SERVICE_NOT_AVAILABLE: return "MAPS_ERROR_SERVICE_NOT_AVAILABLE";
110 case MAPS_ERROR_NOT_FOUND: return "MAPS_ERROR_NOT_FOUND";
112 return "MAPS_ERROR_UNKNOWN";
117 HERE_PLUGIN_BEGIN_NAMESPACE
119 HereUtils::HereUtils()
123 HereUtils::~HereUtils()
127 GeoRouteQuery::TravelMode HereUtils::Convert(maps_route_transport_mode_e nVal)
131 case MAPS_ROUTE_TRANSPORT_MODE_CAR: return GeoRouteQuery::TM_CarTravel;
132 case MAPS_ROUTE_TRANSPORT_MODE_PEDESTRIAN: return GeoRouteQuery::TM_PedestrianTravel;
133 case MAPS_ROUTE_TRANSPORT_MODE_PUBLICTRANSIT: return GeoRouteQuery::TM_PublicTransitTravel;
136 return GeoRouteQuery::TM_CarTravel;
139 maps_route_transport_mode_e HereUtils::Convert(GeoRouteQuery::TravelMode nVal)
143 case GeoRouteQuery::TM_CarTravel: return MAPS_ROUTE_TRANSPORT_MODE_CAR;
144 case GeoRouteQuery::TM_PedestrianTravel: return MAPS_ROUTE_TRANSPORT_MODE_PEDESTRIAN;
145 case GeoRouteQuery::TM_PublicTransitTravel: return MAPS_ROUTE_TRANSPORT_MODE_PUBLICTRANSIT;
148 return MAPS_ROUTE_TRANSPORT_MODE_CAR;
151 GeoRouteQuery::FeatureType HereUtils::Convert(maps_route_feature_e nVal)
155 case MAPS_ROUTE_FEATURE_NO: return GeoRouteQuery::FT_NoFeature;
156 case MAPS_ROUTE_FEATURE_TOLL: return GeoRouteQuery::FT_TollFeature;
157 case MAPS_ROUTE_FEATURE_MOTORWAY: return GeoRouteQuery::FT_MotorwayFeature;
158 case MAPS_ROUTE_FEATURE_BOATFERRY: return GeoRouteQuery::FT_BoatFerryFeature;
159 case MAPS_ROUTE_FEATURE_RAILFERRY: return GeoRouteQuery::FT_RailFerryFeature;
160 case MAPS_ROUTE_FEATURE_PUBLICTTRANSIT: return GeoRouteQuery::FT_PublicTransitFeature;
161 case MAPS_ROUTE_FEATURE_TUNNEL: return GeoRouteQuery::FT_TunnelFeature;
162 case MAPS_ROUTE_FEATURE_DIRTROAD: return GeoRouteQuery::FT_DirtRoadFeature;
163 case MAPS_ROUTE_FEATURE_PARKS: return GeoRouteQuery::FT_ParksFeature;
164 case MAPS_ROUTE_FEATURE_HOVLANE: return GeoRouteQuery::FT_HOVLane;
165 case MAPS_ROUTE_FEATURE_STAIRS: return GeoRouteQuery::FT_Stairs;
168 return GeoRouteQuery::FT_NoFeature;
171 GeoRouteQuery::FeatureWeight HereUtils::Convert(maps_route_feature_weight_e nVal)
175 case MAPS_ROUTE_FEATURE_WEIGHT_NORMAL: return GeoRouteQuery::FW_NormalFeatureWeight;
176 case MAPS_ROUTE_FEATURE_WEIGHT_PREFER: return GeoRouteQuery::FW_PreferFeatureWeight;
177 case MAPS_ROUTE_FEATURE_WEIGHT_AVOID: return GeoRouteQuery::FW_AvoidFeatureWeight;
178 case MAPS_ROUTE_FEATURE_WEIGHT_SOFTEXCLUDE: return GeoRouteQuery::FW_SoftExcludeFeatureWeight;
179 case MAPS_ROUTE_FEATURE_WEIGHT_STRICTEXCLUDE: return GeoRouteQuery::FW_StrictExcludeFeatureWeight;
182 return GeoRouteQuery::FW_NormalFeatureWeight;
185 maps_route_turn_type_e HereUtils::Convert(Maneuver::InstructionDirection nVal)
189 case Maneuver::ID_NoDirection: return MAPS_ROUTE_TURN_TYPE_NONE;
190 case Maneuver::ID_DirectionForward: return MAPS_ROUTE_TURN_TYPE_STRAIGHT;
191 case Maneuver::ID_DirectionBearRight: return MAPS_ROUTE_TURN_TYPE_BEAR_RIGHT;
192 case Maneuver::ID_DirectionLightRight: return MAPS_ROUTE_TURN_TYPE_LIGHT_RIGHT;
193 case Maneuver::ID_DirectionRight: return MAPS_ROUTE_TURN_TYPE_RIGHT;
194 case Maneuver::ID_DirectionHardRight: return MAPS_ROUTE_TURN_TYPE_HARD_RIGHT;
195 case Maneuver::ID_DirectionUTurnRight: return MAPS_ROUTE_TURN_TYPE_UTURN_RIGHT;
196 case Maneuver::ID_DirectionUTurnLeft: return MAPS_ROUTE_TURN_TYPE_UTURN_LEFT;
197 case Maneuver::ID_DirectionHardLeft: return MAPS_ROUTE_TURN_TYPE_HARD_LEFT;
198 case Maneuver::ID_DirectionLeft: return MAPS_ROUTE_TURN_TYPE_LEFT;
199 case Maneuver::ID_DirectionLightLeft: return MAPS_ROUTE_TURN_TYPE_LIGHT_LEFT;
200 case Maneuver::ID_DirectionBearLeft: return MAPS_ROUTE_TURN_TYPE_BEAR_LEFT;
203 return MAPS_ROUTE_TURN_TYPE_NONE;
206 Maneuver::InstructionDirection HereUtils::Convert(maps_route_turn_type_e nVal)
210 case MAPS_ROUTE_TURN_TYPE_NONE: return Maneuver::ID_NoDirection;
211 case MAPS_ROUTE_TURN_TYPE_STRAIGHT: return Maneuver::ID_DirectionForward;
212 case MAPS_ROUTE_TURN_TYPE_BEAR_RIGHT: return Maneuver::ID_DirectionBearRight;
213 case MAPS_ROUTE_TURN_TYPE_LIGHT_RIGHT: return Maneuver::ID_DirectionLightRight;
214 case MAPS_ROUTE_TURN_TYPE_RIGHT: return Maneuver::ID_DirectionRight;
215 case MAPS_ROUTE_TURN_TYPE_HARD_RIGHT: return Maneuver::ID_DirectionHardRight;
216 case MAPS_ROUTE_TURN_TYPE_UTURN_RIGHT: return Maneuver::ID_DirectionUTurnRight;
217 case MAPS_ROUTE_TURN_TYPE_UTURN_LEFT: return Maneuver::ID_DirectionUTurnLeft;
218 case MAPS_ROUTE_TURN_TYPE_HARD_LEFT: return Maneuver::ID_DirectionHardLeft;
219 case MAPS_ROUTE_TURN_TYPE_LEFT: return Maneuver::ID_DirectionLeft;
220 case MAPS_ROUTE_TURN_TYPE_LIGHT_LEFT: return Maneuver::ID_DirectionLightLeft;
221 case MAPS_ROUTE_TURN_TYPE_BEAR_LEFT: return Maneuver::ID_DirectionBearLeft;
224 return Maneuver::ID_NoDirection;
227 maps_error_e HereUtils::ConvertHttpCodeToMapsError(int nVal)
231 case 200:/*Ok*/ return MAPS_ERROR_NONE;
232 case 408:/*Request timeout*/
233 case 504:/*Gateway timeout*/
234 case 598:/*Network reading timeout*/
235 case 599:/*Network connection timeout*/ return MAPS_ERROR_CONNECTION_TIME_OUT;
237 case 404:/*Not found*/
238 case 407:/*Proxy auth. required*/
239 case 502:/*Bad gateway*/ return MAPS_ERROR_NETWORK_UNREACHABLE;
241 case 401:/*Unauthorized*/
242 case 402:/*Payment required*/ return MAPS_ERROR_KEY_NOT_AVAILABLE;
244 case 405:/*Method not allowed*/
245 case 413:/*Request entity too larget*/
246 case 414:/*Request uri too large*/ return MAPS_ERROR_INVALID_OPERATION;
248 case 403:/*Forbidden*/
249 case 500:/*Server internal error*/
250 case 501:/*Not implemented*/
251 case 503:/*Service unavailable*/ return MAPS_ERROR_SERVICE_NOT_AVAILABLE;
254 if (nVal > 0 && nVal < 100) // curl error code
255 return MAPS_ERROR_NETWORK_UNREACHABLE;
257 if (nVal >= 400 && nVal < 500) // http code 4xx (client-side error)
258 return MAPS_ERROR_INVALID_OPERATION;
260 if (nVal >= 500 && nVal < 600) // http code 5xx (server-side error)
261 return MAPS_ERROR_SERVICE_NOT_AVAILABLE;
263 return MAPS_ERROR_UNKNOWN;
266 GeoBoundingBox& HereUtils::Convert(maps_area_h hArea, GeoBoundingBox& Box)
268 maps_area_s* area_s = (maps_area_s*)hArea;
270 if (!area_s || area_s->type != MAPS_AREA_RECTANGLE) return Box;
272 GeoCoordinates hereCoordLT(area_s->rect.top_left.latitude, area_s->rect.top_left.longitude);
273 GeoCoordinates hereCoordRB(area_s->rect.bottom_right.latitude, area_s->rect.bottom_right.longitude);
275 Box.SetTopLeft(hereCoordLT);
276 Box.SetBottomRight(hereCoordRB);
281 maps_area_h& HereUtils::Convert(GeoBoundingBox Box, maps_area_h& hArea)
283 maps_coordinates_h mapsCoordLT, mapsCoordRB;
284 GeoCoordinates hereCoordLT, hereCoordRB;
286 hereCoordLT = Box.GetTopLeft();
287 hereCoordRB = Box.GetBottomRight();
289 maps_coordinates_create(hereCoordLT.GetLatitude(), hereCoordLT.GetLongitude(), &mapsCoordLT);
290 maps_coordinates_create(hereCoordRB.GetLatitude(), hereCoordRB.GetLongitude(), &mapsCoordRB);
292 maps_area_create_rectangle(mapsCoordLT, mapsCoordRB, &hArea);
294 maps_coordinates_destroy(mapsCoordLT);
295 maps_coordinates_destroy(mapsCoordRB);
300 GeoBoundingCircle& HereUtils::Convert(maps_area_h hArea, GeoBoundingCircle& circle)
302 maps_area_s* area_s = (maps_area_s*)hArea;
304 if (!area_s || area_s->type != MAPS_AREA_CIRCLE) return circle;
306 GeoCoordinates hereCoord(area_s->circle.center.latitude, area_s->circle.center.longitude);
308 circle.SetCenter(hereCoord);
309 circle.SetRadius(area_s->circle.radius);
314 void HereUtils::Convert(String strUtf8, WString& strUtf16)
316 strUtf16.assign(strUtf8.begin(), strUtf8.end());
319 void HereUtils::Convert(WString strUtf16, String& strUtf8)
321 strUtf8.assign(strUtf16.begin(), strUtf16.end());
324 GeoBoundingBox& HereUtils::Convert(const char *src, GeoBoundingBox &box)
328 double coord[4] = { 0.0, };
330 token = strtok_r((char*)src, ",;", &next);
331 while (token && i < 4)
333 coord[i++] = atof(token);
334 token = strtok_r(NULL, ",;", &next);
336 box.SetTopLeft(GeoCoordinates(coord[0], coord[1]));
337 box.SetBottomRight(GeoCoordinates(coord[2], coord[3]));
341 bool HereUtils::IsValid(GeoCoordinates geoCoord)
343 return IsValidCoord(geoCoord.GetLatitude(), geoCoord.GetLongitude());
346 bool HereUtils::IsValid(maps_coordinates_s geoCoord)
348 return IsValidCoord(geoCoord.latitude, geoCoord.longitude);
351 bool HereUtils::IsValidCoord(double dLat, double dLng)
353 return ((dLat <= LATITUDE_RANGE && dLat >= -LATITUDE_RANGE) &&
354 (dLng <= LONGITUDE_RANGE && dLng >= -LONGITUDE_RANGE));
357 bool HereUtils::IsValid(maps_area_s hArea)
359 if (hArea.type == MAPS_AREA_RECTANGLE)
361 return (HereUtils::IsValid(hArea.rect.top_left) &&
362 HereUtils::IsValid(hArea.rect.bottom_right));
364 else if(hArea.type == MAPS_AREA_CIRCLE)
366 return HereUtils::IsValid(hArea.circle.center);
372 const double HereUtils::ConvertDistance(const double originValue, maps_distance_unit_e destUnit)
374 return ConvertDistance(originValue, MAPS_DISTANCE_UNIT_M, destUnit);
377 const double HereUtils::ConvertDistance(const double originValue, maps_distance_unit_e originUnit, maps_distance_unit_e destUnit)
379 double meterConstant[MAPS_DISTANCE_UNIT_YD+1] = { 1.0, 0.001, 3.2808399, 1.0936133 };
380 return originValue / meterConstant[originUnit] * meterConstant[destUnit];
383 HERE_PLUGIN_END_NAMESPACE