r67119 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r67118‎ | r67119 | r67120 >
Date:22:34, 30 May 2010
Author:jeroendedauw
Status:deferred
Tags:
Comment:
Fixed issues with findDestination
Modified paths:
  • /trunk/extensions/Maps/Maps_CoordinateParser.php (modified) (history)
  • /trunk/extensions/Maps/ParserFunctions/GeoFunctions/Maps_GeoFunctions.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Maps/ParserFunctions/GeoFunctions/Maps_GeoFunctions.php
@@ -16,8 +16,8 @@
1717 die( 'Not an entry point.' );
1818 }
1919
20 -// The approximate radius of the earth in km.
21 -define( 'Maps_EARTH_RADIUS', 20000 / M_PI );
 20+// The approximate radius of the earth in meters, according to http://en.wikipedia.org/wiki/Earth_radius.
 21+define( 'Maps_EARTH_RADIUS', 6371000 );
2222
2323 if ( version_compare( $wgVersion, '1.16alpha', '<' ) ) {
2424 $wgHooks['LanguageGetMagic'][] = 'efMapsGeoFunctionsMagic';
@@ -203,7 +203,8 @@
204204 }
205205
206206 if ( $location ) {
207 - $destination = self::findDestination( $location, $parameters['bearing'], $parameters['distance'] );
 207+ // TODO: have distance unit support here
 208+ $destination = self::findDestination( $location, $parameters['bearing'], (float)$parameters['distance'] * 1000 );
208209 $output = MapsCoordinateParser::formatCoordinates( $destination, $parameters['format'], $parameters['directional'] );
209210 } else {
210211 global $egValidatorFatalLevel;
@@ -259,14 +260,11 @@
260261
261262 $sinNorth2 = sin( $northRad2 );
262263 $sinEast2 = sin( $eastRad2 );
 264+
 265+ $distNOacos = $sinNorth1 * $sinNorth2 + $cosNorth1 * $cosNorth2 + cos ($eastRad1 - $eastRad2);
263266
264 - $term1 = $cosNorth1 * $sinEast1 - $cosNorth2 * $sinEast2;
265 - $term2 = $cosNorth1 * $cosEast1 - $cosNorth2 * $cosEast2;
266 - $term3 = $sinNorth1 - $sinNorth2;
267 -
268 - $distThruSquared = $term1 * $term1 + $term2 * $term2 + $term3 * $term3;
269 -
270 - return 2 * Maps_EARTH_RADIUS * asin( sqrt( $distThruSquared ) / 2 );
 267+ // Divide by 1000, as the radius is defined in meters.
 268+ return Maps_EARTH_RADIUS * acos( $distNOacos ) / 1000;
271269 }
272270
273271 /**
@@ -281,22 +279,17 @@
282280 public static function findDestination( array $startingCoordinates, $bearing, $distance ) {
283281 $startingCoordinates['lat'] = deg2rad( (float)$startingCoordinates['lat'] );
284282 $startingCoordinates['lon'] = deg2rad( (float)$startingCoordinates['lon'] );
 283+
 284+ $radBearing = deg2rad ( (float)$bearing );
 285+ $angularDistance = $distance / Maps_EARTH_RADIUS;
285286
286 - $angularDistance = deg2rad( $distance / Maps_EARTH_RADIUS );
287 -
288 - $lat = asin(
289 - sin( $startingCoordinates['lat'] ) * cos( $angularDistance ) +
290 - cos( $startingCoordinates['lat'] ) * sin( $angularDistance ) * cos( $bearing )
291 - );
292 -
 287+ $lat = asin (sin ( $startingCoordinates['lat'] ) * cos ( $angularDistance ) + cos ( $startingCoordinates['lat'] ) * sin ( $angularDistance ) * cos ( $radBearing ) );
 288+ $lon = $startingCoordinates['lon'] + atan2 ( sin ( $radBearing ) * sin ( $angularDistance ) * cos ( $startingCoordinates['lat'] ), cos ( $angularDistance ) - sin ( $startingCoordinates['lat'] ) * sin ( $lat ) );
 289+
293290 return array(
294291 'lat' => rad2deg( $lat ),
295 - 'lon' => rad2deg( $startingCoordinates['lon'] + atan2(
296 - sin( $bearing ) * sin( $angularDistance ) * cos( $startingCoordinates['lat'] ),
297 - cos( $angularDistance ) - sin( $startingCoordinates['lat'] ) * sin( $lat ) )
298 - )
 292+ 'lon' => rad2deg( $lon )
299293 );
300294 }
301295
302 -}
303 -
 296+}
\ No newline at end of file
Index: trunk/extensions/Maps/Maps_CoordinateParser.php
@@ -20,7 +20,6 @@
2121 * @ingroup Maps
2222 *
2323 * @author Jeroen De Dauw
24 - *
2524 */
2625 class MapsCoordinateParser {
2726
@@ -67,8 +66,8 @@
6867 }
6968
7069 $coordinates = array(
71 - 'lat' => trim ( $split[0] ),
72 - 'lon' => trim ( $split[1] ),
 70+ 'lat' => trim( $split[0] ),
 71+ 'lon' => trim( $split[1] ),
7372 );
7473
7574 // Ensure the coordinates are in non-directional notation.
@@ -416,8 +415,6 @@
417416 * @param string $coordinate
418417 *
419418 * @return string
420 - *
421 - * FIXME: fix innacuracy
422419 */
423420 private static function parseDMSCoordinate( $coordinate ) {
424421 $isNegative = $coordinate{0} == '-';
@@ -459,8 +456,6 @@
460457 * @param string $coordinate
461458 *
462459 * @return string
463 - *
464 - * TODO: fix innacuracy
465460 */
466461 private static function parseDMCoordinate( $coordinate ) {
467462 $isNegative = $coordinate{0} == '-';

Status & tagging log