main.c 4.9 KB

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