main.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /****************************************************************************
  2. *
  3. * MODULE: r.clump
  4. *
  5. * AUTHOR(S): Michael Shapiro - CERL
  6. * Markus Metz
  7. *
  8. * PURPOSE: Recategorizes data in a raster map layer by grouping cells
  9. * that form physically discrete areas into unique categories.
  10. *
  11. * COPYRIGHT: (C) 2006-2016 by the GRASS Development Team
  12. *
  13. * This program is free software under the GNU General Public
  14. * License (>=v2). Read the file COPYING that comes with GRASS
  15. * for details.
  16. *
  17. ***************************************************************************/
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <sys/types.h>
  21. #include <inttypes.h>
  22. #include <grass/gis.h>
  23. #include <grass/raster.h>
  24. #include <grass/glocale.h>
  25. #include "local_proto.h"
  26. int main(int argc, char *argv[])
  27. {
  28. struct Colors colr;
  29. struct Range range;
  30. struct History hist;
  31. CELL min, max;
  32. int range_return, n_clumps;
  33. int *in_fd, out_fd;
  34. int i, n;
  35. double threshold;
  36. int minsize;
  37. char title[512];
  38. char name[GNAME_MAX];
  39. char *OUTPUT;
  40. char *INPUT;
  41. struct GModule *module;
  42. struct Option *opt_in;
  43. struct Option *opt_out;
  44. struct Option *opt_thresh;
  45. struct Option *opt_minsize;
  46. struct Option *opt_title;
  47. struct Flag *flag_diag;
  48. struct Flag *flag_print;
  49. G_gisinit(argv[0]);
  50. /* Define the different options */
  51. module = G_define_module();
  52. G_add_keyword(_("raster"));
  53. G_add_keyword(_("statistics"));
  54. G_add_keyword(_("reclass"));
  55. G_add_keyword(_("clumps"));
  56. module->description =
  57. _("Recategorizes data in a raster map by grouping cells "
  58. "that form physically discrete areas into unique categories.");
  59. opt_in = G_define_standard_option(G_OPT_R_INPUTS);
  60. opt_out = G_define_standard_option(G_OPT_R_OUTPUT);
  61. opt_out->required = NO;
  62. opt_title = G_define_option();
  63. opt_title->key = "title";
  64. opt_title->type = TYPE_STRING;
  65. opt_title->required = NO;
  66. opt_title->description = _("Title for output raster map");
  67. opt_thresh = G_define_option();
  68. opt_thresh->key = "threshold";
  69. opt_thresh->type = TYPE_DOUBLE;
  70. opt_thresh->required = NO;
  71. opt_thresh->answer = "0";
  72. opt_thresh->label = _("Threshold to identify similar cells");
  73. opt_thresh->description = _("Valid range: 0 = identical to < 1 = maximal difference");
  74. opt_minsize = G_define_option();
  75. opt_minsize->key = "minsize";
  76. opt_minsize->type = TYPE_INTEGER;
  77. opt_minsize->required = NO;
  78. opt_minsize->answer = "1";
  79. opt_minsize->label = _("Minimum clump size in cells");
  80. opt_minsize->description = _("Clumps smaller than minsize will be merged to form larger clumps");
  81. flag_diag = G_define_flag();
  82. flag_diag->key = 'd';
  83. flag_diag->label = _("Clump also diagonal cells");
  84. flag_diag->description = _("Clumps are also traced along diagonal neighboring cells");
  85. flag_print = G_define_flag();
  86. flag_print->key = 'g';
  87. flag_print->label = _("Print only the number of clumps in shell script style");
  88. G_option_exclusive(flag_print, opt_out, NULL);
  89. G_option_required(flag_print, opt_out, NULL);
  90. /* parse options */
  91. if (G_parser(argc, argv))
  92. exit(EXIT_FAILURE);
  93. #if defined(int64_t)
  94. G_message("have int64_t");
  95. #endif
  96. #if defined(_int64_t)
  97. G_message("have _int64_t");
  98. #endif
  99. #if defined(__int64_t)
  100. G_message("have __int64_t");
  101. #endif
  102. threshold = atof(opt_thresh->answer);
  103. if (threshold < 0 || threshold >= 1)
  104. G_fatal_error(_("Valid range for option <%s> is 0 <= value < 1"),
  105. opt_thresh->key);
  106. minsize = atoi(opt_minsize->answer);
  107. n = 0;
  108. while (opt_in->answers[n])
  109. n++;
  110. in_fd = G_malloc(sizeof(int) * n);
  111. for (i = 0; i < n; i++)
  112. in_fd[i] = Rast_open_old(opt_in->answers[i], "");
  113. INPUT = opt_in->answers[0];
  114. strcpy(name, INPUT);
  115. OUTPUT = NULL;
  116. out_fd = -1;
  117. if (!flag_print->answer) {
  118. OUTPUT = opt_out->answer;
  119. out_fd = Rast_open_c_new(OUTPUT);
  120. }
  121. if (n == 1 && threshold == 0)
  122. clump(in_fd, out_fd, flag_diag->answer, minsize);
  123. else
  124. clump_n(in_fd, opt_in->answers, n, threshold, out_fd,
  125. flag_diag->answer, minsize);
  126. for (i = 0; i < n; i++)
  127. Rast_close(in_fd[i]);
  128. if (!flag_print->answer) {
  129. Rast_close(out_fd);
  130. G_debug(1, "Creating support files...");
  131. /* build title */
  132. if (opt_title->answer != NULL)
  133. strcpy(title, opt_title->answer);
  134. else
  135. sprintf(title, "clump of <%s@%s>", name, G_mapset());
  136. Rast_put_cell_title(OUTPUT, title);
  137. /* colors */
  138. range_return = Rast_read_range(OUTPUT, G_mapset(), &range);
  139. Rast_get_range_min_max(&range, &min, &max);
  140. Rast_make_random_colors(&colr, min, max);
  141. Rast_write_colors(OUTPUT, G_mapset(), &colr);
  142. /* history */
  143. Rast_short_history(OUTPUT, "raster", &hist);
  144. Rast_set_history(&hist, HIST_DATSRC_1, INPUT);
  145. Rast_command_history(&hist);
  146. Rast_write_history(OUTPUT, &hist);
  147. n_clumps = range_return == 2 ? 0 : range.max;
  148. G_done_msg(n_("%d clump.", "%d clumps.", n_clumps), n_clumps);
  149. }
  150. exit(EXIT_SUCCESS);
  151. }