main.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /****************************************************************************
  2. *
  3. * MODULE: i.cluster
  4. * AUTHOR(S): Michael Shapiro (USACERL) and Tao Wen (UIUC)
  5. * (original contributors)
  6. * Markus Neteler <neteler itc.it>,
  7. * Roberto Flor <flor itc.it>,
  8. * Bernhard Reiter <bernhard intevation.de>,
  9. * Brad Douglas <rez touchofmadness.com>,
  10. * Glynn Clements <glynn gclements.plus.com>,
  11. * Jan-Oliver Wagner <jan intevation.de>
  12. * PURPOSE: builds pixel clusters based on multi-image pixel values
  13. * COPYRIGHT: (C) 1999-2008 by the GRASS Development Team
  14. *
  15. * This program is free software under the GNU General Public
  16. * License (>=v2). Read the file COPYING that comes with GRASS
  17. * for details.
  18. *
  19. *****************************************************************************/
  20. #include <stdlib.h>
  21. #include <time.h>
  22. #include <grass/gis.h>
  23. #include <grass/raster.h>
  24. #include <grass/glocale.h>
  25. #define GLOBAL
  26. #include "global.h"
  27. #include "local_proto.h"
  28. struct Cluster C;
  29. struct Signature in_sig;
  30. int maxclass;
  31. double conv;
  32. double sep;
  33. int iters;
  34. int mcs;
  35. char *group;
  36. char *subgroup;
  37. struct Ref ref;
  38. char *outsigfile;
  39. char *insigfile;
  40. char *reportfile;
  41. DCELL **cell;
  42. int *cellfd;
  43. FILE *report;
  44. int sample_rows, sample_cols;
  45. time_t start_time;
  46. static int interrupted = 0;
  47. int main(int argc, char *argv[])
  48. {
  49. int count;
  50. int n;
  51. int row, nrows;
  52. int col, ncols;
  53. DCELL *x;
  54. struct Cell_head window;
  55. FILE *fd;
  56. struct GModule *module;
  57. struct
  58. {
  59. struct Option *group_name, *subgroup_name, *out_sig, *seed_sig,
  60. *class, *sample_interval, *iterations, *separation,
  61. *convergence, *min_size, *report_file;
  62. } parm;
  63. G_gisinit(argv[0]);
  64. module = G_define_module();
  65. G_add_keyword(_("imagery"));
  66. G_add_keyword(_("classification"));
  67. G_add_keyword(_("signatures"));
  68. module->label =
  69. _("Generates spectral signatures for land cover "
  70. "types in an image using a clustering algorithm.");
  71. module->description =
  72. _("The resulting signature file is used as input for i.maxlik, "
  73. "to generate an unsupervised image classification.");
  74. parm.group_name = G_define_standard_option(G_OPT_I_GROUP);
  75. parm.subgroup_name = G_define_standard_option(G_OPT_I_SUBGROUP);
  76. parm.out_sig = G_define_option();
  77. parm.out_sig->key = "signaturefile";
  78. parm.out_sig->type = TYPE_STRING;
  79. parm.out_sig->key_desc = "name";
  80. parm.out_sig->required = YES;
  81. parm.out_sig->description = _("Name for output file containing result signatures");
  82. parm.class = G_define_option();
  83. parm.class->key = "classes";
  84. parm.class->type = TYPE_INTEGER;
  85. parm.class->options = "1-255";
  86. parm.class->required = YES;
  87. parm.class->description = _("Initial number of classes");
  88. parm.class->guisection = _("Settings");
  89. parm.seed_sig = G_define_option();
  90. parm.seed_sig->key = "seed";
  91. parm.seed_sig->required = NO;
  92. parm.seed_sig->type = TYPE_STRING;
  93. parm.seed_sig->key_desc = "name";
  94. parm.seed_sig->description = _("Name of file containing initial signatures");
  95. parm.sample_interval = G_define_option();
  96. parm.sample_interval->key = "sample";
  97. parm.sample_interval->key_desc = "row_interval,col_interval";
  98. parm.sample_interval->type = TYPE_INTEGER;
  99. parm.sample_interval->required = NO;
  100. parm.sample_interval->description =
  101. _("Sampling intervals (by row and col); default: ~10,000 pixels");
  102. parm.sample_interval->guisection = _("Settings");
  103. parm.iterations = G_define_option();
  104. parm.iterations->key = "iterations";
  105. parm.iterations->type = TYPE_INTEGER;
  106. parm.iterations->required = NO;
  107. parm.iterations->description = _("Maximum number of iterations");
  108. parm.iterations->answer = "30";
  109. parm.iterations->guisection = _("Settings");
  110. parm.convergence = G_define_option();
  111. parm.convergence->key = "convergence";
  112. parm.convergence->type = TYPE_DOUBLE;
  113. parm.convergence->required = NO;
  114. parm.convergence->options = "0-100";
  115. parm.convergence->description = _("Percent convergence");
  116. parm.convergence->answer = "98.0";
  117. parm.convergence->guisection = _("Settings");
  118. parm.separation = G_define_option();
  119. parm.separation->key = "separation";
  120. parm.separation->type = TYPE_DOUBLE;
  121. parm.separation->required = NO;
  122. parm.separation->description = _("Cluster separation");
  123. parm.separation->answer = "0.0";
  124. parm.separation->guisection = _("Settings");
  125. parm.min_size = G_define_option();
  126. parm.min_size->key = "min_size";
  127. parm.min_size->type = TYPE_INTEGER;
  128. parm.min_size->required = NO;
  129. parm.min_size->description = _("Minimum number of pixels in a class");
  130. parm.min_size->answer = "17";
  131. parm.min_size->guisection = _("Settings");
  132. parm.report_file = G_define_standard_option(G_OPT_F_OUTPUT);
  133. parm.report_file->key = "reportfile";
  134. parm.report_file->required = NO;
  135. parm.report_file->description = _("Name for output file containing final report");
  136. if (G_parser(argc, argv))
  137. exit(EXIT_FAILURE);
  138. G_get_window(&window);
  139. nrows = Rast_window_rows();
  140. ncols = Rast_window_cols();
  141. I_cluster_clear(&C);
  142. group = parm.group_name->answer; /* a required parameter */
  143. subgroup = parm.subgroup_name->answer; /* required */
  144. outsigfile = parm.out_sig->answer;
  145. if (sscanf(parm.class->answer, "%d", &maxclass) != 1 || maxclass < 1
  146. || maxclass > 255) {
  147. G_fatal_error(_("Illegal number of initial classes (%s)"),
  148. parm.class->answer);
  149. }
  150. insigfile = parm.seed_sig->answer;
  151. if (parm.sample_interval->answer) {
  152. if (sscanf
  153. (parm.sample_interval->answer, "%d,%d", &sample_rows,
  154. &sample_cols) != 2 || sample_rows < 1 || sample_cols < 1 ||
  155. sample_rows > nrows || sample_cols > ncols) {
  156. G_fatal_error(_("Illegal value(s) of sample intervals (%s)"),
  157. parm.sample_interval->answer);
  158. }
  159. }
  160. else {
  161. sample_rows = nrows / 100;
  162. if (sample_rows < 1)
  163. sample_rows = 1;
  164. sample_cols = ncols / 100;
  165. if (sample_cols < 1)
  166. sample_cols = 1;
  167. }
  168. if (sscanf(parm.iterations->answer, "%d", &iters) != 1 || iters < 1) {
  169. G_fatal_error(_("Illegal value of iterations (%s)"),
  170. parm.iterations->answer);
  171. }
  172. if (sscanf(parm.convergence->answer, "%lf", &conv) != 1 || conv < 0.0 ||
  173. conv > 100.0) {
  174. G_fatal_error(_("Illegal value of convergence (%s)"),
  175. parm.convergence->answer);
  176. }
  177. if (sscanf(parm.separation->answer, "%lf", &sep) != 1 || sep < 0.0) {
  178. G_fatal_error(_("Illegal value of separation (%s)"),
  179. parm.separation->answer);
  180. }
  181. if (sscanf(parm.min_size->answer, "%d", &mcs) != 1 || mcs < 2) {
  182. G_fatal_error(_("Illegal value of min_size (%s)"),
  183. parm.min_size->answer);
  184. }
  185. if ((reportfile = parm.report_file->answer) == NULL)
  186. report = fopen(G_DEV_NULL, "w");
  187. else
  188. report = fopen(reportfile, "w");
  189. if (report == NULL) {
  190. G_fatal_error(_("Unable to create report file <%s>"),
  191. reportfile);
  192. }
  193. open_files();
  194. fprintf(report,
  195. _("#################### CLUSTER (%s) ####################\n\n"),
  196. G_date());
  197. fprintf(report, _("Location: %s\n"), G_location());
  198. fprintf(report, _("Mapset: %s\n"), G_mapset());
  199. fprintf(report, _("Group: %s\n"), group);
  200. fprintf(report, _("Subgroup: %s\n"), subgroup);
  201. for (n = 0; n < ref.nfiles; n++) {
  202. fprintf(report, _(" %s\n"),
  203. G_fully_qualified_name(ref.file[n].name, ref.file[n].mapset));
  204. }
  205. fprintf(report, _("Result signature file: %s\n"), outsigfile);
  206. fprintf(report, "\n");
  207. fprintf(report, _("Region\n"));
  208. fprintf(report, _(" North: %12.2f East: %12.2f\n"), window.north,
  209. window.east);
  210. fprintf(report, _(" South: %12.2f West: %12.2f\n"), window.south,
  211. window.west);
  212. fprintf(report, _(" Res: %12.2f Res: %12.2f\n"), window.ns_res,
  213. window.ew_res);
  214. fprintf(report, _(" Rows: %12d Cols: %12d Cells: %d\n"), nrows, ncols,
  215. nrows * ncols);
  216. fprintf(report, _("Mask: %s\n"), Rast_mask_info());
  217. fprintf(report, "\n");
  218. fprintf(report, _("Cluster parameters\n"));
  219. fprintf(report, _(" Number of initial classes: %d"), maxclass);
  220. if (insigfile)
  221. fprintf(report, _(" [from signature file %s]"), insigfile);
  222. fprintf(report, "\n");
  223. fprintf(report, _(" Minimum class size: %d\n"), mcs);
  224. fprintf(report, _(" Minimum class separation: %f\n"), sep);
  225. fprintf(report, _(" Percent convergence: %f\n"), conv);
  226. fprintf(report, _(" Maximum number of iterations: %d\n"), iters);
  227. fprintf(report, "\n");
  228. fprintf(report, _(" Row sampling interval: %d\n"), sample_rows);
  229. fprintf(report, _(" Col sampling interval: %d\n"), sample_cols);
  230. fprintf(report, "\n");
  231. fflush(report);
  232. x = (DCELL *) G_malloc(ref.nfiles * sizeof(DCELL));
  233. I_cluster_begin(&C, ref.nfiles);
  234. count = 0;
  235. G_message(_("Reading raster maps..."));
  236. for (row = sample_rows - 1; row < nrows; row += sample_rows) {
  237. G_percent(row, nrows, 2);
  238. for (n = 0; n < ref.nfiles; n++)
  239. Rast_get_d_row(cellfd[n], cell[n], row);
  240. for (col = sample_cols - 1; col < ncols; col += sample_cols) {
  241. count++;
  242. for (n = 0; n < ref.nfiles; n++)
  243. x[n] = cell[n][col];
  244. if (I_cluster_point(&C, x) < 0)
  245. G_fatal_error(_("Out of Memory. Please run again and choose a "
  246. "smaller sample size."));
  247. }
  248. }
  249. G_percent(nrows, nrows, 2);
  250. fprintf(report, _("Sample size: %d points\n"), C.npoints);
  251. fprintf(report, "\n");
  252. if (count < 2)
  253. G_fatal_error(_("Not enough sample points. Please run again and "
  254. "choose a larger sample size."));
  255. if (C.npoints < 2)
  256. G_fatal_error(_("Not enough non-zero sample data points. Check "
  257. "your current region (and mask)."));
  258. for (n = 0; n < ref.nfiles; n++) {
  259. G_free(cell[n]);
  260. Rast_close(cellfd[n]);
  261. }
  262. G_free(x);
  263. start_time = time(NULL);
  264. I_cluster_exec(&C, maxclass, iters, conv, sep, mcs, checkpoint,
  265. &interrupted);
  266. fprintf(report, _("\n########## final results #############\n"));
  267. fprintf(report, _("%d classes (convergence=%.1f%%)\n"),
  268. I_cluster_nclasses(&C, mcs), (double)C.percent_stable);
  269. print_separability(report, &C);
  270. print_class_means(report, &C);
  271. if ((fd =
  272. I_fopen_signature_file_new(group, subgroup, outsigfile)) != NULL) {
  273. I_write_signatures(fd, &C.S);
  274. fclose(fd);
  275. }
  276. else {
  277. G_fatal_error(_("Unable to create signature file <%s> for group "
  278. "<%s>, subsgroup <%s>"), outsigfile, group, subgroup);
  279. }
  280. fprintf(report,
  281. _("\n\n#################### CLASSES ####################\n"));
  282. fprintf(report, _("\n%d classes, %.2f%% points stable\n"),
  283. I_cluster_nclasses(&C, 1), (double)C.percent_stable);
  284. fprintf(report, _("\n######## CLUSTER END (%s) ########\n"), G_date());
  285. fclose(report);
  286. G_done_msg(_("File <%s> created."),
  287. outsigfile);
  288. exit(EXIT_SUCCESS);
  289. }