ll_format.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /***************************************************************
  2. G_lat_format (lat, buf)
  3. double lat;
  4. char *buf;
  5. G_lon_format (lon, buf)
  6. double lon;
  7. char *buf;
  8. G_llres_format (res, buf)
  9. double res;
  10. char *buf;
  11. formats lat (latitude in degrees), or lon (longitude in degrees)
  12. into buf as dd:mm:ssH, where H (hemishpere) is
  13. N for northern hemishpere, S for southern,
  14. W for western hemishpere, E for eastern
  15. none for resolution
  16. (lat > 0 is northern, lat < 0 is southern)
  17. (lon > 0 is eastern, lon < 0 is western)
  18. Note: lat should be in the range -90 to 90s, but
  19. the range is NOT checked by G_lat_format().
  20. lon can be anything, but
  21. values outside [-180,180] are moved into this range
  22. by adding (or subtracting) 360.
  23. NOTE: These routines are used by G_format_northing(), G_format_easting(), and
  24. G_format_resolution(). Those routines are intended to provide
  25. a general interface to window values and should be used instead of
  26. these projection specific routines. In other words, these routines
  27. are for the library only, programmers shouldn't use them.
  28. ***************************************************************/
  29. #include <grass/gis.h>
  30. #include <string.h>
  31. static void format(char *, int, int, double, char);
  32. static void ll_parts(double, int *, int *, double *);
  33. void G_lat_format(double lat, char *buf)
  34. {
  35. int d, m;
  36. char h;
  37. double s;
  38. G_lat_parts(lat, &d, &m, &s, &h);
  39. format(buf, d, m, s, h);
  40. }
  41. const char *G_lat_format_string(void)
  42. {
  43. return "dd:mm:ss{N|S}";
  44. }
  45. void G_lon_format(double lon, char *buf)
  46. {
  47. int d, m;
  48. char h;
  49. double s;
  50. G_lon_parts(lon, &d, &m, &s, &h);
  51. format(buf, d, m, s, h);
  52. }
  53. const char *G_lon_format_string(void)
  54. {
  55. return "ddd:mm:ss{E|W}";
  56. }
  57. void G_llres_format(double res, char *buf)
  58. {
  59. int d, m;
  60. char h;
  61. double s;
  62. G_lat_parts(res, &d, &m, &s, &h);
  63. h = 0;
  64. format(buf, d, m, s, h);
  65. }
  66. const char *G_llres_format_string(void)
  67. {
  68. return "dd:mm:ss";
  69. }
  70. static void format(char *buf, int d, int m, double s, char h)
  71. {
  72. char temp[50];
  73. double ss;
  74. sprintf(temp, "%f", s);
  75. sscanf(temp, "%lf", &ss);
  76. if (ss >= 60) {
  77. ss = 0; /* force it to zero */
  78. if (++m >= 60) {
  79. m = 0;
  80. d++;
  81. }
  82. }
  83. if (ss < 10.0)
  84. sprintf(temp, "0%f", ss);
  85. else
  86. sprintf(temp, "%f", ss);
  87. G_trim_decimal(temp);
  88. if (strcmp(temp, "00") != 0 && strcmp(temp, "0") != 0)
  89. sprintf(buf, "%d:%02d:%s%c", d, m, temp, h);
  90. else if (m > 0)
  91. sprintf(buf, "%d:%02d%c", d, m, h);
  92. else if (d > 0)
  93. sprintf(buf, "%d%c", d, h);
  94. else
  95. sprintf(buf, "0");
  96. }
  97. void G_lat_parts(double lat, /* lat in degrees to be split into parts */
  98. int *d, int *m, /* degrees, minutes */
  99. double *s, /* seconds */
  100. char *h /* hemisphere */
  101. )
  102. {
  103. if (lat < 0) {
  104. *h = 'S';
  105. lat = -lat;
  106. }
  107. else
  108. *h = 'N';
  109. ll_parts(lat, d, m, s);
  110. }
  111. void G_lon_parts(double lon, /* lon in degrees to be split into parts */
  112. int *d, int *m, /* degrees, minutes */
  113. double *s, /* seconds */
  114. char *h /* hemisphere */
  115. )
  116. {
  117. #if 0
  118. while (lon > 180.0)
  119. lon -= 360.0;
  120. while (lon < -180.0)
  121. lon += 360.0;
  122. #endif
  123. if (lon < 0) {
  124. *h = 'W';
  125. lon = -lon;
  126. }
  127. else
  128. *h = 'E';
  129. ll_parts(lon, d, m, s);
  130. }
  131. static void ll_parts(double ll, /* ll in degrees to be split into parts */
  132. int *d, int *m, /* degrees, minutes */
  133. double *s /* seconds */
  134. )
  135. {
  136. if (ll == 0.0) {
  137. *d = 0;
  138. *m = 0;
  139. *s = 0.0;
  140. }
  141. else {
  142. *d = ll;
  143. *m = (ll - *d) * 60;
  144. if (*m < 0)
  145. *m = 0;
  146. *s = ((ll - *d) * 60 - *m) * 60;
  147. if (*s < 0)
  148. *s = 0;
  149. }
  150. }