read_map.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. /* read the input map. convert non-nulls to 1 */
  24. int read_input_map (char *input, char *mapset, int ZEROFLAG)
  25. {
  26. int fd;
  27. int row;
  28. int hit;
  29. register int col;
  30. register CELL *cell;
  31. register MAPTYPE *ptr;
  32. map = (MAPTYPE *) G_malloc (window.rows * window.cols * sizeof(MAPTYPE));
  33. fd = G_open_cell_old (input, mapset);
  34. if (fd < 0)
  35. G_fatal_error(_("Unable to open raster map <%s>"), input);
  36. cell = G_allocate_cell_buf();
  37. ptr = map;
  38. minrow = -1; maxrow = -1;
  39. mincol = window.cols; maxcol = 0;
  40. G_message(_("Reading input raster map <%s>..."), G_fully_qualified_name(input, mapset));
  41. count_rows_with_data = 0;
  42. for (row = 0; row < window.rows; row++)
  43. {
  44. hit = 0;
  45. G_percent (row, window.rows, 2);
  46. if (G_get_c_raster_row (fd, cell, row) < 0)
  47. G_fatal_error(_("Unable to read raster map <%s> row %d"),
  48. G_fully_qualified_name(input, mapset), row);
  49. for (col = 0; col < window.cols; col++)
  50. {
  51. if (ZEROFLAG)
  52. {
  53. if ((*ptr++ = (*cell++ != 0)))
  54. {
  55. if (minrow < 0) minrow = row;
  56. maxrow = row;
  57. if (col < mincol) mincol = col;
  58. if (col > maxcol) maxcol = col;
  59. if (!hit)
  60. {
  61. count_rows_with_data++;
  62. hit = 1;
  63. }
  64. }
  65. }
  66. else /* use NULL */
  67. {
  68. if ((*ptr++ = !G_is_c_null_value(cell++)))
  69. {
  70. if (minrow < 0) minrow = row;
  71. maxrow = row;
  72. if (col < mincol) mincol = col;
  73. if (col > maxcol) maxcol = col;
  74. if (!hit)
  75. {
  76. count_rows_with_data++;
  77. hit = 1;
  78. }
  79. }
  80. }
  81. }
  82. cell -= window.cols;
  83. }
  84. G_percent (row, window.rows, 2);
  85. G_close_cell(fd);
  86. G_free (cell);
  87. return 0;
  88. }