align_window.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*!
  2. * \file gis/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. /*!
  17. * \brief Align two regions.
  18. *
  19. * Modifies the input <i>window</i> to align to <i>ref</i> region. The
  20. * resolutions in <i>window</i> are set to match those in <i>ref</i>
  21. * and the <i>window</i> edges (north, south, east, west) are modified
  22. * to align with the grid of the <i>ref</i> region.
  23. *
  24. * The <i>window</i> may be enlarged if necessary to achieve the
  25. * alignment. The north is rounded northward, the south southward,
  26. * the east eastward and the west westward. Lon-lon constraints are
  27. * taken into consideration to make sure that the north doesn't go
  28. * above 90 degrees (for lat/lon) or that the east does "wrap" past
  29. * the west, etc.
  30. *
  31. * \param[in,out] window pointer to Cell_head to be modified
  32. * \param ref pointer to Cell_head
  33. *
  34. * \return NULL on success
  35. */
  36. void G_align_window(struct Cell_head *window, const struct Cell_head *ref)
  37. {
  38. int preserve;
  39. window->ns_res = ref->ns_res;
  40. window->ew_res = ref->ew_res;
  41. window->zone = ref->zone;
  42. window->proj = ref->proj;
  43. preserve = window->proj == PROJECTION_LL &&
  44. window->east == (window->west + 360);
  45. window->south =
  46. G_row_to_northing(ceil(G_northing_to_row(window->south, ref)), ref);
  47. window->north =
  48. G_row_to_northing(floor(G_northing_to_row(window->north, ref)), ref);
  49. window->east =
  50. G_col_to_easting(ceil(G_easting_to_col(window->east, ref)), ref);
  51. window->west =
  52. G_col_to_easting(floor(G_easting_to_col(window->west, ref)), ref);
  53. if (window->proj == PROJECTION_LL) {
  54. while (window->north > 90.0)
  55. window->north -= window->ns_res;
  56. while (window->south < -90.0)
  57. window->south += window->ns_res;
  58. if (preserve)
  59. window->east = window->west + 360;
  60. else
  61. while (window->east - window->west > 360.0)
  62. window->east -= window->ew_res;
  63. }
  64. G_adjust_Cell_head(window, 0, 0);
  65. }