main.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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[4];
  42. struct Popen stats_child, reclass_child;
  43. struct Categories cover_cats;
  44. FILE *stats, *reclass;
  45. int first;
  46. long basecat, covercat, catb, catc;
  47. double value, max;
  48. G_gisinit(argv[0]);
  49. module = G_define_module();
  50. G_add_keyword(_("raster"));
  51. G_add_keyword(_("algebra"));
  52. G_add_keyword(_("statistics"));
  53. module->description =
  54. _("Finds the mode of values in a cover map within "
  55. "areas assigned the same category value in a "
  56. "user-specified base map.");
  57. parm.base = G_define_option();
  58. parm.base->key = "base";
  59. parm.base->description = _("Base map to be reclassified");
  60. parm.base->required = YES;
  61. parm.base->type = TYPE_STRING;
  62. parm.base->gisprompt = "old,cell,raster";
  63. parm.cover = G_define_option();
  64. parm.cover->key = "cover";
  65. parm.cover->description = _("Coverage map");
  66. parm.cover->required = YES;
  67. parm.cover->type = TYPE_STRING;
  68. parm.cover->gisprompt = "old,cell,raster";
  69. parm.output = G_define_option();
  70. parm.output->key = "output";
  71. parm.output->description = _("Output map");
  72. parm.output->required = YES;
  73. parm.output->type = TYPE_STRING;
  74. parm.output->gisprompt = "new,cell,raster";
  75. if (G_parser(argc, argv))
  76. exit(1);
  77. basemap = parm.base->answer;
  78. covermap = parm.cover->answer;
  79. outmap = parm.output->answer;
  80. if (Rast_read_cats(covermap, "", &cover_cats) < 0) {
  81. G_fatal_error(_("%s: Unable to read category labels"), covermap);
  82. }
  83. sprintf(input, "input=%s,%s", basemap, covermap);
  84. args[0] = "r.stats";
  85. args[1] = "-an";
  86. args[2] = input;
  87. args[3] = NULL;
  88. stats = G_popen_read(&stats_child, "r.stats", args);
  89. sprintf(input, "input=%s", basemap);
  90. sprintf(output, "output=%s", outmap);
  91. args[0] = "r.reclass";
  92. args[1] = input;
  93. args[2] = output;
  94. args[3] = NULL;
  95. reclass = G_popen_write(&reclass_child, "r.reclass", args);
  96. first = 1;
  97. while (read_stats(stats, &basecat, &covercat, &value)) {
  98. if (first) {
  99. first = 0;
  100. catb = basecat;
  101. catc = covercat;
  102. max = value;
  103. }
  104. if (basecat != catb) {
  105. write_reclass(reclass, catb, catc, Rast_get_c_cat((CELL *) &catc, &cover_cats));
  106. catb = basecat;
  107. catc = covercat;
  108. max = value;
  109. }
  110. if (value > max) {
  111. catc = covercat;
  112. max = value;
  113. }
  114. }
  115. if (first) {
  116. catb = catc = 0;
  117. }
  118. write_reclass(reclass, catb, catc, Rast_get_c_cat((CELL *) &catc, &cover_cats));
  119. G_popen_close(&reclass_child);
  120. G_popen_close(&stats_child);
  121. return 0;
  122. }