/**
  * Renders and returns the output.
  * @see ParserHook::render
  * 
  * @since 0.7
  * 
  * @param array $parameters
  * 
  * @return string
  */
 public function render(array $parameters)
 {
     $canGeocode = MapsGeocoders::canGeocode();
     if ($canGeocode) {
         $location = MapsGeocoders::attemptToGeocode($parameters['location'], $parameters['geoservice'], $parameters['mappingservice']);
     } else {
         $location = MapsCoordinateParser::parseCoordinates($parameters['location']);
     }
     // TODO
     if ($location) {
         $destination = MapsGeoFunctions::findDestination($location, $parameters['bearing'], MapsDistanceParser::parseDistance($parameters['distance']));
         $output = MapsCoordinateParser::formatCoordinates($destination, $parameters['format'], $parameters['directional']);
     } else {
         // The location should be valid when this method gets called.
         throw new MWException('Attempt to find a destination from an invalid location');
     }
     return $output;
 }
 /**
  * Renders and returns the output.
  * @see ParserHook::render
  * 
  * @since 0.7
  * 
  * @param array $parameters
  * 
  * @return string
  */
 public function render(array $parameters)
 {
     if (MapsGeocoders::canGeocode()) {
         $start = MapsGeocoders::attemptToGeocode($parameters['location1'], $parameters['geoservice'], $parameters['mappingservice']);
         $end = MapsGeocoders::attemptToGeocode($parameters['location2'], $parameters['geoservice'], $parameters['mappingservice']);
     } else {
         $start = MapsCoordinateParser::parseCoordinates($parameters['location1']);
         $end = MapsCoordinateParser::parseCoordinates($parameters['location2']);
     }
     if ($start && $end) {
         $output = MapsDistanceParser::formatDistance(MapsGeoFunctions::calculateDistance($start, $end), $parameters['unit'], $parameters['decimals']);
     } else {
         // The locations should be valid when this method gets called.
         throw new Exception('Attempt to find the distance between locations of at least one is invalid');
     }
     return $output;
 }
Beispiel #3
0
 public function __construct(&$results, SMWPrintRequest $printRequest, SRFFiltered &$queryPrinter)
 {
     global $wgParser;
     parent::__construct($results, $printRequest, $queryPrinter);
     if (!defined('Maps_VERSION') || version_compare(Maps_VERSION, '1.0', '<')) {
         throw new FatalError('You need to have the <a href="http://www.mediawiki.org/wiki/Extension:Maps">Maps</a> extension version 1.0 or higher installed in order to use the distance filter.<br />');
     }
     MapsGeocoders::init();
     $params = $this->getActualParameters();
     if (array_key_exists('distance filter origin', $params)) {
         $origin = MapsGeocoders::attemptToGeocode($wgParser->recursiveTagParse($params['distance filter origin']));
     } else {
         $origin = array('lat' => '0', 'lon' => '0');
     }
     if (array_key_exists('distance filter unit', $params)) {
         $this->mUnit = MapsDistanceParser::getValidUnit($wgParser->recursiveTagParse($params['distance filter unit']));
     } else {
         $this->mUnit = MapsDistanceParser::getValidUnit();
     }
     // Is the real position stored in a property?
     if (array_key_exists('distance filter property', $params)) {
         $property = trim($wgParser->recursiveTagParse($params['distance filter property']));
         $locations = array();
     } else {
         $property = null;
         $locations = null;
     }
     $targetLabel = $printRequest->getLabel();
     foreach ($this->getQueryResults() as $id => $filteredItem) {
         $row = $filteredItem->getValue();
         // $filteredItem is of class SRF_Filtered_Item
         // $row is an array of SMWResultArray
         foreach ($row as $field) {
             // $field is an SMWResultArray
             $label = $field->getPrintRequest()->getLabel();
             if ($label === $targetLabel) {
                 $field->reset();
                 $dataValue = $field->getNextDataValue();
                 // only use first value
                 if ($dataValue !== false) {
                     $posText = $dataValue->getShortText(SMW_OUTPUT_WIKI, false);
                     if ($property === null) {
                         // position is directly given
                         $pos = MapsGeocoders::attemptToGeocode($posText);
                     } else {
                         // position is given in a property of a page
                         // if we used this page before, just look up the coordinates
                         if (array_key_exists($posText, $locations)) {
                             $pos = $locations[$posText];
                         } else {
                             // query the position's page for the coordinates or address
                             $posText = SMWQueryProcessor::getResultFromFunctionParams(array($posText, '?' . $property), SMW_OUTPUT_WIKI, SMWQueryProcessor::INLINE_QUERY, true);
                             //
                             if ($posText !== '') {
                                 // geocode
                                 $pos = MapsGeocoders::attemptToGeocode($posText);
                             } else {
                                 $pos = array('lat' => '0', 'lon' => '0');
                             }
                             // store coordinates in case we need them again
                             $locations[$posText] = $pos;
                         }
                     }
                     if (is_array($pos)) {
                         $distance = round(MapsGeoFunctions::calculateDistance($origin, $pos) / MapsDistanceParser::getUnitRatio($this->mUnit));
                         if ($distance > $this->mMaxDistance) {
                             $this->mMaxDistance = $distance;
                         }
                     } else {
                         $distance = -1;
                     }
                 } else {
                     $distance = -1;
                     // no location given
                 }
                 $filteredItem->setData('distance-filter', $distance);
                 break;
             }
         }
     }
     if (array_key_exists('distance filter max distance', $params) && is_numeric($maxDist = trim($wgParser->recursiveTagParse($params['distance filter max distance'])))) {
         // this assignation ^^^ is ugly, but intentional
         $this->mMaxDistance = $maxDist;
     } else {
         if ($this->mMaxDistance > 1) {
             $base = pow(10, floor(log10($this->mMaxDistance)));
             $this->mMaxDistance = ceil($this->mMaxDistance / $base) * $base;
         }
     }
 }
 /**
  * Translates the coordinates field to the centre field and makes sure it's set to it's default when invalid. 
  * 
  * @since 1.0
  * 
  * @param array &$params
  */
 protected function setCentre(array &$params)
 {
     // If it's false, the coordinate was invalid, or geocoding failed. Either way, the default's should be used.
     if ($params['coordinates'] === false) {
         global $egMapsDefaultMapCentre;
         $centre = MapsGeocoders::attemptToGeocode($egMapsDefaultMapCentre, $params['geoservice'], $this->service->getName());
         if ($centre === false) {
             throw new Exception('Failed to parse the default centre for the map. Please check the value of $egMapsDefaultMapCentre.');
         } else {
             $params['centre'] = $centre;
         }
     } else {
         $params['centre'] = $params['coordinates'];
     }
     unset($params['coordinates']);
 }