main.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /****************************************************************************
  2. *
  3. * MODULE: r.resample
  4. * AUTHOR(S): Michael Shapiro (original CERL contributor),
  5. * Markus Neteler <neteler itc.it>,
  6. * Roberto Flor <flor itc.it>,
  7. * Bernhard Reiter <bernhard intevation.de>,
  8. * Brad Douglas <rez touchofmadness.com>,
  9. * Glynn Clements <glynn gclements.plus.com>,
  10. * Jachym Cepicky <jachym les-ejk.cz>,
  11. * Jan-Oliver Wagner <jan intevation.de>
  12. * PURPOSE: resamples the data values in a user-specified raster
  13. * input map layer
  14. * COPYRIGHT: (C) 1999-2006 by the GRASS Development Team
  15. *
  16. * This program is free software under the GNU General Public
  17. * License (>=v2). Read the file COPYING that comes with GRASS
  18. * for details.
  19. *
  20. *****************************************************************************/
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <stdio.h>
  24. #include <grass/gis.h>
  25. #include <grass/raster.h>
  26. #include <grass/glocale.h>
  27. int main(int argc, char *argv[])
  28. {
  29. struct History hist;
  30. struct Categories cats;
  31. struct Categories newcats;
  32. struct Colors colr;
  33. struct Cell_head cellhd;
  34. struct Range range;
  35. int hist_ok, colr_ok, cats_ok;
  36. char *name;
  37. char *result;
  38. void *rast;
  39. int nrows, ncols;
  40. int row;
  41. int infd, outfd;
  42. RASTER_MAP_TYPE data_type, out_type;
  43. struct GModule *module;
  44. struct
  45. {
  46. struct Option *input, *output;
  47. } option;
  48. G_gisinit(argv[0]);
  49. module = G_define_module();
  50. G_add_keyword(_("raster"));
  51. G_add_keyword(_("resample"));
  52. module->description =
  53. _("GRASS raster map layer data resampling capability.");
  54. /* Define the different options */
  55. option.input = G_define_option();
  56. option.input->key = "input";
  57. option.input->type = TYPE_STRING;
  58. option.input->required = YES;
  59. option.input->gisprompt = "old,cell,raster";
  60. option.input->description = _("Name of an input layer");
  61. option.output = G_define_option();
  62. option.output->key = "output";
  63. option.output->type = TYPE_STRING;
  64. option.output->required = YES;
  65. option.output->gisprompt = "new,cell,raster";
  66. option.output->description = _("Name of an output layer");
  67. /* Define the different flags */
  68. if (G_parser(argc, argv))
  69. exit(EXIT_FAILURE);
  70. name = option.input->answer;
  71. result = option.output->answer;
  72. hist_ok = Rast_read_history(name, "", &hist) >= 0;
  73. colr_ok = Rast_read_colors(name, "", &colr) > 0;
  74. cats_ok = Rast_read_cats(name, "", &cats) >= 0;
  75. if (cats_ok) {
  76. Rast_unmark_cats(&cats);
  77. Rast_init_cats(Rast_get_cats_title(&cats), &newcats);
  78. }
  79. infd = Rast_open_old(name, "");
  80. /* determine the map type;
  81. data_type is the type of data being processed,
  82. out_type is the type of map being created. */
  83. data_type = Rast_get_map_type(infd);
  84. out_type = data_type;
  85. Rast_get_cellhd(name, "", &cellhd);
  86. /* raster buffer is big enough to hold data */
  87. rast = Rast_allocate_buf(data_type);
  88. nrows = Rast_window_rows();
  89. ncols = Rast_window_cols();
  90. if (ncols <= 1)
  91. rast = G_realloc(rast, 2 * Rast_cell_size(data_type));
  92. /* we need the buffer at least 2 cells large */
  93. outfd = Rast_open_new(result, out_type);
  94. Rast_set_null_value(rast, ncols, out_type);
  95. G_message(_("Percent complete: "));
  96. for (row = 0; row < nrows; row++) {
  97. G_percent(row, nrows, 2);
  98. Rast_get_row(infd, rast, row, data_type);
  99. Rast_put_row(outfd, rast, out_type);
  100. Rast_mark_cats(rast, ncols, &cats, data_type);
  101. }
  102. G_percent(row, nrows, 2);
  103. Rast_close(infd);
  104. G_message(_("Creating support files for <%s>..."), result);
  105. Rast_close(outfd);
  106. Rast_rewind_cats(&cats);
  107. if (cats_ok) {
  108. long count;
  109. void *rast1, *rast2;
  110. rast1 = rast;
  111. rast2 = G_incr_void_ptr(rast, Rast_cell_size(data_type));
  112. G_message(_("Creating new cats file..."));
  113. while (Rast_get_next_marked_cat(&cats,
  114. rast1, rast2, &count, data_type))
  115. Rast_set_cat(rast1, rast2,
  116. Rast_get_cat(rast1, &cats, data_type),
  117. &newcats, data_type);
  118. Rast_write_cats(result, &newcats);
  119. Rast_free_cats(&cats);
  120. Rast_free_cats(&newcats);
  121. }
  122. if (colr_ok) {
  123. if (Rast_read_range(result, G_mapset(), &range) > 0) {
  124. CELL min, max, cmin, cmax;
  125. Rast_get_range_min_max(&range, &min, &max);
  126. Rast_get_c_color_range(&cmin, &cmax, &colr);
  127. if (min > cmin)
  128. cmin = min;
  129. if (max < cmax)
  130. cmax = max;
  131. Rast_set_c_color_range(cmin, cmax, &colr);
  132. }
  133. Rast_write_colors(result, G_mapset(), &colr);
  134. }
  135. if (hist_ok)
  136. Rast_write_history(result, &hist);
  137. exit(EXIT_SUCCESS);
  138. }