wind_scan.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*!
  2. \file lib/gis/wind_scan.c
  3. \brief GIS Library - Coordinate scanning functions.
  4. (C) 2001-2009, 2011 by the GRASS Development Team
  5. This program is free software under the GNU General Public License
  6. (>=v2). Read the file COPYING that comes with GRASS for details.
  7. \author Original author CERL
  8. */
  9. #include <stdio.h>
  10. #include <grass/gis.h>
  11. static int scan_double(const char *, double *);
  12. /*!
  13. \brief ASCII northing to double.
  14. Converts the ASCII "northing" coordinate string in <i>buf</i> to its
  15. double representation (into <i>northing</i>).
  16. Supported projection codes (see gis.h):
  17. - PROJECTION_XY
  18. - PROJECTION_UTM
  19. - PROJECTION_SP
  20. - PROJECTION_LL
  21. - PROJECTION_OTHER
  22. \param buf buffer hold string northing
  23. \param[out] northing northing
  24. \param projection projection code
  25. \return 0 on error
  26. \return 1 on success
  27. */
  28. int G_scan_northing(const char *buf, double *northing, int projection)
  29. {
  30. if (projection == PROJECTION_LL) {
  31. if (G_lat_scan(buf, northing))
  32. return 1;
  33. if (!scan_double(buf, northing))
  34. return 0;
  35. return (*northing <= 90.0 && *northing >= -90.0);
  36. }
  37. return scan_double(buf, northing);
  38. }
  39. /*!
  40. \brief ASCII easting to double.
  41. Converts the ASCII "easting" coordinate string in <i>buf</i> to its
  42. double representation (into <i>easting</i>).
  43. Supported projection codes (see gis.h):
  44. - PROJECTION_XY
  45. - PROJECTION_UTM
  46. - PROJECTION_SP
  47. - PROJECTION_LL
  48. - PROJECTION_OTHER
  49. \param buf buffer containing string easting
  50. \param[out] easting easting
  51. \param projection projection code
  52. \return 0 on error
  53. \return 1 on success
  54. */
  55. int G_scan_easting(const char *buf, double *easting, int projection)
  56. {
  57. if (projection == PROJECTION_LL) {
  58. if (G_lon_scan(buf, easting))
  59. return 1;
  60. if (!scan_double(buf, easting))
  61. return 0;
  62. while (*easting > 180.0)
  63. *easting -= 360.0;
  64. while (*easting < -180.0)
  65. *easting += 360.0;
  66. return 1;
  67. }
  68. return scan_double(buf, easting);
  69. }
  70. /*!
  71. \brief ASCII resolution to double.
  72. Converts the ASCII "resolution" string in <i>buf</i> to its double
  73. representation (into resolution).
  74. Supported projection codes (see gis.h):
  75. - PROJECTION_XY
  76. - PROJECTION_UTM
  77. - PROJECTION_SP
  78. - PROJECTION_LL
  79. - PROJECTION_OTHER
  80. \param buf buffer containing string resolution
  81. \param[out] resolution resolution value
  82. \param projection projection code
  83. \return 0 on error
  84. \return 1 on success
  85. */
  86. int G_scan_resolution(const char *buf, double *res, int projection)
  87. {
  88. if (projection == PROJECTION_LL) {
  89. if (G_llres_scan(buf, res))
  90. return 1;
  91. }
  92. return (scan_double(buf, res) && *res > 0.0);
  93. }
  94. static int scan_double(const char *buf, double *value)
  95. {
  96. char junk[2];
  97. /* use sscanf to convert buf to double
  98. * make sure value doesn't have other characters after it */
  99. *junk = 0;
  100. *value = 0.0;
  101. if (sscanf(buf, "%lf%1s", value, junk) == 1 && *junk == 0) {
  102. while (*buf)
  103. buf++;
  104. buf--;
  105. if (*buf >= 'A' && *buf <= 'Z')
  106. return 0;
  107. if (*buf >= 'a' && *buf <= 'z')
  108. return 0;
  109. return 1; /* success */
  110. }
  111. return 0; /* failure */
  112. }