get_range.c 1007 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #include <stdlib.h>
  2. #include <grass/gis.h>
  3. #include <grass/glocale.h>
  4. #include "local_proto.h"
  5. int get_range(const char *name, long *min, long *max)
  6. {
  7. struct Range range;
  8. int nrows, ncols, row, col;
  9. CELL *cell;
  10. int fd;
  11. CELL cmin, cmax;
  12. struct Cell_head cellhd;
  13. if (G_read_range(name, "", &range) < 0) {
  14. G_init_range(&range); /* read the file to get the range */
  15. G_get_cellhd(name, "", &cellhd);
  16. G_set_window(&cellhd);
  17. cell = G_allocate_cell_buf();
  18. fd = G_open_cell_old(name, "");
  19. if (fd < 0)
  20. exit(1);
  21. nrows = G_window_rows();
  22. ncols = G_window_cols();
  23. G_message(_("Reading %s ..."), name);
  24. for (row = 0; row < nrows; row++) {
  25. G_percent(row, nrows, 2);
  26. if (G_get_map_row_nomask(fd, cell, row) < 0)
  27. exit(1);
  28. for (col = 0; col < ncols; col++)
  29. G_update_range(cell[col], &range);
  30. }
  31. G_percent(row, nrows, 2);
  32. G_close_cell(fd);
  33. G_free(cell);
  34. }
  35. G_get_range_min_max(&range, &cmin, &cmax);
  36. *min = cmin;
  37. *max = cmax;
  38. return 0;
  39. }