GRASS GIS 7 Programmer's Manual  7.2.1(2017)-exported
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 (V <= 0.0)
98  return 0.0;
99 
100  /* global wrap-around, part 1 */
101  if (window->proj == PROJECTION_LL) {
102  shift = 0.0;
103  while (E + shift > window->east)
104  shift -= 360.0;
105  while (E + shift < window->west)
106  shift += 360.0;
107  E += shift;
108  W += shift;
109  }
110 
111  /* horizontal width of the box that overlaps the window */
112  if ((e = window->east) > E)
113  e = E;
114  if ((w = window->west) < W)
115  w = W;
116  H = e - w;
117  if (H <= 0.0)
118  return 0.0;
119 
120  /* global wrap-around, part 2 */
121  if (window->proj == PROJECTION_LL) {
122  shift = 0.0;
123  while (W + shift < window->west)
124  shift += 360.0;
125  while (W + shift > window->east)
126  shift -= 360.0;
127  if (shift) {
128  E += shift;
129  W += shift;
130  if ((e = window->east) > E)
131  e = E;
132  if ((w = window->west) < W)
133  w = W;
134  H += e - w;
135  }
136  }
137 
138  return (H * V) / ((N - S) * (E - W));
139 }
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 H
Definition: as177.c:15
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