views:

187

answers:

3

Hi guys, 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...

+1  A: 

Assuming those are the only two formats, you could find which format a string was by searching in it for ' or " (you could use strpos for this).

If the format is the first type there is almost no conversion needed - use N/S and E/W to determine if you need to make the value negative or not.

If you have degrees, minutes and seconds then you can get the values from the string using sscanf. Once you have the individual values then you can find the decimal degrees as degrees + minutes / 60 + seconds / (60 * 60).

tttppp
+4  A: 

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
)
Peter Lindqvist
Thanks for the great help guys :)
Ali
+1  A: 

What you need are some regular expressions. You can use a regular expression in combination with the functions in php to extract whatever you want from a given string. What I would probably do is use a regex to check the format of a given file and then I would iterate through each line of a file, using a regex to extract the relevant information. For example, you could use a regular expression like [0-9]{1,3}\.[0-9]{1,4}°(N|W|E|S) to check whether something matches the first type. Then you could match each line of the input against something like ([0-9]{1,3})\.([0-9]{1,4})°(N|W|E|S) with preg_match_all and extract the relevant data from the matches array. From here, you will have all the data needed for the conversion. A similar thing could be done for the other type. Note that I don't have a php install to test these Regular Expressions against, so they may need some slight tweaking to avoid php blowing up when it reads them (most likely the ° character may need to be encoded differently, although you could likely just use the . meta-character, as it will match any single non-space character).

Some good resources for regular expressions:

As mentioned in the SO link above, having some sort of tester is quite useful for generating regular expressions, whether that be something like RegEx Buddy, Reggy, or Kodos.

Paul Wicks
It could make for an elegant solution.
Peter Lindqvist