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