main.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /****************************************************************************
  2. *
  3. * MODULE: r.statistics
  4. *
  5. * AUTHOR(S): Martin Schroeder, Geographisches Institut Heidelberg, Germany
  6. *
  7. * PURPOSE: Category or object oriented statistics
  8. *
  9. * COPYRIGHT: (C) 2007 by the GRASS Development Team
  10. *
  11. * This program is free software under the GNU General Public
  12. * License (>=v2). Read the file COPYING that comes with GRASS
  13. * for details.
  14. *
  15. *****************************************************************************/
  16. #include <string.h>
  17. #include <stdlib.h>
  18. #include <grass/gis.h>
  19. #include <grass/raster.h>
  20. #include <grass/glocale.h>
  21. #include "method.h"
  22. /* modify this table to add new methods */
  23. struct menu menu[] = {
  24. {"diversity", o_divr, "Diversity of values in specified objects in %%"},
  25. {"average", o_average, "Average of values in specified objects"},
  26. {"mode", o_mode, "Mode of values in specified objects"},
  27. {"median", o_median, "Median of values in specified objects"},
  28. {"avedev", o_adev, "Average deviation of values in specified objects"},
  29. {"stddev", o_sdev, "Standard deviation of values in specified objects"},
  30. {"variance", o_var, "Variance of values in specified objects"},
  31. {"skewness", o_skew, "Skewnes of values in specified objects"},
  32. {"kurtosis", o_kurt, "Kurtosis of values in specified objects"},
  33. {"min", o_min, "Minimum of values in specified objects"},
  34. {"max", o_max, "Maximum of values in specified objects"},
  35. {"sum", o_sum, "Sum of values in specified objects"},
  36. {NULL, NULL, NULL}
  37. };
  38. int main(int argc, char **argv)
  39. {
  40. int o_method;
  41. struct GModule *module;
  42. struct Option *method, *basemap, *covermap, *outputmap;
  43. struct Flag *flag_c;
  44. struct Categories cats;
  45. char methods[1024];
  46. G_gisinit(argv[0]);
  47. module = G_define_module();
  48. G_add_keyword(_("raster"));
  49. G_add_keyword(_("statistics"));
  50. module->description =
  51. _("Calculates category or object oriented statistics.");
  52. basemap = G_define_standard_option(G_OPT_R_BASE);
  53. covermap = G_define_standard_option(G_OPT_R_COVER);
  54. for (o_method = 0; menu[o_method].name; o_method++) {
  55. if (o_method)
  56. strcat(methods, ",");
  57. else
  58. *(methods) = 0;
  59. strcat(methods, menu[o_method].name);
  60. }
  61. method = G_define_option();
  62. method->key = "method";
  63. method->type = TYPE_STRING;
  64. method->required = YES;
  65. method->description = _("Method of object-based statistic");
  66. method->options = methods;
  67. outputmap = G_define_standard_option(G_OPT_R_OUTPUT);
  68. outputmap->description = _("Resultant raster map");
  69. outputmap->required = YES;
  70. flag_c = G_define_flag();
  71. flag_c->key = 'c';
  72. flag_c->description =
  73. _("Cover values extracted from the category labels of the cover map");
  74. if (G_parser(argc, argv))
  75. exit(EXIT_FAILURE);
  76. if (Rast_map_is_fp(basemap->answer, "") != 0)
  77. G_fatal_error(_("This module currently only works for integer (CELL) maps"));
  78. if (Rast_map_is_fp(covermap->answer, "") != 0)
  79. G_fatal_error(_("This module currently only works for integer (CELL) maps"));
  80. if (Rast_read_cats(covermap->answer, "", &cats) < 0)
  81. G_fatal_error(_("Unable to read category file of raster map <%s>"),
  82. covermap->answer);
  83. for (o_method = 0; menu[o_method].name; o_method++)
  84. if (strcmp(menu[o_method].name, method->answer) == 0)
  85. break;
  86. if (!menu[o_method].name) {
  87. G_warning(_("<%s=%s> unknown %s"), method->key, method->answer,
  88. method->key);
  89. G_usage();
  90. exit(EXIT_FAILURE);
  91. }
  92. (*menu[o_method].func)(basemap->answer, covermap->answer,
  93. outputmap->answer,
  94. flag_c->answer, &cats);
  95. return 0;
  96. }