Index: trunk/extensions/Maps/ParserFunctions/GeoFunctions/Maps_GeoFunctions.php |
— | — | @@ -0,0 +1,121 @@ |
| 2 | +<?php |
| 3 | + |
| 4 | +/** |
| 5 | + * This file contains registration for the geographical coordinate functions |
| 6 | + * such as #geodistance for the Maps extension. |
| 7 | + * |
| 8 | + * @file Maps_GeoFunctions.php |
| 9 | + * @ingroup Maps |
| 10 | + * |
| 11 | + * @author Jeroen De Dauw |
| 12 | + * @author Pnelnik |
| 13 | + * @author Matěj Grabovský |
| 14 | + */ |
| 15 | + |
| 16 | +if ( !defined( 'MEDIAWIKI' ) ) { |
| 17 | + die( 'Not an entry point.' ); |
| 18 | +} |
| 19 | + |
| 20 | +$wgHooks['LanguageGetMagic'][] = 'efMapsGeoFunctionsMagic'; |
| 21 | +$wgHooks['ParserFirstCallInit'][] = 'efMapsGeoFunctions'; |
| 22 | + |
| 23 | +/** |
| 24 | + * Adds the magic words for the parser functions. |
| 25 | + */ |
| 26 | +function efMapsGeoFunctionsMagic( &$magicWords, $langCode ) { |
| 27 | + $magicWords['geodistance'] = array( 0, 'geodistance' ); |
| 28 | + |
| 29 | + return true; // Unless we return true, other parser functions won't get loaded. |
| 30 | +} |
| 31 | + |
| 32 | +/** |
| 33 | + * Adds the parser function hooks. |
| 34 | + */ |
| 35 | +function efMapsGeoFunctions( &$wgParser ) { |
| 36 | + // Hooks to enable the geocoding parser functions. |
| 37 | + $wgParser->setFunctionHook( 'geodistance', 'efMapsRenderGeoDistance' ); |
| 38 | + |
| 39 | + return true; |
| 40 | +} |
| 41 | + |
| 42 | +// TODO: add support for smart geocoding |
| 43 | +// TODO: add coordinate validation |
| 44 | +function efMapsRenderGeoDistance( Parser &$parser ) { |
| 45 | + $args = func_get_args(); |
| 46 | + |
| 47 | + // We already know the $parser. |
| 48 | + array_shift( $args ); |
| 49 | + |
| 50 | + // Default parameter assignment, to allow for nameless syntax. |
| 51 | + $defaultParams = array( 'location1', 'location2' ); |
| 52 | + $parameters = array(); |
| 53 | + |
| 54 | + // Determine all parameter names and value, and take care of default (nameless) |
| 55 | + // parameters, by turning them into named ones. |
| 56 | + foreach( $args as $arg ) { |
| 57 | + $parts = explode( '=', $arg ); |
| 58 | + if ( count( $parts ) == 1 ) { |
| 59 | + if ( count( $defaultParams ) > 0 ) { |
| 60 | + $defaultParam = array_shift( $defaultParams ); |
| 61 | + $parameters[$defaultParam] = trim( $parts[0] ); |
| 62 | + } |
| 63 | + } else { |
| 64 | + $name = strtolower( trim( array_shift( $parts ) ) ); |
| 65 | + $parameters[$name] = trim( implode( $parts ) ); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + $parameterInfo = array( |
| 70 | + 'location1' => array( |
| 71 | + 'required' => true |
| 72 | + ), |
| 73 | + 'location2' => array( |
| 74 | + 'required' => true |
| 75 | + ), |
| 76 | + ); |
| 77 | + |
| 78 | + $manager = new ValidatorManager(); |
| 79 | + |
| 80 | + $parameters = $manager->manageMapparameters( $parameters, $parameterInfo ); |
| 81 | + |
| 82 | + $doCalculation = $parameters !== false; |
| 83 | + |
| 84 | + if ( $doCalculation ) { |
| 85 | + $start = MapsCoordinateParser::parseCoordinates( $parameters['location1'] ); |
| 86 | + $end = MapsCoordinateParser::parseCoordinates( $parameters['location2'] ); |
| 87 | + |
| 88 | + $radiusOfEarthKm = 20000 / M_PI; |
| 89 | + |
| 90 | + $northRad1 = deg2rad( $start['lat'] ); |
| 91 | + $eastRad1 = deg2rad( $start['lon'] ); |
| 92 | + |
| 93 | + $cosNorth1 = cos( $northRad1 ); |
| 94 | + $cosEast1 = cos( $eastRad1 ); |
| 95 | + |
| 96 | + $sinNorth1 = sin( $northRad1 ); |
| 97 | + $sinEast1 = sin( $eastRad1 ); |
| 98 | + |
| 99 | + $northRad2 = deg2rad( $end['lat'] ); |
| 100 | + $eastRad2 = deg2rad( $end['lon'] ); |
| 101 | + |
| 102 | + $cosNorth2 = cos( $northRad2 ); |
| 103 | + $cosEast2 = cos( $eastRad2 ); |
| 104 | + |
| 105 | + $sinNorth2 = sin( $northRad2 ); |
| 106 | + $sinEast2 = sin( $eastRad2 ); |
| 107 | + |
| 108 | + $term1 = $cosNorth1 * $sinEast1 - $cosNorth2 * $sinEast2; |
| 109 | + $term2 = $cosNorth1 * $cosEast1 - $cosNorth2 * $cosEast2; |
| 110 | + $term3 = $sinNorth1 - $sinNorth2; |
| 111 | + |
| 112 | + $distThruSquared = $term1 * $term1 + $term2 * $term2 + $term3 * $term3; |
| 113 | + |
| 114 | + $surfaceDistance = 2 * $radiusOfEarthKm * asin( sqrt( $distThruSquared ) / 2 ); |
| 115 | + |
| 116 | + $output = $surfaceDistance . ' km<br />' . $manager->getErrorList(); |
| 117 | + } else { |
| 118 | + $output = $manager->getErrorList(); |
| 119 | + } |
| 120 | + |
| 121 | + return array( $output, 'noparse' => true, 'isHTML' => true ); |
| 122 | +} |
\ No newline at end of file |
Property changes on: trunk/extensions/Maps/ParserFunctions/GeoFunctions/Maps_GeoFunctions.php |
___________________________________________________________________ |
Name: svn:eol-style |
1 | 123 | + native |
Index: trunk/extensions/Maps/Maps_Settings.php |
— | — | @@ -47,7 +47,7 @@ |
48 | 48 | |
49 | 49 | # String. The default geocoding service, which will be used when no service is provided by the user. |
50 | 50 | # This service needs to be enabled, if not, the first one from the available services will be taken. |
51 | | -$egMapsDefaultGeoService = 'geonames'; |
| 51 | +$egMapsDefaultGeoService = 'google'; |
52 | 52 | |
53 | 53 | $egMapsUserGeoOverrides = true; |
54 | 54 | |
— | — | @@ -59,6 +59,7 @@ |
60 | 60 | include_once $egMapsDir . 'ParserFunctions/DisplayPoint/Maps_DisplayPoint.php'; // #display_point(s) |
61 | 61 | include_once $egMapsDir . 'ParserFunctions/Geocode/Maps_GeocodeFunctions.php'; // #geocode, #geocodelat, #geocodelon |
62 | 62 | include_once $egMapsDir . 'ParserFunctions/Coordinates/Maps_Coordinates.php'; // #coordinates |
| 63 | +include_once $egMapsDir . 'ParserFunctions/GeoFunctions/Maps_GeoFunctions.php'; // #geodistance |
63 | 64 | |
64 | 65 | |
65 | 66 | |