File: dms.c

package info (click to toggle)
gpstrans 0.34-2
  • links: PTS
  • area: main
  • in suites: slink
  • size: 476 kB
  • ctags: 348
  • sloc: ansic: 3,463; makefile: 97
file content (122 lines) | stat: -rw-r--r-- 3,686 bytes parent folder | download | duplicates (3)
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
/****************************************************************************/
/*                                                                          */
/* ./gps/dms.c   -   Convert to various position formats                    */
/*                                                                          */
/* This file is part of gpstrans - a program to communicate with garmin gps */
/* Parts are taken from John F. Waers (jfwaers@csn.net) program MacGPS.     */
/*                                                                          */
/*                                                                          */
/*    Copyright (c) 1995 by Carsten Tschach (tschach@zedat.fu-berlin.de)    */
/*                                                                          */
/* Permission  to use, copy,  modify, and distribute  this software and its */
/* documentation for non-commercial purpose, is hereby granted without fee, */
/* providing that the  copyright notice  appear in all copies and that both */
/* the  copyright notice and  this permission  notice appear in  supporting */
/* documentation.  I make no representations about  the suitability of this */
/* software  for any  purpose.  It is  provides "as is" without  express or */
/* implid warranty.                                                         */
/*                                                                          */
/****************************************************************************/
#include "defs.h"
#include "Garmin.h"



/****************************************************************************/
/* Convert degrees to dd mm'ss.s" (DMS-Format)                              */
/****************************************************************************/
char *toDMS(double a)
{
  short        neg = 0;
  double       d, m, s;
  static char  dms[20];

  if (a < 0.0) {
    a   = -a;
    neg = 1;
  }
  d = (double) ((int) a); 
  a = (a - d) * 60.0;
  m = (double) ((int) a);
  s = (a - m) * 60.0;
  
  if (s > 59.5) {
    s  = 0.0;
    m += 1.0;
  }
  if (m > 59.5) {
    m  = 0.0;
    d += 1.0;
  }
  if (neg)
    d = -d;
  
  sprintf(dms, "%.0f%02.0f'%04.1f\"", d, m, s);
  return dms;
}


/****************************************************************************/
/* Convert dd mm'ss.s" (DMS-Format) to degrees.                             */
/****************************************************************************/
double DMStoDegrees(char *dms)
{
  int     neg = 0;
  int     d, m;
  double  s;

  sscanf(dms, "%d%d%lf", &d, &m, &s); 
  s = (double) (abs(d)) + ((double)m + s / 60.0) / 60.0;
  
  if (d >= 0) 
    return s;
  else
    return -s;
}


/****************************************************************************/
/* Convert degrees to dd mm.mmm' (DMM-Format)                               */
/****************************************************************************/
char *toDM(double a)
{
  short        neg = 0;
  double       d, m;
  static char  dm[13];
  
  if (a < 0.0) {
    a = -a;
    neg = 1;
  }

  d = (double) ( (int) a);
  m = (a - d) * 60.0;
  
  if (m > 59.5) {
    m  = 0.0;
    d += 1.0;
  }
  if (neg) d = -d;
  
  sprintf(dm, "%.0f%06.3f'", d, m);
  return dm;
}


/****************************************************************************/
/* Convert dd mm.mmm' (DMM-Format) to degree.                               */
/****************************************************************************/
double DMtoDegrees(char *dms)
{
  short   neg = 0;
  short   d;
  double  m;
  
  sscanf(dms, "%d%lf", &d, &m);
  m = (double) (abs(d)) + m / 60.0;
  
  if (d >= 0)
    return m;
  else
    return -m;
}