main.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. size_t out_cell_size;
  33. struct History history;
  34. void *presult, *patch;
  35. int nfiles;
  36. char *rname;
  37. int i;
  38. int row, nrows, ncols;
  39. int use_zero;
  40. char *new_name;
  41. char **names;
  42. char **ptr;
  43. struct Cell_head window;
  44. struct Cell_head *cellhd;
  45. struct GModule *module;
  46. struct Flag *zeroflag;
  47. struct Option *opt1, *opt2;
  48. G_gisinit(argv[0]);
  49. module = G_define_module();
  50. G_add_keyword(_("raster"));
  51. G_add_keyword(_("geometry"));
  52. G_add_keyword(_("mosaicking"));
  53. G_add_keyword(_("merge"));
  54. G_add_keyword(_("patching"));
  55. module->description =
  56. _("Creates a composite raster map layer by using "
  57. "known category values from one (or more) map layer(s) "
  58. "to fill in areas of \"no data\" in another map layer.");
  59. /* Define the different options */
  60. opt1 = G_define_standard_option(G_OPT_R_INPUTS);
  61. opt1->description = _("Name of raster maps to be patched together");
  62. opt2 = G_define_standard_option(G_OPT_R_OUTPUT);
  63. opt2->description = _("Name for resultant raster map");
  64. /* Define the different flags */
  65. zeroflag = G_define_flag();
  66. zeroflag->key = 'z';
  67. zeroflag->description =
  68. _("Use zero (0) for transparency instead of NULL");
  69. if (G_parser(argc, argv))
  70. exit(EXIT_FAILURE);
  71. use_zero = (zeroflag->answer);
  72. names = opt1->answers;
  73. out_type = CELL_TYPE;
  74. for (ptr = names, nfiles = 0; *ptr != NULL; ptr++, nfiles++) ;
  75. if (nfiles < 2)
  76. G_fatal_error(_("The minimum number of input raster maps is two"));
  77. infd = G_malloc(nfiles * sizeof(int));
  78. statf = G_malloc(nfiles * sizeof(struct Cell_stats));
  79. cellhd = G_malloc(nfiles * sizeof(struct Cell_head));
  80. for (i = 0; i < nfiles; i++) {
  81. const char *name = names[i];
  82. int fd;
  83. fd = Rast_open_old(name, "");
  84. infd[i] = fd;
  85. map_type = Rast_get_map_type(fd);
  86. if (map_type == FCELL_TYPE && out_type == CELL_TYPE)
  87. out_type = FCELL_TYPE;
  88. else if (map_type == DCELL_TYPE)
  89. out_type = DCELL_TYPE;
  90. Rast_init_cell_stats(&statf[i]);
  91. Rast_get_cellhd(name, "", &cellhd[i]);
  92. }
  93. out_cell_size = Rast_cell_size(out_type);
  94. rname = opt2->answer;
  95. outfd = Rast_open_new(new_name = rname, out_type);
  96. presult = Rast_allocate_buf(out_type);
  97. patch = Rast_allocate_buf(out_type);
  98. Rast_get_window(&window);
  99. nrows = Rast_window_rows();
  100. ncols = Rast_window_cols();
  101. G_verbose_message(_("Percent complete..."));
  102. for (row = 0; row < nrows; row++) {
  103. double north_edge, south_edge;
  104. G_percent(row, nrows, 2);
  105. Rast_get_row(infd[0], presult, row, out_type);
  106. north_edge = Rast_row_to_northing(row, &window);
  107. south_edge = north_edge - window.ns_res;
  108. if (out_type == CELL_TYPE)
  109. Rast_update_cell_stats((CELL *) presult, ncols, &statf[0]);
  110. for (i = 1; i < nfiles; i++) {
  111. /* check if raster i overlaps with the current row */
  112. if (south_edge >= cellhd[i].north ||
  113. north_edge <= cellhd[i].south ||
  114. window.west >= cellhd[i].east ||
  115. window.east <= cellhd[i].west)
  116. continue;
  117. Rast_get_row(infd[i], patch, row, out_type);
  118. if (!do_patch
  119. (presult, patch, &statf[i], ncols, out_type, out_cell_size,
  120. use_zero))
  121. break;
  122. }
  123. Rast_put_row(outfd, presult, out_type);
  124. }
  125. G_percent(row, nrows, 2);
  126. G_free(patch);
  127. G_free(presult);
  128. for (i = 0; i < nfiles; i++)
  129. Rast_close(infd[i]);
  130. /*
  131. * build the new cats and colors. do this before closing the new
  132. * file, in case the new file is one of the patching files as well.
  133. */
  134. G_verbose_message(_("Creating support files for raster map <%s>..."), new_name);
  135. support(names, statf, nfiles, &cats, &cats_ok, &colr, &colr_ok, out_type);
  136. /* now close (and create) the result */
  137. Rast_close(outfd);
  138. if (cats_ok)
  139. Rast_write_cats(new_name, &cats);
  140. if (colr_ok)
  141. Rast_write_colors(new_name, G_mapset(), &colr);
  142. Rast_short_history(new_name, "raster", &history);
  143. Rast_command_history(&history);
  144. Rast_write_history(new_name, &history);
  145. exit(EXIT_SUCCESS);
  146. }