# all files / ol/ sphere.js

73.96% Statements 71/96
60% Branches 18/30
88.89% Functions 8/9
73.96% Lines 71/96

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289

    1×

    1×
    1×

    1×

    4×

    1×
    1×

    1×
    1005×

    1×

    1×

    1×
    8×
    8×
    8×
    8×
    8×
    8×
    8×
    8×

    2×

    4×
    4×
    4×

    1×
    1×
    2×

    1×

    1×
    1×
    2×

    1×

    8×

    1×
    6×
    6×
    18×

    6×

    1×
    1023×
    1023×
    1023×
    1023×
    1023×

    1023×

    1×
    1×
    1×
    1×
    1×
    1×
    1×
    1×
    1×

    1×
    1×
    1×
    1×
    1×

    1×

    1×

    1×
    2×
    2×
    2×
    2×
    652×
    652×

    652×
    652×

    2×

     /**
     * @license
     * Latitude/longitude spherical geodesy formulae taken from
     * http://www.movable-type.co.uk/scripts/latlong.html
     * Licensed under CC-BY-3.0.
     */

    goog.provide('ol.Sphere');

    goog.require('ol.math');
    goog.require('ol.geom.GeometryType');

    /**
     * @classdesc
     * Class to create objects that can be used with {@link
     * ol.geom.Polygon.circular}.
     *
     * For example to create a sphere whose radius is equal to the semi-major
     * axis of the WGS84 ellipsoid:
     *
     * ```js
     * var wgs84Sphere= new ol.Sphere(6378137);
     * ```
     *
     * @constructor
     * @param {number} radius Radius.
     * @api
     */
    ol.Sphere = function(radius) {

      /**
       * @type {number}
       */
      this.radius = radius;

    };

    /**
     * Returns the geodesic area for a list of coordinates.
     *
     * [Reference](https://trs-new.jpl.nasa.gov/handle/2014/40409)
     * Robert. G. Chamberlain and William H. Duquette, "Some Algorithms for
     * Polygons on a Sphere", JPL Publication 07-03, Jet Propulsion
     * Laboratory, Pasadena, CA, June 2007
     *
     * @param {Array.<ol.Coordinate>} coordinates List of coordinates of a linear
     * ring. If the ring is oriented clockwise, the area will be positive,
     * otherwise it will be negative.
     * @return {number} Area.
     * @api
     */
    ol.Sphere.prototype.geodesicArea = function(coordinates) {
      return ol.Sphere.getArea_(coordinates, this.radius);
    };

    /**
     * Returns the distance from c1 to c2 using the haversine formula.
     *
     * @param {ol.Coordinate} c1 Coordinate 1.
     * @param {ol.Coordinate} c2 Coordinate 2.
     * @return {number} Haversine distance.
     * @api
     */
    ol.Sphere.prototype.haversineDistance = function(c1, c2) {
      return ol.Sphere.getDistance_(c1, c2, this.radius);
    };

    /**
     * Returns the coordinate at the given distance and bearing from `c1`.
     *
     * @param {ol.Coordinate} c1 The origin point (`[lon, lat]` in degrees).
     * @param {number} distance The great-circle distance between the origin
     *     point and the target point.
     * @param {number} bearing The bearing (in radians).
     * @return {ol.Coordinate} The target point.
     */
    ol.Sphere.prototype.offset = function(c1, distance, bearing) {
      var lat1 = ol.math.toRadians(c1[1]);
      var lon1 = ol.math.toRadians(c1[0]);
      var dByR = distance / this.radius;
      var lat = Math.asin(
          Math.sin(lat1) * Math.cos(dByR) +
          Math.cos(lat1) * Math.sin(dByR) * Math.cos(bearing));
      var lon = lon1 + Math.atan2(
          Math.sin(bearing) * Math.sin(dByR) * Math.cos(lat1),
          Math.cos(dByR) - Math.sin(lat1) * Math.sin(lat));
      return [ol.math.toDegrees(lon), ol.math.toDegrees(lat)];
    };

    /**
     * The mean Earth radius (1/3 * (2a + b)) for the WGS84 ellipsoid.
     * https://en.wikipedia.org/wiki/Earth_radius#Mean_radius
     * @type {number}
     */
    ol.Sphere.DEFAULT_RADIUS = 6371008.8;

    /**
     * Get the spherical length of a geometry.  This length is the sum of the
     * great circle distances between coordinates.  For polygons, the length is
     * the sum of all rings.  For points, the length is zero.  For multi-part
     * geometries, the length is the sum of the length of each part.
     * @param {ol.geom.Geometry} geometry A geometry.
     * @param {olx.SphereMetricOptions=} opt_options Options for the length
     *     calculation.  By default, geometries are assumed to be in 'EPSG:3857'.
     *     You can change this by providing a `projection` option.
     * @return {number} The spherical length (in meters).
     * @api
     */
    ol.Sphere.getLength = function(geometry, opt_options) {
      var options = opt_options || {};
      var radius = options.radius || ol.Sphere.DEFAULT_RADIUS;
      var projection = options.projection || 'EPSG:3857';
      geometry = geometry.clone().transform(projection, 'EPSG:4326');
      var type = geometry.getType();
      var length = 0;
      var coordinates, coords, i, ii, j, jj;
      switch (type) {
        case ol.geom.GeometryType.POINT:
        case ol.geom.GeometryType.MULTI_POINT: {
          break;
        }
        case ol.geom.GeometryType.LINE_STRING:
        case ol.geom.GeometryType.LINEAR_RING: {
          coordinates = /** @type {ol.geom.SimpleGeometry} */ (geometry).getCoordinates();
          length = ol.Sphere.getLength_(coordinates, radius);
          break;
        }
        case ol.geom.GeometryType.MULTI_LINE_STRING:
        case ol.geom.GeometryType.POLYGON: {
          coordinates = /** @type {ol.geom.SimpleGeometry} */ (geometry).getCoordinates();
          for (i = 0, ii = coordinates.length; i < ii; ++i) {
            length += ol.Sphere.getLength_(coordinates[i], radius);
          }
          break;
        }
        case ol.geom.GeometryType.MULTI_POLYGON: {
          coordinates = /** @type {ol.geom.SimpleGeometry} */ (geometry).getCoordinates();
          for (i = 0, ii = coordinates.length; i < ii; ++i) {
            coords = coordinates[i];
            for (j = 0, jj = coords.length; j < jj; ++j) {
              length += ol.Sphere.getLength_(coords[j], radius);
            }
          }
          break;
        }
        case ol.geom.GeometryType.GEOMETRY_COLLECTION: {
          var geometries = /** @type {ol.geom.GeometryCollection} */ (geometry).getGeometries();
          for (i = 0, ii = geometries.length; i < ii; ++i) {
            length += ol.Sphere.getLength(geometries[i], opt_options);
          }
          break;
        }
        default: {
          throw new Error('Unsupported geometry type: ' + type);
        }
      }
      return length;
    };

    /**
     * Get the cumulative great circle length of linestring coordinates (geographic).
     * @param {Array} coordinates Linestring coordinates.
     * @param {number} radius The sphere radius to use.
     * @return {number} The length (in meters).
     */
    ol.Sphere.getLength_ = function(coordinates, radius) {
      var length = 0;
      for (var i = 0, ii = coordinates.length; i < ii - 1; ++i) {
        length += ol.Sphere.getDistance_(coordinates[i], coordinates[i + 1], radius);
      }
      return length;
    };

    /**
     * Get the great circle distance between two geographic coordinates.
     * @param {Array} c1 Starting coordinate.
     * @param {Array} c2 Ending coordinate.
     * @param {number} radius The sphere radius to use.
     * @return {number} The great circle distance between the points (in meters).
     */
    ol.Sphere.getDistance_ = function(c1, c2, radius) {
      var lat1 = ol.math.toRadians(c1[1]);
      var lat2 = ol.math.toRadians(c2[1]);
      var deltaLatBy2 = (lat2 - lat1) / 2;
      var deltaLonBy2 = ol.math.toRadians(c2[0] - c1[0]) / 2;
      var a = Math.sin(deltaLatBy2) * Math.sin(deltaLatBy2) +
          Math.sin(deltaLonBy2) * Math.sin(deltaLonBy2) *
          Math.cos(lat1) * Math.cos(lat2);
      return 2 * radius * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
    };

    /**
     * Get the spherical area of a geometry.  This is the area (in meters) assuming
     * that polygon edges are segments of great circles on a sphere.
     * @param {ol.geom.Geometry} geometry A geometry.
     * @param {olx.SphereMetricOptions=} opt_options Options for the area
     *     calculation.  By default, geometries are assumed to be in 'EPSG:3857'.
     *     You can change this by providing a `projection` option.
     * @return {number} The spherical area (in square meters).
     * @api
     */
    ol.Sphere.getArea = function(geometry, opt_options) {
      var options = opt_options || {};
      var radius = options.radius || ol.Sphere.DEFAULT_RADIUS;
      var projection = options.projection || 'EPSG:3857';
      geometry = geometry.clone().transform(projection, 'EPSG:4326');
      var type = geometry.getType();
      var area = 0;
      var coordinates, coords, i, ii, j, jj;
      switch (type) {
        case ol.geom.GeometryType.POINT:
        case ol.geom.GeometryType.MULTI_POINT:
        case ol.geom.GeometryType.LINE_STRING:
        case ol.geom.GeometryType.MULTI_LINE_STRING:
        case ol.geom.GeometryType.LINEAR_RING: {
          break;
        }
        case ol.geom.GeometryType.POLYGON: {
          coordinates = /** @type {ol.geom.Polygon} */ (geometry).getCoordinates();
          area = Math.abs(ol.Sphere.getArea_(coordinates[0], radius));
          for (i = 1, ii = coordinates.length; i < ii; ++i) {
            area -= Math.abs(ol.Sphere.getArea_(coordinates[i], radius));
          }
          break;
        }
        case ol.geom.GeometryType.MULTI_POLYGON: {
          coordinates = /** @type {ol.geom.SimpleGeometry} */ (geometry).getCoordinates();
          for (i = 0, ii = coordinates.length; i < ii; ++i) {
            coords = coordinates[i];
            area += Math.abs(ol.Sphere.getArea_(coords[0], radius));
            for (j = 1, jj = coords.length; j < jj; ++j) {
              area -= Math.abs(ol.Sphere.getArea_(coords[j], radius));
            }
          }
          break;
        }
        case ol.geom.GeometryType.GEOMETRY_COLLECTION: {
          var geometries = /** @type {ol.geom.GeometryCollection} */ (geometry).getGeometries();
          for (i = 0, ii = geometries.length; i < ii; ++i) {
            area += ol.Sphere.getArea(geometries[i], opt_options);
          }
          break;
        }
        default: {
          throw new Error('Unsupported geometry type: ' + type);
        }
      }
      return area;
    };

    /**
     * Returns the spherical area for a list of coordinates.
     *
     * [Reference](https://trs-new.jpl.nasa.gov/handle/2014/40409)
     * Robert. G. Chamberlain and William H. Duquette, "Some Algorithms for
     * Polygons on a Sphere", JPL Publication 07-03, Jet Propulsion
     * Laboratory, Pasadena, CA, June 2007
     *
     * @param {Array.<ol.Coordinate>} coordinates List of coordinates of a linear
     * ring. If the ring is oriented clockwise, the area will be positive,
     * otherwise it will be negative.
     * @param {number} radius The sphere radius.
     * @return {number} Area (in square meters).
     */
    ol.Sphere.getArea_ = function(coordinates, radius) {
      var area = 0, len = coordinates.length;
      var x1 = coordinates[len - 1][0];
      var y1 = coordinates[len - 1][1];
      for (var i = 0; i < len; i++) {
        var x2 = coordinates[i][0], y2 = coordinates[i][1];
        area += ol.math.toRadians(x2 - x1) *
            (2 + Math.sin(ol.math.toRadians(y1)) +
            Math.sin(ol.math.toRadians(y2)));
        x1 = x2;
        y1 = y2;
      }
      return area * radius * radius / 2.0;
    };