GRASS GIS 8 Programmer's Manual  8.4.0dev(2024)-112dd97adf
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 -
58  floor((ref->north - window->north) / ref->ns_res) * ref->ns_res;
59  window->south =
60  ref->south -
61  ceil((ref->south - window->south) / ref->ns_res) * ref->ns_res;
62  /* Rast_easting_to_col() wraps easting:
63  * east can become < west, or both west and east are shifted */
64  window->west = ref->west + floor((window->west - ref->west) / ref->ew_res) *
65  ref->ew_res;
66  window->east = ref->east +
67  ceil((window->east - ref->east) / ref->ew_res) * ref->ew_res;
68 
69  if (window->proj == PROJECTION_LL) {
70  while (window->north > 90.0 + window->ns_res / 2.0)
71  window->north -= window->ns_res;
72  while (window->south < -90.0 - window->ns_res / 2.0)
73  window->south += window->ns_res;
74  }
75 
76  G_debug(1, "after alignment:");
77  G_debug(1, "North: %.15g", window->north);
78  G_debug(1, "South: %.15g", window->south);
79  G_debug(1, "West: %.15g", window->west);
80  G_debug(1, "East: %.15g", window->east);
81 
82  G_adjust_Cell_head(window, 0, 0);
83 }
void Rast_align_window(struct Cell_head *window, const struct Cell_head *ref)
Align two regions.
Definition: align_window.c:41
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:130
2D/3D raster map header (used also for region)
Definition: gis.h:437
double ew_res
Resolution - east to west cell size for 2D data.
Definition: gis.h:473
double north
Extent coordinates (north)
Definition: gis.h:483
int zone
Projection zone (UTM)
Definition: gis.h:471
double east
Extent coordinates (east)
Definition: gis.h:487
double ns_res
Resolution - north to south cell size for 2D data.
Definition: gis.h:477
int proj
Projection code.
Definition: gis.h:469
double south
Extent coordinates (south)
Definition: gis.h:485
double west
Extent coordinates (west)
Definition: gis.h:489