main.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /****************************************************************************
  2. *
  3. * MODULE: r.mode
  4. * AUTHOR(S): Michael Shapiro (CERL) (original contributor),
  5. * Markus Neteler <neteler itc.it>,
  6. * Roberto Flor <flor itc.it>,
  7. * Bernhard Reiter <bernhard intevation.de>,
  8. * Glynn Clements <glynn gclements.plus.com>,
  9. * Jachym Cepicky <jachym les-ejk.cz>,
  10. * Jan-Oliver Wagner <jan intevation.de>
  11. * PURPOSE: calculates the most frequently occurring value (i. e., mode)
  12. * of data contained in a cover raster map layer for areas
  13. * assigned the same category value in the user-specified
  14. * base raster map
  15. * COPYRIGHT: (C) 1999-2006 by the GRASS Development Team
  16. *
  17. * This program is free software under the GNU General Public
  18. * License (>=v2). Read the file COPYING that comes with GRASS
  19. * for details.
  20. *
  21. *****************************************************************************/
  22. #include <stdlib.h>
  23. #include <unistd.h>
  24. #include <string.h>
  25. #include <grass/gis.h>
  26. #include <grass/raster.h>
  27. #include "local_proto.h"
  28. #include <grass/glocale.h>
  29. int main(int argc, char *argv[])
  30. {
  31. struct GModule *module;
  32. struct
  33. {
  34. struct Option *base, *cover, *output;
  35. } parm;
  36. char *basemap;
  37. char *covermap;
  38. char *outmap;
  39. char input[GNAME_MAX*2+8];
  40. char output[GNAME_MAX+8];
  41. const char *args[5];
  42. struct Popen stats_child, reclass_child;
  43. struct Categories cover_cats;
  44. struct Colors colors;
  45. FILE *stats, *reclass;
  46. int first;
  47. long basecat, covercat, catb, catc;
  48. double value, max;
  49. G_gisinit(argv[0]);
  50. module = G_define_module();
  51. G_add_keyword(_("raster"));
  52. G_add_keyword(_("statistics"));
  53. G_add_keyword(_("algebra"));
  54. module->description =
  55. _("Finds the mode of values in a cover map within "
  56. "areas assigned the same category value in a "
  57. "user-specified base map.");
  58. parm.base = G_define_option();
  59. parm.base->key = "base";
  60. parm.base->description = _("Base map to be reclassified");
  61. parm.base->required = YES;
  62. parm.base->type = TYPE_STRING;
  63. parm.base->gisprompt = "old,cell,raster";
  64. parm.cover = G_define_option();
  65. parm.cover->key = "cover";
  66. parm.cover->description = _("Coverage map");
  67. parm.cover->required = YES;
  68. parm.cover->type = TYPE_STRING;
  69. parm.cover->gisprompt = "old,cell,raster";
  70. parm.output = G_define_option();
  71. parm.output->key = "output";
  72. parm.output->description = _("Output map");
  73. parm.output->required = YES;
  74. parm.output->type = TYPE_STRING;
  75. parm.output->gisprompt = "new,cell,raster";
  76. if (G_parser(argc, argv))
  77. exit(1);
  78. basemap = parm.base->answer;
  79. covermap = parm.cover->answer;
  80. outmap = parm.output->answer;
  81. if (Rast_read_cats(covermap, "", &cover_cats) < 0) {
  82. G_fatal_error(_("%s: Unable to read category labels"), covermap);
  83. }
  84. sprintf(input, "input=%s,%s", basemap, covermap);
  85. args[0] = "r.stats";
  86. args[1] = "-an";
  87. args[2] = input;
  88. args[3] = NULL;
  89. stats = G_popen_read(&stats_child, "r.stats", args);
  90. sprintf(input, "input=%s", basemap);
  91. sprintf(output, "output=%s", outmap);
  92. args[0] = "r.reclass";
  93. args[1] = input;
  94. args[2] = output;
  95. args[3] = "rules=-";
  96. args[4] = NULL;
  97. reclass = G_popen_write(&reclass_child, "r.reclass", args);
  98. first = 1;
  99. while (read_stats(stats, &basecat, &covercat, &value)) {
  100. if (first) {
  101. first = 0;
  102. catb = basecat;
  103. catc = covercat;
  104. max = value;
  105. }
  106. if (basecat != catb) {
  107. write_reclass(reclass, catb, catc, Rast_get_c_cat((CELL *) &catc, &cover_cats));
  108. catb = basecat;
  109. catc = covercat;
  110. max = value;
  111. }
  112. if (value > max) {
  113. catc = covercat;
  114. max = value;
  115. }
  116. }
  117. if (first) {
  118. catb = catc = 0;
  119. }
  120. write_reclass(reclass, catb, catc, Rast_get_c_cat((CELL *) &catc, &cover_cats));
  121. G_popen_close(&reclass_child);
  122. G_popen_close(&stats_child);
  123. if (Rast_read_colors(parm.cover->answer, "", &colors) < 0)
  124. G_fatal_error(_("Unable to read color table for %s"),
  125. parm.cover->answer);
  126. Rast_write_colors(parm.output->answer, G_mapset(), &colors);
  127. return 0;
  128. }