Created
December 20, 2011 09:21
-
-
Save ivoba/1500921 to your computer and use it in GitHub Desktop.
parse Coordinates and convert to Lat/Lon from Wikipedia Markup
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class WikipediaMarkupCoordinates{ | |
/** | |
* @param string $text | |
* @return boolean|array | |
* | |
* bsp: http://de.wikipedia.org/w/api.php?action=query&prop=revisions&rvprop=content&format=json&titles=Berufskolleg_Deutzer_Freiheit | |
*/ | |
public static function parseCoordinatesFromInfobox($text) { | |
$out = array(); | |
#Breitengrad = 50/56/10.54/N\n| Längengrad = 6/58/49.86/E\n| | |
$pattern_b = '/(Breitengrad|NS)(.*?)\|/s'; | |
//TODO refactor | |
preg_match_all($pattern_b, $text, $matches, PREG_PATTERN_ORDER); | |
if (!empty($matches[2][0])) { | |
$br = $matches[2][0]; | |
$br = substr($br, strpos($br, '=') + 1); | |
$br = trim($br); | |
$lla = self::explodeGPS($br); | |
if ($lla !== false) { | |
$out['Lat'] = self::DMStoLatLon($lla[0], $lla[1], $lla[2]); | |
} else { | |
$out['Lat'] = $br; | |
} | |
} | |
$pattern_l = '/(Längengrad|EW)(.*?)\|/s'; | |
preg_match_all($pattern_l, $text, $matches, PREG_PATTERN_ORDER); | |
if (!empty($matches[2][0])) { | |
$br = $matches[2][0]; | |
$br = substr($br, strpos($br, '=') + 1); | |
$br = trim($br); | |
$lla = self::explodeGPS($br); | |
if ($lla !== false) { | |
$out['Lon'] = self::DMStoLatLon($lla[0], $lla[1], $lla[2]); | |
} else { | |
$out['Lon'] = $br; | |
} | |
} | |
//we fake that | |
if (isset($out['Lat']) && isset($out['Lon'])) { | |
$out['type'] = 'landmark'; | |
} | |
return $out; | |
} | |
public static function parseCoordinates($text) { | |
/* | |
* {{Coordinate |NS=50/46/28.9/N |EW=6/5/2/E |type=landmark|dim=200|region=DE-NW}} | |
* get Lat/Lon convert | |
*/ | |
$pattern = '/{{Coordinate(.*?)}}/'; | |
preg_match_all($pattern, $text, $matches, PREG_PATTERN_ORDER); | |
if (isset($matches[1][0])) { | |
$a = explode('|', $matches[1][0]); | |
array_shift($a); | |
$out = array(); | |
for ($i = 0; $i < count($a); $i++) { | |
$tmp = explode('=', $a[$i]); | |
if (isset($tmp[1])) { | |
switch (trim($tmp[0])) { | |
case 'NS': | |
$lla = self::explodeGPS($tmp[1]); | |
if ($lla !== false) { | |
$out['Lat'] = self::DMStoLatLon($lla[0], $lla[1], $lla[2]); | |
} else { | |
$out['Lat'] = trim($tmp[1]); | |
} | |
break; | |
case 'EW': | |
$lla = self::explodeGPS($tmp[1]); | |
if ($lla !== false) { | |
$out['Lon'] = self::DMStoLatLon($lla[0], $lla[1], $lla[2]); | |
} else { | |
$out['Lon'] = trim($tmp[1]); | |
} | |
break; | |
default: | |
$out[trim($tmp[0])] = trim($tmp[1]); | |
break; | |
} | |
} | |
} | |
return $out; | |
} | |
return false; | |
} | |
/** | |
* expects 50/46/28.9/N | |
* | |
* @param type $gps | |
* @return type | |
*/ | |
public static function explodeGPS($gps) { | |
$tmp = explode('/', $gps); | |
if (sizeof($tmp) > 2) { | |
return $tmp; | |
} | |
return false; | |
} | |
/** | |
* | |
* Converts DMS ( Degrees / minutes / seconds ) | |
* to decimal format longitude / latitude | |
* | |
* @param type $deg | |
* @param type $min | |
* @param type $sec | |
* @return type | |
*/ | |
public static function DMStoLatLon($deg, $min, $sec) { | |
/* | |
* {{coord|27|59|16|N|86|56|40|E}} | |
* degrees + (minutes/60) + (seconds/3600) = lat/lon | |
* Type Dir. Sign Test | |
Lat. N + > 0 | |
Lat. S - < 0 | |
Long. E + > 0 | |
Long. W - < 0 | |
*/ | |
return $deg + ((($min * 60) + ($sec)) / 3600); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment