wind_scan.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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_LL
  20. - PROJECTION_OTHER
  21. \param buf buffer hold string northing
  22. \param[out] northing northing
  23. \param projection projection code
  24. \return 0 on error
  25. \return 1 on success
  26. */
  27. int G_scan_northing(const char *buf, double *northing, int projection)
  28. {
  29. if (projection == PROJECTION_LL) {
  30. if (G_lat_scan(buf, northing))
  31. return 1;
  32. if (!scan_double(buf, northing))
  33. return 0;
  34. return (*northing <= 90.0 && *northing >= -90.0);
  35. }
  36. return scan_double(buf, northing);
  37. }
  38. /*!
  39. \brief ASCII easting to double.
  40. Converts the ASCII "easting" coordinate string in <i>buf</i> to its
  41. double representation (into <i>easting</i>).
  42. Supported projection codes (see gis.h):
  43. - PROJECTION_XY
  44. - PROJECTION_UTM
  45. - PROJECTION_LL
  46. - PROJECTION_OTHER
  47. \param buf buffer containing string easting
  48. \param[out] easting easting
  49. \param projection projection code
  50. \return 0 on error
  51. \return 1 on success
  52. */
  53. int G_scan_easting(const char *buf, double *easting, int projection)
  54. {
  55. if (projection == PROJECTION_LL) {
  56. if (G_lon_scan(buf, easting))
  57. return 1;
  58. if (!scan_double(buf, easting))
  59. return 0;
  60. while (*easting > 180.0)
  61. *easting -= 360.0;
  62. while (*easting < -180.0)
  63. *easting += 360.0;
  64. return 1;
  65. }
  66. return scan_double(buf, easting);
  67. }
  68. /*!
  69. \brief ASCII resolution to double.
  70. Converts the ASCII "resolution" string in <i>buf</i> to its double
  71. representation (into resolution).
  72. Supported projection codes (see gis.h):
  73. - PROJECTION_XY
  74. - PROJECTION_UTM
  75. - PROJECTION_LL
  76. - PROJECTION_OTHER
  77. \param buf buffer containing string resolution
  78. \param[out] resolution resolution value
  79. \param projection projection code
  80. \return 0 on error
  81. \return 1 on success
  82. */
  83. int G_scan_resolution(const char *buf, double *res, int projection)
  84. {
  85. if (projection == PROJECTION_LL) {
  86. if (G_llres_scan(buf, res))
  87. return 1;
  88. }
  89. return (scan_double(buf, res) && *res > 0.0);
  90. }
  91. static int scan_double(const char *buf, double *value)
  92. {
  93. char junk[2];
  94. /* use sscanf to convert buf to double
  95. * make sure value doesn't have other characters after it */
  96. *junk = 0;
  97. *value = 0.0;
  98. if (sscanf(buf, "%lf%1s", value, junk) == 1 && *junk == 0) {
  99. while (*buf)
  100. buf++;
  101. buf--;
  102. if (*buf >= 'A' && *buf <= 'Z')
  103. return 0;
  104. if (*buf >= 'a' && *buf <= 'z')
  105. return 0;
  106. return 1; /* success */
  107. }
  108. return 0; /* failure */
  109. }