main.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /****************************************************************************
  2. *
  3. * MODULE: r.quant
  4. * AUTHOR(S): Michael Shapiro, Olga Waupotitsch, CERL (original contributors)
  5. * Markus Neteler <neteler itc.it>, Roberto Flor <flor itc.it>,
  6. * Glynn Clements <glynn gclements.plus.com>, Jachym Cepicky <jachym les-ejk.cz>,
  7. * Jan-Oliver Wagner <jan intevation.de>
  8. * PURPOSE:
  9. * COPYRIGHT: (C) 1999-2006 by the GRASS Development Team
  10. *
  11. * This program is free software under the GNU General Public
  12. * License (>=v2). Read the file COPYING that comes with GRASS
  13. * for details.
  14. *
  15. *****************************************************************************/
  16. #include <stdlib.h>
  17. #include <stdio.h>
  18. #include <unistd.h>
  19. #include <string.h>
  20. #include "global.h"
  21. #include <grass/raster.h>
  22. #include <grass/glocale.h>
  23. struct Quant quant_struct;
  24. CELL old_min, old_max;
  25. DCELL old_dmin, old_dmax;
  26. char **name; /* input map names */
  27. int noi;
  28. int main(int argc, char *argv[])
  29. {
  30. struct GModule *module;
  31. struct Option *input, *basemap, *fprange, *range, *rules;
  32. struct Flag *trunc, *rnd;
  33. int truncate;
  34. int round;
  35. int i;
  36. CELL new_min, new_max;
  37. DCELL new_dmin, new_dmax;
  38. char *basename;
  39. G_gisinit(argv[0]);
  40. module = G_define_module();
  41. G_add_keyword(_("raster"));
  42. G_add_keyword(_("statistics"));
  43. G_add_keyword(_("quantization"));
  44. module->description =
  45. _("Produces the quantization file for a floating-point map.");
  46. input = G_define_option();
  47. input->key = "input";
  48. input->required = YES;
  49. input->multiple = YES;
  50. input->type = TYPE_STRING;
  51. input->gisprompt = "old,cell,raster";
  52. input->description = _("Raster map(s) to be quantized");
  53. rules = G_define_standard_option(G_OPT_F_INPUT);
  54. rules->key = "rules";
  55. rules->required = NO;
  56. rules->description = _("Path to rules file (\"-\" to read from stdin)");
  57. basemap = G_define_option();
  58. basemap->key = "basemap";
  59. basemap->type = TYPE_STRING;
  60. basemap->gisprompt = "old,cell,raster";
  61. basemap->description = _("Base map to take quant rules from");
  62. fprange = G_define_option();
  63. fprange->key = "fprange";
  64. fprange->key_desc = "dmin,dmax";
  65. fprange->description = _("Floating point range: dmin,dmax");
  66. fprange->type = TYPE_STRING;
  67. range = G_define_option();
  68. range->key = "range";
  69. range->key_desc = "min,max";
  70. range->description = _("Integer range: min,max");
  71. range->type = TYPE_STRING;
  72. trunc = G_define_flag();
  73. trunc->key = 't';
  74. trunc->description = _("Truncate floating point data");
  75. rnd = G_define_flag();
  76. rnd->key = 'r';
  77. rnd->description = _("Round floating point data");
  78. if (G_parser(argc, argv))
  79. exit(EXIT_FAILURE);
  80. truncate = trunc->answer;
  81. round = rnd->answer;
  82. basename = basemap->answer;
  83. i = 0;
  84. if (truncate)
  85. i++;
  86. if (round)
  87. i++;
  88. if (basename)
  89. i++;
  90. if (fprange->answer || range->answer)
  91. i++;
  92. if (rules->answer)
  93. i++;
  94. if (i > 1)
  95. G_fatal_error(_("-%c, -%c, %s=, %s= and %s= are mutually exclusive"),
  96. trunc->key, rnd->key, rules->key, basemap->key, fprange->key);
  97. i = 0;
  98. if (fprange->answer)
  99. i++;
  100. if (range->answer)
  101. i++;
  102. if (i == 1)
  103. G_fatal_error(_("%s= and %s= must be used together"),
  104. fprange->key, range->key);
  105. for (noi = 0; input->answers[noi]; noi++)
  106. ;
  107. name = G_malloc(noi * sizeof(char *));
  108. /* read and check inputs */
  109. for (noi = 0; input->answers[noi]; noi++) {
  110. name[noi] = G_store(input->answers[noi]);
  111. if (Rast_map_type(name[noi], G_mapset()) == CELL_TYPE)
  112. G_fatal_error(_("%s is integer map, it can't be quantized"),
  113. name[noi]);
  114. }
  115. Rast_quant_init(&quant_struct);
  116. /* now figure out what new quant rules to write */
  117. if (truncate) {
  118. G_message(_("Truncating..."));
  119. Rast_quant_truncate(&quant_struct);
  120. }
  121. else if (round) {
  122. G_message(_("Rounding..."));
  123. Rast_quant_round(&quant_struct);
  124. }
  125. else if (basename)
  126. /* set the quant to that of basemap */
  127. {
  128. if (Rast_map_type(basename, "") == CELL_TYPE)
  129. G_fatal_error(_("%s is integer map, it can't be used as basemap"),
  130. basename);
  131. if (Rast_read_quant(basename, "", &quant_struct) <= 0)
  132. G_fatal_error(_("unable to read quant rules for basemap <%s>"),
  133. basename);
  134. }
  135. else if (fprange->answer)
  136. {
  137. if (sscanf(fprange->answer, "%lf,%lf", &new_dmin, &new_dmax) != 2)
  138. G_fatal_error(_("invalid value for %s= <%s>"),
  139. fprange->key, fprange->answer);
  140. if (sscanf(range->answer, "%d,%d", &new_min, &new_max) != 2)
  141. G_fatal_error(_("invalid value for %s= <%s>"),
  142. range->key, range->answer);
  143. G_message(_("Setting quant rules for input map(s) to (%f,%f) -> (%d,%d)"),
  144. new_dmin, new_dmax, new_min, new_max);
  145. Rast_quant_add_rule(&quant_struct, new_dmin, new_dmax, new_min, new_max);
  146. }
  147. else if (rules->answer) {
  148. if (!read_rules(rules->answer))
  149. G_fatal_error("No rules specified");
  150. }
  151. else { /* ask user for quant rules */
  152. if (!read_rules("-")) {
  153. if (isatty(0))
  154. G_message(_("No rules specified. Quant table(s) not changed."));
  155. else
  156. G_fatal_error(_("No rules specified"));
  157. }
  158. } /* use rules */
  159. for (i = 0; i < noi; i++) {
  160. Rast_write_quant(name[i], G_mapset(), &quant_struct);
  161. G_message(_("New quant table created for %s"), name[i]);
  162. }
  163. exit(EXIT_SUCCESS);
  164. }