GCC Code Coverage Report


Directory: ./
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 100.0% 21 / 0 / 21
Functions: 100.0% 6 / 0 / 6
Branches: 66.7% 4 / 0 / 6

shumate/shumate-location.c
Line Branch Exec Source
1 /*
2 * Copyright (C) 2011-2013 Jiri Techet <techet@gmail.com>
3 * Copyright (C) 2019 Marcus Lundblad <ml@update.uu.se>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 /**
21 * ShumateLocation:
22 *
23 * An interface common to objects having latitude and longitude
24 *
25 * By implementing #ShumateLocation the object declares that it has latitude
26 * and longitude and can be used to specify location on the map.
27 */
28
29 #include <math.h>
30
31 #include "shumate-location.h"
32
33
3/4
✓ Branch 2 → 3 taken 8 times.
✓ Branch 2 → 9 taken 45 times.
✓ Branch 4 → 5 taken 8 times.
✗ Branch 4 → 9 not taken.
53 G_DEFINE_INTERFACE (ShumateLocation, shumate_location, G_TYPE_OBJECT);
34
35
36 static void
37 8 shumate_location_default_init (ShumateLocationInterface *iface)
38 {
39 /**
40 * ShumateLocation:longitude:
41 *
42 * The longitude coordinate in degrees
43 */
44 8 g_object_interface_install_property (iface,
45 g_param_spec_double ("longitude", NULL, NULL,
46 -180.0f,
47 180.0f,
48 0.0f,
49 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
50
51 /**
52 * ShumateLocation:latitude:
53 *
54 * The latitude coordinate in degrees
55 */
56 8 g_object_interface_install_property (iface,
57 g_param_spec_double ("latitude", NULL, NULL,
58 -90.0f,
59 90.0f,
60 0.0f,
61 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
62 8 }
63
64
65 /**
66 * shumate_location_set_location:
67 * @location: a #ShumateLocation
68 * @latitude: the latitude in degrees
69 * @longitude: the longitude in degrees
70 *
71 * Sets the coordinates of the location
72 */
73 void
74 8 shumate_location_set_location (ShumateLocation *location,
75 double latitude,
76 double longitude)
77 {
78 8 SHUMATE_LOCATION_GET_IFACE (location)->set_location (location,
79 latitude,
80 longitude);
81 8 }
82
83
84 /**
85 * shumate_location_get_latitude:
86 * @location: a #ShumateLocation
87 *
88 * Gets the latitude coordinate in degrees.
89 *
90 * Returns: the latitude coordinate in degrees.
91 */
92 double
93 16 shumate_location_get_latitude (ShumateLocation *location)
94 {
95 16 return SHUMATE_LOCATION_GET_IFACE (location)->get_latitude (location);
96 }
97
98
99 /**
100 * shumate_location_get_longitude:
101 * @location: a #ShumateLocation
102 *
103 * Gets the longitude coordinate in degrees.
104 *
105 * Returns: the longitude coordinate in degrees.
106 */
107 double
108 16 shumate_location_get_longitude (ShumateLocation *location)
109 {
110 16 return SHUMATE_LOCATION_GET_IFACE (location)->get_longitude (location);
111 }
112
113 #define EARTH_RADIUS 6378137.0 /* meters, Equatorial radius */
114
115 /**
116 * shumate_location_distance:
117 * @self: a [iface@Location]
118 * @other: a [iface@Location]
119 *
120 * Calculates the distance in meters between two locations.
121 *
122 * This function uses the great-circle distance formula, which assumes
123 * Earth is a perfect sphere. This limits the accuracy of the result,
124 * but is good enough for most purposes.
125 *
126 * Returns: the distance in meters between @self and @other
127 *
128 * Since: 1.2
129 */
130 double
131 8 shumate_location_distance (ShumateLocation *self, ShumateLocation *other)
132 {
133 8 double distance;
134 8 double lat1 = shumate_location_get_latitude (self) * G_PI / 180.0f;
135 8 double lon1 = shumate_location_get_longitude (self) * G_PI / 180.0f;
136 8 double lat2 = shumate_location_get_latitude (other) * G_PI / 180.0f;
137 8 double lon2 = shumate_location_get_longitude (other) * G_PI / 180.0f;
138
139 8 distance = acos (sin (lat1) * sin (lat2) + cos (lat1) * cos (lat2) * cos (lon2 - lon1)) * EARTH_RADIUS;
140
141
1/2
✓ Branch 6 → 7 taken 8 times.
✗ Branch 6 → 8 not taken.
8 if (isnan(distance))
142 return 0;
143 else
144 8 return distance;
145 }
146