align_window.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*!
  2. * \file lib/raster/align_window.c
  3. *
  4. * \brief GIS Library - Window alignment functions.
  5. *
  6. * (C) 2001-2009 by the GRASS Development Team
  7. *
  8. * This program is free software under the GNU General Public License
  9. * (>=v2). Read the file COPYING that comes with GRASS for details.
  10. *
  11. * \author Original author CERL
  12. */
  13. #include <stdio.h>
  14. #include <math.h>
  15. #include <grass/gis.h>
  16. #include <grass/raster.h>
  17. /*!
  18. * \brief Align two regions.
  19. *
  20. * Modifies the input <i>window</i> to align to <i>ref</i> region. The
  21. * resolutions in <i>window</i> are set to match those in <i>ref</i>
  22. * and the <i>window</i> edges (north, south, east, west) are modified
  23. * to align with the grid of the <i>ref</i> region.
  24. *
  25. * The <i>window</i> may be enlarged if necessary to achieve the
  26. * alignment. The north is rounded northward, the south southward,
  27. * the east eastward and the west westward. Lon-lon constraints are
  28. * taken into consideration to make sure that the north doesn't go
  29. * above 90 degrees (for lat/lon) or that the east does "wrap" past
  30. * the west, etc.
  31. *
  32. * \param[in,out] window pointer to Cell_head to be modified
  33. * \param ref pointer to Cell_head
  34. *
  35. * \return NULL on success
  36. */
  37. void Rast_align_window(struct Cell_head *window, const struct Cell_head *ref)
  38. {
  39. int preserve;
  40. window->ns_res = ref->ns_res;
  41. window->ew_res = ref->ew_res;
  42. window->zone = ref->zone;
  43. window->proj = ref->proj;
  44. preserve = window->proj == PROJECTION_LL &&
  45. window->east == (window->west + 360);
  46. window->south =
  47. Rast_row_to_northing(ceil(Rast_northing_to_row(window->south, ref)), ref);
  48. window->north =
  49. Rast_row_to_northing(floor(Rast_northing_to_row(window->north, ref)), ref);
  50. window->east =
  51. Rast_col_to_easting(ceil(Rast_easting_to_col(window->east, ref)), ref);
  52. window->west =
  53. Rast_col_to_easting(floor(Rast_easting_to_col(window->west, ref)), ref);
  54. if (window->proj == PROJECTION_LL) {
  55. while (window->north > 90.0)
  56. window->north -= window->ns_res;
  57. while (window->south < -90.0)
  58. window->south += window->ns_res;
  59. if (preserve)
  60. window->east = window->west + 360;
  61. else
  62. while (window->east - window->west > 360.0)
  63. window->east -= window->ew_res;
  64. }
  65. G_adjust_Cell_head(window, 0, 0);
  66. }