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