main.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /****************************************************************************
  2. *
  3. * MODULE: r.reclass
  4. * AUTHOR(S): James Westervelt, Michael Shapiro, CERL (original contributors)
  5. * Huidae Cho <grass4u gmail.com>, Glynn Clements <glynn gclements.plus.com>,
  6. * Hamish Bowman <hamish_b yahoo.com>, Jan-Oliver Wagner <jan intevation.de>,
  7. * Markus Neteler <neteler itc.it>
  8. * PURPOSE:
  9. * COPYRIGHT: (C) 1999-2006, 2010 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 <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <unistd.h>
  20. #include <grass/gis.h>
  21. #include <grass/raster.h>
  22. #include <grass/glocale.h>
  23. #include "rule.h"
  24. int main(int argc, char *argv[])
  25. {
  26. struct Categories cats;
  27. struct FPRange range;
  28. DCELL min, max;
  29. RASTER_MAP_TYPE map_type;
  30. char buf[1024];
  31. RULE *rules, *tail;
  32. int any;
  33. const char *old_mapset;
  34. FILE *srcfp;
  35. int tty;
  36. struct GModule *module;
  37. struct
  38. {
  39. struct Option *input, *output, *title, *rules;
  40. } parm;
  41. /* any interaction must run in a term window */
  42. G_putenv("GRASS_UI_TERM", "1");
  43. G_gisinit(argv[0]);
  44. module = G_define_module();
  45. G_add_keyword(_("raster"));
  46. G_add_keyword(_("reclassification"));
  47. module->label = _("Reclassify raster map based on category values.");
  48. module->description =
  49. _("Creates a new raster map whose category values are based "
  50. "upon a reclassification of the categories in an existing "
  51. "raster map.");
  52. parm.input = G_define_standard_option(G_OPT_R_INPUT);
  53. parm.input->description = _("Name of raster map to be reclassified");
  54. parm.output = G_define_standard_option(G_OPT_R_OUTPUT);
  55. parm.rules = G_define_standard_option(G_OPT_F_INPUT);
  56. parm.rules->key = "rules";
  57. parm.rules->label = _("File containing reclass rules");
  58. parm.rules->description = _("\"-\" to read from stdin");
  59. parm.title = G_define_option();
  60. parm.title->key = "title";
  61. parm.title->required = NO;
  62. parm.title->type = TYPE_STRING;
  63. parm.title->description = _("Title for the resulting raster map");
  64. if (G_parser(argc, argv))
  65. exit(EXIT_FAILURE);
  66. old_mapset = G_find_raster2(parm.input->answer, "");
  67. if (old_mapset == NULL)
  68. G_fatal_error(_("Raster map <%s> not found"), parm.input->answer);
  69. if (strcmp(parm.input->answer, parm.output->answer) == 0 &&
  70. strcmp(old_mapset, G_mapset()) == 0)
  71. G_fatal_error(_("Input map can NOT be the same as output map"));
  72. srcfp = stdin;
  73. if (strcmp(parm.rules->answer, "-") != 0) {
  74. srcfp = fopen(parm.rules->answer, "r");
  75. if (!srcfp)
  76. G_fatal_error(_("Cannot open rules file <%s>"),
  77. parm.rules->answer);
  78. }
  79. tty = isatty(fileno(srcfp));
  80. Rast_init_cats("", &cats);
  81. map_type = Rast_map_type(parm.input->answer, old_mapset);
  82. Rast_read_fp_range(parm.input->answer, old_mapset, &range);
  83. Rast_get_fp_range_min_max(&range, &min, &max);
  84. rules = tail = NULL;
  85. any = 0;
  86. if (tty) {
  87. fprintf(stdout,
  88. _("Enter rule(s), \"end\" when done, \"help\" if you need it\n"));
  89. if (map_type == FCELL_TYPE)
  90. fprintf(stdout, _("FCELL: Data range is %.7g to %.7g\n"),
  91. (double)min, (double)max);
  92. else if (map_type == DCELL_TYPE)
  93. fprintf(stdout, _("DCELL: Data range is %.15g to %.15g\n"),
  94. (double)min, (double)max);
  95. else
  96. fprintf(stdout, _("CELL: Data range is %ld to %ld\n"), (long)min,
  97. (long)max);
  98. }
  99. while (input(srcfp, tty, buf)) {
  100. switch (parse(buf, &rules, &tail, &cats)) {
  101. case -1:
  102. if (tty) {
  103. fprintf(stderr, _("Illegal reclass rule -"));
  104. fprintf(stderr, _(" ignored\n"));
  105. }
  106. else {
  107. strcat(buf, _(" - invalid reclass rule"));
  108. G_fatal_error(buf);
  109. }
  110. break;
  111. case 0:
  112. break;
  113. default:
  114. any = 1;
  115. break;
  116. }
  117. }
  118. if (!any) {
  119. if (tty)
  120. G_fatal_error(_("No rules specified. Raster map <%s> not created"),
  121. parm.output->answer);
  122. else
  123. G_fatal_error(_("No rules specified"));
  124. }
  125. reclass(parm.input->answer, old_mapset, parm.output->answer, rules, &cats,
  126. parm.title->answer);
  127. exit(EXIT_SUCCESS);
  128. }