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