GRASS 8 Programmer's Manual 8.6.0dev(2026)-4bb960b182
Loading...
Searching...
No Matches
area_poly1.c
Go to the documentation of this file.
1/*!
2 * \file lib/gis/area_poly1.c
3 *
4 * \brief GIS Library - Polygon area calculation routines.
5 *
6 * SPDX-FileCopyrightText: 2001-2013 GRASS Development Team
7 * SPDX-License-Identifier: GPL-2.0-or-later
8 *
9 * \author Original author CERL
10 */
11
12#include <math.h>
13#include <grass/gis.h>
14#include <geodesic.h>
15#include "pi.h"
16
17#define TWOPI M_PI + M_PI
18
19#define USE_GEOGRAPHICLIB 1
20
21static struct state {
22#ifndef USE_GEOGRAPHICLIB
23 double QA, QB, QC;
24 double QbarA, QbarB, QbarC, QbarD;
25
26 double AE; /** a^2(1-e^2) */
27
28 double Qp; /** Q at the north pole */
29
30 double E; /** Area of the earth */
31
32#else
33 struct geod_geodesic g; /** GeographicLib */
34#endif
35} state;
36
37static struct state *st = &state;
38
39#ifndef USE_GEOGRAPHICLIB
40static double Q(double x)
41{
42 double sinx, sinx2;
43
44 sinx = sin(x);
45 sinx2 = sinx * sinx;
46
47 return sinx * (1 + sinx2 * (st->QA + sinx2 * (st->QB + sinx2 * st->QC)));
48}
49
50static double Qbar(double x)
51{
52 double cosx, cosx2;
53
54 cosx = cos(x);
55 cosx2 = cosx * cosx;
56
57 return cosx *
58 (st->QbarA +
59 cosx2 * (st->QbarB + cosx2 * (st->QbarC + cosx2 * st->QbarD)));
60}
61#endif
62
63/*!
64 * \brief Begin area calculations.
65 *
66 * This initializes the polygon area calculations for the
67 * ellipsoid with semi-major axis <i>a</i> (in meters) and ellipsoid
68 * eccentricity squared <i>e2</i>.
69 *
70 * \param a semi-major axis
71 * \param e2 ellipsoid eccentricity squared
72 */
73void G_begin_ellipsoid_polygon_area(double a, double e2)
74{
75#ifdef USE_GEOGRAPHICLIB
76 double f;
77
78 /* GeographicLib */
80 f = 1.0 - sqrt(1.0 - e2);
81 geod_init(&st->g, a, f);
82
83#else
84 double e4, e6;
85
86 /* GRASS native */
87 e4 = e2 * e2;
88 e6 = e4 * e2;
89
90 st->AE = a * a * (1 - e2);
91
92 st->QA = (2.0 / 3.0) * e2;
93 st->QB = (3.0 / 5.0) * e4;
94 st->QC = (4.0 / 7.0) * e6;
95
96 st->QbarA = -1.0 - (2.0 / 3.0) * e2 - (3.0 / 5.0) * e4 - (4.0 / 7.0) * e6;
97 st->QbarB = (2.0 / 9.0) * e2 + (2.0 / 5.0) * e4 + (4.0 / 7.0) * e6;
98 st->QbarC = -(3.0 / 25.0) * e4 - (12.0 / 35.0) * e6;
99 st->QbarD = (4.0 / 49.0) * e6;
100
101 st->Qp = Q(M_PI_2);
102 st->E = 4 * M_PI * st->Qp * st->AE;
103 if (st->E < 0.0)
104 st->E = -st->E;
105#endif
106}
107
108/*!
109 * \brief Area of lat-long polygon.
110 *
111 * Returns the area in square meters of the polygon described by the
112 * <i>n</i> pairs of <i>lat,long</i> vertices for latitude-longitude
113 * grids.
114 *
115 * <b>Note:</b> This routine computes the area of a polygon on the
116 * ellipsoid. The sides of the polygon are rhumb lines and, in general,
117 * not geodesics. Each side is actually defined by a linear relationship
118 * between latitude and longitude, i.e., on a rectangular/equidistant
119 * cylindrical/Plate Carr{'e}e grid, the side would appear as a
120 * straight line. For two consecutive vertices of the polygon,
121 * (lat_1, long1) and (lat_2,long_2), the line joining them (i.e., the
122 * polygon's side) is defined by:
123 *
124 \verbatim
125 lat_2 - lat_1
126 lat = lat_1 + (long - long_1) * ---------------
127 long_2 - long_1
128 \endverbatim
129 *
130 * where long_1 < long < long_2.
131 * The values of QbarA, etc., are determined by the integration of
132 * the Q function. Into www.integral-calculator.com, paste this
133 * expression :
134 *
135 \verbatim
136 sin(x)+ (2/3)e^2(sin(x))^3 + (3/5)e^4(sin(x))^5 + (4/7)e^6(sin(x))^7
137 \endverbatim
138 *
139 * and you'll get their values. (Last checked 30 Oct 2013).
140 *
141 * This function correctly computes (within the limits of the series
142 * approximation) the area of a quadrilateral on the ellipsoid when
143 * two of its sides run along meridians and the other two sides run
144 * along parallels of latitude.
145 *
146 * \param lon array of longitudes
147 * \param lat array of latitudes
148 * \param n number of lat,lon pairs
149 *
150 * \return area in square meters
151 */
152double G_ellipsoid_polygon_area(const double *lon, const double *lat, int n)
153{
154 double area;
155
156#ifdef USE_GEOGRAPHICLIB
157 /* GeographicLib */
158 struct geod_polygon p;
159 double pP;
160 int i;
161
163 /* GeographicLib does not need a closed ring,
164 * see example for geod_polygonarea() in geodesic.h */
165 /* add points in reverse order */
166 i = n;
167 while (--i)
168 geod_polygon_addpoint(&st->g, &p, (double)lat[i], (double)lon[i]);
169 /* The area returned is signed
170 * with counter-clockwise traversal being treated as positive. */
171 geod_polygon_compute(&st->g, &p, FALSE, TRUE, &area, &pP);
172 area = fabs(area);
173
174#else
175 double x1, y1, x2, y2, dx, dy;
176 double Qbar1, Qbar2;
177 /* threshold for dy, should be between 1e-4 and 1e-7 */
178 double thresh = 1e-6;
179
180 /* GRASS native */
181 x2 = Radians(lon[n - 1]);
182 y2 = Radians(lat[n - 1]);
183 Qbar2 = Qbar(y2);
184
185 area = 0.0;
186
187 while (--n >= 0) {
188 x1 = x2;
189 y1 = y2;
190 Qbar1 = Qbar2;
191
192 x2 = Radians(*lon++);
193 y2 = Radians(*lat++);
194 Qbar2 = Qbar(y2);
195
196 if (x1 > x2)
197 while (x1 - x2 > M_PI)
198 x2 += TWOPI;
199 else if (x2 > x1)
200 while (x2 - x1 > M_PI)
201 x1 += TWOPI;
202
203 dx = x2 - x1;
204 dy = y2 - y1;
205
206 if (fabs(dy) > thresh) {
207 /* account for different latitudes y1, y2 */
208 area += dx * (st->Qp - (Qbar2 - Qbar1) / dy);
209 /* original:
210 * area += dx * st->Qp - (dx / dy) * (Qbar2 - Qbar1);
211 */
212 }
213 else {
214 /* latitudes y1, y2 are (nearly) identical */
215 /* if y2 becomes similar to y1, i.e. y2 -> y1
216 * Qbar2 - Qbar1 -> 0 and dy -> 0
217 * (Qbar2 - Qbar1) / dy -> ?
218 * (Qbar2 - Qbar1) / dy should approach Q((y1 + y2) / 2)
219 * Metz 2017
220 */
221 area += dx * (st->Qp - Q((y1 + y2) / 2));
222 }
223 }
224 if ((area *= st->AE) < 0.0)
225 area = -area;
226
227 /* kludge - if polygon circles the south pole the area will be
228 * computed as if it circled the north pole. The correction is
229 * the difference between total surface area of the earth and
230 * the "north pole" area.
231 */
232 if (area > st->E)
233 area = st->E;
234 if (area > st->E / 2)
235 area = st->E - area;
236#endif
237
238 return area;
239}
#define TWOPI
Definition area_poly1.c:17
void G_begin_ellipsoid_polygon_area(double a, double e2)
Begin area calculations.
Definition area_poly1.c:73
double G_ellipsoid_polygon_area(const double *lon, const double *lat, int n)
Area of lat-long polygon.
Definition area_poly1.c:152
int G_get_ellipsoid_parameters(double *, double *)
get ellipsoid parameters
Definition get_ellipse.c:64
#define M_PI_2
Definition gis.h:157
#define TRUE
Definition gis.h:75
#define FALSE
Definition gis.h:79
#define M_PI
Definition gis.h:154
float g
Definition named_colr.c:7
#define Radians(x)
Definition pi.h:6