GRASS 8 Programmer's Manual  8.5.0dev(2025)-c070206eb1
align_window.c
Go to the documentation of this file.
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 
14 #include <stdio.h>
15 #include <math.h>
16 
17 #include <grass/gis.h>
18 #include <grass/raster.h>
19 
20 /*!
21  * \brief Align two regions.
22  *
23  * Modifies the input <i>window</i> to align to <i>ref</i> region. The
24  * resolutions in <i>window</i> are set to match those in <i>ref</i>
25  * and the <i>window</i> edges (north, south, east, west) are modified
26  * to align with the grid of the <i>ref</i> region.
27  *
28  * The <i>window</i> may be enlarged if necessary to achieve the
29  * alignment. The north is rounded northward, the south southward,
30  * the east eastward and the west westward. Lon-lon constraints are
31  * taken into consideration to make sure that the north doesn't go
32  * above 90 degrees (for lat/lon) or that the east does "wrap" past
33  * the west, etc.
34  *
35  * \param[in,out] window pointer to Cell_head to be modified
36  * \param ref pointer to Cell_head
37  *
38  * \return NULL on success
39  */
40 void Rast_align_window(struct Cell_head *window, const struct Cell_head *ref)
41 {
42  G_debug(1, "Rast_align_window()");
43 
44  window->ns_res = ref->ns_res;
45  window->ew_res = ref->ew_res;
46  window->zone = ref->zone;
47  window->proj = ref->proj;
48 
49  G_debug(1, "before alignment:");
50  G_debug(1, "North: %.15g", window->north);
51  G_debug(1, "South: %.15g", window->south);
52  G_debug(1, "West: %.15g", window->west);
53  G_debug(1, "East: %.15g", window->east);
54 
55  window->north =
56  ref->north -
57  floor((ref->north - window->north) / ref->ns_res) * ref->ns_res;
58  window->south =
59  ref->south -
60  ceil((ref->south - window->south) / ref->ns_res) * ref->ns_res;
61  /* Rast_easting_to_col() wraps easting:
62  * east can become < west, or both west and east are shifted */
63  window->west = ref->west + floor((window->west - ref->west) / ref->ew_res) *
64  ref->ew_res;
65  window->east = ref->east +
66  ceil((window->east - ref->east) / ref->ew_res) * ref->ew_res;
67 
68  if (window->proj == PROJECTION_LL) {
69  while (window->north > 90.0 + window->ns_res / 2.0)
70  window->north -= window->ns_res;
71  while (window->south < -90.0 - window->ns_res / 2.0)
72  window->south += window->ns_res;
73  }
74 
75  G_debug(1, "after alignment:");
76  G_debug(1, "North: %.15g", window->north);
77  G_debug(1, "South: %.15g", window->south);
78  G_debug(1, "West: %.15g", window->west);
79  G_debug(1, "East: %.15g", window->east);
80 
81  G_adjust_Cell_head(window, 0, 0);
82 }
void Rast_align_window(struct Cell_head *window, const struct Cell_head *ref)
Align two regions.
Definition: align_window.c:40
void G_adjust_Cell_head(struct Cell_head *, int, int)
Adjust cell header.
Definition: adj_cellhd.c:51
int G_debug(int, const char *,...) __attribute__((format(printf
#define PROJECTION_LL
Projection code - Latitude-Longitude.
Definition: gis.h:129
2D/3D raster map header (used also for region)
Definition: gis.h:446
double ew_res
Resolution - east to west cell size for 2D data.
Definition: gis.h:482
double north
Extent coordinates (north)
Definition: gis.h:492
int zone
Projection zone (UTM)
Definition: gis.h:480
double east
Extent coordinates (east)
Definition: gis.h:496
double ns_res
Resolution - north to south cell size for 2D data.
Definition: gis.h:486
int proj
Projection code.
Definition: gis.h:478
double south
Extent coordinates (south)
Definition: gis.h:494
double west
Extent coordinates (west)
Definition: gis.h:498