r64883 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r64882‎ | r64883 | r64884 >
Date:14:40, 10 April 2010
Author:jeroendedauw
Status:deferred
Tags:
Comment:
Changes for 0.6 - added geocoding capabilities to #geodistance
Modified paths:
  • /trunk/extensions/Maps/Geocoders/Maps_Geocoder.php (modified) (history)
  • /trunk/extensions/Maps/ParserFunctions/GeoFunctions/Maps_GeoFunctions.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Maps/ParserFunctions/GeoFunctions/Maps_GeoFunctions.php
@@ -53,7 +53,7 @@
5454
5555 // Default parameter assignment, to allow for nameless syntax.
5656 $defaultParams = array( 'location1', 'location2' );
57 - $parameters = array();
 57+ $parameters = array();
5858
5959 // Determine all parameter names and value, and take care of default (nameless)
6060 // parameters, by turning them into named ones.
@@ -86,41 +86,74 @@
8787 $doCalculation = $parameters !== false;
8888
8989 if ( $doCalculation ) {
90 - $start = MapsCoordinateParser::parseCoordinates( $parameters['location1'] );
91 - $end = MapsCoordinateParser::parseCoordinates( $parameters['location2'] );
92 -
93 - $northRad1 = deg2rad( $start['lat'] );
94 - $eastRad1 = deg2rad( $start['lon'] );
95 -
96 - $cosNorth1 = cos( $northRad1 );
97 - $cosEast1 = cos( $eastRad1 );
 90+ if ( self::geocoderIsAvailable() ) {
 91+ $start = MapsGeocoder::attemptToGeocode( $parameters['location1'] );
 92+ $end = MapsGeocoder::attemptToGeocode( $parameters['location2'] );
 93+ } else {
 94+ $start = MapsCoordinateParser::parseCoordinates( $parameters['location1'] );
 95+ $end = MapsCoordinateParser::parseCoordinates( $parameters['location2'] );
 96+ }
9897
99 - $sinNorth1 = sin( $northRad1 );
100 - $sinEast1 = sin( $eastRad1 );
101 -
102 - $northRad2 = deg2rad( $end['lat'] );
103 - $eastRad2 = deg2rad( $end['lon'] );
104 -
105 - $cosNorth2 = cos( $northRad2 );
106 - $cosEast2 = cos( $eastRad2 );
107 -
108 - $sinNorth2 = sin( $northRad2 );
109 - $sinEast2 = sin( $eastRad2 );
110 -
111 - $term1 = $cosNorth1 * $sinEast1 - $cosNorth2 * $sinEast2;
112 - $term2 = $cosNorth1 * $cosEast1 - $cosNorth2 * $cosEast2;
113 - $term3 = $sinNorth1 - $sinNorth2;
114 -
115 - $distThruSquared = $term1 * $term1 + $term2 * $term2 + $term3 * $term3;
116 -
117 - $surfaceDistance = 2 * Maps_EARTH_RADIUS * asin( sqrt( $distThruSquared ) / 2 );
118 -
119 - $output = $surfaceDistance . ' km<br />' . $manager->getErrorList();
 98+ if ( $start && $end ) {
 99+ $northRad1 = deg2rad( $start['lat'] );
 100+ $eastRad1 = deg2rad( $start['lon'] );
 101+
 102+ $cosNorth1 = cos( $northRad1 );
 103+ $cosEast1 = cos( $eastRad1 );
 104+
 105+ $sinNorth1 = sin( $northRad1 );
 106+ $sinEast1 = sin( $eastRad1 );
 107+
 108+ $northRad2 = deg2rad( $end['lat'] );
 109+ $eastRad2 = deg2rad( $end['lon'] );
 110+
 111+ $cosNorth2 = cos( $northRad2 );
 112+ $cosEast2 = cos( $eastRad2 );
 113+
 114+ $sinNorth2 = sin( $northRad2 );
 115+ $sinEast2 = sin( $eastRad2 );
 116+
 117+ $term1 = $cosNorth1 * $sinEast1 - $cosNorth2 * $sinEast2;
 118+ $term2 = $cosNorth1 * $cosEast1 - $cosNorth2 * $cosEast2;
 119+ $term3 = $sinNorth1 - $sinNorth2;
 120+
 121+ $distThruSquared = $term1 * $term1 + $term2 * $term2 + $term3 * $term3;
 122+
 123+ $surfaceDistance = 2 * Maps_EARTH_RADIUS * asin( sqrt( $distThruSquared ) / 2 );
 124+
 125+ $output = $surfaceDistance . ' km';
 126+ $errorList = $manager->getErrorList();
 127+
 128+ if ( $errorList != '' ) {
 129+ $output .= '<br />' . $errorList;
 130+ }
 131+ } else {
 132+ $errorList = '';
 133+
 134+ if ( !$start ) {
 135+ $errorList .= wfMsgExt( 'maps-invalid-coordinates', array( 'parsemag' ), $parameters['location1'] );
 136+ }
 137+
 138+ if ( !$end ) {
 139+ if ( $errorList != '' ) $errorList .= '<br />';
 140+ $errorList .= wfMsgExt( 'maps-invalid-coordinates', array( 'parsemag' ), $parameters['location2'] );
 141+ }
 142+
 143+ $output = $errorList;
 144+ }
120145 } else {
121146 $output = $manager->getErrorList();
122147 }
123148
124149 return array( $output, 'noparse' => true, 'isHTML' => true );
 150+ }
 151+
 152+ /**
 153+ * Returns a boolean indicating if MapsGeocoder is available.
 154+ */
 155+ private static function geocoderIsAvailable() {
 156+ global $wgAutoloadClasses;
 157+ return array_key_exists( 'MapsGeocoder', $wgAutoloadClasses );
125158 }
126159
127160 }
Index: trunk/extensions/Maps/Geocoders/Maps_Geocoder.php
@@ -31,20 +31,20 @@
3232 *
3333 * @param string $coordsOrAddress
3434 * @param string $geoservice
35 - * @param string $service
 35+ * @param string $mappingService
3636 * @param boolean $checkForCoords
3737 *
3838 * @return array or false
3939 */
40 - public static function attemptToGeocode( $coordsOrAddress, $geoservice, $service, $checkForCoords = true ) {
 40+ public static function attemptToGeocode( $coordsOrAddress, $geoservice = '', $mappingService = false, $checkForCoords = true ) {
4141 if ( $checkForCoords ) {
4242 if ( MapsCoordinateParser::areCoordinates( $coordsOrAddress ) ) {
4343 return MapsCoordinateParser::parseCoordinates( $coordsOrAddress );
4444 } else {
45 - return self::geocode( $coordsOrAddress, $geoservice, $service );
 45+ return self::geocode( $coordsOrAddress, $geoservice, $mappingService );
4646 }
4747 } else {
48 - return self::geocode( $coordsOrAddress, $geoservice, $service );
 48+ return self::geocode( $coordsOrAddress, $geoservice, $mappingService );
4949 }
5050 }
5151
@@ -75,7 +75,7 @@
7676 *
7777 * @return array with coordinates or false
7878 */
79 - public static function geocode( $address, $service, $mappingService ) {
 79+ public static function geocode( $address, $service = '', $mappingService = false ) {
8080 global $egMapsGeoServices, $wgAutoloadClasses, $egMapsDir, $IP, $egMapsEnableGeoCache;
8181
8282 // If the adress is already in the cache and the cache is enabled, return the coordinates.

Status & tagging log