main.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. G_add_keyword(_("zonal statistics"));
  51. module->description =
  52. _("Calculates category or object oriented statistics.");
  53. basemap = G_define_standard_option(G_OPT_R_BASE);
  54. covermap = G_define_standard_option(G_OPT_R_COVER);
  55. method = G_define_option();
  56. method->key = "method";
  57. method->type = TYPE_STRING;
  58. method->required = YES;
  59. method->description = _("Method of object-based statistic");
  60. for (o_method = 0; menu[o_method].name; o_method++) {
  61. if (o_method)
  62. strcat(methods, ",");
  63. else
  64. *(methods) = 0;
  65. strcat(methods, menu[o_method].name);
  66. }
  67. method->options = G_store(methods);
  68. for (o_method = 0; menu[o_method].name; o_method++) {
  69. if (o_method)
  70. strcat(methods, ";");
  71. else
  72. *(methods) = 0;
  73. strcat(methods, menu[o_method].name);
  74. strcat(methods, ";");
  75. strcat(methods, menu[o_method].text);
  76. }
  77. method->descriptions = G_store(methods);
  78. outputmap = G_define_standard_option(G_OPT_R_OUTPUT);
  79. outputmap->description = _("Resultant raster map");
  80. outputmap->required = YES;
  81. flag_c = G_define_flag();
  82. flag_c->key = 'c';
  83. flag_c->description =
  84. _("Cover values extracted from the category labels of the cover map");
  85. if (G_parser(argc, argv))
  86. exit(EXIT_FAILURE);
  87. if (Rast_map_is_fp(basemap->answer, "") != 0)
  88. G_fatal_error(_("This module currently only works for integer (CELL) maps"));
  89. if (Rast_map_is_fp(covermap->answer, "") != 0)
  90. G_fatal_error(_("This module currently only works for integer (CELL) maps"));
  91. if (Rast_read_cats(covermap->answer, "", &cats) < 0)
  92. G_fatal_error(_("Unable to read category file of raster map <%s>"),
  93. covermap->answer);
  94. for (o_method = 0; menu[o_method].name; o_method++)
  95. if (strcmp(menu[o_method].name, method->answer) == 0)
  96. break;
  97. if (!menu[o_method].name) {
  98. G_warning(_("<%s=%s> unknown %s"), method->key, method->answer,
  99. method->key);
  100. G_usage();
  101. exit(EXIT_FAILURE);
  102. }
  103. (*menu[o_method].func)(basemap->answer, covermap->answer,
  104. outputmap->answer,
  105. flag_c->answer, &cats);
  106. return 0;
  107. }