/** * Tests MapsCoordinateParser::parseCoordinates() */ public function testParseCoordinates() { foreach (self::$fakeCoordinates as $coord) { $this->assertFalse(MapsCoordinateParser::parseCoordinates($coord), "parseCoordinates did not return false for {$coord}."); } foreach (self::$parsingTests as $coord => $destination) { $this->assertEquals($destination, MapsCoordinateParser::parseCoordinates($coord), "Parsing test failed at " . __METHOD__); } }
/** * Renders and returns the output. * @see ParserHook::render * * @since 0.7 * * @param array $parameters * * @return string */ public function render(array $parameters) { $parsedCoords = MapsCoordinateParser::parseCoordinates($parameters['location']); if ($parsedCoords) { $output = MapsCoordinateParser::formatCoordinates($parsedCoords, $parameters['format'], $parameters['directional']); } else { // The coordinates should be valid when this method gets called. throw new MWException('Attempt to format an invalid set of coordinates'); } return $output; }
/** * This function first determines wether the provided string is a pair or coordinates * or an address. If it's the later, an attempt to geocode will be made. The function will * return the coordinates or false, in case a geocoding attempt was made but failed. * * @since 0.7 * * @param string $coordsOrAddress * @param string $geoservice * @param string|false $mappingService * @param boolean $checkForCoords * * @return array or false */ public static function attemptToGeocode($coordsOrAddress, $geoservice = '', $mappingService = false, $checkForCoords = true) { if ($checkForCoords) { if (MapsCoordinateParser::areCoordinates($coordsOrAddress)) { return MapsCoordinateParser::parseCoordinates($coordsOrAddress); } else { return self::geocode($coordsOrAddress, $geoservice, $mappingService); } } else { return self::geocode($coordsOrAddress, $geoservice, $mappingService); } }
/** * Sets the location to a set of coordinates. You can provide a string * of raw coordinates, an array with lat and lon values and false. * * @since 0.7.1 * * @param mixed $coordinates * * @return boolean Success indicator */ public function setCoordinates($coordinates) { $coordSet = MapsCoordinateParser::parseCoordinates($coordinates); $this->isValid = $coordSet !== false; $this->latitude = $coordSet['lat']; $this->longitude = $coordSet['lon']; return $this->isValid; }
/** * 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; }
/** * Parses the value into the coordinates and any meta data provided, such as distance. * * @since 0.6 * * @param $value String * @param $asQuery Boolean */ protected function parseUserValueOrQuery($value, $asQuery = false) { $this->wikiValue = $value; $comparator = SMW_CMP_EQ; if ($value === '') { $this->addError(wfMsg('smw_novalues')); } else { SMWDataValue::prepareValue($value, $comparator); $parts = explode('(', $value); $coordinates = trim(array_shift($parts)); $distance = count($parts) > 0 ? trim(array_shift($parts)) : false; if ($distance !== false) { $distance = substr(trim($distance), 0, -1); if (!MapsDistanceParser::isDistance($distance)) { $this->addError(wfMsgExt('semanticmaps-unrecognizeddistance', array('parsemag'), $distance)); $distance = false; } } $parsedCoords = MapsCoordinateParser::parseCoordinates($coordinates); if ($parsedCoords) { $this->m_dataitem = new SMWDIGeoCoord($parsedCoords); } else { $this->addError(wfMsgExt('maps_unrecognized_coords', array('parsemag'), $coordinates, 1)); // Make sure this is always set // TODO: Why is this needed?! $this->m_dataitem = new SMWDIGeoCoord(array('lat' => 0, 'lon' => 0)); } } if ($asQuery) { $this->setUserValue($value); switch (true) { case !$this->isValid(): return new SMWThingDescription(); case $distance !== false: return new SMAreaValueDescription($this->getDataItem(), $comparator, $distance); default: return new SMGeoCoordsValueDescription($this->getDataItem(), $comparator); } } }