zoom.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 zoom(struct Cell_head *window, const char *name, const char *mapset)
  7. {
  8. int fd;
  9. void *raster, *rast_ptr;
  10. RASTER_MAP_TYPE map_type;
  11. int row, col;
  12. int nrows, ncols;
  13. int top, bottom, left, right, mark;
  14. double north, south, east, west;
  15. G_adjust_Cell_head3(window, 0, 0, 0);
  16. Rast_set_window(window);
  17. nrows = window->rows;
  18. ncols = window->cols;
  19. fd = Rast_open_old(name, mapset);
  20. map_type = Rast_get_map_type(fd);
  21. raster = Rast_allocate_buf(map_type);
  22. /* find first non-null row */
  23. top = nrows;
  24. bottom = -1;
  25. left = ncols;
  26. right = -1;
  27. for (row = 0; row < nrows; row++) {
  28. Rast_get_row(fd, rast_ptr = raster, row, map_type);
  29. for (col = 0; col < ncols; col++) {
  30. if (!Rast_is_null_value(rast_ptr, map_type))
  31. break;
  32. rast_ptr = G_incr_void_ptr(rast_ptr, Rast_cell_size(map_type));
  33. }
  34. if (col == ncols)
  35. continue;
  36. if (row < top)
  37. top = row;
  38. if (row > bottom)
  39. bottom = row;
  40. if (col < left)
  41. left = col;
  42. for (mark = col; col < ncols; col++) {
  43. if (!Rast_is_null_value(rast_ptr, map_type))
  44. mark = col;
  45. rast_ptr = G_incr_void_ptr(rast_ptr, Rast_cell_size(map_type));
  46. }
  47. if (mark > right)
  48. right = mark;
  49. }
  50. Rast_close(fd);
  51. G_free(raster);
  52. /* no data everywhere? */
  53. if (bottom < 0)
  54. return 0;
  55. north = window->north - top * window->ns_res;
  56. south = window->north - (bottom + 1) * window->ns_res;
  57. west = window->west + left * window->ew_res;
  58. east = window->west + (right + 1) * window->ew_res;
  59. window->north = north;
  60. window->south = south;
  61. window->east = east;
  62. window->west = west;
  63. return 1;
  64. }