LCOV - code coverage report
Current view: top level - shumate - shumate-location.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 18 18 100.0 %
Date: 2024-05-11 21:41:31 Functions: 6 6 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 3 4 75.0 %

           Branch data     Line data    Source code
       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   [ +  +  +  - ]:         79 : G_DEFINE_INTERFACE (ShumateLocation, shumate_location, G_TYPE_OBJECT);
      34                 :            : 
      35                 :            : 
      36                 :            : static void
      37                 :         13 : shumate_location_default_init (ShumateLocationInterface *iface)
      38                 :            : {
      39                 :            :   /**
      40                 :            :    * ShumateLocation:longitude:
      41                 :            :    *
      42                 :            :    * The longitude coordonate in degrees
      43                 :            :    */
      44                 :         13 :   g_object_interface_install_property (iface,
      45                 :            :       g_param_spec_double ("longitude",
      46                 :            :           "Longitude",
      47                 :            :           "The longitude coordonate in degrees",
      48                 :            :           -180.0f,
      49                 :            :           180.0f,
      50                 :            :           0.0f,
      51                 :            :           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
      52                 :            : 
      53                 :            :   /**
      54                 :            :    * ShumateLocation:latitude:
      55                 :            :    *
      56                 :            :    * The latitude coordonate in degrees
      57                 :            :    */
      58                 :         13 :   g_object_interface_install_property (iface,
      59                 :            :       g_param_spec_double ("latitude",
      60                 :            :           "Latitude",
      61                 :            :           "The latitude coordonate in degrees",
      62                 :            :           -90.0f,
      63                 :            :           90.0f,
      64                 :            :           0.0f,
      65                 :            :           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
      66                 :         13 : }
      67                 :            : 
      68                 :            : 
      69                 :            : /**
      70                 :            :  * shumate_location_set_location:
      71                 :            :  * @location: a #ShumateLocation
      72                 :            :  * @latitude: the latitude in degrees
      73                 :            :  * @longitude: the longitude in degrees
      74                 :            :  *
      75                 :            :  * Sets the coordinates of the location
      76                 :            :  */
      77                 :            : void
      78                 :         12 : shumate_location_set_location (ShumateLocation *location,
      79                 :            :     double latitude,
      80                 :            :     double longitude)
      81                 :            : {
      82                 :         12 :   SHUMATE_LOCATION_GET_IFACE (location)->set_location (location,
      83                 :            :       latitude,
      84                 :            :       longitude);
      85                 :         12 : }
      86                 :            : 
      87                 :            : 
      88                 :            : /**
      89                 :            :  * shumate_location_get_latitude:
      90                 :            :  * @location: a #ShumateLocation
      91                 :            :  *
      92                 :            :  * Gets the latitude coordinate in degrees.
      93                 :            :  *
      94                 :            :  * Returns: the latitude coordinate in degrees.
      95                 :            :  */
      96                 :            : double
      97                 :         24 : shumate_location_get_latitude (ShumateLocation *location)
      98                 :            : {
      99                 :         24 :   return SHUMATE_LOCATION_GET_IFACE (location)->get_latitude (location);
     100                 :            : }
     101                 :            : 
     102                 :            : 
     103                 :            : /**
     104                 :            :  * shumate_location_get_longitude:
     105                 :            :  * @location: a #ShumateLocation
     106                 :            :  *
     107                 :            :  * Gets the longitude coordinate in degrees.
     108                 :            :  *
     109                 :            :  * Returns: the longitude coordinate in degrees.
     110                 :            :  */
     111                 :            : double
     112                 :         24 : shumate_location_get_longitude (ShumateLocation *location)
     113                 :            : {
     114                 :         24 :   return SHUMATE_LOCATION_GET_IFACE (location)->get_longitude (location);
     115                 :            : }
     116                 :            : 
     117                 :            : #define EARTH_RADIUS 6378137.0 /* meters, Equatorial radius */
     118                 :            : 
     119                 :            : /**
     120                 :            :  * shumate_location_distance:
     121                 :            :  * @self: a [iface@Location]
     122                 :            :  * @other: a [iface@Location]
     123                 :            :  *
     124                 :            :  * Calculates the distance in meters between two locations.
     125                 :            :  *
     126                 :            :  * This function uses the great-circle distance formula, which assumes
     127                 :            :  * Earth is a perfect sphere. This limits the accuracy of the result,
     128                 :            :  * but is good enough for most purposes.
     129                 :            :  *
     130                 :            :  * Returns: the distance in meters between @self and @other
     131                 :            :  *
     132                 :            :  * Since: 1.2
     133                 :            :  */
     134                 :            : double
     135                 :         12 : shumate_location_distance (ShumateLocation *self, ShumateLocation *other)
     136                 :            : {
     137                 :         12 :   double lat1 = shumate_location_get_latitude (self) * G_PI / 180.0f;
     138                 :         12 :   double lon1 = shumate_location_get_longitude (self) * G_PI / 180.0f;
     139                 :         12 :   double lat2 = shumate_location_get_latitude (other) * G_PI / 180.0f;
     140                 :         12 :   double lon2 = shumate_location_get_longitude (other) * G_PI / 180.0f;
     141                 :            : 
     142                 :         12 :   return acos (sin (lat1) * sin (lat2) + cos (lat1) * cos (lat2) * cos (lon2 - lon1)) * EARTH_RADIUS;
     143                 :            : }

Generated by: LCOV version 1.14