I'm stuck with another coordinates related query here. I have a number of CSV files and they all have a bunch of coordinates mentioned however the coordinates are in differing formats like some files have the coordinates shown as so:
40.0873°N 20.1531°E
While some have them as so:
27°50′21″N 00°11′07″W
I need a way to set up a function in PHP that:
First of all deduces what format the coordinates are in
Then convert the coordinates into latitude and longitudes as so:
37.235
-115.811111
Plus I understand the second format is Hours Minutes Seconds - the thing is that how do I extract the hours minutes and seconds from this kind of string.
Any help would be greatly appreciated...
Code:
print_r(parse("40.0873°N 20.1531°E"));
print_r(parse("27°50′21″N 00°11′07″W"));
function parse($coord)
{
$strings = split(' ',$coord);
$ret['lat'] = degree2decimal($strings[0]);
$ret['lon'] = degree2decimal($strings[1]);
return $ret;
}
function degree2decimal($deg_coord="")
{
$dpos=strpos($deg_coord,'°');
$mpos=strpos($deg_coord,'‘');
$spos=strpos($deg_coord,'"');
$mlen=(($mpos-$dpos)-1);
$slen=(($spos-$mpos)-1);
$direction=substr(strrev($deg_coord),0,1);
$degrees=substr($deg_coord,0,$dpos);
$minutes=substr($deg_coord,$dpos+1,$mlen);
$seconds=substr($deg_coord,$mpos+1,$slen);
$seconds=($seconds/60);
$minutes=($minutes+$seconds);
$minutes=($minutes/60);
$decimal=($degrees+$minutes);
if (($direction=="S") or ($direction=="W"))
{ $decimal=$decimal*(-1);}
return $decimal;
}
Output:
Array
(
[lat] => 40.08732425
[lon] => 20.153142527778
)
Array
(
[lat] => 27.835277777778
[lon] => -0.18333333333333
)