GRASS GIS 8 Programmer's Manual  8.2.2dev(2023)-3d2c704037
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 
41 void Rast_align_window(struct Cell_head *window, const struct Cell_head *ref)
42 {
43  G_debug(1, "Rast_align_window()");
44 
45  window->ns_res = ref->ns_res;
46  window->ew_res = ref->ew_res;
47  window->zone = ref->zone;
48  window->proj = ref->proj;
49 
50  G_debug(1, "before alignment:");
51  G_debug(1, "North: %.15g", window->north);
52  G_debug(1, "South: %.15g", window->south);
53  G_debug(1, "West: %.15g", window->west);
54  G_debug(1, "East: %.15g", window->east);
55 
56  window->north =
57  ref->north - floor((ref->north - window->north) / ref->ns_res) * ref->ns_res;
58  window->south =
59  ref->south - ceil((ref->south - window->south) / ref->ns_res) * ref->ns_res;
60  /* Rast_easting_to_col() wraps easting:
61  * east can become < west, or both west and east are shifted */
62  window->west =
63  ref->west + floor((window->west - ref->west) / ref->ew_res) * ref->ew_res;
64  window->east =
65  ref->east + ceil((window->east - ref->east) / ref->ew_res) * ref->ew_res;
66 
67  if (window->proj == PROJECTION_LL) {
68  while (window->north > 90.0 + window->ns_res / 2.0)
69  window->north -= window->ns_res;
70  while (window->south < -90.0 - window->ns_res / 2.0)
71  window->south += window->ns_res;
72  }
73 
74  G_debug(1, "after alignment:");
75  G_debug(1, "North: %.15g", window->north);
76  G_debug(1, "South: %.15g", window->south);
77  G_debug(1, "West: %.15g", window->west);
78  G_debug(1, "East: %.15g", window->east);
79 
80  G_adjust_Cell_head(window, 0, 0);
81 }
2D/3D raster map header (used also for region)
Definition: gis.h:423
double west
Extent coordinates (west)
Definition: gis.h:475
double north
Extent coordinates (north)
Definition: gis.h:469
double south
Extent coordinates (south)
Definition: gis.h:471
int zone
Projection zone (UTM)
Definition: gis.h:457
#define PROJECTION_LL
Projection code - Latitude-Longitude.
Definition: gis.h:116
int proj
Projection code.
Definition: gis.h:455
void G_adjust_Cell_head(struct Cell_head *, int, int)
Adjust cell header.
Definition: adj_cellhd.c:51
double ns_res
Resolution - north to south cell size for 2D data.
Definition: gis.h:463
double east
Extent coordinates (east)
Definition: gis.h:473
void Rast_align_window(struct Cell_head *window, const struct Cell_head *ref)
Align two regions.
Definition: align_window.c:41
double ew_res
Resolution - east to west cell size for 2D data.
Definition: gis.h:459
int G_debug(int, const char *,...) __attribute__((format(printf