GRASS Programmer's Manual 6.4.1(2011)
ll_format.c
Go to the documentation of this file.
00001 
00002 /***************************************************************
00003 G_lat_format (lat, buf)
00004     double lat;
00005     char *buf;
00006 
00007 G_lon_format (lon, buf)
00008     double lon;
00009     char *buf;
00010 
00011 G_llres_format (res, buf)
00012     double res;
00013     char *buf;
00014 
00015   formats lat (latitude in degrees), or lon (longitude in degrees)
00016   into buf as dd:mm:ssH, where H (hemishpere) is
00017       N for nothern hemishpere, S for southern,
00018       W for western hemishpere, E for eastern
00019       none for resolution
00020   (lat > 0 is northern, lat < 0 is southern)
00021   (lon > 0 is eastern,  lon < 0 is western)
00022 
00023 Note: lat should be in the range -90 to 90s, but
00024           the range is NOT checked by G_lat_format().
00025       lon can be anything, but
00026           values outside [-180,180] are moved into this range 
00027           by adding (or subtracting) 360.
00028 
00029 NOTE: These routines are used by G_format_northing(), G_format_easting(), and
00030       G_format_resolution(). Those routines are intended to provide
00031       a general interface to window values and should be used instead of
00032       these projection specific routines. In other words, these routines
00033       are for the library only, programmers shouldn't use them.
00034 ***************************************************************/
00035 #include <grass/gis.h>
00036 #include <string.h>
00037 
00038 static int format(char *, int, int, double, char);
00039 static int ll_parts(double, int *, int *, double *);
00040 
00041 int G_lat_format(double lat, char *buf)
00042 {
00043     int d, m;
00044     char h;
00045     double s;
00046 
00047     G_lat_parts(lat, &d, &m, &s, &h);
00048     format(buf, d, m, s, h);
00049 
00050     return 0;
00051 }
00052 
00053 char *G_lat_format_string(void)
00054 {
00055     return "dd:mm:ss{N|S}";
00056 }
00057 
00058 int G_lon_format(double lon, char *buf)
00059 {
00060     int d, m;
00061     char h;
00062     double s;
00063 
00064     G_lon_parts(lon, &d, &m, &s, &h);
00065     format(buf, d, m, s, h);
00066 
00067     return 0;
00068 }
00069 char *G_lon_format_string(void)
00070 {
00071     return "ddd:mm:ss{E|W}";
00072 }
00073 
00074 int G_llres_format(double res, char *buf)
00075 {
00076     int d, m;
00077     char h;
00078     double s;
00079 
00080     G_lat_parts(res, &d, &m, &s, &h);
00081     h = 0;
00082     format(buf, d, m, s, h);
00083 
00084     return 0;
00085 }
00086 char *G_llres_format_string(void)
00087 {
00088     return "dd:mm:ss";
00089 }
00090 
00091 
00092 static int format(char *buf, int d, int m, double s, char h)
00093 {
00094     char temp[50];
00095     double ss;
00096 
00097     sprintf(temp, "%f", s);
00098     sscanf(temp, "%lf", &ss);
00099     if (ss >= 60) {
00100         ss = 0;                 /* force it to zero */
00101         if (++m >= 60) {
00102             m = 0;
00103             d++;
00104         }
00105     }
00106 
00107     if (ss < 10.0)
00108         sprintf(temp, "0%f", ss);
00109     else
00110         sprintf(temp, "%f", ss);
00111     G_trim_decimal(temp);
00112     if (strcmp(temp, "00") != 0 && strcmp(temp, "0") != 0)
00113         sprintf(buf, "%d:%02d:%s%c", d, m, temp, h);
00114     else if (m > 0)
00115         sprintf(buf, "%d:%02d%c", d, m, h);
00116     else if (d > 0)
00117         sprintf(buf, "%d%c", d, h);
00118     else
00119         sprintf(buf, "0");
00120 
00121     return 0;
00122 }
00123 
00124 int G_lat_parts(double lat,     /* lat in degrees to be split into parts */
00125                 int *d, int *m, /* degrees, minutes */
00126                 double *s,      /* seconds */
00127                 char *h         /* hemisphere */
00128     )
00129 {
00130     if (lat < 0) {
00131         *h = 'S';
00132         lat = -lat;
00133     }
00134     else
00135         *h = 'N';
00136 
00137     ll_parts(lat, d, m, s);
00138 
00139     return 0;
00140 }
00141 
00142 int G_lon_parts(double lon,     /* lon in degrees to be split into parts */
00143                 int *d, int *m, /* degrees, minutes */
00144                 double *s,      /* seconds */
00145                 char *h         /* hemisphere */
00146     )
00147 {
00148     while (lon > 180.0)
00149         lon -= 360.0;
00150     while (lon < -180.0)
00151         lon += 360.0;
00152 
00153     if (lon < 0) {
00154         *h = 'W';
00155         lon = -lon;
00156     }
00157     else
00158         *h = 'E';
00159 
00160     ll_parts(lon, d, m, s);
00161 
00162     return 0;
00163 }
00164 
00165 static int ll_parts(double ll,  /* ll in degrees to be split into parts */
00166                     int *d, int *m,     /* degrees, minutes */
00167                     double *s)
00168 {                               /* seconds */
00169     if (ll == 0.0) {
00170         *d = 0;
00171         *m = 0;
00172         *s = 0.0;
00173     }
00174     else {
00175         *d = ll;
00176         *m = (ll - *d) * 60;
00177         if (*m < 0)
00178             *m = 0;
00179         *s = ((ll - *d) * 60 - *m) * 60;
00180         if (*s < 0)
00181             *s = 0;
00182     }
00183 
00184     return 0;
00185 }
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines