write_map.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 (offset)
  35. fd_in = Rast_open_old(output, G_mapset());
  36. cell = Rast_allocate_c_buf();
  37. G_message(_("Writing output raster map <%s>..."), output);
  38. ptr = map;
  39. for (row = 0; row < window.rows; row++) {
  40. G_percent(row, window.rows, 2);
  41. col = window.cols;
  42. if (!offset) {
  43. while (col-- > 0)
  44. *cell++ = (CELL) * ptr++;
  45. }
  46. else {
  47. Rast_get_c_row_nomask(fd_in, cell, row);
  48. while (col-- > 0) {
  49. if (*cell == 0 && *ptr != 0)
  50. *cell = (CELL) * ptr + offset;
  51. cell++;
  52. ptr++;
  53. }
  54. }
  55. cell -= window.cols;
  56. /* set 0 to NULL */
  57. for (k = 0; k < window.cols; k++)
  58. if (cell[k] == 0)
  59. Rast_set_null_value(&cell[k], 1, CELL_TYPE);
  60. Rast_put_row(fd_out, cell, CELL_TYPE);
  61. }
  62. G_percent(row, window.rows, 2);
  63. G_free(cell);
  64. if (offset)
  65. Rast_close(fd_in);
  66. Rast_close(fd_out);
  67. return 0;
  68. }