write_map.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /****************************************************************************
  2. *
  3. * MODULE: r.buffer
  4. *
  5. * AUTHOR(S): Michael Shapiro - CERL
  6. *
  7. * PURPOSE: This program creates distance zones from non-zero
  8. * cells in a grid layer. Distances are specified in
  9. * meters (on the command-line). Window does not have to
  10. * have square cells. Works both for planimetric
  11. * (UTM, State Plane) and lat-long.
  12. *
  13. * COPYRIGHT: (C) 2005 by the GRASS Development Team
  14. *
  15. * This program is free software under the GNU General Public
  16. * License (>=v2). Read the file COPYING that comes with GRASS
  17. * for details.
  18. *
  19. ****************************************************************************/
  20. #include <stdlib.h>
  21. #include "distance.h"
  22. #include <grass/raster.h>
  23. #include <grass/glocale.h>
  24. /* write out result */
  25. int write_output_map(char *output, int offset)
  26. {
  27. int fd_in = 0, fd_out;
  28. int row;
  29. register int col;
  30. register CELL *cell;
  31. register MAPTYPE *ptr;
  32. int k;
  33. fd_out = Rast_open_c_new(output);
  34. if (fd_out < 0)
  35. G_fatal_error(_("Unable to create raster map <%s>"), output);
  36. if (offset) {
  37. fd_in = Rast_open_old(output, G_mapset());
  38. if (fd_in < 0)
  39. G_fatal_error(_("Unable to open raster map <%s>"), output);
  40. }
  41. cell = Rast_allocate_c_buf();
  42. G_message(_("Writing output raster map <%s>..."), output);
  43. ptr = map;
  44. for (row = 0; row < window.rows; row++) {
  45. G_percent(row, window.rows, 2);
  46. col = window.cols;
  47. if (!offset) {
  48. while (col-- > 0)
  49. *cell++ = (CELL) * ptr++;
  50. }
  51. else {
  52. if (Rast_get_c_row_nomask(fd_in, cell, row) < 0)
  53. G_fatal_error(_("Unable to read raster map <%s> row %d"),
  54. output, row);
  55. while (col-- > 0) {
  56. if (*cell == 0 && *ptr != 0)
  57. *cell = (CELL) * ptr + offset;
  58. cell++;
  59. ptr++;
  60. }
  61. }
  62. cell -= window.cols;
  63. /* set 0 to NULL */
  64. for (k = 0; k < window.cols; k++)
  65. if (cell[k] == 0)
  66. Rast_set_null_value(&cell[k], 1, CELL_TYPE);
  67. if (Rast_put_raster_row(fd_out, cell, CELL_TYPE) < 0)
  68. G_fatal_error(_("Failed writing raster map <%s> row %d"), output,
  69. row);
  70. }
  71. G_percent(row, window.rows, 2);
  72. G_free(cell);
  73. if (offset)
  74. Rast_close(fd_in);
  75. Rast_close(fd_out);
  76. return 0;
  77. }