main.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /****************************************************************************
  2. *
  3. * MODULE: r.kappa
  4. * AUTHOR(S): Tao Wen, UIUC (original contributor)
  5. * Markus Neteler <neteler itc.it>,
  6. * Roberto Flor <flor itc.it>,
  7. * Bernhard Reiter <bernhard intevation.de>,
  8. * Brad Douglas <rez touchofmadness.com>,
  9. * Glynn Clements <glynn gclements.plus.com>,
  10. * Jachym Cepicky <jachym les-ejk.cz>,
  11. * Jan-Oliver Wagner <jan intevation.de>
  12. * PURPOSE: tabulates the error matrix of classification result by
  13. * crossing classified map layer with respect to reference map
  14. * layer
  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 <stdio.h>
  24. #include <string.h>
  25. #include <grass/gis.h>
  26. #include <grass/raster.h>
  27. #include <grass/glocale.h>
  28. #include "local_proto.h"
  29. #include "kappa.h"
  30. struct Cell_head window;
  31. const char *maps[2];
  32. const char *output;
  33. const char *title;
  34. long *matr;
  35. long *rlst;
  36. int ncat;
  37. const char *stats_file;
  38. LAYER *layers;
  39. int nlayers;
  40. GSTATS *Gstats;
  41. size_t nstats;
  42. /* function prototypes */
  43. static void layer(const char *s);
  44. int main(int argc, char **argv)
  45. {
  46. int i;
  47. struct GModule *module;
  48. struct
  49. {
  50. struct Option *map, *ref, *output, *titles;
  51. } parms;
  52. struct
  53. {
  54. struct Flag *n, *w, *h;
  55. } flags;
  56. G_gisinit(argv[0]);
  57. module = G_define_module();
  58. G_add_keyword(_("raster"));
  59. G_add_keyword(_("statistics"));
  60. G_add_keyword(_("classification"));
  61. module->description =
  62. _("Calculates error matrix and kappa "
  63. "parameter for accuracy assessment of classification result.");
  64. parms.map = G_define_standard_option(G_OPT_R_INPUT);
  65. parms.map->key = "classification";
  66. parms.map->description =
  67. _("Name of raster map containing classification result");
  68. parms.ref = G_define_standard_option(G_OPT_R_INPUT);
  69. parms.ref->key = "reference";
  70. parms.ref->description =
  71. _("Name of raster map containing reference classes");
  72. parms.output = G_define_standard_option(G_OPT_F_OUTPUT);
  73. parms.output->required = NO;
  74. parms.output->label =
  75. _("Name for output file containing error matrix and kappa");
  76. parms.output->description = _("If not given write to standard output");
  77. parms.output->guisection = _("Output settings");
  78. parms.titles = G_define_option();
  79. parms.titles->key = "title";
  80. parms.titles->type = TYPE_STRING;
  81. parms.titles->required = NO;
  82. parms.titles->description = _("Title for error matrix and kappa");
  83. parms.titles->answer = "ACCURACY ASSESSMENT";
  84. parms.titles->guisection = _("Output settings");
  85. flags.w = G_define_flag();
  86. flags.w->key = 'w';
  87. flags.w->label = _("Wide report");
  88. flags.w->description = _("132 columns (default: 80)");
  89. flags.w->guisection = _("Formatting");
  90. flags.h = G_define_flag();
  91. flags.h->key = 'h';
  92. flags.h->description = _("No header in the report");
  93. flags.h->guisection = _("Formatting");
  94. if (G_parser(argc, argv))
  95. exit(EXIT_FAILURE);
  96. G_get_window(&window);
  97. maps[0] = parms.ref->answer;
  98. maps[1] = parms.map->answer;
  99. for (i = 0; i < 2; i++)
  100. layer(maps[i]);
  101. output = parms.output->answer;
  102. title = parms.titles->answer;
  103. /* run r.stats to obtain statistics of map layers */
  104. stats();
  105. /* print header of the output */
  106. if (!flags.h->answer)
  107. prn_header();
  108. /* prepare the data for calculation */
  109. prn_error_mat(flags.w->answer ? 132 : 80, flags.h->answer);
  110. /* generate the error matrix, kappa and variance */
  111. calc_kappa();
  112. return EXIT_SUCCESS;
  113. }
  114. static void layer(const char *s)
  115. {
  116. char name[GNAME_MAX];
  117. const char *mapset;
  118. int n;
  119. strcpy(name, s);
  120. if ((mapset = G_find_raster2(name, "")) == NULL)
  121. G_fatal_error(_("Raster map <%s> not found"), s);
  122. n = nlayers++;
  123. layers = (LAYER *) G_realloc(layers, 2 * sizeof(LAYER));
  124. layers[n].name = G_store(name);
  125. layers[n].mapset = mapset;
  126. Rast_read_cats(name, mapset, &layers[n].labels);
  127. }