write_map.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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/glocale.h>
  23. /* write out result */
  24. int write_output_map (char *output, int offset)
  25. {
  26. int fd_in=0, fd_out;
  27. int row;
  28. register int col;
  29. register CELL *cell;
  30. register MAPTYPE *ptr;
  31. int k;
  32. fd_out = G_open_cell_new (output);
  33. if (fd_out < 0)
  34. G_fatal_error(_("Unable to create raster map <%s>"), output);
  35. if (offset)
  36. {
  37. fd_in = G_open_cell_old (output, G_mapset());
  38. if (fd_in < 0)
  39. G_fatal_error(_("Unable to open raster map <%s>"), output);
  40. }
  41. cell = G_allocate_cell_buf();
  42. G_message( _("Writing output raster map <%s>..."),
  43. output);
  44. ptr = map;
  45. for (row = 0; row < window.rows; row++)
  46. {
  47. G_percent (row, window.rows, 2);
  48. col = window.cols;
  49. if (!offset)
  50. {
  51. while (col-- > 0)
  52. *cell++ = (CELL) *ptr++ ;
  53. }
  54. else
  55. {
  56. if (G_get_map_row_nomask(fd_in, cell, row) < 0)
  57. G_fatal_error(_("Unable to read raster map <%s> row %d"), output, row);
  58. while (col-- > 0)
  59. {
  60. if (*cell == 0 && *ptr != 0)
  61. *cell = (CELL) *ptr + offset ;
  62. cell++;
  63. ptr++;
  64. }
  65. }
  66. cell -= window.cols;
  67. /* set 0 to NULL */
  68. for (k=0; k < window.cols; k++)
  69. if (cell[k] == 0) G_set_null_value(&cell[k], 1, CELL_TYPE);
  70. if (G_put_raster_row (fd_out, cell, CELL_TYPE) < 0)
  71. G_fatal_error(_("Failed writing raster map <%s> row %d"), output, row);
  72. }
  73. G_percent (row, window.rows, 2);
  74. G_free(cell);
  75. if (offset)
  76. G_close_cell(fd_in);
  77. G_close_cell(fd_out);
  78. return 0;
  79. }