ll_format.c

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

Generated on Wed Dec 19 14:59:06 2007 for GRASS by  doxygen 1.5.4