quant_rw.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. #include <grass/gis.h>
  2. #include <grass/glocale.h>
  3. #include <string.h>
  4. /*********************************************************************
  5. *
  6. * G_quantize_fp_map(name, mapset, min, max)
  7. * char *name, *mapset; name of the map
  8. * CELL min, max; resulting int range
  9. *
  10. * Writes necessary quant rules for map <name> so that
  11. * a floating range of <name> is mapped into integer range (min, max)
  12. *
  13. **********************************************************************
  14. *
  15. * G_quantize_fp_map_range(name, mapset, d_min, d_max, min, max)
  16. * char *name, *mapset; name of the map
  17. * CELL min, max; resulting int range
  18. * DCELL d_min, d_max; floating point range
  19. *
  20. * Make a rule for map <name> that maps floating range (d_min, d_max)
  21. * into integer range (min, max)
  22. * This function is useful when the quant rule doesn't depend of the
  23. * range of produced float data, for example the slope map whould
  24. * want to have a quant rule: 0.0, 90.0 -> 0 , 90
  25. * no matter what the min and max slope of this map is.
  26. *
  27. **********************************************************************
  28. *
  29. * G_write_quant(name, mapset, quant)
  30. * char *name, *mapset;
  31. * struct Quant *quant;
  32. * writes the quant rule table for the map <name>
  33. *
  34. **********************************************************************
  35. *
  36. * G_read_quant(name, mapset, quant)
  37. * char *name, *mapset;
  38. *
  39. * reads the quant table for name@mapset
  40. *
  41. **********************************************************************
  42. *
  43. * G_truncate_fp_map(name, mapset)
  44. * char *name, *mapset;
  45. * struct Quant *quant;
  46. *
  47. * writes the quant rules which indicate that all floating numbers
  48. * should be truncated instead of applying any quant rules from
  49. * floats to integers
  50. *
  51. **********************************************************************
  52. *
  53. * G_round_fp_map(name, mapset)
  54. * char *name, *mapset;
  55. * struct Quant *quant;
  56. *
  57. * writes the quant rules which indicate that all floating numbers
  58. * should be rounded instead of applying any quant rules from
  59. * floats to integers
  60. *
  61. **********************************************************************/
  62. int G_truncate_fp_map(const char *name, const char *mapset)
  63. {
  64. char buf[300];
  65. struct Quant quant;
  66. G_quant_init(&quant);
  67. G_quant_truncate(&quant);
  68. /* quantize the map */
  69. if (G_write_quant(name, mapset, &quant) < 0) {
  70. sprintf(buf, "G_truncate_fp_map: can't write quant rules for map %s",
  71. name);
  72. G_warning(buf);
  73. return -1;
  74. }
  75. return 1;
  76. }
  77. int G_round_fp_map(const char *name, const char *mapset)
  78. {
  79. char buf[300];
  80. struct Quant quant;
  81. G_quant_init(&quant);
  82. G_quant_round(&quant);
  83. /* round the map */
  84. if (G_write_quant(name, mapset, &quant) < 0) {
  85. sprintf(buf, "G_truncate_fp_map: can't write quant rules for map %s",
  86. name);
  87. G_warning(buf);
  88. return -1;
  89. }
  90. return 1;
  91. }
  92. /*!
  93. * \brief
  94. *
  95. * Writes
  96. * the <tt>f_quant</tt> file for the raster map <em>name</em> with one rule. The rule
  97. * is generated using the floating-point range in <tt>f_range</tt> producing the
  98. * integer range [<em>cmin,cmax</em>].
  99. *
  100. * \param name
  101. * \param cmin
  102. * \param cmax
  103. * \return int
  104. */
  105. int G_quantize_fp_map(const char *name, const char *mapset,
  106. CELL min, CELL max)
  107. {
  108. char buf[300];
  109. DCELL d_min, d_max;
  110. struct FPRange fp_range;
  111. if (G_read_fp_range(name, mapset, &fp_range) < 0) {
  112. sprintf(buf, "G_quantize_fp_map: can't read fp range for map %s",
  113. name);
  114. G_warning(buf);
  115. return -1;
  116. }
  117. G_get_fp_range_min_max(&fp_range, &d_min, &d_max);
  118. if (G_is_d_null_value(&d_min) || G_is_d_null_value(&d_max)) {
  119. sprintf(buf, "G_quantize_fp_map: raster map %s is empty", name);
  120. G_warning(buf);
  121. return -1;
  122. }
  123. return G_quantize_fp_map_range(name, mapset, d_min, d_max, min, max);
  124. }
  125. /*-------------------------------------------------------------------------*/
  126. /*!
  127. * \brief
  128. *
  129. * Writes the <tt>f_quant</tt> file for the raster map
  130. * <em>name</em> with one rule. The rule is generated using the floating-point
  131. * range [<em>dmin,dmax</em>] and the integer range
  132. * [<em>min,max</em>].
  133. * This routine differs from the one above in that the application controls the
  134. * floating-point range. For example, r.slope.aspect will use this routine to
  135. * quantize the slope map from [0.0, 90.0] to [0,
  136. * 90] even if the range of slopes is not 0-90. The aspect map would be
  137. * quantized from [0.0, 360.0] to [0, 360].
  138. *
  139. * \param name
  140. * \param dmin
  141. * \param dmax
  142. * \param cmin
  143. * \param cmax
  144. * \return int
  145. */
  146. int G_quantize_fp_map_range(const char *name, const char *mapset,
  147. DCELL d_min, DCELL d_max, CELL min, CELL max)
  148. {
  149. char buf[300];
  150. struct Quant quant;
  151. G_quant_init(&quant);
  152. G_quant_add_rule(&quant, d_min, d_max, min, max);
  153. /* quantize the map */
  154. if (G_write_quant(name, mapset, &quant) < 0) {
  155. sprintf(buf,
  156. "G_quantize_fp_map_range: can't write quant rules for map %s",
  157. name);
  158. G_warning(buf);
  159. return -1;
  160. }
  161. return 1;
  162. }
  163. /*-------------------------------------------------------------------------*/
  164. /*!
  165. * \brief
  166. *
  167. * Writes the <tt>f_quant</tt> file for the raster map <em>name</em> from <em>q</em>.
  168. * if mapset==G_mapset() i.e. the map is in current mapset, then the original
  169. * quant file in cell_misc/map/f_quant is written. Otherwise <em>q</em> is
  170. * written into quant2/mapset/name (much like colr2 element). This results in
  171. * map@mapset being read using quant rules stored in <em>q</em> from
  172. * G_mapset(). See G_read_quant() for detailes.
  173. *
  174. * \param name
  175. * \param mapset
  176. * \param q
  177. * \return int
  178. */
  179. int G_write_quant(const char *name, const char *mapset,
  180. const struct Quant *quant)
  181. {
  182. CELL cell_min, cell_max;
  183. DCELL d_min, d_max;
  184. char buf[300];
  185. if (G_raster_map_type(name, mapset) == CELL_TYPE) {
  186. sprintf(buf, _("Cannot write quant rules: map %s is integer"), name);
  187. G_warning(buf);
  188. return -1;
  189. }
  190. G_quant_get_limits(quant, &d_min, &d_max, &cell_min, &cell_max);
  191. /* first actually write the rules */
  192. if (G__quant_export(name, mapset, quant) < 0) {
  193. sprintf(buf, _("Cannot write quant rules for map %s"), name);
  194. G_warning(buf);
  195. return -1;
  196. }
  197. return 1;
  198. }
  199. /*-------------------------------------------------------------------------*/
  200. /*!
  201. * \brief
  202. *
  203. * reads quantization rules for <tt>"name"</tt> in <tt>"mapset"</tt> and stores them
  204. * in the quantization structure <tt>"quant"</tt>. If the map is in another
  205. * mapset, first checks for quant2 table for this map in current mapset.
  206. * Return codes:
  207. * -2 if raster map is of type integer
  208. * -1 if (! G__name_is_fully_qualified ())
  209. * 0 if quantization file does not exist, or the file is empty or has wrong
  210. * format.
  211. * 1 if non-empty quantization file exists.
  212. *
  213. * \param name
  214. * \param mapset
  215. * \param q
  216. * \return int
  217. */
  218. int G_read_quant(const char *name, const char *mapset, struct Quant *quant)
  219. {
  220. G_quant_init(quant);
  221. return G__quant_import(name, mapset, quant);
  222. }