getGeographicArea function

double getGeographicArea(
  1. List<List<double>> ring
)

Computes the area of a geographic polygon ring using the WGS84 ellipsoid.

This function calculates the area using the method of spherical excess. This method requires the coordinates to be geographic (longitude and latitude in degrees). It takes into account the Earth's ellipsoidal shape and gives accurate results for large polygons.

The polygon ring is represented as a list of points, where each point is a list of two doubles (longitude, latitude). The points must be ordered either clockwise or counter-clockwise and the first point should be the same as the last point to ensure the polygon ring is closed.

Throws ArgumentError if the input is not a list of at least four points.

Example:

var ring = [
  [0.0, 0.0],
  [1.0, 0.0],
  [1.0, 1.0],
  [0.0, 1.0],
  [0.0, 0.0] // repeat the first point to close the polygon
];
print(getGeographicArea(ring));  // prints the area in square meters

ring: A list of points representing the polygon ring. Each point is a list of two doubles (longitude, latitude).

Returns the area of the polygon ring in square meters.

Implementation

double getGeographicArea(List<List<double>> ring) {
  if (ring.length < 4 || !numListEquals(ring.first, ring.last)) {
    throw ArgumentError(
        "Input must be a list of at least four points, where the first and last points are the same.");
  }

  const double wgs84Radius = 6378137.0;
  const double degToRad = pi / 180.0;
  var area = 0.0;
  for (var i = 0; i < ring.length - 1; i++) {
    var p1 = ring[i];
    var p2 = ring[i + 1];
    area += (p2[0] * degToRad - p1[0] * degToRad) *
        (2.0 + sin(p1[1] * degToRad) + sin(p2[1] * degToRad));
  }
  area = area * wgs84Radius * wgs84Radius / 2.0;
  return area.abs();
}