main.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /****************************************************************************
  2. *
  3. * MODULE: r.example
  4. * AUTHOR(S): Markus Neteler - neteler itc.it
  5. * with hints from: Glynn Clements - glynn gclements.plus.com
  6. * PURPOSE: Just copies a raster map, preserving the raster map type
  7. * Intended to explain GRASS raster programming
  8. *
  9. * COPYRIGHT: (C) 2002,2005 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 <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <grass/gis.h>
  20. #include <grass/glocale.h>
  21. /*
  22. * global function declaration
  23. */
  24. extern CELL f_c(CELL);
  25. extern FCELL f_f(FCELL);
  26. extern DCELL f_d(DCELL);
  27. /*
  28. * function definitions
  29. */
  30. CELL c_calc(CELL x)
  31. {
  32. /* we do nothing exciting here */
  33. return x;
  34. }
  35. FCELL f_calc(FCELL x)
  36. {
  37. /* we do nothing exciting here */
  38. return x;
  39. }
  40. DCELL d_calc(DCELL x)
  41. {
  42. /* we do nothing exciting here */
  43. return x;
  44. }
  45. /*
  46. * main function
  47. * it copies raster input raster map, calling the appropriate function for each
  48. * data type (CELL, DCELL, FCELL)
  49. */
  50. int main(int argc, char *argv[])
  51. {
  52. struct Cell_head cellhd; /* it stores region information,
  53. and header information of rasters */
  54. char *name; /* input raster name */
  55. char *result; /* output raster name */
  56. char *mapset; /* mapset name */
  57. void *inrast; /* input buffer */
  58. unsigned char *outrast; /* output buffer */
  59. int nrows, ncols;
  60. int row, col;
  61. int infd, outfd; /* file descriptor */
  62. int verbose;
  63. RASTER_MAP_TYPE data_type; /* type of the map (CELL/DCELL/...) */
  64. struct History history; /* holds meta-data (title, comments,..) */
  65. struct GModule *module; /* GRASS module for parsing arguments */
  66. struct Option *input, *output; /* options */
  67. struct Flag *flag1; /* flags */
  68. /* initialize GIS environment */
  69. G_gisinit(argv[0]); /* reads grass env, stores program name to G_program_name() */
  70. /* initialize module */
  71. module = G_define_module();
  72. G_add_keyword(_("raster"));
  73. G_add_keyword(_("keyword2"));
  74. G_add_keyword(_("keyword3"));
  75. module->description = _("My first raster module");
  76. /* Define the different options as defined in gis.h */
  77. input = G_define_standard_option(G_OPT_R_INPUT);
  78. output = G_define_standard_option(G_OPT_R_OUTPUT);
  79. /* Define the different flags */
  80. flag1 = G_define_flag();
  81. flag1->key = 'q';
  82. flag1->description = _("Quiet");
  83. /* options and flags parser */
  84. if (G_parser(argc, argv))
  85. exit(EXIT_FAILURE);
  86. /* stores options and flags to variables */
  87. name = input->answer;
  88. result = output->answer;
  89. verbose = (!flag1->answer);
  90. /* returns NULL if the map was not found in any mapset,
  91. * mapset name otherwise */
  92. mapset = G_find_cell2(name, "");
  93. if (mapset == NULL)
  94. G_fatal_error(_("Raster map <%s> not found"), name);
  95. /* determine the inputmap type (CELL/FCELL/DCELL) */
  96. data_type = Rast_map_type(name, mapset);
  97. /* Rast_open_old - returns file destriptor (>0) */
  98. if ((infd = Rast_open_old(name, mapset)) < 0)
  99. G_fatal_error(_("Unable to open raster map <%s>"), name);
  100. /* controlling, if we can open input raster */
  101. if (Rast_get_cellhd(name, mapset, &cellhd) < 0)
  102. G_fatal_error(_("Unable to read file header of <%s>"), name);
  103. G_debug(3, "number of rows %d", cellhd.rows);
  104. /* Allocate input buffer */
  105. inrast = Rast_allocate_buf(data_type);
  106. /* Allocate output buffer, use input map data_type */
  107. nrows = G_window_rows();
  108. ncols = G_window_cols();
  109. outrast = Rast_allocate_buf(data_type);
  110. /* controlling, if we can write the raster */
  111. if ((outfd = Rast_open_new(result, data_type)) < 0)
  112. G_fatal_error(_("Unable to create raster map <%s>"), result);
  113. /* for each row */
  114. for (row = 0; row < nrows; row++) {
  115. CELL c;
  116. FCELL f;
  117. DCELL d;
  118. if (verbose)
  119. G_percent(row, nrows, 2);
  120. /* read input map */
  121. if (Rast_get_row(infd, inrast, row, data_type) < 0)
  122. G_fatal_error(_("Unable to read raster map <%s> row %d"), name,
  123. row);
  124. /* process the data */
  125. for (col = 0; col < ncols; col++) {
  126. /* use different function for each data type */
  127. switch (data_type) {
  128. case CELL_TYPE:
  129. c = ((CELL *) inrast)[col];
  130. c = c_calc(c); /* calculate */
  131. ((CELL *) outrast)[col] = c;
  132. break;
  133. case FCELL_TYPE:
  134. f = ((FCELL *) inrast)[col];
  135. f = f_calc(f); /* calculate */
  136. ((FCELL *) outrast)[col] = f;
  137. break;
  138. case DCELL_TYPE:
  139. d = ((DCELL *) inrast)[col];
  140. d = d_calc(d); /* calculate */
  141. ((DCELL *) outrast)[col] = d;
  142. break;
  143. }
  144. }
  145. /* write raster row to output raster map */
  146. if (Rast_put_raster_row(outfd, outrast, data_type) < 0)
  147. G_fatal_error(_("Failed writing raster map <%s>"), result);
  148. }
  149. /* memory cleanup */
  150. G_free(inrast);
  151. G_free(outrast);
  152. /* closing raster maps */
  153. Rast_close(infd);
  154. Rast_close(outfd);
  155. /* add command line incantation to history file */
  156. Rast_short_history(result, "raster", &history);
  157. Rast_command_history(&history);
  158. Rast_write_history(result, &history);
  159. exit(EXIT_SUCCESS);
  160. }