diff --git a/src/ol/control/scalelinecontrol.js b/src/ol/control/scalelinecontrol.js index 7c043e616c..0472972131 100644 --- a/src/ol/control/scalelinecontrol.js +++ b/src/ol/control/scalelinecontrol.js @@ -6,13 +6,13 @@ goog.require('goog.asserts'); goog.require('goog.dom'); goog.require('goog.dom.TagName'); goog.require('goog.events'); -goog.require('goog.math'); goog.require('goog.style'); goog.require('ol'); goog.require('ol.Object'); goog.require('ol.TransformFunction'); goog.require('ol.control.Control'); goog.require('ol.css'); +goog.require('ol.math'); goog.require('ol.proj'); goog.require('ol.proj.METERS_PER_UNIT'); goog.require('ol.proj.Units'); @@ -218,7 +218,7 @@ ol.control.ScaleLine.prototype.updateElement_ = function() { // Convert pointResolution from degrees to meters this.toEPSG4326_ = null; - cosLatitude = Math.cos(goog.math.toRadians(center[1])); + cosLatitude = Math.cos(ol.math.toRadians(center[1])); pointResolution *= Math.PI * cosLatitude * ol.sphere.NORMAL.radius / 180; projectionUnits = ol.proj.Units.METERS; @@ -230,7 +230,7 @@ ol.control.ScaleLine.prototype.updateElement_ = function() { this.toEPSG4326_ = ol.proj.getTransformFromProjections( projection, ol.proj.get('EPSG:4326')); } - cosLatitude = Math.cos(goog.math.toRadians(this.toEPSG4326_(center)[1])); + cosLatitude = Math.cos(ol.math.toRadians(this.toEPSG4326_(center)[1])); var radius = ol.sphere.NORMAL.radius; goog.asserts.assert(ol.proj.METERS_PER_UNIT[projectionUnits], 'Meters per unit should be defined for the projection unit'); diff --git a/src/ol/deviceorientation.js b/src/ol/deviceorientation.js index ac08dbd56c..a1724f559b 100644 --- a/src/ol/deviceorientation.js +++ b/src/ol/deviceorientation.js @@ -2,10 +2,10 @@ goog.provide('ol.DeviceOrientation'); goog.provide('ol.DeviceOrientationProperty'); goog.require('goog.events'); -goog.require('goog.math'); goog.require('ol'); goog.require('ol.Object'); goog.require('ol.has'); +goog.require('ol.math'); /** @@ -115,24 +115,24 @@ ol.DeviceOrientation.prototype.orientationChange_ = function(browserEvent) { var event = /** @type {DeviceOrientationEvent} */ (browserEvent.getBrowserEvent()); if (event.alpha !== null) { - var alpha = goog.math.toRadians(event.alpha); + var alpha = ol.math.toRadians(event.alpha); this.set(ol.DeviceOrientationProperty.ALPHA, alpha); // event.absolute is undefined in iOS. if (goog.isBoolean(event.absolute) && event.absolute) { this.set(ol.DeviceOrientationProperty.HEADING, alpha); } else if (goog.isNumber(event.webkitCompassHeading) && event.webkitCompassAccuracy != -1) { - var heading = goog.math.toRadians(event.webkitCompassHeading); + var heading = ol.math.toRadians(event.webkitCompassHeading); this.set(ol.DeviceOrientationProperty.HEADING, heading); } } if (event.beta !== null) { this.set(ol.DeviceOrientationProperty.BETA, - goog.math.toRadians(event.beta)); + ol.math.toRadians(event.beta)); } if (event.gamma !== null) { this.set(ol.DeviceOrientationProperty.GAMMA, - goog.math.toRadians(event.gamma)); + ol.math.toRadians(event.gamma)); } this.changed(); }; diff --git a/src/ol/format/kmlformat.js b/src/ol/format/kmlformat.js index 7978721c86..3cf88b9e19 100644 --- a/src/ol/format/kmlformat.js +++ b/src/ol/format/kmlformat.js @@ -10,7 +10,6 @@ goog.require('goog.Uri'); goog.require('goog.array'); goog.require('goog.asserts'); goog.require('goog.dom.NodeType'); -goog.require('goog.math'); goog.require('ol'); goog.require('ol.Feature'); goog.require('ol.FeatureStyleFunction'); @@ -30,6 +29,7 @@ goog.require('ol.geom.MultiPoint'); goog.require('ol.geom.MultiPolygon'); goog.require('ol.geom.Point'); goog.require('ol.geom.Polygon'); +goog.require('ol.math'); goog.require('ol.proj'); goog.require('ol.style.Fill'); goog.require('ol.style.Icon'); @@ -533,7 +533,7 @@ ol.format.KML.IconStyleParser_ = function(node, objectStack) { var heading = /** @type {number} */ (object['heading']); if (heading !== undefined) { - rotation = goog.math.toRadians(heading); + rotation = ol.math.toRadians(heading); } var scale = /** @type {number|undefined} */ diff --git a/src/ol/geolocation.js b/src/ol/geolocation.js index cb72ecc502..001dd8c15c 100644 --- a/src/ol/geolocation.js +++ b/src/ol/geolocation.js @@ -5,12 +5,12 @@ goog.provide('ol.GeolocationProperty'); goog.require('goog.events'); goog.require('goog.events.EventType'); -goog.require('goog.math'); goog.require('ol.Coordinate'); goog.require('ol.Object'); goog.require('ol.geom.Geometry'); goog.require('ol.geom.Polygon'); goog.require('ol.has'); +goog.require('ol.math'); goog.require('ol.proj'); goog.require('ol.sphere.WGS84'); @@ -160,7 +160,7 @@ ol.Geolocation.prototype.positionChange_ = function(position) { coords.altitudeAccuracy === null ? undefined : coords.altitudeAccuracy); this.set(ol.GeolocationProperty.HEADING, coords.heading === null ? - undefined : goog.math.toRadians(coords.heading)); + undefined : ol.math.toRadians(coords.heading)); if (!this.position_) { this.position_ = [coords.longitude, coords.latitude]; } else { diff --git a/src/ol/geom/flat/geodesicflatgeom.js b/src/ol/geom/flat/geodesicflatgeom.js index c39811bccd..3054f9f8c6 100644 --- a/src/ol/geom/flat/geodesicflatgeom.js +++ b/src/ol/geom/flat/geodesicflatgeom.js @@ -1,7 +1,6 @@ goog.provide('ol.geom.flat.geodesic'); goog.require('goog.asserts'); -goog.require('goog.math'); goog.require('goog.object'); goog.require('ol.TransformFunction'); goog.require('ol.math'); @@ -102,12 +101,12 @@ ol.geom.flat.geodesic.greatCircleArc = function( var geoProjection = ol.proj.get('EPSG:4326'); - var cosLat1 = Math.cos(goog.math.toRadians(lat1)); - var sinLat1 = Math.sin(goog.math.toRadians(lat1)); - var cosLat2 = Math.cos(goog.math.toRadians(lat2)); - var sinLat2 = Math.sin(goog.math.toRadians(lat2)); - var cosDeltaLon = Math.cos(goog.math.toRadians(lon2 - lon1)); - var sinDeltaLon = Math.sin(goog.math.toRadians(lon2 - lon1)); + var cosLat1 = Math.cos(ol.math.toRadians(lat1)); + var sinLat1 = Math.sin(ol.math.toRadians(lat1)); + var cosLat2 = Math.cos(ol.math.toRadians(lat2)); + var sinLat2 = Math.sin(ol.math.toRadians(lat2)); + var cosDeltaLon = Math.cos(ol.math.toRadians(lon2 - lon1)); + var sinDeltaLon = Math.sin(ol.math.toRadians(lon2 - lon1)); var d = sinLat1 * sinLat2 + cosLat1 * cosLat2 * cosDeltaLon; return ol.geom.flat.geodesic.line_( @@ -126,10 +125,10 @@ ol.geom.flat.geodesic.greatCircleArc = function( var x = cosLat1 * sinLat2 - sinLat1 * cosLat2 * cosDeltaLon; var theta = Math.atan2(y, x); var lat = Math.asin(sinLat1 * cosD + cosLat1 * sinD * Math.cos(theta)); - var lon = goog.math.toRadians(lon1) + + var lon = ol.math.toRadians(lon1) + Math.atan2(Math.sin(theta) * sinD * cosLat1, cosD - sinLat1 * Math.sin(lat)); - return [goog.math.toDegrees(lon), goog.math.toDegrees(lat)]; + return [ol.math.toDegrees(lon), ol.math.toDegrees(lat)]; }, ol.proj.getTransform(geoProjection, projection), squaredTolerance); }; diff --git a/src/ol/math.js b/src/ol/math.js index 0d2e582d44..a2659c34d2 100644 --- a/src/ol/math.js +++ b/src/ol/math.js @@ -123,3 +123,25 @@ ol.math.tanh = function(x) { var expMinusTwoX = Math.exp(-2 * x); return (1 - expMinusTwoX) / (1 + expMinusTwoX); }; + + +/** + * Converts radians to to degrees. + * + * @param {number} angleInRadians Angle in radians. + * @return {number} Angle in degrees. + */ +ol.math.toDegrees = function(angleInRadians) { + return angleInRadians * 180 / Math.PI; +}; + + +/** + * Converts degrees to radians. + * + * @param {number} angleInDegrees Angle in degrees. + * @return {number} Angle in radians. + */ +ol.math.toRadians = function(angleInDegrees) { + return angleInDegrees * Math.PI / 180; +}; diff --git a/src/ol/rotationconstraint.js b/src/ol/rotationconstraint.js index 69996cfaf4..b660a36444 100644 --- a/src/ol/rotationconstraint.js +++ b/src/ol/rotationconstraint.js @@ -1,7 +1,7 @@ goog.provide('ol.RotationConstraint'); goog.provide('ol.RotationConstraintType'); -goog.require('goog.math'); +goog.require('ol.math'); /** @@ -66,7 +66,7 @@ ol.RotationConstraint.createSnapToN = function(n) { * @return {ol.RotationConstraintType} Rotation constraint. */ ol.RotationConstraint.createSnapToZero = function(opt_tolerance) { - var tolerance = opt_tolerance || goog.math.toRadians(5); + var tolerance = opt_tolerance || ol.math.toRadians(5); return ( /** * @param {number|undefined} rotation Rotation. diff --git a/src/ol/sphere/sphere.js b/src/ol/sphere/sphere.js index 259e6d3bfb..ebd6f96592 100644 --- a/src/ol/sphere/sphere.js +++ b/src/ol/sphere/sphere.js @@ -7,7 +7,7 @@ goog.provide('ol.Sphere'); -goog.require('goog.math'); +goog.require('ol.math'); @@ -57,9 +57,9 @@ ol.Sphere.prototype.geodesicArea = function(coordinates) { var y1 = coordinates[len - 1][1]; for (var i = 0; i < len; i++) { var x2 = coordinates[i][0], y2 = coordinates[i][1]; - area += goog.math.toRadians(x2 - x1) * - (2 + Math.sin(goog.math.toRadians(y1)) + - Math.sin(goog.math.toRadians(y2))); + area += ol.math.toRadians(x2 - x1) * + (2 + Math.sin(ol.math.toRadians(y1)) + + Math.sin(ol.math.toRadians(y2))); x1 = x2; y1 = y2; } @@ -76,10 +76,10 @@ ol.Sphere.prototype.geodesicArea = function(coordinates) { * @api */ ol.Sphere.prototype.haversineDistance = function(c1, c2) { - var lat1 = goog.math.toRadians(c1[1]); - var lat2 = goog.math.toRadians(c2[1]); + var lat1 = ol.math.toRadians(c1[1]); + var lat2 = ol.math.toRadians(c2[1]); var deltaLatBy2 = (lat2 - lat1) / 2; - var deltaLonBy2 = goog.math.toRadians(c2[0] - c1[0]) / 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); @@ -97,8 +97,8 @@ ol.Sphere.prototype.haversineDistance = function(c1, c2) { * @return {ol.Coordinate} The target point. */ ol.Sphere.prototype.offset = function(c1, distance, bearing) { - var lat1 = goog.math.toRadians(c1[1]); - var lon1 = goog.math.toRadians(c1[0]); + 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) + @@ -106,5 +106,5 @@ ol.Sphere.prototype.offset = function(c1, distance, 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 [goog.math.toDegrees(lon), goog.math.toDegrees(lat)]; + return [ol.math.toDegrees(lon), ol.math.toDegrees(lat)]; }; diff --git a/test/spec/ol/math.test.js b/test/spec/ol/math.test.js index 44b41ccc23..fd0796329d 100644 --- a/test/spec/ol/math.test.js +++ b/test/spec/ol/math.test.js @@ -196,4 +196,31 @@ describe('ol.math.tanh', function() { }); +describe('ol.math.toDegrees', function() { + it('returns the correct value at -π', function() { + expect(ol.math.toDegrees(-Math.PI)).to.be(-180); + }); + it('returns the correct value at 0', function() { + expect(ol.math.toDegrees(0)).to.be(0); + }); + it('returns the correct value at π', function() { + expect(ol.math.toDegrees(Math.PI)).to.be(180); + }); +}); + + +describe('ol.math.toRadians', function() { + it('returns the correct value at -180', function() { + expect(ol.math.toRadians(-180)).to.be(-Math.PI); + }); + it('returns the correct value at 0', function() { + expect(ol.math.toRadians(0)).to.be(0); + }); + it('returns the correct value at 180', function() { + expect(ol.math.toRadians(180)).to.be(Math.PI); + }); +}); + + + goog.require('ol.math');