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