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