main.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /****************************************************************************
  2. *
  3. * MODULE: r.mfilter
  4. * AUTHOR(S): Michael Shapiro, CERL (original contributor)
  5. * Roberto Flor <flor itc.it>, Markus Neteler <neteler itc.it>
  6. * Glynn Clements <glynn gclements.plus.com>, Jachym Cepicky <jachym les-ejk.cz>,
  7. * Jan-Oliver Wagner <jan intevation.de>
  8. * PURPOSE:
  9. * COPYRIGHT: (C) 1999-2006, 2010 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 <stdlib.h>
  17. #include <string.h>
  18. #include <stdio.h>
  19. #include <grass/gis.h>
  20. #include <grass/raster.h>
  21. #include <grass/glocale.h>
  22. #include "filter.h"
  23. #include "glob.h"
  24. int nrows, ncols;
  25. int buflen;
  26. int direction;
  27. int null_only;
  28. int preserve_edges;
  29. int main(int argc, char **argv)
  30. {
  31. FILTER *filter;
  32. int nfilters;
  33. int repeat;
  34. char *in_name;
  35. char *filt_name;
  36. char *out_name;
  37. char title[1024];
  38. char temp[300];
  39. int i;
  40. struct GModule *module;
  41. struct Flag *flag2;
  42. struct Option *opt1;
  43. struct Option *opt2;
  44. struct Option *opt3;
  45. struct Option *opt4;
  46. struct Option *opt5;
  47. G_gisinit(argv[0]);
  48. module = G_define_module();
  49. G_add_keyword(_("raster"));
  50. G_add_keyword(_("algebra"));
  51. G_add_keyword(_("statistics"));
  52. module->description = _("Performs raster map matrix filter.");
  53. /* Define the different options */
  54. opt1 = G_define_standard_option(G_OPT_R_INPUT);
  55. opt2 = G_define_standard_option(G_OPT_R_OUTPUT);
  56. opt3 = G_define_standard_option(G_OPT_F_INPUT);
  57. opt3->key = "filter";
  58. opt3->required = YES;
  59. opt3->description = _("Path to filter file");
  60. opt4 = G_define_option();
  61. opt4->key = "repeat";
  62. opt4->type = TYPE_INTEGER;
  63. opt4->multiple = NO;
  64. opt4->required = NO;
  65. opt4->answer = "1";
  66. opt4->description = _("Number of times to repeat the filter");
  67. opt4->guisection = _("Filter");
  68. opt5 = G_define_option();
  69. opt5->key = "title";
  70. opt5->type = TYPE_STRING;
  71. opt5->required = NO;
  72. opt5->description = _("Output raster map title");
  73. /* Define the different flags */
  74. /* this isn't implemented at all
  75. flag3 = G_define_flag() ;
  76. flag3->key = 'p' ;
  77. flag3->description = _("Preserved edge") ;
  78. */
  79. flag2 = G_define_flag();
  80. flag2->key = 'z';
  81. flag2->description = _("Apply filter only to null data values");
  82. flag2->guisection = _("Filter");
  83. if (G_parser(argc, argv))
  84. exit(EXIT_FAILURE);
  85. /*
  86. preserve_edges = flag3->answer;
  87. */
  88. null_only = flag2->answer;
  89. sscanf(opt4->answer, "%d", &repeat);
  90. out_name = opt2->answer;
  91. filt_name = opt3->answer;
  92. in_name = opt1->answer;
  93. nrows = Rast_window_rows();
  94. ncols = Rast_window_cols();
  95. buflen = ncols * sizeof(DCELL);
  96. /* get the filter */
  97. filter = get_filter(filt_name, &nfilters, temp);
  98. /* make sure filter matrix won't extend outside the raster map */
  99. for (i = 0; i < nfilters; i++) {
  100. if (filter[i].size > ncols || filter[i].size > nrows)
  101. G_fatal_error(_("Raster map too small for the size of the filter"));
  102. }
  103. /* make a title for result */
  104. if (opt5->answer)
  105. strcpy(title, opt5->answer);
  106. else {
  107. if (*temp == 0)
  108. strcpy(temp, "unknown filter");
  109. sprintf(title, "%s filtered using %s", in_name, temp);
  110. }
  111. perform_filter(in_name, out_name, filter, nfilters, repeat);
  112. Rast_put_cell_title(out_name, title);
  113. exit(EXIT_SUCCESS);
  114. }