get_range.c 1008 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #include <stdlib.h>
  2. #include <grass/gis.h>
  3. #include <grass/raster.h>
  4. #include <grass/glocale.h>
  5. #include "local_proto.h"
  6. int get_range(const char *name, long *min, long *max)
  7. {
  8. struct Range range;
  9. int nrows, ncols, row, col;
  10. CELL *cell;
  11. int fd;
  12. CELL cmin, cmax;
  13. struct Cell_head cellhd;
  14. if (Rast_read_range(name, "", &range) < 0) {
  15. Rast_init_range(&range); /* read the file to get the range */
  16. Rast_get_cellhd(name, "", &cellhd);
  17. Rast_set_window(&cellhd);
  18. cell = Rast_allocate_c_buf();
  19. fd = Rast_open_old(name, "");
  20. nrows = Rast_window_rows();
  21. ncols = Rast_window_cols();
  22. G_message(_("Reading %s ..."), name);
  23. for (row = 0; row < nrows; row++) {
  24. G_percent(row, nrows, 2);
  25. Rast_get_c_row_nomask(fd, cell, row);
  26. for (col = 0; col < ncols; col++)
  27. Rast_update_range(cell[col], &range);
  28. }
  29. G_percent(row, nrows, 2);
  30. Rast_close(fd);
  31. G_free(cell);
  32. }
  33. Rast_get_range_min_max(&range, &cmin, &cmax);
  34. *min = cmin;
  35. *max = cmax;
  36. return 0;
  37. }