main.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /****************************************************************************
  2. *
  3. * MODULE: r.patch
  4. * AUTHOR(S): Michael Shapiro, CERL (original contributor),
  5. * Hamish Bowman <hamish_b yahoo.com>,
  6. * Markus Neteler <neteler itc.it>,
  7. * Glynn Clements <glynn gclements.plus.com>,
  8. * Jachym Cepicky <jachym les-ejk.cz>,
  9. * Jan-Oliver Wagner <jan intevation.de>,
  10. * Huidae Cho <grass4u gmail.com>
  11. * PURPOSE:
  12. * COPYRIGHT: (C) 1999-2014 by the GRASS Development Team
  13. *
  14. * This program is free software under the GNU General Public
  15. * License (>=v2). Read the file COPYING that comes with GRASS
  16. * for details.
  17. *
  18. *****************************************************************************/
  19. #include <stdlib.h>
  20. #include <unistd.h>
  21. #include <grass/gis.h>
  22. #include <grass/raster.h>
  23. #include <grass/glocale.h>
  24. #include "local_proto.h"
  25. int main(int argc, char *argv[])
  26. {
  27. int *infd;
  28. struct Categories cats;
  29. struct Cell_stats *statf;
  30. struct Colors colr;
  31. int cats_ok;
  32. int colr_ok;
  33. int outfd;
  34. RASTER_MAP_TYPE out_type, map_type;
  35. size_t out_cell_size;
  36. struct History history;
  37. void *presult, *patch;
  38. int nfiles;
  39. char *rname;
  40. int i;
  41. int row, nrows, ncols;
  42. int use_zero, no_support;
  43. char *new_name;
  44. char **names;
  45. char **ptr;
  46. struct Cell_head window;
  47. struct Cell_head *cellhd;
  48. struct GModule *module;
  49. struct Flag *zeroflag, *nosupportflag;
  50. struct Option *opt1, *opt2;
  51. G_gisinit(argv[0]);
  52. module = G_define_module();
  53. G_add_keyword(_("raster"));
  54. G_add_keyword(_("geometry"));
  55. G_add_keyword(_("mosaicking"));
  56. G_add_keyword(_("merge"));
  57. G_add_keyword(_("patching"));
  58. G_add_keyword(_("aggregation"));
  59. G_add_keyword(_("series"));
  60. module->description =
  61. _("Creates a composite raster map layer by using "
  62. "known category values from one (or more) map layer(s) "
  63. "to fill in areas of \"no data\" in another map layer.");
  64. /* Define the different options */
  65. opt1 = G_define_standard_option(G_OPT_R_INPUTS);
  66. opt1->description = _("Name of raster maps to be patched together");
  67. opt2 = G_define_standard_option(G_OPT_R_OUTPUT);
  68. opt2->description = _("Name for resultant raster map");
  69. /* Define the different flags */
  70. zeroflag = G_define_flag();
  71. zeroflag->key = 'z';
  72. zeroflag->description =
  73. _("Use zero (0) for transparency instead of NULL");
  74. nosupportflag = G_define_flag();
  75. nosupportflag->key = 's';
  76. nosupportflag->description =
  77. _("Do not create color and category files");
  78. if (G_parser(argc, argv))
  79. exit(EXIT_FAILURE);
  80. use_zero = (zeroflag->answer);
  81. no_support = (nosupportflag->answer);
  82. names = opt1->answers;
  83. out_type = CELL_TYPE;
  84. for (ptr = names, nfiles = 0; *ptr != NULL; ptr++, nfiles++) ;
  85. if (nfiles < 2)
  86. G_fatal_error(_("The minimum number of input raster maps is two"));
  87. infd = G_malloc(nfiles * sizeof(int));
  88. statf = G_malloc(nfiles * sizeof(struct Cell_stats));
  89. cellhd = G_malloc(nfiles * sizeof(struct Cell_head));
  90. for (i = 0; i < nfiles; i++) {
  91. const char *name = names[i];
  92. int fd;
  93. fd = Rast_open_old(name, "");
  94. infd[i] = fd;
  95. map_type = Rast_get_map_type(fd);
  96. if (map_type == FCELL_TYPE && out_type == CELL_TYPE)
  97. out_type = FCELL_TYPE;
  98. else if (map_type == DCELL_TYPE)
  99. out_type = DCELL_TYPE;
  100. Rast_init_cell_stats(&statf[i]);
  101. Rast_get_cellhd(name, "", &cellhd[i]);
  102. }
  103. out_cell_size = Rast_cell_size(out_type);
  104. rname = opt2->answer;
  105. outfd = Rast_open_new(new_name = rname, out_type);
  106. presult = Rast_allocate_buf(out_type);
  107. patch = Rast_allocate_buf(out_type);
  108. Rast_get_window(&window);
  109. nrows = Rast_window_rows();
  110. ncols = Rast_window_cols();
  111. G_verbose_message(_("Percent complete..."));
  112. for (row = 0; row < nrows; row++) {
  113. double north_edge, south_edge;
  114. G_percent(row, nrows, 2);
  115. Rast_get_row(infd[0], presult, row, out_type);
  116. north_edge = Rast_row_to_northing(row, &window);
  117. south_edge = north_edge - window.ns_res;
  118. if (out_type == CELL_TYPE)
  119. Rast_update_cell_stats((CELL *) presult, ncols, &statf[0]);
  120. for (i = 1; i < nfiles; i++) {
  121. /* check if raster i overlaps with the current row */
  122. if (south_edge >= cellhd[i].north ||
  123. north_edge <= cellhd[i].south ||
  124. window.west >= cellhd[i].east ||
  125. window.east <= cellhd[i].west)
  126. continue;
  127. Rast_get_row(infd[i], patch, row, out_type);
  128. if (!do_patch
  129. (presult, patch, &statf[i], ncols, out_type, out_cell_size,
  130. use_zero))
  131. break;
  132. }
  133. Rast_put_row(outfd, presult, out_type);
  134. }
  135. G_percent(row, nrows, 2);
  136. G_free(patch);
  137. G_free(presult);
  138. for (i = 0; i < nfiles; i++)
  139. Rast_close(infd[i]);
  140. if(!no_support) {
  141. /*
  142. * build the new cats and colors. do this before closing the new
  143. * file, in case the new file is one of the patching files as well.
  144. */
  145. G_verbose_message(_("Creating support files for raster map <%s>..."), new_name);
  146. support(names, statf, nfiles, &cats, &cats_ok, &colr, &colr_ok, out_type);
  147. }
  148. /* now close (and create) the result */
  149. Rast_close(outfd);
  150. if(!no_support) {
  151. if (cats_ok)
  152. Rast_write_cats(new_name, &cats);
  153. if (colr_ok)
  154. Rast_write_colors(new_name, G_mapset(), &colr);
  155. }
  156. Rast_short_history(new_name, "raster", &history);
  157. Rast_command_history(&history);
  158. Rast_write_history(new_name, &history);
  159. exit(EXIT_SUCCESS);
  160. }