read_rast.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. * Copyright (C) 2000 by the GRASS Development Team
  3. * Author: Bob Covill <bcovill@tekmap.ns.ca>
  4. *
  5. * This Program is free software under the GPL (>=v2)
  6. * Read the file COPYING coming with GRASS for details
  7. *
  8. */
  9. #include <grass/gis.h>
  10. #include <grass/raster.h>
  11. #include <grass/glocale.h>
  12. #include "local_proto.h"
  13. int read_rast(double east, double north, double dist, int fd, int coords,
  14. RASTER_MAP_TYPE data_type, FILE * fp, char *null_string)
  15. {
  16. static DCELL *dcell;
  17. static int cur_row = -1;
  18. static CELL nullcell;
  19. static int nrows, ncols;
  20. static struct Cell_head window;
  21. int row, col;
  22. int outofbounds = FALSE;
  23. if (!dcell) {
  24. Rast_set_c_null_value(&nullcell, 1);
  25. dcell = Rast_allocate_d_buf();
  26. G_get_window(&window);
  27. nrows = window.rows;
  28. ncols = window.cols;
  29. }
  30. row = (window.north - north) / window.ns_res;
  31. col = (east - window.west) / window.ew_res;
  32. G_debug(4, "row=%d:%d col=%d:%d", row, nrows, col, ncols);
  33. if ((row < 0) || (row >= nrows) || (col < 0) || (col >= ncols))
  34. outofbounds = TRUE;
  35. if (!outofbounds) {
  36. if (row != cur_row)
  37. Rast_get_d_row(fd, dcell, row);
  38. cur_row = row;
  39. }
  40. if (coords)
  41. fprintf(fp, "%f %f", east, north);
  42. fprintf(fp, " %f", dist);
  43. if (outofbounds || Rast_is_d_null_value(&dcell[col]))
  44. fprintf(fp, " %s", null_string);
  45. else {
  46. if (data_type == CELL_TYPE)
  47. fprintf(fp, " %d", (int) dcell[col]);
  48. else
  49. fprintf(fp, " %f", dcell[col]);
  50. }
  51. if (clr) {
  52. int red, green, blue;
  53. if (outofbounds)
  54. Rast_get_c_color(&nullcell, &red, &green, &blue, &colors);
  55. else
  56. Rast_get_d_color(&dcell[col], &red, &green, &blue,
  57. &colors);
  58. fprintf(fp, " %03d:%03d:%03d", red, green, blue);
  59. }
  60. fprintf(fp, "\n");
  61. return 0;
  62. }