io.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /* Line thinning program */
  2. /* Input/output and file support functions */
  3. /* Mike Baba */
  4. /* DBA Systems */
  5. /* Fairfax, Va */
  6. /* Jan 1990 */
  7. /* Jean Ezell */
  8. /* US Army Corps of Engineers */
  9. /* Construction Engineering Research Laboratory */
  10. /* Modelling and Simulation Team */
  11. /* Champaign, IL 61820 */
  12. /* January - February 1988 */
  13. /* Entry points: */
  14. /* get_a_row get row from temporary work file */
  15. /* open_file open input raster map and read it into work file */
  16. /* close_file copy work file into new raster map */
  17. /* map_size get size of map and its pad */
  18. /* Global variables: */
  19. /* row_io place to store pointer to row manager stuff */
  20. /* n_rows number of rows in the work file (includes pads) */
  21. /* n_cols number of columns in the work file (includes pads) */
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <unistd.h>
  25. #include <math.h>
  26. #include <sys/types.h>
  27. #include <sys/stat.h>
  28. #include <errno.h>
  29. #include <fcntl.h>
  30. #include <grass/config.h>
  31. #include <grass/gis.h>
  32. #include <grass/raster.h>
  33. #include <grass/glocale.h>
  34. #include <grass/rowio.h>
  35. #define PAD 2
  36. #define MAX_ROW 7
  37. static int n_rows, n_cols;
  38. static int work_file;
  39. static char *work_file_name;
  40. static ROWIO row_io;
  41. /* function prototypes */
  42. static int write_row(int file, const void *buf, int row, int buf_len);
  43. static int read_row(int file, void *buf, int row, int buf_len);
  44. CELL *get_a_row(int row)
  45. {
  46. if (row < 0 || row >= n_rows)
  47. return (NULL);
  48. return ((CELL *) Rowio_get(&row_io, row));
  49. }
  50. int put_a_row(int row, CELL * buf)
  51. {
  52. /* rowio.h defines this withe 2nd argument as char * */
  53. Rowio_put(&row_io, (char *)buf, row);
  54. return 0;
  55. }
  56. static int read_row(int file, void *buf, int row, int buf_len)
  57. {
  58. lseek(file, ((off_t) row) * buf_len, 0);
  59. return (read(file, buf, buf_len) == buf_len);
  60. }
  61. static int write_row(int file, const void *buf, int row, int buf_len)
  62. {
  63. lseek(file, ((off_t) row) * buf_len, 0);
  64. return (write(file, buf, buf_len) == buf_len);
  65. }
  66. int open_file(char *name)
  67. {
  68. int cell_file, buf_len;
  69. int i, row;
  70. CELL *buf;
  71. char *tmpstr1, *tmpstr2;
  72. char rname[GNAME_MAX];
  73. char rmapset[GMAPSET_MAX];
  74. /* open raster map */
  75. cell_file = Rast_open_old(name, "");
  76. if (Rast_is_reclass(name, "", rname, rmapset) <= 0 &&
  77. Rast_get_map_type(cell_file) != CELL_TYPE) {
  78. Rast_close(cell_file);
  79. G_fatal_error(_("Input raster must be of type CELL."));
  80. }
  81. n_rows = Rast_window_rows();
  82. n_cols = Rast_window_cols();
  83. /* GTC Count of raster rows */
  84. G_asprintf(&tmpstr1, n_("%d row", "%d rows", n_rows), n_rows);
  85. /* GTC Count of raster columns */
  86. G_asprintf(&tmpstr2, n_("%d column", "%d columns", n_cols), n_cols);
  87. /* GTC First argument is the raster map name, second and third - a string representing number of rows and cols */
  88. G_message(_("Raster map <%s> - %s X %s"), name, tmpstr1, tmpstr2);
  89. G_free(tmpstr1);
  90. G_free(tmpstr2);
  91. n_cols += (PAD << 1);
  92. /* copy raster map into our read/write file */
  93. work_file_name = G_tempfile();
  94. /* create the file and then open it for read and write */
  95. close(creat(work_file_name, 0666));
  96. if ((work_file = open(work_file_name, 2)) < 0) {
  97. unlink(work_file_name);
  98. G_fatal_error(_("Unable to create temporary file <%s> -- errno = %d"),
  99. work_file_name, errno);
  100. }
  101. buf_len = n_cols * sizeof(CELL);
  102. buf = (CELL *) G_malloc(buf_len);
  103. Rast_set_c_null_value(buf, n_cols);
  104. for (i = 0; i < PAD; i++) {
  105. if (write(work_file, buf, buf_len) != buf_len) {
  106. unlink(work_file_name);
  107. G_fatal_error(_("Error writing temporary file <%s>"), work_file_name);
  108. }
  109. }
  110. for (row = 0; row < n_rows; row++) {
  111. Rast_get_c_row(cell_file, buf + PAD, row);
  112. if (write(work_file, buf, buf_len) != buf_len) {
  113. unlink(work_file_name);
  114. G_fatal_error(_("Error writing temporary file <%s>"), work_file_name);
  115. }
  116. }
  117. Rast_set_c_null_value(buf, n_cols);
  118. for (i = 0; i < PAD; i++) {
  119. if (write(work_file, buf, buf_len) != buf_len) {
  120. unlink(work_file_name);
  121. G_fatal_error(_("Error writing temporary file <%s>"), work_file_name);
  122. }
  123. }
  124. n_rows += (PAD << 1);
  125. G_free(buf);
  126. Rast_close(cell_file);
  127. Rowio_setup(&row_io, work_file, MAX_ROW, n_cols * sizeof(CELL), read_row,
  128. write_row);
  129. return 0;
  130. }
  131. int close_file(char *name)
  132. {
  133. int cell_file, row, k;
  134. int row_count, col_count;
  135. CELL *buf;
  136. char *tmpstr1, *tmpstr2;
  137. cell_file = Rast_open_c_new(name);
  138. row_count = n_rows - (PAD << 1);
  139. col_count = n_cols - (PAD << 1);
  140. /* GTC Count of raster rows */
  141. G_asprintf(&tmpstr1, n_("%d row", "%d rows", row_count), row_count);
  142. /* GTC Count of raster columns */
  143. G_asprintf(&tmpstr2, n_("%d column", "%d columns", col_count), col_count);
  144. /* GTC %s will be replaced with number of rows and columns */
  145. G_message(_("Output map %s X %s"), tmpstr1, tmpstr2);
  146. G_free(tmpstr1);
  147. G_free(tmpstr2);
  148. /* GTC Count of window rows */
  149. G_asprintf(&tmpstr1, n_("%d row", "%d rows", Rast_window_rows()), Rast_window_rows());
  150. /* GTC Count of window columns */
  151. G_asprintf(&tmpstr2, n_("%d column", "%d columns", Rast_window_cols()), Rast_window_cols());
  152. /* GTC %s will be replaced with number of rows and columns */
  153. G_message(_("Window %s X %s"), tmpstr1, tmpstr2);
  154. G_free(tmpstr1);
  155. G_free(tmpstr2);
  156. for (row = 0, k = PAD; row < row_count; row++, k++) {
  157. buf = get_a_row(k);
  158. Rast_put_row(cell_file, buf + PAD, CELL_TYPE);
  159. }
  160. Rast_close(cell_file);
  161. Rowio_flush(&row_io);
  162. close(Rowio_fileno(&row_io));
  163. Rowio_release(&row_io);
  164. unlink(work_file_name);
  165. return 0;
  166. }
  167. int map_size(int *r, int *c, int *p)
  168. {
  169. *r = n_rows;
  170. *c = n_cols;
  171. *p = PAD;
  172. return 0;
  173. }