2 // This extension parses the <geo>47.236189 N 11.301471 E</geo> tags.
3 // It does not depend on other extensions.
6 /// \brief This function converts a string like "47.236189 N 11.301471 E" to geographical coordinates in degrees.
9 /// try {list($latitude, $longitude) = wrGeoStringToGeo("47.236189 N 11.301471 E");}
10 /// catch (Exception $e) {return $e->getMessage();}
12 /// \throw exception if input is not formatted correctly with german UTF-8 error message. Don't forget to catch it!
13 /// \param UTF-8 encoded input string.
14 /// \return array($longitude, $latitude)
15 static function stringToGeo($input) {
17 $result = preg_match('/\s*(\d+\.?\d*)\s*N?\s+(\d+\.?\d*)\s*E?\s*$/', $input, $matches);
18 assert($result !== FALSE); // FALSE would mean syntax error
22 return array(floatval($matches[2]), floatval($matches[1]));
26 if (strlen($input) == 0) throw new Exception('Keine Koordinaten angegeben.');
27 throw new Exception('Das Koordinatenformat "' . $input . '" ist ungültig. Gültig wäre z.B. "47.236189 N 11.301471 E".');
31 static function geoToString($latitude, $longitude) {
32 return sprintf('%.6F N %.6F E', $latitude, $longitude);
36 /// Format inside <geo>...</geo> has to be like this: <geo>47.236189 N 11.301471 E</geo>
37 public static function render($content, $args, $parser, $frame) {
38 try {list($longitude, $latitude) = WrGeo::stringToGeo($content);}
39 catch (Exception $e) {return '<span class="error">' . htmlspecialchars($e->getMessage()) . '</span>';}
40 return '<a href="http://maps.google.at/maps?q=' . sprintf('%F,%F', $latitude, $longitude) . '">' . WrGeo::geoToString($latitude, $longitude) . '</a>';
44 public static function ParserFirstCallInitHook(Parser &$parser) {
45 $parser->setHook('geo', 'WrGeo::render');