wind_scan.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*!
  2. * \file gis/wind_scan.c
  3. *
  4. * \brief GIS Library - Window scanning 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 int scan_double(const char *, double *);
  16. /*!
  17. * \brief ASCII northing to double.
  18. *
  19. * Converts the ASCII "northing" coordinate string in <i>buf</i> to its
  20. * double representation (into <i>northing</i>).
  21. *
  22. * \param buf buffer hold string northing
  23. * \param[out] northing northing
  24. * \param projection projection code
  25. *
  26. * \return 0 on error
  27. * \return 1 on success
  28. */
  29. int G_scan_northing(const char *buf, double *northing, int projection)
  30. {
  31. if (projection == PROJECTION_LL) {
  32. if (G_lat_scan(buf, northing))
  33. return 1;
  34. if (!scan_double(buf, northing))
  35. return 0;
  36. return (*northing <= 90.0 && *northing >= -90.0);
  37. }
  38. return scan_double(buf, northing);
  39. }
  40. /*!
  41. * \brief ASCII easting to double.
  42. *
  43. * Converts the ASCII "easting" coordinate string in <i>buf</i> to its
  44. * double representation (into <i>easting</i>).
  45. *
  46. * \param buf buffer containing string easting
  47. * \param[out] easting easting
  48. * \param projection projection code
  49. *
  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. *
  71. * Converts the ASCII "resolution" string in <i>buf</i> to its double
  72. * representation (into resolution).
  73. *
  74. * \param buf buffer containing string resolution
  75. * \param[out] resolution resolution value
  76. * \param projection projection code
  77. *
  78. * \return 0 on error
  79. * \return 1 on success
  80. */
  81. int G_scan_resolution(const char *buf, double *res, int projection)
  82. {
  83. if (projection == PROJECTION_LL) {
  84. if (G_llres_scan(buf, res))
  85. return 1;
  86. }
  87. return (scan_double(buf, res) && *res > 0.0);
  88. }
  89. static int scan_double(const char *buf, double *value)
  90. {
  91. char junk[2];
  92. /* use sscanf to convert buf to double
  93. * make sure value doesn't have other characters after it */
  94. *junk = 0;
  95. *value = 0.0;
  96. if (sscanf(buf, "%lf%1s", value, junk) == 1 && *junk == 0) {
  97. while (*buf)
  98. buf++;
  99. buf--;
  100. if (*buf >= 'A' && *buf <= 'Z')
  101. return 0;
  102. if (*buf >= 'a' && *buf <= 'z')
  103. return 0;
  104. return 1; /* success */
  105. }
  106. return 0; /* failure */
  107. }