be9570897c87c82dc6aabeae117129eac7f6dd81
[platform/core/csapi/tizenfx.git] / src / Tizen.Location / Tizen.Location / LocationBoundary.cs
1 // Copyright 2016 by Samsung Electronics, Inc.,
2 //
3 // This software is the confidential and proprietary information
4 // of Samsung Electronics, Inc. ("Confidential Information"). You
5 // shall not disclose such Confidential Information and shall use
6 // it only in accordance with the terms of the license agreement
7 // you entered into with Samsung.
8
9 using System;
10 using System.Collections.Generic;
11 using System.Runtime.InteropServices;
12
13 namespace Tizen.Location
14 {
15     /// <summary>
16     /// Abstract class which provides functions related to geographic bounds information.
17     /// </summary>
18     public abstract class LocationBoundary
19     {
20         internal IntPtr handle;
21
22         /// <summary>
23         /// Gets the location boundary type.
24         /// </summary>
25         public BoundaryType BoundaryType{ get; internal set; }
26
27         internal IntPtr GetHandle()
28         {
29             return handle;
30         }
31
32         /// <summary>
33         /// Checks if the boundary contains the specified geographical coordinates.
34         /// </summary>
35         /// <param name="coordinate"> The coordinate which needs to be checked.</param>
36         /// <returns>Returns a boolean value indicating whether or not the specified coordinate lies in the geographical area.</returns>
37         public bool BoundaryContainsCoordinates(Coordinate coordinate)
38         {
39             Log.Info(Globals.LogTag, "Checking if coordinates are contained within boundary");
40             return Interop.LocationBoundary.IsValidCoordinates(handle, coordinate);
41         }
42     }
43
44     /// <summary>
45     /// Class representing a rectangular location boundary.
46     /// Inherits the Abstract LocationBoundary class.
47     /// </summary>
48     public class RectangleBoundary : LocationBoundary
49     {
50         /// <summary>
51         /// Constructor of the Rectangle boundary class.
52         /// </summary>
53         /// <param name="topLeft"> The coordinate which constitute the top left handside of the rectangular boundary.</param>
54         /// <param name="bottomRight"> The coordinate which constitute the bottom right handside of the rectangular boundary.</param>
55         public RectangleBoundary(Coordinate topLeft, Coordinate bottomRight)
56         {
57             Log.Info(Globals.LogTag, "Calling RectangleBoundary constructor");
58             BoundaryType = BoundaryType.Rectangle;
59             IntPtr boundsHandle;
60             int ret = Interop.LocationBoundary.CreateRectangularBoundary(topLeft, bottomRight, out boundsHandle);
61             if ((LocationBoundError)ret != LocationBoundError.None)
62             {
63                 Log.Error(Globals.LogTag, "Error Creating Rectangular Boundary," + (LocationBoundError)ret);
64                 LocationErrorFactory.ThrowLocationBoundaryException(ret);
65             }
66             handle = boundsHandle;
67         }
68
69         /// <summary>
70         /// Gets the Top Left handside coordinate of a rectangular boundary.
71         /// </summary>
72         public Coordinate TopLeft
73         {
74             get
75             {
76                 Log.Info(Globals.LogTag, "Calling to get CoordinateItem TopLeft");
77                 return GetRectangleCoordinate("TopLeft");
78             }
79         }
80
81         /// <summary>
82         /// Gets the Bottom Right handside coordinate of a rectangular boundary.
83         /// </summary>
84         public Coordinate BottomRight
85         {
86             get
87             {
88                 Log.Info(Globals.LogTag, "Calling to get CoordinateItem BottomRight");
89                 return GetRectangleCoordinate("BottomRight");
90             }
91         }
92
93         private Coordinate GetRectangleCoordinate(string tag)
94         {
95             Coordinate topLeft;
96             Coordinate bottomRight;
97
98             Interop.LocationBoundary.GetRectangleCoordinates(handle, out topLeft, out bottomRight);
99
100             if (tag.Equals("TopLeft"))
101             {
102                 return topLeft;
103             }
104             else
105             {
106                 return bottomRight;
107             }
108         }
109     }
110
111     /// <summary>
112     /// Class representing a circular location boundary.
113     /// Inherits the Abstract LocationBoundary class.
114     /// </summary>
115     public class CircleBoundary : LocationBoundary
116     {
117         /// <summary>
118         /// Constructor of the Circular boundary class.
119         /// </summary>
120         /// <param name="coordinate"> The coordinates which constitute the center of the circular boundary.</param>
121         /// <param name="radius"> The radius value of the circular boundary.</param>
122         public CircleBoundary(Coordinate coordinate, double radius)
123         {
124             Log.Info(Globals.LogTag, "Calling CircleBoundary constructor");
125             BoundaryType = BoundaryType.Circle;
126             IntPtr boundsHandle;
127             int ret = Interop.LocationBoundary.CreateCircleBoundary(coordinate, radius, out boundsHandle);
128             if ((LocationBoundError)ret != LocationBoundError.None)
129             {
130                 Log.Error(Globals.LogTag, "Error Creating Circular Boundary," + (LocationBoundError)ret);
131                 LocationErrorFactory.ThrowLocationBoundaryException(ret);
132             }
133             handle = boundsHandle;
134         }
135
136         /// <summary>
137         /// Gets the coordinate of the center of a circular boundary.
138         /// </summary>
139         public Coordinate Center
140         {
141             get
142             {
143                 return GetCircleCenter();
144             }
145         }
146
147         /// <summary>
148         /// Gets the radius of a circular boundary.
149         /// </summary>
150         public double Radius
151         {
152             get
153             {
154                 return GetRadius();
155             }
156         }
157
158         private Coordinate GetCircleCenter()
159         {
160             Log.Info(Globals.LogTag, "Calling to get CoordinateItem Center");
161             Coordinate center;
162             double radius;
163             Interop.LocationBoundary.GetCircleCoordinates(handle, out center, out radius);
164             return center;
165         }
166
167         private double GetRadius()
168         {
169             Coordinate center;
170             double radius = 0;
171             Interop.LocationBoundary.GetCircleCoordinates(handle, out center, out radius);
172             return radius;
173         }
174     }
175
176     /// <summary>
177     /// Class representing a polygonal location boundary.
178     /// Inherits the Abstract LocationBoundary class.
179     /// </summary>
180     public class PolygonBoundary : LocationBoundary
181     {
182         /// <summary>
183         /// Constructor of the polygon boundary class.
184         /// </summary>
185         /// <param name="coordinates"> The coordinates which constitute the polgonal boundary.</param>
186         public PolygonBoundary(IList<Coordinate> coordinates)
187         {
188             Log.Info(Globals.LogTag, "Calling PolygonBoundary Constructor");
189             BoundaryType = BoundaryType.Polygon;
190             IntPtr[] pointers = new IntPtr[coordinates.Count];
191             IntPtr listPointer = Marshal.AllocHGlobal(Marshal.SizeOf(coordinates[0]) * coordinates.Count);
192             IntPtr boundsHandle;
193             for (int i = 0; i < coordinates.Count; i++)
194             {
195                 pointers[i] = Marshal.AllocHGlobal(Marshal.SizeOf(coordinates[0]));
196                 Marshal.StructureToPtr(coordinates[i], pointers[i], true);
197                 Marshal.WriteIntPtr(listPointer, i * Marshal.SizeOf(coordinates[0]), pointers[i]);
198             }
199             int ret = Interop.LocationBoundary.CreatePolygonBoundary(listPointer, coordinates.Count, out boundsHandle);
200             if ((LocationBoundError)ret != LocationBoundError.None)
201             {
202                 Log.Error(Globals.LogTag, "Error Creating Polygon Boundary," + (LocationBoundError)ret);
203                 LocationErrorFactory.ThrowLocationBoundaryException(ret);
204             }
205             handle = boundsHandle;
206         }
207
208         /// <summary>
209         /// Gets the list of coordinates which constitute a polygonal boundary
210         /// </summary>
211         public IList<Coordinate> Coordinates
212         {
213             get
214             {
215                 return GetCoordinates();
216             }
217         }
218
219         private IList<Coordinate> GetCoordinates()
220         {
221             Log.Info(Globals.LogTag, "Calling to get Polygon coordinates");
222             List<Coordinate> coordinateList = new List<Coordinate>();
223             Interop.LocationBoundary.PolygonCoordinatesCallback callback = (Coordinate coordinate, IntPtr userData) =>
224             {
225                 Coordinate item;
226                 item.Latitude = coordinate.Latitude;
227                 item.Longitude = coordinate.Longitude;
228                 coordinateList.Add(item);
229                 return true;
230             };
231
232             Interop.LocationBoundary.GetForEachPolygonCoordinates(handle, callback, IntPtr.Zero);
233             return coordinateList;
234         }
235     }
236
237     /// <summary>
238     /// The structure which represents the  co-ordinates of a geographical location.
239     /// </summary>
240     [StructLayout(LayoutKind.Sequential)]
241     public struct Coordinate
242     {
243         /// <summary>
244         /// Latitude component of the co-ordinate.
245         /// Should have a value between [-90.0 ~ 90.0] (degrees).
246         /// </summary>
247         public double Latitude;
248
249         /// <summary>
250         /// Longitude component of the co-ordinate.
251         /// Should have a value between [-180.0 ~ 180.0] (degrees).
252         /// </summary>
253         public double Longitude;
254     }
255 }