wind_format.c 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. #include <stdio.h>
  14. #include <grass/gis.h>
  15. static void format_double(double, char *, int);
  16. /*!
  17. * \brief Northing to ASCII.
  18. *
  19. * Converts the double representation of the <i>north</i> coordinate to
  20. * its ASCII representation (into <i>buf</i>).
  21. *
  22. * \param north northing
  23. * \param[out] buf buffer to hold formatted string
  24. * \param projection projection code, or -1 to force full precision FP
  25. */
  26. void G_format_northing(double north, char *buf, int projection)
  27. {
  28. if (projection == PROJECTION_LL)
  29. G_lat_format(north, buf);
  30. else if (projection == -1)
  31. format_double(north, buf, TRUE);
  32. else
  33. format_double(north, buf, FALSE);
  34. }
  35. /*!
  36. * \brief Easting to ASCII.
  37. *
  38. * Converts the double representation of the <i>east</i> coordinate to
  39. * its ASCII representation (into <i>buf</i>).
  40. *
  41. * \param east easting
  42. * \param[out] buf buffer to hold formatted string
  43. * \param projection projection code, or -1 to force full precision FP
  44. */
  45. void G_format_easting(double east, char *buf, int projection)
  46. {
  47. if (projection == PROJECTION_LL)
  48. G_lon_format(east, buf);
  49. else if (projection == -1)
  50. format_double(east, buf, TRUE);
  51. else
  52. format_double(east, buf, FALSE);
  53. }
  54. /*!
  55. * \brief Resolution to ASCII.
  56. *
  57. * Converts the double representation of the <i>resolution</i> to its
  58. * ASCII representation (into <i>buf</i>).
  59. *
  60. * \param resolution resolution value
  61. * \param[out] buf buffer to hold formatted string
  62. * \param projection projection code, or -1 to force full precision FP
  63. */
  64. void G_format_resolution(double res, char *buf, int projection)
  65. {
  66. if (projection == PROJECTION_LL)
  67. G_llres_format(res, buf);
  68. else if (projection == -1)
  69. format_double(res, buf, TRUE);
  70. else
  71. format_double(res, buf, FALSE);
  72. }
  73. /*
  74. * 'full_prec' is boolean, FALSE uses %.8f, TRUE uses %.15g
  75. * The reason to have this is that for lat/lon "%.8f" is not
  76. * enough to preserve fidelity once converted back into D:M:S,
  77. * which leads to rounding errors, especially for resolution.
  78. */
  79. static void format_double(double value, char *buf, int full_prec)
  80. {
  81. if (full_prec)
  82. sprintf(buf, "%.15g", value);
  83. else
  84. sprintf(buf, "%.8f", value);
  85. G_trim_decimal(buf);
  86. }