From 1a1cb9fe4e1b70b3a09faa936122dc7d0d85697c Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Wed, 25 Jan 2012 16:25:36 +0100 Subject: [PATCH 1/6] Add OpenLayers/Spherical.js --- lib/OpenLayers/Spherical.js | 59 +++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 lib/OpenLayers/Spherical.js diff --git a/lib/OpenLayers/Spherical.js b/lib/OpenLayers/Spherical.js new file mode 100644 index 0000000000..ed64c844f8 --- /dev/null +++ b/lib/OpenLayers/Spherical.js @@ -0,0 +1,59 @@ +/* Copyright (c) 2006-2012 by OpenLayers Contributors (see authors.txt for + * full list of contributors). Published under the Clear BSD license. + * See http://svn.openlayers.org/trunk/openlayers/license.txt for the + * full text of the license. */ + +/** + * @see http://www.movable-type.co.uk/scripts/latlong.html + * @see http://code.google.com/apis/maps/documentation/javascript/reference.html#spherical + */ + + +/** + * Namespace: Spherical + */ +OpenLayers.Spherical = OpenLayers.Spherical || {}; + + +OpenLayers.Spherical.DEFAULT_RADIUS = 6378137; + + +/** + * APIFunction: computeDistanceBetween + * Computes the distance between two LonLats. + * + * Parameters: + * from - {} + * to - {} + * radius - {Float} + * + * Returns: + * {Float} The distance in meters. + */ +OpenLayers.Spherical.computeDistanceBetween = function(from, to, radius) { + var R = radius || OpenLayers.Spherical.DEFAULT_RADIUS; + var sinHalfDeltaLon = Math.sin(Math.PI * (to.lon - from.lon) / 360); + var sinHalfDeltaLat = Math.sin(Math.PI * (to.lat - from.lat) / 360); + var a = sinHalfDeltaLat * sinHalfDeltaLat + + sinHalfDeltaLon * sinHalfDeltaLon * Math.cos(Math.PI * from.lat / 180) * Math.cos(Math.PI * to.lat / 180); + return 2 * R * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); +}; + + +/** + * APIFunction: computeHeading + * Computes the heading from one LonLat to another LonLat. + * + * Parameters: + * from - {} + * to - {} + * + * Returns: + * {Float} The heading in degrees. + */ +OpenLayers.Spherical.computeHeading = function(from, to) { + var y = Math.sin(Math.PI * (from.lon - to.lon) / 180) * Math.cos(Math.PI * to.lat / 180); + var x = Math.cos(Math.PI * from.lat / 180) * Math.sin(Math.PI * to.lat / 180) - + Math.sin(Math.PI * from.lat / 180) * Math.cos(Math.PI * to.lat / 180) * Math.cos(Math.PI * (from.lon - to.lon) / 180); + return 180 * Math.atan2(y, x) / Math.PI; +}; From b1cc0c1b9dd6df83569b6d2b6a26386cf76b185c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89ric=20Lemoine?= Date: Thu, 9 Feb 2012 16:58:36 +0100 Subject: [PATCH 2/6] make OpenLayers.js auto-load Spherical.js --- lib/OpenLayers.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/OpenLayers.js b/lib/OpenLayers.js index 3ffa1b6a84..78b35da424 100644 --- a/lib/OpenLayers.js +++ b/lib/OpenLayers.js @@ -348,7 +348,8 @@ "OpenLayers/Symbolizer/Text.js", "OpenLayers/Symbolizer/Raster.js", "OpenLayers/Lang.js", - "OpenLayers/Lang/en.js" + "OpenLayers/Lang/en.js", + "OpenLayers/Spherical.js" ]; // etc. } From 4cc1bf18993486d0bde9a6849994b639c064bd20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89ric=20Lemoine?= Date: Thu, 9 Feb 2012 16:59:02 +0100 Subject: [PATCH 3/6] better docs for the Spherical namespace --- lib/OpenLayers/Spherical.js | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/lib/OpenLayers/Spherical.js b/lib/OpenLayers/Spherical.js index ed64c844f8..b19edc8939 100644 --- a/lib/OpenLayers/Spherical.js +++ b/lib/OpenLayers/Spherical.js @@ -3,29 +3,32 @@ * See http://svn.openlayers.org/trunk/openlayers/license.txt for the * full text of the license. */ -/** - * @see http://www.movable-type.co.uk/scripts/latlong.html - * @see http://code.google.com/apis/maps/documentation/javascript/reference.html#spherical - */ - - /** * Namespace: Spherical + * The OpenLayers.Spherical namespace includes utility functions for + * calculations on the basis of a spherical earth (ignoring ellipsoidal + * effects), which is accurate enough for most purposes. + * + * Relevant links: + * * http://www.movable-type.co.uk/scripts/latlong.html + * * http://code.google.com/apis/maps/documentation/javascript/reference.html#spherical */ + OpenLayers.Spherical = OpenLayers.Spherical || {}; - OpenLayers.Spherical.DEFAULT_RADIUS = 6378137; - /** * APIFunction: computeDistanceBetween * Computes the distance between two LonLats. * * Parameters: - * from - {} - * to - {} - * radius - {Float} + * from - {} or {Object} Starting point. A LonLat or + * a JavaScript litteral with lon lat properties. + * to - {} or {Object} Ending point. A LonLat or a + * JavaScript litteral with lon lat properties. + * radius - {Float} The radius. Optional. Defaults to the earth's + * radius, i.e. 6378137 meters. * * Returns: * {Float} The distance in meters. @@ -45,8 +48,10 @@ OpenLayers.Spherical.computeDistanceBetween = function(from, to, radius) { * Computes the heading from one LonLat to another LonLat. * * Parameters: - * from - {} - * to - {} + * from - {} or {Object} Starting point. A LonLat or + * a JavaScript litteral with lon lat properties. + * to - {} or {Object} Ending point. A LonLat or a + * JavaScript litteral with lon lat properties. * * Returns: * {Float} The heading in degrees. From 50629f3e50793233419c25cf4695751621151d98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89ric=20Lemoine?= Date: Thu, 9 Feb 2012 16:59:20 +0100 Subject: [PATCH 4/6] add Spherical to the API doc menu --- doc_config/Menu.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/doc_config/Menu.txt b/doc_config/Menu.txt index 7d685cb73c..1a941749ae 100644 --- a/doc_config/Menu.txt +++ b/doc_config/Menu.txt @@ -458,6 +458,7 @@ Group: OpenLayers { File: Tween (no auto-title, OpenLayers/Tween.js) File: Util (no auto-title, OpenLayers/Util.js) + File: Spherical (no auto-title, OpenLayers/Spherical.js) File: Deprecated (no auto-title, deprecated.js) } # Group: OpenLayers From 3c3092985808aca0b27ed20246b6aeced68bcd6f Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Fri, 10 Feb 2012 09:42:08 +0100 Subject: [PATCH 5/6] Correct spelling --- lib/OpenLayers/Spherical.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/OpenLayers/Spherical.js b/lib/OpenLayers/Spherical.js index b19edc8939..2fcaf44756 100644 --- a/lib/OpenLayers/Spherical.js +++ b/lib/OpenLayers/Spherical.js @@ -24,9 +24,9 @@ OpenLayers.Spherical.DEFAULT_RADIUS = 6378137; * * Parameters: * from - {} or {Object} Starting point. A LonLat or - * a JavaScript litteral with lon lat properties. + * a JavaScript literal with lon lat properties. * to - {} or {Object} Ending point. A LonLat or a - * JavaScript litteral with lon lat properties. + * JavaScript literal with lon lat properties. * radius - {Float} The radius. Optional. Defaults to the earth's * radius, i.e. 6378137 meters. * @@ -49,9 +49,9 @@ OpenLayers.Spherical.computeDistanceBetween = function(from, to, radius) { * * Parameters: * from - {} or {Object} Starting point. A LonLat or - * a JavaScript litteral with lon lat properties. + * a JavaScript literal with lon lat properties. * to - {} or {Object} Ending point. A LonLat or a - * JavaScript litteral with lon lat properties. + * JavaScript literal with lon lat properties. * * Returns: * {Float} The heading in degrees. From 8d6c9ef1eee650149e0b764fab140c752cb29d9a Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Fri, 10 Feb 2012 09:42:52 +0100 Subject: [PATCH 6/6] Correct documentation --- lib/OpenLayers/Spherical.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/OpenLayers/Spherical.js b/lib/OpenLayers/Spherical.js index 2fcaf44756..4a8956a5b7 100644 --- a/lib/OpenLayers/Spherical.js +++ b/lib/OpenLayers/Spherical.js @@ -27,8 +27,7 @@ OpenLayers.Spherical.DEFAULT_RADIUS = 6378137; * a JavaScript literal with lon lat properties. * to - {} or {Object} Ending point. A LonLat or a * JavaScript literal with lon lat properties. - * radius - {Float} The radius. Optional. Defaults to the earth's - * radius, i.e. 6378137 meters. + * radius - {Float} The radius. Optional. Defaults to 6378137 meters. * * Returns: * {Float} The distance in meters.