GRASS GIS 7 Programmer's Manual  7.5.svn(2018)-r73120
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
wind_format.c
Go to the documentation of this file.
1 /*!
2  * \file lib/gis/wind_format.c
3  *
4  * \brief GIS Library - Window formatting functions.
5  *
6  * (C) 2001-2009 by the GRASS Development Team
7  *
8  * This program is free software under the GNU General Public License
9  * (>=v2). Read the file COPYING that comes with GRASS for details.
10  *
11  * \author Original author CERL
12  */
13 
14 #include <stdio.h>
15 #include <grass/gis.h>
16 
17 static void format_double(double, char *, int);
18 
19 /*!
20  * \brief Northing to ASCII.
21  *
22  * Converts the double representation of the <i>north</i> coordinate to
23  * its ASCII representation (into <i>buf</i>).
24  *
25  * \param north northing
26  * \param[out] buf buffer to hold formatted string
27  * \param projection projection code, or -1 to force full precision FP
28  */
29 void G_format_northing(double north, char *buf, int projection)
30 {
31  if (projection == PROJECTION_LL)
32  G_lat_format(north, buf);
33  else if (projection == -1)
34  format_double(north, buf, TRUE);
35  else
36  format_double(north, buf, FALSE);
37 }
38 
39 /*!
40  * \brief Easting to ASCII.
41  *
42  * Converts the double representation of the <i>east</i> coordinate to
43  * its ASCII representation (into <i>buf</i>).
44  *
45  * \param east easting
46  * \param[out] buf buffer to hold formatted string
47  * \param projection projection code, or -1 to force full precision FP
48  */
49 void G_format_easting(double east, char *buf, int projection)
50 {
51  if (projection == PROJECTION_LL)
52  G_lon_format(east, buf);
53  else if (projection == -1)
54  format_double(east, buf, TRUE);
55  else
56  format_double(east, buf, FALSE);
57 }
58 
59 /*!
60  * \brief Resolution to ASCII.
61  *
62  * Converts the double representation of the <i>resolution</i> to its
63  * ASCII representation (into <i>buf</i>).
64  *
65  * \param resolution resolution value
66  * \param[out] buf buffer to hold formatted string
67  * \param projection projection code, or -1 to force full precision FP
68  */
69 void G_format_resolution(double res, char *buf, int projection)
70 {
71  if (projection == PROJECTION_LL)
72  G_llres_format(res, buf);
73  else if (projection == -1)
74  format_double(res, buf, TRUE);
75  else
76  format_double(res, buf, FALSE);
77 }
78 
79 /*
80  * 'full_prec' is boolean, FALSE uses %.8f, TRUE uses %.15g
81  * The reason to have this is that for lat/lon "%.8f" is not
82  * enough to preserve fidelity once converted back into D:M:S,
83  * which leads to rounding errors, especially for resolution.
84  */
85 static void format_double(double value, char *buf, int full_prec)
86 {
87  if (full_prec)
88  sprintf(buf, "%.15g", value);
89  else
90  sprintf(buf, "%.8f", value);
91 
92  G_trim_decimal(buf);
93 }
#define TRUE
Definition: gis.h:49
void G_trim_decimal(char *buf)
Removes trailing zeros from decimal number.
Definition: trim_dec.c:24
void G_format_easting(double east, char *buf, int projection)
Easting to ASCII.
Definition: wind_format.c:49
void G_format_resolution(double res, char *buf, int projection)
Resolution to ASCII.
Definition: wind_format.c:69
void G_format_northing(double north, char *buf, int projection)
Northing to ASCII.
Definition: wind_format.c:29
void G_lon_format(double lon, char *buf)
Definition: ll_format.c:56
#define FALSE
Definition: gis.h:53
#define PROJECTION_LL
Projection code - Latitude-Longitude.
Definition: gis.h:99
void G_llres_format(double res, char *buf)
Definition: ll_format.c:71
void G_lat_format(double lat, char *buf)
Definition: ll_format.c:41