area_poly1.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*!
  2. * \file lib/gis/area_poly1.c
  3. *
  4. * \brief GIS Library - Polygon area calculation routines.
  5. *
  6. * (C) 2001-2013 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 Original author CERL
  12. */
  13. #include <math.h>
  14. #include <grass/gis.h>
  15. #include "pi.h"
  16. #define TWOPI M_PI + M_PI
  17. static struct state {
  18. double QA, QB, QC;
  19. double QbarA, QbarB, QbarC, QbarD;
  20. double AE; /** a^2(1-e^2) */
  21. double Qp; /** Q at the north pole */
  22. double E; /** Area of the earth */
  23. } state;
  24. static struct state *st = &state;
  25. static double Q(double x)
  26. {
  27. double sinx, sinx2;
  28. sinx = sin(x);
  29. sinx2 = sinx * sinx;
  30. return sinx * (1 + sinx2 * (st->QA + sinx2 * (st->QB + sinx2 * st->QC)));
  31. }
  32. static double Qbar(double x)
  33. {
  34. double cosx, cosx2;
  35. cosx = cos(x);
  36. cosx2 = cosx * cosx;
  37. return cosx * (st->QbarA + cosx2 * (st->QbarB + cosx2 * (st->QbarC + cosx2 * st->QbarD)));
  38. }
  39. /*!
  40. * \brief Begin area calculations.
  41. *
  42. * This initializes the polygon area calculations for the
  43. * ellipsoid with semi-major axis <i>a</i> (in meters) and ellipsoid
  44. * eccentricity squared <i>e2</i>.
  45. *
  46. * \param a semi-major axis
  47. * \param e2 ellipsoid eccentricity squared
  48. */
  49. void G_begin_ellipsoid_polygon_area(double a, double e2)
  50. {
  51. double e4, e6;
  52. e4 = e2 * e2;
  53. e6 = e4 * e2;
  54. st->AE = a * a * (1 - e2);
  55. st->QA = (2.0 / 3.0) * e2;
  56. st->QB = (3.0 / 5.0) * e4;
  57. st->QC = (4.0 / 7.0) * e6;
  58. st->QbarA = -1.0 - (2.0 / 3.0) * e2 - (3.0 / 5.0) * e4 - (4.0 / 7.0) * e6;
  59. st->QbarB = (2.0 / 9.0) * e2 + (2.0 / 5.0) * e4 + (4.0 / 7.0) * e6;
  60. st->QbarC = -(3.0 / 25.0) * e4 - (12.0 / 35.0) * e6;
  61. st->QbarD = (4.0 / 49.0) * e6;
  62. st->Qp = Q(M_PI_2);
  63. st->E = 4 * M_PI * st->Qp * st->AE;
  64. if (st->E < 0.0)
  65. st->E = -st->E;
  66. }
  67. /*!
  68. * \brief Area of lat-long polygon.
  69. *
  70. * Returns the area in square meters of the polygon described by the
  71. * <i>n</i> pairs of <i>lat,long</i> vertices for latitude-longitude
  72. * grids.
  73. *
  74. * <b>Note:</b> This routine computes the area of a polygon on the
  75. * ellipsoid. The sides of the polygon are rhumb lines and, in general,
  76. * not geodesics. Each side is actually defined by a linear relationship
  77. * between latitude and longitude, i.e., on a rectangular/equidistant
  78. * cylindrical/Plate Carr{'e}e grid, the side would appear as a
  79. * straight line. For two consecutive vertices of the polygon,
  80. * (lat_1, long1) and (lat_2,long_2), the line joining them (i.e., the
  81. * polygon's side) is defined by:
  82. *
  83. \verbatim
  84. lat_2 - lat_1
  85. lat = lat_1 + (long - long_1) * ---------------
  86. long_2 - long_1
  87. \endverbatim
  88. *
  89. * where long_1 < long < long_2.
  90. * The values of QbarA, etc., are determined by the integration of
  91. * the Q function. Into www.integral-calculator.com, paste this
  92. * expression :
  93. *
  94. \verbatim
  95. sin(x)+ (2/3)e^2(sin(x))^3 + (3/5)e^4(sin(x))^5 + (4/7)e^6(sin(x))^7
  96. \endverbatim
  97. *
  98. * and you'll get their values. (Last checked 30 Oct 2013).
  99. *
  100. * This function correctly computes (within the limits of the series
  101. * approximation) the area of a quadrilateral on the ellipsoid when
  102. * two of its sides run along meridians and the other two sides run
  103. * along parallels of latitude.
  104. *
  105. * \param lon array of longitudes
  106. * \param lat array of latitudes
  107. * \param n number of lat,lon pairs
  108. *
  109. * \return area in square meters
  110. */
  111. double G_ellipsoid_polygon_area(const double *lon, const double *lat, int n)
  112. {
  113. double x1, y1, x2, y2, dx, dy;
  114. double Qbar1, Qbar2;
  115. double area;
  116. double thresh = 1e-6; /* threshold for dy, should be between 1e-4 and 1e-7 */
  117. x2 = Radians(lon[n - 1]);
  118. y2 = Radians(lat[n - 1]);
  119. Qbar2 = Qbar(y2);
  120. area = 0.0;
  121. while (--n >= 0) {
  122. x1 = x2;
  123. y1 = y2;
  124. Qbar1 = Qbar2;
  125. x2 = Radians(*lon++);
  126. y2 = Radians(*lat++);
  127. Qbar2 = Qbar(y2);
  128. if (x1 > x2)
  129. while (x1 - x2 > M_PI)
  130. x2 += TWOPI;
  131. else if (x2 > x1)
  132. while (x2 - x1 > M_PI)
  133. x1 += TWOPI;
  134. dx = x2 - x1;
  135. dy = y2 - y1;
  136. if (fabs(dy) > thresh) {
  137. /* account for different latitudes y1, y2 */
  138. area += dx * (st->Qp - (Qbar2 - Qbar1) / dy);
  139. /* original:
  140. * area += dx * st->Qp - (dx / dy) * (Qbar2 - Qbar1);
  141. */
  142. }
  143. else {
  144. /* latitudes y1, y2 are (nearly) identical */
  145. /* if y2 becomes similar to y1, i.e. y2 -> y1
  146. * Qbar2 - Qbar1 -> 0 and dy -> 0
  147. * (Qbar2 - Qbar1) / dy -> ?
  148. * (Qbar2 - Qbar1) / dy should approach Q((y1 + y2) / 2)
  149. * Metz 2017
  150. */
  151. area += dx * (st->Qp - Q((y1 + y2) / 2));
  152. }
  153. }
  154. if ((area *= st->AE) < 0.0)
  155. area = -area;
  156. /* kludge - if polygon circles the south pole the area will be
  157. * computed as if it cirlced the north pole. The correction is
  158. * the difference between total surface area of the earth and
  159. * the "north pole" area.
  160. */
  161. if (area > st->E)
  162. area = st->E;
  163. if (area > st->E / 2)
  164. area = st->E - area;
  165. return area;
  166. }