main.c 5.1 KB

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