align_window.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /**
  2. * \file align_window.c
  3. *
  4. * \brief GIS Library - Window alignment functions.
  5. *
  6. * (C) 2001-2008 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 GRASS GIS Development Team
  12. *
  13. * \date 1999-2008
  14. */
  15. #include <stdio.h>
  16. #include <math.h>
  17. #include <grass/gis.h>
  18. /**
  19. * \brief Align two regions.
  20. *
  21. * Modifies the input <b>window</b> to align to
  22. * <b>ref</b> region. The resolutions in <b>window</b> are set to match those
  23. * in <b>ref</b> and the <b>window</b> edges (north, south, east, west) are
  24. * modified to align with the grid of the <b>ref</b> region.
  25. * The <b>window</b> may be enlarged if necessary to achieve the alignment.
  26. * The north is rounded northward, the south southward, the east eastward and the
  27. * west westward. Lon-lon constraints are taken into consideration
  28. * to make sure that the north doesn't go above 90 degrees (for lat/lon)
  29. * or that the east does "wrap" past the west, etc.
  30. *
  31. * \param[in,out] window
  32. * \param[in] ref
  33. * \return NULL on success
  34. * \return Pointer to an error string on failure
  35. */
  36. char *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. return G_adjust_Cell_head(window, 0, 0);
  65. }