align_window.c 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. G_debug(1, "Rast_align_window()");
  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. G_debug(1, "before alignment:");
  45. G_debug(1, "North: %.15g", window->north);
  46. G_debug(1, "South: %.15g", window->south);
  47. G_debug(1, "West: %.15g", window->west);
  48. G_debug(1, "East: %.15g", window->east);
  49. window->north =
  50. ref->north - floor((ref->north - window->north) / ref->ns_res) * ref->ns_res;
  51. window->south =
  52. ref->south - ceil((ref->south - window->south) / ref->ns_res) * ref->ns_res;
  53. /* Rast_easting_to_col() wraps easting:
  54. * east can become < west, or both west and east are shifted */
  55. window->west =
  56. ref->west + floor((window->west - ref->west) / ref->ew_res) * ref->ew_res;
  57. window->east =
  58. ref->east + ceil((window->east - ref->east) / ref->ew_res) * ref->ew_res;
  59. if (window->proj == PROJECTION_LL) {
  60. while (window->north > 90.0 + window->ns_res / 2.0)
  61. window->north -= window->ns_res;
  62. while (window->south < -90.0 - window->ns_res / 2.0)
  63. window->south += window->ns_res;
  64. }
  65. G_debug(1, "after alignment:");
  66. G_debug(1, "North: %.15g", window->north);
  67. G_debug(1, "South: %.15g", window->south);
  68. G_debug(1, "West: %.15g", window->west);
  69. G_debug(1, "East: %.15g", window->east);
  70. G_adjust_Cell_head(window, 0, 0);
  71. }