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)
30 case HERE_ERROR_NONE: return MAPS_ERROR_NONE;
31 case HERE_ERROR_PERMISSION_DENIED: return MAPS_ERROR_PERMISSION_DENIED;
32 case HERE_ERROR_OUT_OF_MEMORY: return MAPS_ERROR_OUT_OF_MEMORY;
33 case HERE_ERROR_INVALID_PARAMETER: return MAPS_ERROR_INVALID_PARAMETER;
34 case HERE_ERROR_NOT_SUPPORTED: return MAPS_ERROR_NOT_SUPPORTED;
35 case HERE_ERROR_CONNECTION_TIME_OUT: return MAPS_ERROR_CONNECTION_TIME_OUT;
36 case HERE_ERROR_NETWORK_UNREACHABLE: return MAPS_ERROR_NETWORK_UNREACHABLE;
37 case HERE_ERROR_INVALID_OPERATION: return MAPS_ERROR_INVALID_OPERATION;
38 case HERE_ERROR_KEY_NOT_AVAILABLE: return MAPS_ERROR_KEY_NOT_AVAILABLE;
39 case HERE_ERROR_RESOURCE_BUSY: return MAPS_ERROR_RESOURCE_BUSY;
40 case HERE_ERROR_CANCELED: return MAPS_ERROR_CANCELED;
41 case HERE_ERROR_UNKNOWN: return MAPS_ERROR_UNKNOWN;
42 case HERE_ERROR_SERVICE_NOT_AVAILABLE: return MAPS_ERROR_SERVICE_NOT_AVAILABLE;
43 case HERE_ERROR_NOT_FOUND: return MAPS_ERROR_NOT_FOUND;
44 case HERE_ERROR_USER_NOT_CONSENTED: return MAPS_ERROR_USER_NOT_CONSENTED;
46 return MAPS_ERROR_UNKNOWN;
49 int ConvertToHereError(int nErr)
52 case MAPS_ERROR_NONE: return HERE_ERROR_NONE;
53 case MAPS_ERROR_PERMISSION_DENIED: return HERE_ERROR_PERMISSION_DENIED;
54 case MAPS_ERROR_OUT_OF_MEMORY: return HERE_ERROR_OUT_OF_MEMORY;
55 case MAPS_ERROR_INVALID_PARAMETER: return HERE_ERROR_INVALID_PARAMETER;
56 case MAPS_ERROR_NOT_SUPPORTED: return HERE_ERROR_NOT_SUPPORTED;
57 case MAPS_ERROR_CONNECTION_TIME_OUT: return HERE_ERROR_CONNECTION_TIME_OUT;
58 case MAPS_ERROR_NETWORK_UNREACHABLE: return HERE_ERROR_NETWORK_UNREACHABLE;
59 case MAPS_ERROR_INVALID_OPERATION: return HERE_ERROR_INVALID_OPERATION;
60 case MAPS_ERROR_KEY_NOT_AVAILABLE: return HERE_ERROR_KEY_NOT_AVAILABLE;
61 case MAPS_ERROR_RESOURCE_BUSY: return HERE_ERROR_RESOURCE_BUSY;
62 case MAPS_ERROR_CANCELED: return HERE_ERROR_CANCELED;
63 case MAPS_ERROR_UNKNOWN: return HERE_ERROR_UNKNOWN;
64 case MAPS_ERROR_SERVICE_NOT_AVAILABLE: return HERE_ERROR_SERVICE_NOT_AVAILABLE;
65 case MAPS_ERROR_NOT_FOUND: return HERE_ERROR_NOT_FOUND;
66 case MAPS_ERROR_USER_NOT_CONSENTED: return HERE_ERROR_USER_NOT_CONSENTED;
68 return HERE_ERROR_UNKNOWN;
71 const char* ConverHereErrorToString(int nErr)
74 case HERE_ERROR_NONE: return "No errors";
75 case HERE_ERROR_PERMISSION_DENIED: return "Permission denied";
76 case HERE_ERROR_OUT_OF_MEMORY: return "Out of memory";
77 case HERE_ERROR_INVALID_PARAMETER: return "Invalid Parameter";
78 case HERE_ERROR_NOT_SUPPORTED: return "Not suppoerted";
79 case HERE_ERROR_CONNECTION_TIME_OUT: return "Connection time out";
80 case HERE_ERROR_NETWORK_UNREACHABLE: return "Network unreachable";
81 case HERE_ERROR_INVALID_OPERATION: return "Invalid operation";
82 case HERE_ERROR_KEY_NOT_AVAILABLE: return "Key not available";
83 case HERE_ERROR_RESOURCE_BUSY: return "Resource busy";
84 case HERE_ERROR_CANCELED: return "Canceled";
85 case HERE_ERROR_UNKNOWN: return "Unknown";
86 case HERE_ERROR_SERVICE_NOT_AVAILABLE: return "Service not available";
87 case HERE_ERROR_NOT_FOUND: return "Not found";
88 case HERE_ERROR_USER_NOT_CONSENTED: return "Not consented";
93 const char* ConvertMapsErrorToChar(int nErr)
96 case MAPS_ERROR_NONE: return "MAPS_ERROR_NONE";
97 case MAPS_ERROR_PERMISSION_DENIED: return "MAPS_ERROR_PERMISSION_DENIED";
98 case MAPS_ERROR_OUT_OF_MEMORY: return "MAPS_ERROR_OUT_OF_MEMORY";
99 case MAPS_ERROR_INVALID_PARAMETER: return "MAPS_ERROR_INVALID_PARAMETER";
100 case MAPS_ERROR_NOT_SUPPORTED: return "MAPS_ERROR_NOT_SUPPORTED";
101 case MAPS_ERROR_CONNECTION_TIME_OUT: return "MAPS_ERROR_CONNECTION_TIME_OUT";
102 case MAPS_ERROR_NETWORK_UNREACHABLE: return "MAPS_ERROR_NETWORK_UNREACHABLE";
103 case MAPS_ERROR_INVALID_OPERATION: return "MAPS_ERROR_INVALID_OPERATION";
104 case MAPS_ERROR_KEY_NOT_AVAILABLE: return "MAPS_ERROR_KEY_NOT_AVAILABLE";
105 case MAPS_ERROR_RESOURCE_BUSY: return "MAPS_ERROR_RESOURCE_BUSY";
106 case MAPS_ERROR_CANCELED: return "MAPS_ERROR_CANCELED";
107 case MAPS_ERROR_UNKNOWN: return "MAPS_ERROR_UNKNOWN";
108 case MAPS_ERROR_SERVICE_NOT_AVAILABLE: return "MAPS_ERROR_SERVICE_NOT_AVAILABLE";
109 case MAPS_ERROR_NOT_FOUND: return "MAPS_ERROR_NOT_FOUND";
110 case MAPS_ERROR_USER_NOT_CONSENTED: return "MAPS_ERROR_USER_NOT_CONSENTED";
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)
130 case MAPS_ROUTE_TRANSPORT_MODE_CAR: return GeoRouteQuery::TM_CarTravel;
131 case MAPS_ROUTE_TRANSPORT_MODE_PEDESTRIAN: return GeoRouteQuery::TM_PedestrianTravel;
132 case MAPS_ROUTE_TRANSPORT_MODE_PUBLICTRANSIT: return GeoRouteQuery::TM_PublicTransitTravel;
135 return GeoRouteQuery::TM_CarTravel;
138 maps_route_transport_mode_e HereUtils::Convert(GeoRouteQuery::TravelMode nVal)
141 case GeoRouteQuery::TM_CarTravel: return MAPS_ROUTE_TRANSPORT_MODE_CAR;
142 case GeoRouteQuery::TM_PedestrianTravel: return MAPS_ROUTE_TRANSPORT_MODE_PEDESTRIAN;
143 case GeoRouteQuery::TM_PublicTransitTravel: return MAPS_ROUTE_TRANSPORT_MODE_PUBLICTRANSIT;
146 return MAPS_ROUTE_TRANSPORT_MODE_CAR;
149 GeoRouteQuery::FeatureType HereUtils::Convert(maps_route_feature_e nVal)
152 case MAPS_ROUTE_FEATURE_NO: return GeoRouteQuery::FT_NoFeature;
153 case MAPS_ROUTE_FEATURE_TOLL: return GeoRouteQuery::FT_TollFeature;
154 case MAPS_ROUTE_FEATURE_MOTORWAY: return GeoRouteQuery::FT_MotorwayFeature;
155 case MAPS_ROUTE_FEATURE_BOATFERRY: return GeoRouteQuery::FT_BoatFerryFeature;
156 case MAPS_ROUTE_FEATURE_RAILFERRY: return GeoRouteQuery::FT_RailFerryFeature;
157 case MAPS_ROUTE_FEATURE_PUBLICTTRANSIT: return GeoRouteQuery::FT_PublicTransitFeature;
158 case MAPS_ROUTE_FEATURE_TUNNEL: return GeoRouteQuery::FT_TunnelFeature;
159 case MAPS_ROUTE_FEATURE_DIRTROAD: return GeoRouteQuery::FT_DirtRoadFeature;
160 case MAPS_ROUTE_FEATURE_PARKS: return GeoRouteQuery::FT_ParksFeature;
161 case MAPS_ROUTE_FEATURE_HOVLANE: return GeoRouteQuery::FT_HOVLane;
162 case MAPS_ROUTE_FEATURE_STAIRS: return GeoRouteQuery::FT_Stairs;
165 return GeoRouteQuery::FT_NoFeature;
168 GeoRouteQuery::FeatureWeight HereUtils::Convert(maps_route_feature_weight_e nVal)
171 case MAPS_ROUTE_FEATURE_WEIGHT_NORMAL: return GeoRouteQuery::FW_NormalFeatureWeight;
172 case MAPS_ROUTE_FEATURE_WEIGHT_PREFER: return GeoRouteQuery::FW_PreferFeatureWeight;
173 case MAPS_ROUTE_FEATURE_WEIGHT_AVOID: return GeoRouteQuery::FW_AvoidFeatureWeight;
174 case MAPS_ROUTE_FEATURE_WEIGHT_SOFTEXCLUDE: return GeoRouteQuery::FW_SoftExcludeFeatureWeight;
175 case MAPS_ROUTE_FEATURE_WEIGHT_STRICTEXCLUDE: return GeoRouteQuery::FW_StrictExcludeFeatureWeight;
178 return GeoRouteQuery::FW_NormalFeatureWeight;
181 maps_route_turn_type_e HereUtils::Convert(Maneuver::InstructionDirection nVal)
184 case Maneuver::ID_NoDirection: return MAPS_ROUTE_TURN_TYPE_NONE;
185 case Maneuver::ID_DirectionForward: return MAPS_ROUTE_TURN_TYPE_STRAIGHT;
186 case Maneuver::ID_DirectionBearRight: return MAPS_ROUTE_TURN_TYPE_BEAR_RIGHT;
187 case Maneuver::ID_DirectionLightRight: return MAPS_ROUTE_TURN_TYPE_LIGHT_RIGHT;
188 case Maneuver::ID_DirectionRight: return MAPS_ROUTE_TURN_TYPE_RIGHT;
189 case Maneuver::ID_DirectionHardRight: return MAPS_ROUTE_TURN_TYPE_HARD_RIGHT;
190 case Maneuver::ID_DirectionUTurnRight: return MAPS_ROUTE_TURN_TYPE_UTURN_RIGHT;
191 case Maneuver::ID_DirectionUTurnLeft: return MAPS_ROUTE_TURN_TYPE_UTURN_LEFT;
192 case Maneuver::ID_DirectionHardLeft: return MAPS_ROUTE_TURN_TYPE_HARD_LEFT;
193 case Maneuver::ID_DirectionLeft: return MAPS_ROUTE_TURN_TYPE_LEFT;
194 case Maneuver::ID_DirectionLightLeft: return MAPS_ROUTE_TURN_TYPE_LIGHT_LEFT;
195 case Maneuver::ID_DirectionBearLeft: return MAPS_ROUTE_TURN_TYPE_BEAR_LEFT;
198 return MAPS_ROUTE_TURN_TYPE_NONE;
201 Maneuver::InstructionDirection HereUtils::Convert(maps_route_turn_type_e nVal)
204 case MAPS_ROUTE_TURN_TYPE_NONE: return Maneuver::ID_NoDirection;
205 case MAPS_ROUTE_TURN_TYPE_STRAIGHT: return Maneuver::ID_DirectionForward;
206 case MAPS_ROUTE_TURN_TYPE_BEAR_RIGHT: return Maneuver::ID_DirectionBearRight;
207 case MAPS_ROUTE_TURN_TYPE_LIGHT_RIGHT: return Maneuver::ID_DirectionLightRight;
208 case MAPS_ROUTE_TURN_TYPE_RIGHT: return Maneuver::ID_DirectionRight;
209 case MAPS_ROUTE_TURN_TYPE_HARD_RIGHT: return Maneuver::ID_DirectionHardRight;
210 case MAPS_ROUTE_TURN_TYPE_UTURN_RIGHT: return Maneuver::ID_DirectionUTurnRight;
211 case MAPS_ROUTE_TURN_TYPE_UTURN_LEFT: return Maneuver::ID_DirectionUTurnLeft;
212 case MAPS_ROUTE_TURN_TYPE_HARD_LEFT: return Maneuver::ID_DirectionHardLeft;
213 case MAPS_ROUTE_TURN_TYPE_LEFT: return Maneuver::ID_DirectionLeft;
214 case MAPS_ROUTE_TURN_TYPE_LIGHT_LEFT: return Maneuver::ID_DirectionLightLeft;
215 case MAPS_ROUTE_TURN_TYPE_BEAR_LEFT: return Maneuver::ID_DirectionBearLeft;
218 return Maneuver::ID_NoDirection;
221 GeoTiledMap::MapType HereUtils::Convert(maps_view_type_e nMapType, bool bBuildings, bool bTraffic, bool bPublicTransit)
224 case MAPS_VIEW_TYPE_NORMAL:
225 if (bPublicTransit) return GeoTiledMap::MT_Normal_Day_Transit;
226 if (bTraffic) return GeoTiledMap::MT_Normal_Traffic_Day;
227 return GeoTiledMap::MT_Normal_Day;
228 case MAPS_VIEW_TYPE_SATELLITE:
229 return GeoTiledMap::MT_Satellite_Day;
230 case MAPS_VIEW_TYPE_TERRAIN:
231 return GeoTiledMap::MT_Terrain_Day;
232 case MAPS_VIEW_TYPE_HYBRID:
233 if (bPublicTransit) return GeoTiledMap::MT_Hybrid_Day_Transit;
234 if (bTraffic) return GeoTiledMap::MT_Hybrid_Traffic_Day;
235 return GeoTiledMap::MT_Hybrid_Day;
238 return GeoTiledMap::MT_Normal_Day;
241 maps_error_e HereUtils::ConvertHttpCodeToMapsError(int nVal)
244 case 200:/*Ok*/ return MAPS_ERROR_NONE;
245 case 408:/*Request timeout*/
246 case 504:/*Gateway timeout*/
247 case 598:/*Network reading timeout*/
248 case 599:/*Network connection timeout*/ return MAPS_ERROR_CONNECTION_TIME_OUT;
250 case 404:/*Not found*/
251 case 407:/*Proxy auth. required*/
252 case 502:/*Bad gateway*/ return MAPS_ERROR_NETWORK_UNREACHABLE;
254 case 401:/*Unauthorized*/
255 case 402:/*Payment required*/ return MAPS_ERROR_KEY_NOT_AVAILABLE;
257 case 405:/*Method not allowed*/
258 case 413:/*Request entity too larget*/
259 case 414:/*Request uri too large*/ return MAPS_ERROR_INVALID_OPERATION;
261 case 403:/*Forbidden*/
262 case 500:/*Server internal error*/
263 case 501:/*Not implemented*/
264 case 503:/*Service unavailable*/ return MAPS_ERROR_SERVICE_NOT_AVAILABLE;
267 if (nVal > 0 && nVal < 100) // curl error code
268 return MAPS_ERROR_NETWORK_UNREACHABLE;
270 if (nVal >= 400 && nVal < 500) // http code 4xx (client-side error)
271 return MAPS_ERROR_INVALID_OPERATION;
273 if (nVal >= 500 && nVal < 600) // http code 5xx (server-side error)
274 return MAPS_ERROR_SERVICE_NOT_AVAILABLE;
276 return MAPS_ERROR_UNKNOWN;
279 GeoBoundingBox& HereUtils::Convert(maps_area_h hArea, GeoBoundingBox& Box)
281 maps_area_s* area_s = (maps_area_s*)hArea;
283 if (!area_s || area_s->type != MAPS_AREA_RECTANGLE) return Box;
285 GeoCoordinates hereCoordLT(area_s->rect.top_left.latitude, area_s->rect.top_left.longitude);
286 GeoCoordinates hereCoordRB(area_s->rect.bottom_right.latitude, area_s->rect.bottom_right.longitude);
288 Box.SetTopLeft(hereCoordLT);
289 Box.SetBottomRight(hereCoordRB);
294 maps_area_h& HereUtils::Convert(GeoBoundingBox Box, maps_area_h& hArea)
296 maps_coordinates_h mapsCoordLT, mapsCoordRB;
297 GeoCoordinates hereCoordLT, hereCoordRB;
299 hereCoordLT = Box.GetTopLeft();
300 hereCoordRB = Box.GetBottomRight();
302 maps_coordinates_create(hereCoordLT.GetLatitude(), hereCoordLT.GetLongitude(), &mapsCoordLT);
303 maps_coordinates_create(hereCoordRB.GetLatitude(), hereCoordRB.GetLongitude(), &mapsCoordRB);
305 maps_area_create_rectangle(mapsCoordLT, mapsCoordRB, &hArea);
307 maps_coordinates_destroy(mapsCoordLT);
308 maps_coordinates_destroy(mapsCoordRB);
313 GeoBoundingCircle& HereUtils::Convert(maps_area_h hArea, GeoBoundingCircle& circle)
315 maps_area_s* area_s = (maps_area_s*)hArea;
317 if (!area_s || area_s->type != MAPS_AREA_CIRCLE) return circle;
319 GeoCoordinates hereCoord(area_s->circle.center.latitude, area_s->circle.center.longitude);
321 circle.SetCenter(hereCoord);
322 circle.SetRadius(area_s->circle.radius);
327 void HereUtils::Convert(String strUtf8, WString& strUtf16)
329 strUtf16.assign(strUtf8.begin(), strUtf8.end());
332 void HereUtils::Convert(WString strUtf16, String& strUtf8)
334 strUtf8.assign(strUtf16.begin(), strUtf16.end());
337 GeoBoundingBox& HereUtils::Convert(const char *src, GeoBoundingBox &box)
341 double coord[4] = { 0.0, };
343 token = strtok_r((char*)src, ",;", &next);
344 while (token && i < 4) {
345 coord[i++] = atof(token);
346 token = strtok_r(NULL, ",;", &next);
348 box.SetTopLeft(GeoCoordinates(coord[0], coord[1]));
349 box.SetBottomRight(GeoCoordinates(coord[2], coord[3]));
353 bool HereUtils::IsValid(GeoCoordinates geoCoord)
355 return IsValidCoord(geoCoord.GetLatitude(), geoCoord.GetLongitude());
358 bool HereUtils::IsValid(maps_coordinates_s geoCoord)
360 return IsValidCoord(geoCoord.latitude, geoCoord.longitude);
363 bool HereUtils::IsValidCoord(double dLat, double dLng)
365 return ((dLat <= LATITUDE_RANGE && dLat >= -LATITUDE_RANGE) &&
366 (dLng <= LONGITUDE_RANGE && dLng >= -LONGITUDE_RANGE));
369 bool HereUtils::IsValid(maps_area_s hArea)
371 if (hArea.type == MAPS_AREA_RECTANGLE) {
372 return (HereUtils::IsValid(hArea.rect.top_left) &&
373 HereUtils::IsValid(hArea.rect.bottom_right));
374 } else if (hArea.type == MAPS_AREA_CIRCLE) {
375 return HereUtils::IsValid(hArea.circle.center);
381 const double HereUtils::ConvertDistance(const double originValue, maps_distance_unit_e destUnit)
383 return ConvertDistance(originValue, MAPS_DISTANCE_UNIT_M, destUnit);
386 const double HereUtils::ConvertDistance(const double originValue, maps_distance_unit_e originUnit, maps_distance_unit_e destUnit)
388 double meterConstant[MAPS_DISTANCE_UNIT_YD+1] = { 1.0, 0.001, 3.2808399, 1.0936133 };
389 return originValue / meterConstant[originUnit] * meterConstant[destUnit];
392 HERE_PLUGIN_END_NAMESPACE