main.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /****************************************************************************
  2. *
  3. * MODULE: r.patch
  4. * AUTHOR(S): Michael Shapiro, CERL (original contributor)
  5. * Hamish Bowman <hamish_b yahoo.com>, 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 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 <unistd.h>
  18. #include <grass/gis.h>
  19. #include <grass/raster.h>
  20. #include <grass/glocale.h>
  21. #include "local_proto.h"
  22. int main(int argc, char *argv[])
  23. {
  24. int *infd;
  25. struct Categories cats;
  26. struct Cell_stats *statf;
  27. struct Colors colr;
  28. int cats_ok;
  29. int colr_ok;
  30. int outfd;
  31. RASTER_MAP_TYPE out_type, map_type;
  32. struct History history;
  33. void *presult, *patch;
  34. int nfiles;
  35. char *rname;
  36. int i;
  37. int row, nrows, ncols;
  38. int ZEROFLAG;
  39. char *new_name;
  40. char **names;
  41. char **ptr;
  42. struct GModule *module;
  43. struct Flag *zeroflag;
  44. struct Option *opt1, *opt2;
  45. G_gisinit(argv[0]);
  46. module = G_define_module();
  47. G_add_keyword(_("raster"));
  48. G_add_keyword(_("geometry"));
  49. module->description =
  50. _("Creates a composite raster map layer by using "
  51. "known category values from one (or more) map layer(s) "
  52. "to fill in areas of \"no data\" in another map layer.");
  53. /* Define the different options */
  54. opt1 = G_define_standard_option(G_OPT_R_INPUTS);
  55. opt1->description = _("Name of raster maps to be patched together");
  56. opt2 = G_define_standard_option(G_OPT_R_OUTPUT);
  57. opt2->description = _("Name for resultant raster map");
  58. /* Define the different flags */
  59. zeroflag = G_define_flag();
  60. zeroflag->key = 'z';
  61. zeroflag->description =
  62. _("Use zero (0) for transparency instead of NULL");
  63. ZEROFLAG = 0; /* default: use NULL for transparency */
  64. if (G_parser(argc, argv))
  65. exit(EXIT_FAILURE);
  66. ZEROFLAG = (zeroflag->answer);
  67. names = opt1->answers;
  68. out_type = CELL_TYPE;
  69. for (ptr = names, nfiles = 0; *ptr != NULL; ptr++, nfiles++) ;
  70. if (nfiles < 2)
  71. G_fatal_error(_("The minimum number of input raster maps is two"));
  72. infd = G_malloc(nfiles * sizeof(int));
  73. statf = G_malloc(nfiles * sizeof(struct Cell_stats));
  74. for (i = 0; i < nfiles; i++) {
  75. const char *name = names[i];
  76. int fd;
  77. fd = Rast_open_old(name, "");
  78. infd[i] = fd;
  79. map_type = Rast_get_map_type(fd);
  80. if (map_type == FCELL_TYPE && out_type == CELL_TYPE)
  81. out_type = FCELL_TYPE;
  82. else if (map_type == DCELL_TYPE)
  83. out_type = DCELL_TYPE;
  84. Rast_init_cell_stats(&statf[i]);
  85. }
  86. rname = opt2->answer;
  87. outfd = Rast_open_new(new_name = rname, out_type);
  88. presult = Rast_allocate_buf(out_type);
  89. patch = Rast_allocate_buf(out_type);
  90. nrows = Rast_window_rows();
  91. ncols = Rast_window_cols();
  92. G_verbose_message(_("Percent complete..."));
  93. for (row = 0; row < nrows; row++) {
  94. G_percent(row, nrows, 2);
  95. Rast_get_row(infd[0], presult, row, out_type);
  96. if (out_type == CELL_TYPE)
  97. Rast_update_cell_stats((CELL *) presult, ncols, &statf[0]);
  98. for (i = 1; i < nfiles; i++) {
  99. Rast_get_row(infd[i], patch, row, out_type);
  100. if (!do_patch
  101. (presult, patch, &statf[i], ncols, out_type, ZEROFLAG))
  102. break;
  103. }
  104. Rast_put_row(outfd, presult, out_type);
  105. }
  106. G_percent(row, nrows, 2);
  107. G_free(patch);
  108. G_free(presult);
  109. for (i = 0; i < nfiles; i++)
  110. Rast_close(infd[i]);
  111. /*
  112. * build the new cats and colors. do this before closing the new
  113. * file, in case the new file is one of the patching files as well.
  114. */
  115. G_message(_("Creating support files for raster map <%s>"), new_name);
  116. support(names, statf, nfiles, &cats, &cats_ok, &colr, &colr_ok, out_type);
  117. /* now close (and create) the result */
  118. Rast_close(outfd);
  119. if (cats_ok)
  120. Rast_write_cats(new_name, &cats);
  121. if (colr_ok)
  122. Rast_write_colors(new_name, G_mapset(), &colr);
  123. Rast_short_history(new_name, "raster", &history);
  124. Rast_command_history(&history);
  125. Rast_write_history(new_name, &history);
  126. exit(EXIT_SUCCESS);
  127. }