geoBounds function

List<List<num>> geoBounds(
  1. Map object
)

Returns the spherical bounding box for the specified GeoJSON object.

The bounding box is represented by a two-dimensional array: [[left, bottom], [right, top]], where left is the minimum longitude, bottom is the minimum latitude, right is maximum longitude, and top is the maximum latitude. All coordinates are given in degrees. (Note that in projected planar coordinates, the minimum latitude is typically the maximum y-value, and the maximum latitude is typically the minimum y-value.) This is the spherical equivalent of GeoPath.bounds.

Implementation

List<List<num>> geoBounds(Map object) {
  int i, n;
  List<num> a, b;
  List<List<num>> merged;
  num deltaMax, delta;

  _phi1 = _lambda1 = -(_lambda0 = _phi0 = double.infinity);
  _ranges = [];
  _boundsStream(object);

  // First, sort ranges by their minimum longitudes.
  if ((n = _ranges!.length) != 0) {
    _ranges!.sort(_rangeCompare);

    // Then, merge any ranges that overlap.
    a = _ranges![0];
    merged = [a];
    for (i = 1; i < n; ++i) {
      b = _ranges![i];
      if (_rangeContains(a, b[0]) || _rangeContains(a, b[1])) {
        if (_angle(a[0], b[1]) > _angle(a[0], a[1])) a[1] = b[1];
        if (_angle(b[0], a[1]) > _angle(a[0], a[1])) a[0] = b[0];
      } else {
        merged.add(a = b);
      }
    }

    // Finally, find the largest gap between the merged ranges.
    // The final bounding box will be the inverse of this gap.
    deltaMax = double.negativeInfinity;
    n = merged.length - 1;
    a = merged[n];
    for ((i = 0); i <= n; a = b, ++i) {
      b = merged[i];
      if ((delta = _angle(a[1], b[0])) > deltaMax) {
        deltaMax = delta;
        _lambda0 = b[0];
        _lambda1 = a[1];
      }
    }
  }

  _ranges = _range = null;

  return _lambda0 == double.infinity || _phi0 == double.infinity
      ? [
          [double.nan, double.nan],
          [double.nan, double.nan]
        ]
      : [
          [_lambda0, _phi0],
          [_lambda1, _phi1]
        ];
}