quant_rw.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*!
  2. * \file lib/raster/quant_rw.c
  3. *
  4. * \brief Raster Library - Quantization rules (read/write).
  5. *
  6. * (C) 1999-2009 by the GRASS Development Team
  7. *
  8. * This program is free software under the GNU General Public
  9. * License (>=v2). Read the file COPYING that comes with GRASS
  10. * for details.
  11. *
  12. * \author USACERL and many others
  13. */
  14. #include <string.h>
  15. #include <grass/gis.h>
  16. #include <grass/raster.h>
  17. #include <grass/glocale.h>
  18. /*!
  19. \brief Writes the quant rules.
  20. Writes the quant rules which indicate that all floating numbers
  21. should be truncated instead of applying any quant rules from
  22. floats to integers.
  23. \param name map name
  24. \param mapset mapset name
  25. */
  26. void Rast_truncate_fp_map(const char *name, const char *mapset)
  27. {
  28. struct Quant quant;
  29. Rast_quant_init(&quant);
  30. Rast_quant_truncate(&quant);
  31. /* quantize the map */
  32. Rast_write_quant(name, mapset, &quant);
  33. }
  34. /*!
  35. \brief Writes the quant rules.
  36. Writes the quant rules which indicate that all floating numbers
  37. should be rounded instead of applying any quant rules from
  38. floats to integers.
  39. \param name map name
  40. \param mapset mapset name
  41. */
  42. void Rast_round_fp_map(const char *name, const char *mapset)
  43. {
  44. struct Quant quant;
  45. Rast_quant_init(&quant);
  46. Rast_quant_round(&quant);
  47. /* round the map */
  48. Rast_write_quant(name, mapset, &quant);
  49. }
  50. /*!
  51. * \brief Write quant rules (f_quant) for floating-point raster map.
  52. *
  53. * Writes the <tt>f_quant</tt> file for the raster map <em>name</em>
  54. * with one rule. The rule is generated using the floating-point range
  55. * in <tt>f_range</tt> producing the integer range
  56. * [<em>cmin,cmax</em>].
  57. *
  58. * Make a rule for map \<name\> that maps floating range (d_min, d_max)
  59. * into integer range (min, max)
  60. * This function is useful when the quant rule doesn't depend of the
  61. * range of produced float data, for example the slope map whould
  62. * want to have a quant rule: 0.0, 90.0 -> 0 , 90
  63. * no matter what the min and max slope of this map is.
  64. * \param name map name
  65. * \param mapset mapset name
  66. * \param cmin minimum value
  67. * \param cmax maximum value
  68. */
  69. void Rast_quantize_fp_map(const char *name, const char *mapset,
  70. CELL min, CELL max)
  71. {
  72. DCELL d_min, d_max;
  73. struct FPRange fp_range;
  74. if (Rast_read_fp_range(name, mapset, &fp_range) < 0)
  75. G_fatal_error(_("Unable to read fp range for raster map <%s>"),
  76. G_fully_qualified_name(name, mapset));
  77. Rast_get_fp_range_min_max(&fp_range, &d_min, &d_max);
  78. if (Rast_is_d_null_value(&d_min) || Rast_is_d_null_value(&d_max))
  79. G_fatal_error(_("Raster map <%s> is empty"),
  80. G_fully_qualified_name(name, mapset));
  81. Rast_quantize_fp_map_range(name, mapset, d_min, d_max, min, max);
  82. }
  83. /*!
  84. * \brief Write quant rules (f_quant) for floating-point raster map.
  85. *
  86. * Writes the <tt>f_quant</tt> file for the raster map
  87. * <em>name</em> with one rule. The rule is generated using the floating-point
  88. * range [<em>dmin,dmax</em>] and the integer range
  89. * [<em>min,max</em>].
  90. * This routine differs from the one above in that the application controls the
  91. * floating-point range. For example, r.slope.aspect will use this routine to
  92. * quantize the slope map from [0.0, 90.0] to [0,
  93. * 90] even if the range of slopes is not 0-90. The aspect map would be
  94. * quantized from [0.0, 360.0] to [0, 360].
  95. *
  96. * Make a rule for map \<name\> that maps floating range (d_min, d_max)
  97. * into integer range (min, max)
  98. * This function is useful when the quant rule doesn't depend of the
  99. * range of produced float data, for example the slope map whould
  100. * want to have a quant rule: 0.0, 90.0 -> 0 , 90
  101. * no matter what the min and max slope of this map is.
  102. *
  103. * \param name map name
  104. * \param mapset mapset name
  105. * \param d_min minimum fp value
  106. * \param d_max maximum fp value
  107. * \param min minimum value
  108. * \param max maximum value
  109. */
  110. void Rast_quantize_fp_map_range(const char *name, const char *mapset,
  111. DCELL d_min, DCELL d_max, CELL min, CELL max)
  112. {
  113. struct Quant quant;
  114. Rast_quant_init(&quant);
  115. Rast_quant_add_rule(&quant, d_min, d_max, min, max);
  116. /* quantize the map */
  117. Rast_write_quant(name, mapset, &quant);
  118. }
  119. /*!
  120. * \brief Writes the quant rule table for the raster map
  121. *
  122. * Writes the <tt>f_quant</tt> file for the raster map <em>name</em>
  123. * from <em>q</em>. if mapset==G_mapset() i.e. the map is in current
  124. * mapset, then the original quant file in cell_misc/map/f_quant is
  125. * written. Otherwise <em>q</em> is written into quant2/mapset/name
  126. * (much like colr2 element). This results in map@mapset being read
  127. * using quant rules stored in <em>q</em> from G_mapset(). See
  128. * Rast_read_quant() for detailes.
  129. *
  130. * \param name map name
  131. * \param mapset mapset name
  132. * \param quant pointer to Quant structure which hold quant rules info
  133. */
  134. void Rast_write_quant(const char *name, const char *mapset,
  135. const struct Quant *quant)
  136. {
  137. CELL cell_min, cell_max;
  138. DCELL d_min, d_max;
  139. if (Rast_map_type(name, mapset) == CELL_TYPE) {
  140. G_warning(_("Unable to write quant rules: raster map <%s> is integer"),
  141. name);
  142. return;
  143. }
  144. Rast_quant_get_limits(quant, &d_min, &d_max, &cell_min, &cell_max);
  145. /* first actually write the rules */
  146. if (Rast__quant_export(name, mapset, quant) < 0)
  147. G_fatal_error(_("Unable to write quant rules for raster map <%s>"), name);
  148. }
  149. /*!
  150. * \brief
  151. *
  152. * Reads quantization rules for <i>name</i> in <i>mapset</i> and
  153. * stores them in the quantization structure. If the map is in another
  154. * mapset, first checks for quant2 table for this map in current
  155. * mapset.
  156. * \param name
  157. * \param mapset
  158. * \param q
  159. *
  160. * \return -2 if raster map is of type integer
  161. * \return -1 if (!G_name_is_fully_qualified())
  162. * \return 0 if quantization file does not exist, or the file is empty or has wrong format
  163. * \return 1 if non-empty quantization file exists
  164. *
  165. */
  166. int Rast_read_quant(const char *name, const char *mapset, struct Quant *quant)
  167. {
  168. Rast_quant_init(quant);
  169. return Rast__quant_import(name, mapset, quant);
  170. }