read_cell.c 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #include <stdlib.h>
  2. #include <unistd.h>
  3. #include <grass/gis.h>
  4. #include <grass/raster.h>
  5. #include <grass/glocale.h>
  6. #include "local_proto.h"
  7. int read_cell(char *name)
  8. {
  9. int fd;
  10. CELL *cell;
  11. struct Cell_head window, cellhd;
  12. int row, col;
  13. double z, north;
  14. G_get_window(&window);
  15. /* Set window to align with input raster map */
  16. Rast_get_cellhd(name, "", &cellhd);
  17. Rast_align_window(&window, &cellhd);
  18. Rast_set_window(&window);
  19. cell = Rast_allocate_c_buf();
  20. fd = Rast_open_old(name, "");
  21. G_message(_("Reading raster map <%s>..."), name);
  22. north = window.north - window.ns_res / 2.0;
  23. for (row = 0; row < window.rows; row++) {
  24. G_percent(row, window.rows, 1);
  25. north += window.ns_res;
  26. Rast_get_c_row_nomask(fd, cell, row);
  27. for (col = 0; col < window.cols; col++)
  28. if ((z = cell[col]))
  29. newpoint(z, window.west + (col + .5) * window.ew_res, north);
  30. }
  31. G_percent(row, window.rows, 1);
  32. Rast_close(fd);
  33. G_free(cell);
  34. /* reset the window */
  35. G_get_window(&window);
  36. Rast_set_window(&window);
  37. return 0;
  38. }