Merge pull request #4259 from marcjansen/math-radians-degrees

Remove use of toDegrees/toRadians util functions
This commit is contained in:
Marc Jansen
2015-10-12 23:27:20 +02:00
9 changed files with 81 additions and 33 deletions

View File

@@ -5,13 +5,13 @@ goog.provide('ol.control.ScaleLineUnits');
goog.require('goog.asserts'); goog.require('goog.asserts');
goog.require('goog.dom'); goog.require('goog.dom');
goog.require('goog.events'); goog.require('goog.events');
goog.require('goog.math');
goog.require('goog.style'); goog.require('goog.style');
goog.require('ol'); goog.require('ol');
goog.require('ol.Object'); goog.require('ol.Object');
goog.require('ol.TransformFunction'); goog.require('ol.TransformFunction');
goog.require('ol.control.Control'); goog.require('ol.control.Control');
goog.require('ol.css'); goog.require('ol.css');
goog.require('ol.math');
goog.require('ol.proj'); goog.require('ol.proj');
goog.require('ol.proj.METERS_PER_UNIT'); goog.require('ol.proj.METERS_PER_UNIT');
goog.require('ol.proj.Units'); goog.require('ol.proj.Units');
@@ -217,7 +217,7 @@ ol.control.ScaleLine.prototype.updateElement_ = function() {
// Convert pointResolution from degrees to meters // Convert pointResolution from degrees to meters
this.toEPSG4326_ = null; 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; pointResolution *= Math.PI * cosLatitude * ol.sphere.NORMAL.radius / 180;
projectionUnits = ol.proj.Units.METERS; projectionUnits = ol.proj.Units.METERS;
@@ -229,7 +229,7 @@ ol.control.ScaleLine.prototype.updateElement_ = function() {
this.toEPSG4326_ = ol.proj.getTransformFromProjections( this.toEPSG4326_ = ol.proj.getTransformFromProjections(
projection, ol.proj.get('EPSG:4326')); 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; var radius = ol.sphere.NORMAL.radius;
goog.asserts.assert(ol.proj.METERS_PER_UNIT[projectionUnits], goog.asserts.assert(ol.proj.METERS_PER_UNIT[projectionUnits],
'Meters per unit should be defined for the projection unit'); 'Meters per unit should be defined for the projection unit');

View File

@@ -2,10 +2,10 @@ goog.provide('ol.DeviceOrientation');
goog.provide('ol.DeviceOrientationProperty'); goog.provide('ol.DeviceOrientationProperty');
goog.require('goog.events'); goog.require('goog.events');
goog.require('goog.math');
goog.require('ol'); goog.require('ol');
goog.require('ol.Object'); goog.require('ol.Object');
goog.require('ol.has'); goog.require('ol.has');
goog.require('ol.math');
/** /**
@@ -115,24 +115,24 @@ ol.DeviceOrientation.prototype.orientationChange_ = function(browserEvent) {
var event = /** @type {DeviceOrientationEvent} */ var event = /** @type {DeviceOrientationEvent} */
(browserEvent.getBrowserEvent()); (browserEvent.getBrowserEvent());
if (event.alpha !== null) { if (event.alpha !== null) {
var alpha = goog.math.toRadians(event.alpha); var alpha = ol.math.toRadians(event.alpha);
this.set(ol.DeviceOrientationProperty.ALPHA, alpha); this.set(ol.DeviceOrientationProperty.ALPHA, alpha);
// event.absolute is undefined in iOS. // event.absolute is undefined in iOS.
if (goog.isBoolean(event.absolute) && event.absolute) { if (goog.isBoolean(event.absolute) && event.absolute) {
this.set(ol.DeviceOrientationProperty.HEADING, alpha); this.set(ol.DeviceOrientationProperty.HEADING, alpha);
} else if (goog.isNumber(event.webkitCompassHeading) && } else if (goog.isNumber(event.webkitCompassHeading) &&
event.webkitCompassAccuracy != -1) { event.webkitCompassAccuracy != -1) {
var heading = goog.math.toRadians(event.webkitCompassHeading); var heading = ol.math.toRadians(event.webkitCompassHeading);
this.set(ol.DeviceOrientationProperty.HEADING, heading); this.set(ol.DeviceOrientationProperty.HEADING, heading);
} }
} }
if (event.beta !== null) { if (event.beta !== null) {
this.set(ol.DeviceOrientationProperty.BETA, this.set(ol.DeviceOrientationProperty.BETA,
goog.math.toRadians(event.beta)); ol.math.toRadians(event.beta));
} }
if (event.gamma !== null) { if (event.gamma !== null) {
this.set(ol.DeviceOrientationProperty.GAMMA, this.set(ol.DeviceOrientationProperty.GAMMA,
goog.math.toRadians(event.gamma)); ol.math.toRadians(event.gamma));
} }
this.changed(); this.changed();
}; };

View File

@@ -10,7 +10,6 @@ goog.require('goog.Uri');
goog.require('goog.array'); goog.require('goog.array');
goog.require('goog.asserts'); goog.require('goog.asserts');
goog.require('goog.dom.NodeType'); goog.require('goog.dom.NodeType');
goog.require('goog.math');
goog.require('ol'); goog.require('ol');
goog.require('ol.Feature'); goog.require('ol.Feature');
goog.require('ol.FeatureStyleFunction'); goog.require('ol.FeatureStyleFunction');
@@ -30,6 +29,7 @@ goog.require('ol.geom.MultiPoint');
goog.require('ol.geom.MultiPolygon'); goog.require('ol.geom.MultiPolygon');
goog.require('ol.geom.Point'); goog.require('ol.geom.Point');
goog.require('ol.geom.Polygon'); goog.require('ol.geom.Polygon');
goog.require('ol.math');
goog.require('ol.proj'); goog.require('ol.proj');
goog.require('ol.style.Fill'); goog.require('ol.style.Fill');
goog.require('ol.style.Icon'); goog.require('ol.style.Icon');
@@ -533,7 +533,7 @@ ol.format.KML.IconStyleParser_ = function(node, objectStack) {
var heading = /** @type {number} */ var heading = /** @type {number} */
(object['heading']); (object['heading']);
if (heading !== undefined) { if (heading !== undefined) {
rotation = goog.math.toRadians(heading); rotation = ol.math.toRadians(heading);
} }
var scale = /** @type {number|undefined} */ var scale = /** @type {number|undefined} */

View File

@@ -5,12 +5,12 @@ goog.provide('ol.GeolocationProperty');
goog.require('goog.events'); goog.require('goog.events');
goog.require('goog.events.EventType'); goog.require('goog.events.EventType');
goog.require('goog.math');
goog.require('ol.Coordinate'); goog.require('ol.Coordinate');
goog.require('ol.Object'); goog.require('ol.Object');
goog.require('ol.geom.Geometry'); goog.require('ol.geom.Geometry');
goog.require('ol.geom.Polygon'); goog.require('ol.geom.Polygon');
goog.require('ol.has'); goog.require('ol.has');
goog.require('ol.math');
goog.require('ol.proj'); goog.require('ol.proj');
goog.require('ol.sphere.WGS84'); goog.require('ol.sphere.WGS84');
@@ -160,7 +160,7 @@ ol.Geolocation.prototype.positionChange_ = function(position) {
coords.altitudeAccuracy === null ? coords.altitudeAccuracy === null ?
undefined : coords.altitudeAccuracy); undefined : coords.altitudeAccuracy);
this.set(ol.GeolocationProperty.HEADING, coords.heading === null ? this.set(ol.GeolocationProperty.HEADING, coords.heading === null ?
undefined : goog.math.toRadians(coords.heading)); undefined : ol.math.toRadians(coords.heading));
if (!this.position_) { if (!this.position_) {
this.position_ = [coords.longitude, coords.latitude]; this.position_ = [coords.longitude, coords.latitude];
} else { } else {

View File

@@ -1,7 +1,6 @@
goog.provide('ol.geom.flat.geodesic'); goog.provide('ol.geom.flat.geodesic');
goog.require('goog.asserts'); goog.require('goog.asserts');
goog.require('goog.math');
goog.require('goog.object'); goog.require('goog.object');
goog.require('ol.TransformFunction'); goog.require('ol.TransformFunction');
goog.require('ol.math'); goog.require('ol.math');
@@ -102,12 +101,12 @@ ol.geom.flat.geodesic.greatCircleArc = function(
var geoProjection = ol.proj.get('EPSG:4326'); var geoProjection = ol.proj.get('EPSG:4326');
var cosLat1 = Math.cos(goog.math.toRadians(lat1)); var cosLat1 = Math.cos(ol.math.toRadians(lat1));
var sinLat1 = Math.sin(goog.math.toRadians(lat1)); var sinLat1 = Math.sin(ol.math.toRadians(lat1));
var cosLat2 = Math.cos(goog.math.toRadians(lat2)); var cosLat2 = Math.cos(ol.math.toRadians(lat2));
var sinLat2 = Math.sin(goog.math.toRadians(lat2)); var sinLat2 = Math.sin(ol.math.toRadians(lat2));
var cosDeltaLon = Math.cos(goog.math.toRadians(lon2 - lon1)); var cosDeltaLon = Math.cos(ol.math.toRadians(lon2 - lon1));
var sinDeltaLon = Math.sin(goog.math.toRadians(lon2 - lon1)); var sinDeltaLon = Math.sin(ol.math.toRadians(lon2 - lon1));
var d = sinLat1 * sinLat2 + cosLat1 * cosLat2 * cosDeltaLon; var d = sinLat1 * sinLat2 + cosLat1 * cosLat2 * cosDeltaLon;
return ol.geom.flat.geodesic.line_( return ol.geom.flat.geodesic.line_(
@@ -126,10 +125,10 @@ ol.geom.flat.geodesic.greatCircleArc = function(
var x = cosLat1 * sinLat2 - sinLat1 * cosLat2 * cosDeltaLon; var x = cosLat1 * sinLat2 - sinLat1 * cosLat2 * cosDeltaLon;
var theta = Math.atan2(y, x); var theta = Math.atan2(y, x);
var lat = Math.asin(sinLat1 * cosD + cosLat1 * sinD * Math.cos(theta)); 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, Math.atan2(Math.sin(theta) * sinD * cosLat1,
cosD - sinLat1 * Math.sin(lat)); 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); }, ol.proj.getTransform(geoProjection, projection), squaredTolerance);
}; };

View File

@@ -123,3 +123,25 @@ ol.math.tanh = function(x) {
var expMinusTwoX = Math.exp(-2 * x); var expMinusTwoX = Math.exp(-2 * x);
return (1 - expMinusTwoX) / (1 + expMinusTwoX); 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;
};

View File

@@ -1,7 +1,7 @@
goog.provide('ol.RotationConstraint'); goog.provide('ol.RotationConstraint');
goog.provide('ol.RotationConstraintType'); 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. * @return {ol.RotationConstraintType} Rotation constraint.
*/ */
ol.RotationConstraint.createSnapToZero = function(opt_tolerance) { ol.RotationConstraint.createSnapToZero = function(opt_tolerance) {
var tolerance = opt_tolerance || goog.math.toRadians(5); var tolerance = opt_tolerance || ol.math.toRadians(5);
return ( return (
/** /**
* @param {number|undefined} rotation Rotation. * @param {number|undefined} rotation Rotation.

View File

@@ -7,7 +7,7 @@
goog.provide('ol.Sphere'); 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]; var y1 = coordinates[len - 1][1];
for (var i = 0; i < len; i++) { for (var i = 0; i < len; i++) {
var x2 = coordinates[i][0], y2 = coordinates[i][1]; var x2 = coordinates[i][0], y2 = coordinates[i][1];
area += goog.math.toRadians(x2 - x1) * area += ol.math.toRadians(x2 - x1) *
(2 + Math.sin(goog.math.toRadians(y1)) + (2 + Math.sin(ol.math.toRadians(y1)) +
Math.sin(goog.math.toRadians(y2))); Math.sin(ol.math.toRadians(y2)));
x1 = x2; x1 = x2;
y1 = y2; y1 = y2;
} }
@@ -76,10 +76,10 @@ ol.Sphere.prototype.geodesicArea = function(coordinates) {
* @api * @api
*/ */
ol.Sphere.prototype.haversineDistance = function(c1, c2) { ol.Sphere.prototype.haversineDistance = function(c1, c2) {
var lat1 = goog.math.toRadians(c1[1]); var lat1 = ol.math.toRadians(c1[1]);
var lat2 = goog.math.toRadians(c2[1]); var lat2 = ol.math.toRadians(c2[1]);
var deltaLatBy2 = (lat2 - lat1) / 2; 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) + var a = Math.sin(deltaLatBy2) * Math.sin(deltaLatBy2) +
Math.sin(deltaLonBy2) * Math.sin(deltaLonBy2) * Math.sin(deltaLonBy2) * Math.sin(deltaLonBy2) *
Math.cos(lat1) * Math.cos(lat2); Math.cos(lat1) * Math.cos(lat2);
@@ -97,8 +97,8 @@ ol.Sphere.prototype.haversineDistance = function(c1, c2) {
* @return {ol.Coordinate} The target point. * @return {ol.Coordinate} The target point.
*/ */
ol.Sphere.prototype.offset = function(c1, distance, bearing) { ol.Sphere.prototype.offset = function(c1, distance, bearing) {
var lat1 = goog.math.toRadians(c1[1]); var lat1 = ol.math.toRadians(c1[1]);
var lon1 = goog.math.toRadians(c1[0]); var lon1 = ol.math.toRadians(c1[0]);
var dByR = distance / this.radius; var dByR = distance / this.radius;
var lat = Math.asin( var lat = Math.asin(
Math.sin(lat1) * Math.cos(dByR) + Math.sin(lat1) * Math.cos(dByR) +
@@ -106,5 +106,5 @@ ol.Sphere.prototype.offset = function(c1, distance, bearing) {
var lon = lon1 + Math.atan2( var lon = lon1 + Math.atan2(
Math.sin(bearing) * Math.sin(dByR) * Math.cos(lat1), Math.sin(bearing) * Math.sin(dByR) * Math.cos(lat1),
Math.cos(dByR) - Math.sin(lat1) * Math.sin(lat)); 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)];
}; };

View File

@@ -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'); goog.require('ol.math');