GRASS 8 Programmer's Manual 8.6.0dev(2026)-1878fdfec5
Loading...
Searching...
No Matches
wind_overlap.c
Go to the documentation of this file.
1/*!
2 * \file lib/gis/wind_overlap.c
3 *
4 * \brief GIS Library - Window overlap functions.
5 *
6 * SPDX-FileCopyrightText: 2001-2014 GRASS Development Team
7 * SPDX-License-Identifier: GPL-2.0-or-later
8 *
9 * \author GRASS Development Team
10 *
11 * \date 1999-2014
12 */
13
14#include <grass/gis.h>
15
16/**
17 * \brief Determines if a box overlays a map window.
18 *
19 * Given a map <b>window</b>, and a box of <b>N</b>,<b>S</b>,<b>E</b>,<b>W</b>
20 * does the box overlap the map <b>window</b>?<br>
21 *
22 * Note: knows about global wrap-around for lat-long.
23 *
24 * \param[in] window pointer to window structure
25 * \param[in] N north
26 * \param[in] S south
27 * \param[in] E east
28 * \param[in] W west
29 * \return 1 if box overlaps window
30 * \return 0 if box does not overlap window
31 */
32int G_window_overlap(const struct Cell_head *window, double N, double S,
33 double E, double W)
34{
35 if (window->north <= S)
36 return 0;
37 if (window->south >= N)
38 return 0;
39
40 if (window->proj == PROJECTION_LL) {
41 while (E < window->west) {
42 E += 360.0;
43 W += 360.0;
44 }
45 while (W > window->east) {
46 E -= 360.0;
47 W -= 360.0;
48 }
49 }
50
51 if (window->east <= W)
52 return 0;
53 if (window->west >= E)
54 return 0;
55
56 return 1;
57}
58
59/**
60 * \brief Determines percentage of box is contained in the <b>window</b>.
61 *
62 * This version returns the percentage (from 0 to 1) of the box
63 * contained in the window. This feature can be used during vector
64 * plotting to decide if it is more efficient to do a level-one
65 * read of the whole vector map, or to pay the price of a
66 * level-two startup so only those arcs that enter the window are
67 * actually read.
68 *
69 * \param[in] window pointer to window structure
70 * \param[in] N north
71 * \param[in] S south
72 * \param[in] E east
73 * \param[in] W west
74 * \return percentage of overlap
75 */
76double G_window_percentage_overlap(const struct Cell_head *window, double N,
77 double S, double E, double W)
78{
79 double V, H;
80 double n, s, e, w;
81 double shift;
82
83 /* vertical height of the box that overlaps the window */
84 if ((n = window->north) > N)
85 n = N;
86 if ((s = window->south) < S)
87 s = S;
88 V = n - s;
89
90 if (N == S) {
91 V = (N < window->north && N > window->south);
92 N = 1;
93 S = 0;
94 }
95
96 if (V <= 0.0)
97 return 0.0;
98
99 /* global wrap-around, part 1 */
100 if (window->proj == PROJECTION_LL) {
101 shift = 0.0;
102 while (E + shift > window->east)
103 shift -= 360.0;
104 while (E + shift < window->west)
105 shift += 360.0;
106 E += shift;
107 W += shift;
108 }
109
110 /* horizontal width of the box that overlaps the window */
111 if ((e = window->east) > E)
112 e = E;
113 if ((w = window->west) < W)
114 w = W;
115 H = e - w;
116 if (W == E)
117 H = (E > window->west && E < window->east);
118 if (H <= 0.0)
119 return 0.0;
120
121 /* global wrap-around, part 2 */
122 if (window->proj == PROJECTION_LL) {
123 shift = 0.0;
124 while (W + shift < window->west)
125 shift += 360.0;
126 while (W + shift > window->east)
127 shift -= 360.0;
128 if (shift) {
129 E += shift;
130 W += shift;
131 if ((e = window->east) > E)
132 e = E;
133 if ((w = window->west) < W)
134 w = W;
135 H += e - w;
136 }
137 }
138 if (W == E) {
139 W = 0;
140 E = 1;
141 }
142
143 return (H * V) / ((N - S) * (E - W));
144}
#define H
Definition as177.c:14
#define N
#define PROJECTION_LL
Projection code - Latitude-Longitude.
Definition gis.h:126
#define W
Definition ogsf.h:144
2D/3D raster map header (used also for region)
Definition gis.h:443
double north
Extent coordinates (north)
Definition gis.h:489
double east
Extent coordinates (east)
Definition gis.h:493
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
double G_window_percentage_overlap(const struct Cell_head *window, double N, double S, double E, double W)
Determines percentage of box is contained in the window.
int G_window_overlap(const struct Cell_head *window, double N, double S, double E, double W)
Determines if a box overlays a map window.