main.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. infd = Rast_open_old(name, mapset);
  93. /* controlling, if we can open input raster */
  94. Rast_get_cellhd(name, mapset, &cellhd);
  95. G_debug(3, "number of rows %d", cellhd.rows);
  96. /* Allocate input buffer */
  97. inrast = Rast_allocate_buf(data_type);
  98. /* Allocate output buffer, use input map data_type */
  99. nrows = Rast_window_rows();
  100. ncols = Rast_window_cols();
  101. outrast = Rast_allocate_buf(data_type);
  102. /* controlling, if we can write the raster */
  103. outfd = Rast_open_new(result, data_type);
  104. /* for each row */
  105. for (row = 0; row < nrows; row++) {
  106. CELL c;
  107. FCELL f;
  108. DCELL d;
  109. G_percent(row, nrows, 2);
  110. /* read input map */
  111. Rast_get_row(infd, inrast, row, data_type);
  112. /* process the data */
  113. for (col = 0; col < ncols; col++) {
  114. /* use different function for each data type */
  115. switch (data_type) {
  116. case CELL_TYPE:
  117. c = ((CELL *) inrast)[col];
  118. c = c_calc(c); /* calculate */
  119. ((CELL *) outrast)[col] = c;
  120. break;
  121. case FCELL_TYPE:
  122. f = ((FCELL *) inrast)[col];
  123. f = f_calc(f); /* calculate */
  124. ((FCELL *) outrast)[col] = f;
  125. break;
  126. case DCELL_TYPE:
  127. d = ((DCELL *) inrast)[col];
  128. d = d_calc(d); /* calculate */
  129. ((DCELL *) outrast)[col] = d;
  130. break;
  131. }
  132. }
  133. /* write raster row to output raster map */
  134. Rast_put_row(outfd, outrast, data_type);
  135. }
  136. /* memory cleanup */
  137. G_free(inrast);
  138. G_free(outrast);
  139. /* closing raster maps */
  140. Rast_close(infd);
  141. Rast_close(outfd);
  142. /* add command line incantation to history file */
  143. Rast_short_history(result, "raster", &history);
  144. Rast_command_history(&history);
  145. Rast_write_history(result, &history);
  146. exit(EXIT_SUCCESS);
  147. }