main.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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(_("quantization"));
  43. G_add_keyword(_("statistics"));
  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(_("-t, -r, rules=, basemap= and fprange= are mutually exclusive"));
  96. i = 0;
  97. if (fprange->answer)
  98. i++;
  99. if (range->answer)
  100. i++;
  101. if (i == 1)
  102. G_fatal_error(_("fprange= and range= must be used together"));
  103. for (noi = 0; input->answers[noi]; noi++)
  104. ;
  105. name = G_malloc(noi * sizeof(char *));
  106. /* read and check inputs */
  107. for (noi = 0; input->answers[noi]; noi++) {
  108. name[noi] = G_store(input->answers[noi]);
  109. if (Rast_map_type(name[noi], G_mapset()) == CELL_TYPE)
  110. G_fatal_error(_("%s is integer map, it can't be quantized"),
  111. name[noi]);
  112. }
  113. Rast_quant_init(&quant_struct);
  114. /* now figure out what new quant rules to write */
  115. if (truncate) {
  116. G_message(_("Truncating..."));
  117. Rast_quant_truncate(&quant_struct);
  118. }
  119. else if (round) {
  120. G_message(_("Rounding..."));
  121. Rast_quant_round(&quant_struct);
  122. }
  123. else if (basename)
  124. /* set the quant to that of basemap */
  125. {
  126. if (Rast_map_type(basename, "") == CELL_TYPE)
  127. G_fatal_error(_("%s is integer map, it can't be used as basemap"),
  128. basename);
  129. if (Rast_read_quant(basename, "", &quant_struct) <= 0)
  130. G_fatal_error(_("unable to read quant rules for basemap <%s>"),
  131. basename);
  132. }
  133. else if (fprange->answer)
  134. {
  135. if (sscanf(fprange->answer, "%lf,%lf", &new_dmin, &new_dmax) != 2)
  136. G_fatal_error(_("invalid value for fprange= <%s>"), fprange->answer);
  137. if (sscanf(range->answer, "%d,%d", &new_min, &new_max) != 2)
  138. G_fatal_error(_("invalid value for range= <%s>"), range->answer);
  139. G_message(_("Setting quant rules for input map(s) to (%f,%f) -> (%d,%d)"),
  140. new_dmin, new_dmax, new_min, new_max);
  141. Rast_quant_add_rule(&quant_struct, new_dmin, new_dmax, new_min, new_max);
  142. }
  143. else if (rules->answer) {
  144. if (!read_rules(rules->answer))
  145. G_fatal_error("No rules specified");
  146. }
  147. else { /* ask user for quant rules */
  148. if (!read_rules("-")) {
  149. if (isatty(0))
  150. G_message(_("No rules specified. Quant table(s) not changed."));
  151. else
  152. G_fatal_error(_("No rules specified"));
  153. }
  154. } /* use rules */
  155. for (i = 0; i < noi; i++) {
  156. Rast_write_quant(name[i], G_mapset(), &quant_struct);
  157. G_message(_("New quant table created for %s"), name[i]);
  158. }
  159. exit(EXIT_SUCCESS);
  160. }