add Maps View APIs
[platform/core/location/maps-plugin-here.git] / src / here_utils.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include "here_utils.h"
18 #include <common/CommunicationError.h>
19
20
21 static const double LATITUDE_RANGE = 85.05113;
22 static const double LONGITUDE_RANGE = 180.0;
23
24
25 extern "C"
26 {
27 int ConvertToMapsError(int nErr)
28 {
29         switch (nErr)
30         {
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;
45         }
46         return MAPS_ERROR_UNKNOWN;
47 }
48
49 int ConvertToHereError(int nErr)
50 {
51         switch (nErr)
52         {
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;
67         }
68         return HERE_ERROR_UNKNOWN;
69 }
70
71 const char* ConverHereErrorToString(int nErr)
72 {
73         switch (nErr)
74         {
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";
89         }
90         return "Unknown";
91 }
92
93 const char* ConvertMapsErrorToChar(int nErr)
94 {
95         switch (nErr)
96         {
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";
111         }
112         return "MAPS_ERROR_UNKNOWN";
113 }
114 }
115
116
117 HERE_PLUGIN_BEGIN_NAMESPACE
118
119 HereUtils::HereUtils()
120 {
121 }
122
123 HereUtils::~HereUtils()
124 {
125 }
126
127 GeoRouteQuery::TravelMode HereUtils::Convert(maps_route_transport_mode_e nVal)
128 {
129         switch (nVal)
130         {
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;
134                 default: break;
135         }
136         return GeoRouteQuery::TM_CarTravel;
137 }
138
139 maps_route_transport_mode_e HereUtils::Convert(GeoRouteQuery::TravelMode nVal)
140 {
141         switch (nVal)
142         {
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;
146                 default: break;
147         }
148         return MAPS_ROUTE_TRANSPORT_MODE_CAR;
149 }
150
151 GeoRouteQuery::FeatureType HereUtils::Convert(maps_route_feature_e nVal)
152 {
153         switch (nVal)
154         {
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;
166                 default: break;
167         }
168         return GeoRouteQuery::FT_NoFeature;
169 }
170
171 GeoRouteQuery::FeatureWeight HereUtils::Convert(maps_route_feature_weight_e nVal)
172 {
173         switch (nVal)
174         {
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;
180                 default: break;
181         }
182         return GeoRouteQuery::FW_NormalFeatureWeight;
183 }
184
185 maps_route_turn_type_e HereUtils::Convert(Maneuver::InstructionDirection nVal)
186 {
187         switch (nVal)
188         {
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;
201                 default: break;
202         }
203         return MAPS_ROUTE_TURN_TYPE_NONE;
204 }
205
206 Maneuver::InstructionDirection HereUtils::Convert(maps_route_turn_type_e nVal)
207 {
208         switch (nVal)
209         {
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;
222                 default: break;
223         }
224         return Maneuver::ID_NoDirection;
225 }
226
227 GeoTiledMap::MapType HereUtils::Convert(maps_view_type_e nMapType, bool bBuildings, bool bTraffic, bool bPublicTransit)
228 {
229         switch (nMapType)
230         {
231                 case MAPS_VIEW_TYPE_NORMAL:
232                         if (bPublicTransit)     return GeoTiledMap::MT_Normal_Day_Transit;
233                         if (bTraffic)           return GeoTiledMap::MT_Normal_Traffic_Day;
234                         return GeoTiledMap::MT_Normal_Day;
235                 case MAPS_VIEW_TYPE_SATELLITE:
236                         return GeoTiledMap::MT_Satellite_Day;
237                 case MAPS_VIEW_TYPE_TERRAIN:
238                         return GeoTiledMap::MT_Terrain_Day;
239                 case MAPS_VIEW_TYPE_HYBRID:
240                         if (bPublicTransit)     return GeoTiledMap::MT_Hybrid_Day_Transit;
241                         if (bTraffic)           return GeoTiledMap::MT_Hybrid_Traffic_Day;
242                         return GeoTiledMap::MT_Hybrid_Day;
243                 default: break;
244         }
245         return GeoTiledMap::MT_Normal_Day;
246 }
247
248 maps_error_e HereUtils::ConvertHttpCodeToMapsError(int nVal)
249 {
250         switch (nVal)
251         {
252                 case 200:/*Ok*/                         return MAPS_ERROR_NONE;
253                 case 408:/*Request timeout*/
254                 case 504:/*Gateway timeout*/
255                 case 598:/*Network reading timeout*/
256                 case 599:/*Network connection timeout*/ return MAPS_ERROR_CONNECTION_TIME_OUT;
257
258                 case 404:/*Not found*/
259                 case 407:/*Proxy auth. required*/
260                 case 502:/*Bad gateway*/                return MAPS_ERROR_NETWORK_UNREACHABLE;
261
262                 case 401:/*Unauthorized*/
263                 case 402:/*Payment required*/           return MAPS_ERROR_KEY_NOT_AVAILABLE;
264
265                 case 405:/*Method not allowed*/
266                 case 413:/*Request entity too larget*/
267                 case 414:/*Request uri too large*/      return MAPS_ERROR_INVALID_OPERATION;
268
269                 case 403:/*Forbidden*/
270                 case 500:/*Server internal error*/
271                 case 501:/*Not implemented*/
272                 case 503:/*Service unavailable*/        return MAPS_ERROR_SERVICE_NOT_AVAILABLE;
273         }
274
275         if (nVal > 0 && nVal < 100) // curl error code
276                 return MAPS_ERROR_NETWORK_UNREACHABLE;
277
278         if (nVal >= 400 && nVal < 500) // http code 4xx (client-side error)
279                 return MAPS_ERROR_INVALID_OPERATION;
280
281         if (nVal >= 500 && nVal < 600) // http code 5xx (server-side error)
282                 return MAPS_ERROR_SERVICE_NOT_AVAILABLE;
283
284         return MAPS_ERROR_UNKNOWN;
285 }
286
287 GeoBoundingBox& HereUtils::Convert(maps_area_h hArea, GeoBoundingBox& Box)
288 {
289         maps_area_s* area_s = (maps_area_s*)hArea;
290
291         if (!area_s || area_s->type != MAPS_AREA_RECTANGLE) return Box;
292
293         GeoCoordinates hereCoordLT(area_s->rect.top_left.latitude, area_s->rect.top_left.longitude);
294         GeoCoordinates hereCoordRB(area_s->rect.bottom_right.latitude, area_s->rect.bottom_right.longitude);
295
296         Box.SetTopLeft(hereCoordLT);
297         Box.SetBottomRight(hereCoordRB);
298
299         return Box;
300 }
301
302 maps_area_h& HereUtils::Convert(GeoBoundingBox Box, maps_area_h& hArea)
303 {
304         maps_coordinates_h mapsCoordLT, mapsCoordRB;
305         GeoCoordinates hereCoordLT, hereCoordRB;
306
307         hereCoordLT = Box.GetTopLeft();
308         hereCoordRB = Box.GetBottomRight();
309
310         maps_coordinates_create(hereCoordLT.GetLatitude(), hereCoordLT.GetLongitude(), &mapsCoordLT);
311         maps_coordinates_create(hereCoordRB.GetLatitude(), hereCoordRB.GetLongitude(), &mapsCoordRB);
312
313         maps_area_create_rectangle(mapsCoordLT, mapsCoordRB, &hArea);
314
315         maps_coordinates_destroy(mapsCoordLT);
316         maps_coordinates_destroy(mapsCoordRB);
317
318         return hArea;
319 }
320
321 GeoBoundingCircle& HereUtils::Convert(maps_area_h hArea, GeoBoundingCircle& circle)
322 {
323         maps_area_s* area_s = (maps_area_s*)hArea;
324
325         if (!area_s || area_s->type != MAPS_AREA_CIRCLE) return circle;
326
327         GeoCoordinates hereCoord(area_s->circle.center.latitude, area_s->circle.center.longitude);
328
329         circle.SetCenter(hereCoord);
330         circle.SetRadius(area_s->circle.radius);
331
332         return circle;
333 }
334
335 void HereUtils::Convert(String strUtf8, WString& strUtf16)
336 {
337         strUtf16.assign(strUtf8.begin(), strUtf8.end());
338 }
339
340 void HereUtils::Convert(WString strUtf16, String& strUtf8)
341 {
342         strUtf8.assign(strUtf16.begin(), strUtf16.end());
343 }
344
345 GeoBoundingBox& HereUtils::Convert(const char *src, GeoBoundingBox &box)
346 {
347         int i = 0;
348         char *token, *next;
349         double coord[4] = { 0.0, };
350
351         token = strtok_r((char*)src, ",;", &next);
352         while (token && i < 4)
353         {
354                 coord[i++] = atof(token);
355                 token = strtok_r(NULL, ",;", &next);
356         }
357         box.SetTopLeft(GeoCoordinates(coord[0], coord[1]));
358         box.SetBottomRight(GeoCoordinates(coord[2], coord[3]));
359         return box;
360 }
361
362 bool HereUtils::IsValid(GeoCoordinates geoCoord)
363 {
364         return IsValidCoord(geoCoord.GetLatitude(), geoCoord.GetLongitude());
365 }
366
367 bool HereUtils::IsValid(maps_coordinates_s geoCoord)
368 {
369         return IsValidCoord(geoCoord.latitude, geoCoord.longitude);
370 }
371
372 bool HereUtils::IsValidCoord(double dLat, double dLng)
373 {
374         return ((dLat <= LATITUDE_RANGE && dLat >= -LATITUDE_RANGE) &&
375                 (dLng <= LONGITUDE_RANGE && dLng >= -LONGITUDE_RANGE));
376 }
377
378 bool HereUtils::IsValid(maps_area_s hArea)
379 {
380         if (hArea.type == MAPS_AREA_RECTANGLE)
381         {
382                 return (HereUtils::IsValid(hArea.rect.top_left) &&
383                         HereUtils::IsValid(hArea.rect.bottom_right));
384         }
385         else if(hArea.type == MAPS_AREA_CIRCLE)
386         {
387                 return HereUtils::IsValid(hArea.circle.center);
388         }
389         else
390                 return false;
391 }
392
393 const double HereUtils::ConvertDistance(const double originValue, maps_distance_unit_e destUnit)
394 {
395         return ConvertDistance(originValue, MAPS_DISTANCE_UNIT_M, destUnit);
396 }
397
398 const double HereUtils::ConvertDistance(const double originValue, maps_distance_unit_e originUnit, maps_distance_unit_e destUnit)
399 {
400         double meterConstant[MAPS_DISTANCE_UNIT_YD+1] = { 1.0, 0.001, 3.2808399, 1.0936133 };
401         return originValue / meterConstant[originUnit] * meterConstant[destUnit];
402 }
403
404 HERE_PLUGIN_END_NAMESPACE