summaryrefslogtreecommitdiff
path: root/geocode-glib/geocode-location.c
blob: debe27b703ac5378e94163d4f89b7f7f4d1f9b3f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
/*
   Copyright 2012 Bastien Nocera

   The Gnome Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public License as
   published by the Free Software Foundation; either version 2 of the
   License, or (at your option) any later version.

   The Gnome Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public
   License along with the Gnome Library; see the file COPYING.LIB.  If not,
   write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301  USA.

   Authors: Bastien Nocera <hadess@hadess.net>
            Zeeshan Ali (Khattak) <zeeshanak@gnome.org>

 */

#include <geocode-glib/geocode-error.h>
#include <geocode-glib/geocode-enum-types.h>
#include <math.h>
#include <string.h>
#include "geocode-location.h"

#define EARTH_RADIUS_KM 6372.795

/**
 * SECTION:geocode-location
 * @short_description: Geocode location object
 * @include: geocode-glib/geocode-glib.h
 *
 * The #GeocodeLocation instance represents a location on earth, with an
 * optional description.
 **/

struct _GeocodeLocationPrivate {
        gdouble            longitude;
        gdouble            latitude;
        gdouble            altitude;
        gdouble            accuracy;
        guint64            timestamp;
        char              *description;
        GeocodeLocationCRS crs;
};

enum {
        PROP_0,

        PROP_LATITUDE,
        PROP_LONGITUDE,
        PROP_ACCURACY,
        PROP_DESCRIPTION,
        PROP_TIMESTAMP,
        PROP_ALTITUDE,
        PROP_CRS,
};

G_DEFINE_TYPE (GeocodeLocation, geocode_location, G_TYPE_OBJECT)

static void
geocode_location_get_property (GObject    *object,
                               guint       property_id,
                               GValue     *value,
                               GParamSpec *pspec)
{
        GeocodeLocation *location = GEOCODE_LOCATION (object);

        switch (property_id) {
        case PROP_DESCRIPTION:
                g_value_set_string (value,
                                    geocode_location_get_description (location));
                break;

        case PROP_LATITUDE:
                g_value_set_double (value,
                                    geocode_location_get_latitude (location));
                break;

        case PROP_LONGITUDE:
                g_value_set_double (value,
                                    geocode_location_get_longitude (location));
                break;

        case PROP_ALTITUDE:
                g_value_set_double (value,
                                    geocode_location_get_altitude (location));
                break;

        case PROP_ACCURACY:
                g_value_set_double (value,
                                    geocode_location_get_accuracy (location));
                break;

        case PROP_CRS:
                g_value_set_enum (value,
                                  geocode_location_get_crs (location));
                break;

        case PROP_TIMESTAMP:
                g_value_set_uint64 (value,
                                    geocode_location_get_timestamp (location));
                break;

        default:
                /* We don't have any other property... */
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
                break;
        }
}

/**
 * geocode_location_equal:
 * @a: a location
 * @b: another location
 *
 * Compare two #GeocodeLocation instances for equality. This compares all fields
 * and only returns %TRUE if the instances are exactly equal. For example, if
 * both locations have the same physical coordinates, but one location has its
 * #GeocodeLocation:description property set and the other does not, %FALSE
 * will be returned. Similarly, if both locations have the same
 * #GeocodeLocation:latitude, #GeocodeLocation:longitude and
 * #GeocodeLocation:altitude, but a different #GeocodeLocation:accuracy or
 * #GeocodeLocation:timestamp, %FALSE will be returned. Or if both locations
 * have the same#GeocodeLocation:latitude and #GeocodeLocation:longitude but a
 * different #GeocodeLocation:altitude, %FALSE will be returned.
 *
 * Both instances must be non-%NULL.
 *
 * Returns: %TRUE if the instances are equal, %FALSE otherwise
 * Since: 3.23.1
 */
gboolean
geocode_location_equal (GeocodeLocation *a,
                        GeocodeLocation *b)
{
        g_return_val_if_fail (GEOCODE_IS_LOCATION (a), FALSE);
        g_return_val_if_fail (GEOCODE_IS_LOCATION (b), FALSE);

        return (a->priv->longitude == b->priv->longitude &&
                a->priv->latitude == b->priv->latitude &&
                a->priv->altitude == b->priv->altitude &&
                a->priv->accuracy == b->priv->accuracy &&
                a->priv->timestamp == b->priv->timestamp &&
                g_strcmp0 (a->priv->description, b->priv->description) == 0 &&
                a->priv->crs == b->priv->crs);
}

static void
geocode_location_set_latitude (GeocodeLocation *loc,
                               gdouble          latitude)
{
        g_return_if_fail (latitude >= -90.0 && latitude <= 90.0);

        loc->priv->latitude = latitude;
}

static void
geocode_location_set_longitude (GeocodeLocation *loc,
                                gdouble          longitude)
{
        g_return_if_fail (longitude >= -180.0 && longitude <= 180.0);

        loc->priv->longitude = longitude;
}

static void
geocode_location_set_altitude (GeocodeLocation *loc,
                               gdouble          altitude)
{
        loc->priv->altitude = altitude;
}

static void
geocode_location_set_accuracy (GeocodeLocation *loc,
                               gdouble          accuracy)
{
        g_return_if_fail (accuracy >= GEOCODE_LOCATION_ACCURACY_UNKNOWN);

        loc->priv->accuracy = accuracy;
}

static void
geocode_location_set_crs(GeocodeLocation   *loc,
                         GeocodeLocationCRS crs)
{
        g_return_if_fail (GEOCODE_IS_LOCATION (loc));

        loc->priv->crs = crs;
}

static void
geocode_location_set_timestamp (GeocodeLocation *loc,
                                guint64          timestamp)
{
        g_return_if_fail (GEOCODE_IS_LOCATION (loc));

        loc->priv->timestamp = timestamp;
}

static void
geocode_location_constructed (GObject *object)
{
        GeocodeLocation *location = GEOCODE_LOCATION (object);
        GTimeVal tv;

        if (location->priv->timestamp != 0)
                return;

        g_get_current_time (&tv);
        geocode_location_set_timestamp (location, tv.tv_sec);
}

static void
geocode_location_set_property(GObject      *object,
                              guint         property_id,
                              const GValue *value,
                              GParamSpec   *pspec)
{
        GeocodeLocation *location = GEOCODE_LOCATION (object);

        switch (property_id) {
        case PROP_DESCRIPTION:
                geocode_location_set_description (location,
                                                  g_value_get_string (value));
                break;

        case PROP_LATITUDE:
                geocode_location_set_latitude (location,
                                               g_value_get_double (value));
                break;

        case PROP_LONGITUDE:
                geocode_location_set_longitude (location,
                                                g_value_get_double (value));
                break;

        case PROP_ALTITUDE:
                geocode_location_set_altitude (location,
                                               g_value_get_double (value));
                break;

        case PROP_ACCURACY:
                geocode_location_set_accuracy (location,
                                                g_value_get_double (value));
                break;

        case PROP_CRS:
                geocode_location_set_crs (location,
                                          g_value_get_enum (value));
                break;

        case PROP_TIMESTAMP:
                geocode_location_set_timestamp (location,
                                                g_value_get_uint64 (value));
                break;

        default:
                /* We don't have any other property... */
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
                break;
        }
}

static gboolean
parse_geo_uri_special_parameters (GeocodeLocation *loc,
                                  const char      *params,
                                  GError         **error)
{
        char *end_ptr;
        char *next_token;
        char *description;
        char *token_end;
        int description_len;

        if (loc->priv->latitude != 0 || loc->priv->longitude != 0)
            goto err;

        if (strncmp (params, "q=", 2) != 0)
                goto err;

        next_token = ((char *)params) + 2;

        loc->priv->latitude = g_ascii_strtod (next_token, &end_ptr);
        if (*end_ptr != ',' || *end_ptr == *params)
                goto err;
        next_token = end_ptr + 1;

        loc->priv->longitude = g_ascii_strtod (next_token, &end_ptr);
        if (*end_ptr == *next_token)
                goto err;

        if (*end_ptr != '(' || *end_ptr == *next_token)
                goto err;
        next_token = end_ptr + 1;

        if ((token_end = strchr (next_token, ')')) == NULL)
                goto err;

        description_len = token_end - next_token;
        if (description_len <= 0)
            goto err;

        description = g_uri_unescape_segment (next_token,
                                              next_token + description_len,
                                              NULL);
        geocode_location_set_description (loc, description);
        g_free (description);
        return TRUE;

 err:
        g_set_error_literal (error,
                             GEOCODE_ERROR,
                             GEOCODE_ERROR_PARSE,
                             "Failed to parse geo URI parameters");
        return FALSE;
}

/*
  From RFC 5870:
      Both 'crs' and 'u' parameters MUST NOT appear more than once each.
      The 'crs' and 'u' parameters MUST be given before any other
      parameters that may be defined in future extensions.  The 'crs'
      parameter MUST be given first if both 'crs' and 'u' are used.
 */
static gboolean
parse_geo_uri_parameters (GeocodeLocation *loc,
                          const char      *params,
                          GError         **error)
{
        char **parameters;
        char *endptr;
        char *val;
        char *u = NULL;
        char *crs = NULL;
        int ret = TRUE;

        parameters = g_strsplit (params, ";", 2);
        if (parameters[0] == NULL)
                goto err;

        if (g_str_has_prefix (parameters[0], "u=")) {
                /*
                 * if u parameter is first, then there should not be any more
                 * parameters.
                 */
                if (parameters[1] != NULL)
                        goto err;

                u = parameters[0];
        } else if (g_str_has_prefix (parameters[0], "crs=")) {
                /*
                 * if crs parameter is first, then the next should be the u
                 * parameter or none.
                 */
                crs = parameters[0];
                if (parameters[1] != NULL){

                        if (!g_str_has_prefix (parameters[1], "u="))
                                goto err;

                        u = parameters[1];
                }
        } else {
                goto err;
        }

        if (u != NULL) {
                val = u + 2; /* len of 'u=' */
                loc->priv->accuracy = g_ascii_strtod (val, &endptr);
                if (*endptr != '\0')
                        goto err;
        }

        if (crs != NULL) {
                val = crs + 4; /* len of 'crs=' */
                if (g_strcmp0 (val, "wgs84"))
                        goto err;
        }
        goto out;

 err:
        ret = FALSE;
        g_set_error_literal (error,
                             GEOCODE_ERROR,
                             GEOCODE_ERROR_PARSE,
                             "Failed to parse geo URI parameters");
 out:
       g_strfreev (parameters);
       return ret;
}

/*
   From RFC 5870:
      geo-URI       = geo-scheme ":" geo-path
      geo-scheme    = "geo"
      geo-path      = coordinates p
      coordinates   = coord-a "," coord-b [ "," coord-c ]

      [...]

      The value of "-0" for <num> is allowed and is identical to "0".

      In case the URI identifies a location in the default CRS of WGS-84,
      the <coordinates> sub-components are further restricted as follows:

      coord-a        = latitude
      coord-b        = longitude
      coord-c        = altitude

      latitude       = [ "-" ] 1*2DIGIT [ "." 1*DIGIT ]
      longitude      = [ "-" ] 1*3DIGIT [ "." 1*DIGIT ]
      altitude       = [ "-" ] 1*DIGIT [ "." 1*DIGIT ]

       p             = [ crsp ] [ uncp ] *parameter
       crsp          = ";crs=" crslabel
       crslabel      = "wgs84" / labeltext
       uncp          = ";u=" uval
       uval          = pnum

       parameter     = ";" pname [ "=" pvalue ]
       pname         = labeltext
       pvalue        = 1*paramchar
       paramchar     = p-unreserved / unreserved / pct-encoded

       labeltext     = 1*( alphanum / "-" )
       pnum          = 1*DIGIT [ "." 1*DIGIT ]
       num           = [ "-" ] pnum
       unreserved    = alphanum / mark
       mark          = "-" / "_" / "." / "!" / "~" / "*" /
                        "'" / "(" / ")"
       pct-encoded   = "%" HEXDIG HEXDIG
*/
static gboolean
parse_geo_uri (GeocodeLocation *loc,
               const char      *uri,
               GError         **error)
{
        const char *uri_part;
        char *end_ptr;
        char *next_token;
        const char *s;

        /* bail out if we encounter whitespace in uri */
        s = uri;
        while (*s) {
                if (g_ascii_isspace (*s++))
                        goto err;
        }

        uri_part = (const char *) uri + strlen("geo") + 1;

        /* g_ascii_strtod is locale safe */
        loc->priv->latitude = g_ascii_strtod (uri_part, &end_ptr);
        if (*end_ptr != ',' || *end_ptr == *uri_part) {
                goto err;
        }
        next_token = end_ptr + 1;

        loc->priv->longitude = g_ascii_strtod (next_token, &end_ptr);
        if (*end_ptr == *next_token) {
                goto err;
        }
        if (*end_ptr == ',') {
                next_token = end_ptr + 1;
                loc->priv->altitude = g_ascii_strtod (next_token, &end_ptr);
                if (*end_ptr == *next_token) {
                        goto err;
                }
        }
        if (*end_ptr == ';') {
                next_token = end_ptr + 1;
                return parse_geo_uri_parameters (loc, next_token, error);
        } else if (*end_ptr == '?') {
                next_token = end_ptr + 1;
                return parse_geo_uri_special_parameters (loc,
                                                         next_token,
                                                         error);
        } else if (*end_ptr == '\0') {
                return TRUE;
        }
 err:
        g_set_error_literal (error,
                             GEOCODE_ERROR,
                             GEOCODE_ERROR_PARSE,
                             "Failed to parse geo URI");
        return FALSE;
}

static gboolean
parse_uri (GeocodeLocation *location,
           const char      *uri,
           GError         **error)
{
        char *scheme;
        int ret = TRUE;

        scheme = g_uri_parse_scheme (uri);
        if (scheme == NULL) {
                ret = FALSE;
                goto err;
        }

        if (g_strcmp0 (scheme, "geo") == 0) {
                if (!parse_geo_uri (location, uri, error))
                        ret = FALSE;
                goto out;
        } else {
                ret = FALSE;
                goto err;
        }

 err:
        if (error) {
                g_set_error_literal (error,
                                     GEOCODE_ERROR,
                                     GEOCODE_ERROR_NOT_SUPPORTED,
                                     "Unsupported or invalid URI scheme");
        }
 out:
        g_free (scheme);
        return ret;
}

static void
geocode_location_finalize (GObject *glocation)
{
        GeocodeLocation *location = (GeocodeLocation *) glocation;

        g_clear_pointer (&location->priv->description, g_free);

        G_OBJECT_CLASS (geocode_location_parent_class)->finalize (glocation);
}

static void
geocode_location_class_init (GeocodeLocationClass *klass)
{
        GObjectClass *glocation_class = G_OBJECT_CLASS (klass);
        GParamSpec *pspec;

        glocation_class->finalize = geocode_location_finalize;
        glocation_class->get_property = geocode_location_get_property;
        glocation_class->set_property = geocode_location_set_property;
        glocation_class->constructed = geocode_location_constructed;

        g_type_class_add_private (klass, sizeof (GeocodeLocationPrivate));

        /**
         * GeocodeLocation:description:
         *
         * The description of this location.
         */
        pspec = g_param_spec_string ("description",
                                     "Description",
                                     "Description of this location",
                                     NULL,
                                     G_PARAM_READWRITE |
                                     G_PARAM_STATIC_STRINGS);
        g_object_class_install_property (glocation_class, PROP_DESCRIPTION, pspec);

        /**
         * GeocodeLocation:latitude:
         *
         * The latitude of this location in degrees.
         */
        pspec = g_param_spec_double ("latitude",
                                     "Latitude",
                                     "The latitude of this location in degrees",
                                     -90.0,
                                     90.0,
                                     0.0,
                                     G_PARAM_READWRITE |
                                     G_PARAM_STATIC_STRINGS);
        g_object_class_install_property (glocation_class, PROP_LATITUDE, pspec);

        /**
         * GeocodeLocation:longitude:
         *
         * The longitude of this location in degrees.
         */
        pspec = g_param_spec_double ("longitude",
                                     "Longitude",
                                     "The longitude of this location in degrees",
                                     -180.0,
                                     180.0,
                                     0.0,
                                     G_PARAM_READWRITE |
                                     G_PARAM_STATIC_STRINGS);
        g_object_class_install_property (glocation_class, PROP_LONGITUDE, pspec);

        /**
         * GeocodeLocation:altitude:
         *
         * The altitude of this location in meters.
         */
        pspec = g_param_spec_double ("altitude",
                                     "Altitude",
                                     "The altitude of this location in meters",
                                     GEOCODE_LOCATION_ALTITUDE_UNKNOWN,
                                     G_MAXDOUBLE,
                                     GEOCODE_LOCATION_ALTITUDE_UNKNOWN,
                                     G_PARAM_READWRITE |
                                     G_PARAM_STATIC_STRINGS);
        g_object_class_install_property (glocation_class, PROP_ALTITUDE, pspec);

        /**
         * GeocodeLocation:accuracy:
         *
         * The accuracy of this location in meters.
         */
        pspec = g_param_spec_double ("accuracy",
                                     "Accuracy",
                                     "The accuracy of this location in meters",
                                     GEOCODE_LOCATION_ACCURACY_UNKNOWN,
                                     G_MAXDOUBLE,
                                     GEOCODE_LOCATION_ACCURACY_UNKNOWN,
                                     G_PARAM_READWRITE |
                                     G_PARAM_STATIC_STRINGS);
        g_object_class_install_property (glocation_class, PROP_ACCURACY, pspec);


        /**
         * GeocodeLocation:crs:
         *
         * The Coordinate Reference System Identification of this location.
         * Only the value 'wgs84' is currently valid.
         */
        pspec = g_param_spec_enum ("crs",
                                   "Coordinate Reference System Identification",
                                   "Coordinate Reference System Identification",
                                   GEOCODE_TYPE_LOCATION_CRS,
                                   GEOCODE_LOCATION_CRS_WGS84,
                                   G_PARAM_READWRITE |
                                   G_PARAM_CONSTRUCT_ONLY |
                                   G_PARAM_STATIC_STRINGS);
        g_object_class_install_property (glocation_class, PROP_CRS, pspec);

        /**
         * GeocodeLocation:timestamp:
         *
         * A timestamp in seconds since
         * <ulink url="http://en.wikipedia.org/wiki/Unix_epoch">Epoch</ulink>,
         * giving when the location was resolved from an address.
         *
         * A value of 0 (zero) will be interpreted as the current time.
         */
        pspec = g_param_spec_uint64 ("timestamp",
                                     "Timestamp",
                                     "The timestamp of this location "
                                     "in seconds since Epoch",
                                     0,
                                     G_MAXINT64,
                                     0,
                                     G_PARAM_READWRITE |
                                     G_PARAM_CONSTRUCT_ONLY |
                                     G_PARAM_STATIC_STRINGS);
        g_object_class_install_property (glocation_class, PROP_TIMESTAMP, pspec);
}

static void
geocode_location_init (GeocodeLocation *location)
{
        location->priv = G_TYPE_INSTANCE_GET_PRIVATE ((location),
                                                      GEOCODE_TYPE_LOCATION,
                                                      GeocodeLocationPrivate);

        location->priv->altitude = GEOCODE_LOCATION_ALTITUDE_UNKNOWN;
        location->priv->accuracy = GEOCODE_LOCATION_ACCURACY_UNKNOWN;
        location->priv->crs = GEOCODE_LOCATION_CRS_WGS84;
}

/**
 * geocode_location_new:
 * @latitude: a valid latitude
 * @longitude: a valid longitude
 * @accuracy: accuracy of location in meters
 *
 * Creates a new #GeocodeLocation object.
 *
 * Returns: a new #GeocodeLocation object. Use g_object_unref() when done.
 **/
GeocodeLocation *
geocode_location_new (gdouble latitude,
                      gdouble longitude,
                      gdouble accuracy)
{
        return g_object_new (GEOCODE_TYPE_LOCATION,
                             "latitude", latitude,
                             "longitude", longitude,
                             "accuracy", accuracy,
                             NULL);
}

/**
 * geocode_location_new_with_description:
 * @latitude: a valid latitude
 * @longitude: a valid longitude
 * @accuracy: accuracy of location in meters
 * @description: a description for the location
 *
 * Creates a new #GeocodeLocation object.
 *
 * Returns: a new #GeocodeLocation object. Use g_object_unref() when done.
 **/
GeocodeLocation *
geocode_location_new_with_description (gdouble     latitude,
                                       gdouble     longitude,
                                       gdouble     accuracy,
                                       const char *description)
{
        return g_object_new (GEOCODE_TYPE_LOCATION,
                             "latitude", latitude,
                             "longitude", longitude,
                             "accuracy", accuracy,
                             "description", description,
                             NULL);
}

/**
 * geocode_location_set_from_uri:
 * @loc: a #GeocodeLocation
 * @uri: a URI mapping out a location
 * @error: #GError for error reporting, or %NULL to ignore
 *
 * Initialize a #GeocodeLocation object with the given @uri.
 *
 * The URI should be in the geo scheme (RFC 5870) which in its simplest form
 * looks like:
 *
 * - geo:latitude,longitude
 *
 * An <ulink
 * url="http://developer.android.com/guide/components/intents-common.html#Maps">
 * Android extension</ulink> to set a description is also supported in the form of:
 *
 * - geo:0,0?q=latitude,longitude(description)
 *
 * Returns: %TRUE on success and %FALSE on error.
 **/
gboolean
geocode_location_set_from_uri (GeocodeLocation *loc,
                               const char      *uri,
                               GError         **error)
{
        return parse_uri (loc, uri, error);
}

/**
 * geocode_location_set_description:
 * @loc: a #GeocodeLocation
 * @description: a description for the location
 *
 * Sets the description of @loc to @description.
 **/
void
geocode_location_set_description (GeocodeLocation *loc,
                                  const char      *description)
{
        g_return_if_fail (GEOCODE_IS_LOCATION (loc));

        g_free (loc->priv->description);
        loc->priv->description = g_strdup (description);
}

/**
 * geocode_location_get_description:
 * @loc: a #GeocodeLocation
 *
 * Gets the description of location @loc.
 *
 * Returns: The description of location @loc.
 **/
const char *
geocode_location_get_description (GeocodeLocation *loc)
{
        g_return_val_if_fail (GEOCODE_IS_LOCATION (loc), NULL);

        return loc->priv->description;
}

/**
 * geocode_location_get_latitude:
 * @loc: a #GeocodeLocation
 *
 * Gets the latitude of location @loc.
 *
 * Returns: The latitude of location @loc.
 **/
gdouble
geocode_location_get_latitude (GeocodeLocation *loc)
{
        g_return_val_if_fail (GEOCODE_IS_LOCATION (loc), 0.0);

        return loc->priv->latitude;
}

/**
 * geocode_location_get_longitude:
 * @loc: a #GeocodeLocation
 *
 * Gets the longitude of location @loc.
 *
 * Returns: The longitude of location @loc.
 **/
gdouble
geocode_location_get_longitude (GeocodeLocation *loc)
{
        g_return_val_if_fail (GEOCODE_IS_LOCATION (loc), 0.0);

        return loc->priv->longitude;
}

/**
 * geocode_location_get_altitude:
 * @loc: a #GeocodeLocation
 *
 * Gets the altitude of location @loc.
 *
 * Returns: The altitude of location @loc.
 **/
gdouble
geocode_location_get_altitude (GeocodeLocation *loc)
{
        g_return_val_if_fail (GEOCODE_IS_LOCATION (loc),
                              GEOCODE_LOCATION_ALTITUDE_UNKNOWN);

        return loc->priv->altitude;
}

/**
 * geocode_location_get_accuracy:
 * @loc: a #GeocodeLocation
 *
 * Gets the accuracy (in meters) of location @loc.
 *
 * Returns: The accuracy of location @loc.
 **/
gdouble
geocode_location_get_accuracy (GeocodeLocation *loc)
{
        g_return_val_if_fail (GEOCODE_IS_LOCATION (loc),
                              GEOCODE_LOCATION_ACCURACY_UNKNOWN);

        return loc->priv->accuracy;
}

/**
 * geocode_location_get_crs:
 * @loc: a #GeocodeLocation
 *
 * Gets the Coordinate Reference System Identification of location @loc.
 *
 * Returns: The CRS of location @loc.
 **/
GeocodeLocationCRS
geocode_location_get_crs (GeocodeLocation *loc)
{
        g_return_val_if_fail (GEOCODE_IS_LOCATION (loc),
                              GEOCODE_LOCATION_CRS_WGS84);

        return loc->priv->crs;
}

/**
 * geocode_location_get_timestamp:
 * @loc: a #GeocodeLocation
 *
 * Gets the timestamp (in seconds since the Epoch) of location @loc. See
 * #GeocodeLocation:timestamp.
 *
 * Returns: The timestamp of location @loc.
 **/
guint64
geocode_location_get_timestamp (GeocodeLocation *loc)
{
        g_return_val_if_fail (GEOCODE_IS_LOCATION (loc), 0);

        return loc->priv->timestamp;
}

static gdouble
round_coord_n (gdouble coord, guint n)
{
  gdouble fac = pow (10, n);

  return round (coord * fac) / fac;
}

static char *
geo_uri_from_location (GeocodeLocation *loc)
{
        guint precision = 6; /* 0.1 meter precision */
        char *uri;
        char *coords;
        char *params;
        const char *crs = "wgs84";
        char lat[G_ASCII_DTOSTR_BUF_SIZE];
        char lon[G_ASCII_DTOSTR_BUF_SIZE];
        char alt[G_ASCII_DTOSTR_BUF_SIZE];
        char acc[G_ASCII_DTOSTR_BUF_SIZE];

        g_return_val_if_fail (GEOCODE_IS_LOCATION (loc), NULL);

        g_ascii_formatd (lat,
                         G_ASCII_DTOSTR_BUF_SIZE,
                         "%.6f",
                         round_coord_n (loc->priv->latitude, precision));
        g_ascii_formatd (lon,
                         G_ASCII_DTOSTR_BUF_SIZE,
                         "%.6f",
                         round_coord_n (loc->priv->longitude, precision));

        if (loc->priv->altitude != GEOCODE_LOCATION_ALTITUDE_UNKNOWN) {
                g_ascii_dtostr (alt, G_ASCII_DTOSTR_BUF_SIZE,
                                loc->priv->altitude);
                coords = g_strdup_printf ("%s,%s,%s", lat, lon, alt);
        } else {
                coords = g_strdup_printf ("%s,%s", lat, lon);
        }

        if (loc->priv->accuracy != GEOCODE_LOCATION_ACCURACY_UNKNOWN) {
                g_ascii_dtostr (acc, G_ASCII_DTOSTR_BUF_SIZE,
                                loc->priv->accuracy);
                params = g_strdup_printf (";crs=%s;u=%s", crs, acc);
        } else {
                params = g_strdup_printf (";crs=%s", crs);
        }

        uri = g_strconcat ("geo:", coords, params, NULL);
        g_free (coords);
        g_free (params);

        return uri;
}

/**
 * geocode_location_to_uri:
 * @loc: a #GeocodeLocation
 * @scheme: the scheme of the requested URI
 *
 * Creates a URI representing @loc in the scheme specified in @scheme.
 *
 * Returns: a URI representing the location. The returned string should be freed
 * with g_free() when no longer needed.
 **/
char *
geocode_location_to_uri (GeocodeLocation *loc,
                         GeocodeLocationURIScheme scheme)
{
        g_return_val_if_fail (GEOCODE_IS_LOCATION (loc), NULL);
        g_return_val_if_fail (scheme == GEOCODE_LOCATION_URI_SCHEME_GEO, NULL);

        return geo_uri_from_location (loc);
}

/**
 * geocode_location_get_distance_from:
 * @loca: a #GeocodeLocation
 * @locb: a #GeocodeLocation
 *
 * Calculates the distance in km, along the curvature of the Earth,
 * between 2 locations. Note that altitude changes are not
 * taken into account.
 *
 * Returns: a distance in km.
 **/
double
geocode_location_get_distance_from (GeocodeLocation *loca,
                                    GeocodeLocation *locb)
{
        gdouble dlat, dlon, lat1, lat2;
        gdouble a, c;

        g_return_val_if_fail (GEOCODE_IS_LOCATION (loca), 0.0);
        g_return_val_if_fail (GEOCODE_IS_LOCATION (locb), 0.0);

        /* Algorithm from:
         * http://www.movable-type.co.uk/scripts/latlong.html */

        dlat = (locb->priv->latitude - loca->priv->latitude) * M_PI / 180.0;
        dlon = (locb->priv->longitude - loca->priv->longitude) * M_PI / 180.0;
        lat1 = loca->priv->latitude * M_PI / 180.0;
        lat2 = locb->priv->latitude * M_PI / 180.0;

        a = sin (dlat / 2) * sin (dlat / 2) +
            sin (dlon / 2) * sin (dlon / 2) * cos (lat1) * cos (lat2);
        c = 2 * atan2 (sqrt (a), sqrt (1-a));
        return EARTH_RADIUS_KM * c;
}