# all files / ol/sphere/ sphere.js

78.57% Statements 22/28
100% Branches 0/0
75% Functions 3/4
78.57% Lines 22/28

    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

    1×

    1×

    1×

    3×

    1×
    1×
    1×
    1×
    1×
    326×
    326×

    326×
    326×

    1×

    1×
    987×
    987×
    987×
    987×
    987×

    987×

    1×

     /**
     * @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');

    /**
     * @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](http://trs-new.jpl.nasa.gov/dspace/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) {
      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 * this.radius * this.radius / 2.0;
    };

    /**
     * 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) {
      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 * this.radius * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
    };

    /**
     * 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)];
    };