window.c 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #include <grass/gis.h>
  2. #include <grass/glocale.h>
  3. #include <gdal.h>
  4. #include "proto.h"
  5. void setup_window(struct Cell_head *cellhd, GDALDatasetH hDS, int *flip)
  6. {
  7. /* -------------------------------------------------------------------- */
  8. /* Set up the window representing the data we have. */
  9. /* -------------------------------------------------------------------- */
  10. double adfGeoTransform[6];
  11. cellhd->rows = GDALGetRasterYSize(hDS);
  12. cellhd->rows3 = GDALGetRasterYSize(hDS);
  13. cellhd->cols = GDALGetRasterXSize(hDS);
  14. cellhd->cols3 = GDALGetRasterXSize(hDS);
  15. if (GDALGetGeoTransform(hDS, adfGeoTransform) == CE_None) {
  16. if (adfGeoTransform[2] != 0.0 || adfGeoTransform[4] != 0.0)
  17. G_fatal_error(_("Input raster map is rotated - cannot import. "
  18. "You may use 'gdalwarp' to transform the map to North-up."));
  19. if (adfGeoTransform[1] <= 0.0) {
  20. G_message(_("Applying horizontal flip"));
  21. *flip |= FLIP_H;
  22. }
  23. if (adfGeoTransform[5] >= 0.0) {
  24. G_message(_("Applying vertical flip"));
  25. *flip |= FLIP_V;
  26. }
  27. cellhd->north = adfGeoTransform[3];
  28. cellhd->ns_res = fabs(adfGeoTransform[5]);
  29. cellhd->ns_res3 = fabs(adfGeoTransform[5]);
  30. cellhd->south = cellhd->north - cellhd->ns_res * cellhd->rows;
  31. cellhd->west = adfGeoTransform[0];
  32. cellhd->ew_res = fabs(adfGeoTransform[1]);
  33. cellhd->ew_res3 = fabs(adfGeoTransform[1]);
  34. cellhd->east = cellhd->west + cellhd->cols * cellhd->ew_res;
  35. cellhd->top = 1.;
  36. cellhd->bottom = 0.;
  37. cellhd->tb_res = 1.;
  38. cellhd->depths = 1;
  39. }
  40. else {
  41. cellhd->north = cellhd->rows;
  42. cellhd->south = 0.0;
  43. cellhd->ns_res = 1.0;
  44. cellhd->ns_res3 = 1.0;
  45. cellhd->west = 0.0;
  46. cellhd->east = cellhd->cols;
  47. cellhd->ew_res = 1.0;
  48. cellhd->ew_res3 = 1.0;
  49. cellhd->top = 1.;
  50. cellhd->bottom = 0.;
  51. cellhd->tb_res = 1.;
  52. cellhd->depths = 1;
  53. }
  54. }
  55. void update_default_window(struct Cell_head *cellhd)
  56. {
  57. /* -------------------------------------------------------------------- */
  58. /* Extend current window based on dataset. */
  59. /* -------------------------------------------------------------------- */
  60. struct Cell_head cur_wind;
  61. if (strcmp(G_mapset(), "PERMANENT") == 0)
  62. /* fixme: expand WIND and DEFAULT_WIND independently. (currently
  63. WIND gets forgotten and DEFAULT_WIND is expanded for both) */
  64. G_get_default_window(&cur_wind);
  65. else
  66. G_get_window(&cur_wind);
  67. cur_wind.north = MAX(cur_wind.north, cellhd->north);
  68. cur_wind.south = MIN(cur_wind.south, cellhd->south);
  69. cur_wind.west = MIN(cur_wind.west, cellhd->west);
  70. cur_wind.east = MAX(cur_wind.east, cellhd->east);
  71. cur_wind.rows = (int)ceil((cur_wind.north - cur_wind.south)
  72. / cur_wind.ns_res);
  73. cur_wind.south = cur_wind.north - cur_wind.rows * cur_wind.ns_res;
  74. cur_wind.cols = (int)ceil((cur_wind.east - cur_wind.west)
  75. / cur_wind.ew_res);
  76. cur_wind.east = cur_wind.west + cur_wind.cols * cur_wind.ew_res;
  77. if (strcmp(G_mapset(), "PERMANENT") == 0) {
  78. G_put_element_window(&cur_wind, "", "DEFAULT_WIND");
  79. G_message(_("Default region for this location updated"));
  80. }
  81. G_put_window(&cur_wind);
  82. G_message(_("Region for the current mapset updated"));
  83. }