Reply Thread Tools Search this Thread
Old 13-08-2010, 13:38   #1 (permalink)
SEOCoder
Registered User
 
Join Date: Nov 2009
Posts: 68
Convert Geo Graphical coordinates

Hi All,

Be looking, but cannot find a formula or script to convert

this: 5131'N 0312'W
to
this: 51.516701, -3.199526

anyone have any idea?
  Reply With Quote
Old 13-08-2010, 13:51   #2 (permalink)
Synook
What happened?
 
Synook's Avatar
 
Join Date: Jul 2008
Posts: 2,196
The fractional portion is just minutes / 1 degree (e.g., 31/60 for 5131'). The numbers are positive for North/East. You could use a regular expression to grab the relevant information, apply the rules, and output the result.
__________________
Web developer • AspektasBlogTwitter


  Reply With Quote
Old 13-08-2010, 14:00   #3 (permalink)
SEOCoder
Registered User
 
Join Date: Nov 2009
Posts: 68
I am afraid you lost me there. 31/60 = 0.516666666666666666666666666...

How does 31/60 get me from 5131 to 51.516701?
  Reply With Quote
Old 13-08-2010, 15:09   #4 (permalink)
smoseley
 
Posts: n/a
Quote:
Originally Posted by SEOCoder
Hi All,

Be looking, but cannot find a formula or script to convert

this: 5131'N 0312'W
to
this: 51.516701, -3.199526

anyone have any idea?

You'll have to make sure there are no spaces.

Not tested, so likely has bugs. Edit: Aw, hell, I went and tested it for ya... no bugs now.

PHP Code:
<?php
// Examples of use
echo convertLatLong("52N") . "<br>";
echo 
convertLatLong("5231'N") . "<br>";
echo 
convertLatLong("5231'46\"W") . "<br>";

function 
convertLatLong($coords) {
    
$coords  preg_split("/[\\\"\']+/"$coords);
    if (empty(
$coords) || count($coords) < 2) {
        return 
false;
    } else {
        
$direction $coords[count($coords)-1];
        for (
$i 0$i count($coords)-1$i++) {
            
$value += $coords[$i]/pow(60$i);
        }
        switch (
strtoupper($direction)) {
            case 
"N":
            case 
"E":
                return 
$value;
                break;
            case 
"S":
            case 
"W":
                return -
$value;
                break;
            default:
                return 
false;
                break;
        }
    }
}
?>

Last edited by smoseley : 13-08-2010 at 15:20.
  Reply With Quote
Old 13-08-2010, 15:32   #5 (permalink)
SEOCoder
Registered User
 
Join Date: Nov 2009
Posts: 68
That is awesome!!

I had just started trying to create a formula. Thanks Smoseley, you have saved me a lot of time and hassle.

Nice one!!
  Reply With Quote
Old 13-08-2010, 15:36   #6 (permalink)
smoseley
 
Posts: n/a
Welcome.
  Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search


Contact Us - Web Design Forums - Archive
vBulletin © 2000-2009 Jelsoft Enterprises Limited.
Search Engine Optimization by vBSEO 3.0.0 RC8