Talk:Geographic coordinate conversion

From Wikipedia, the free encyclopedia

Geographical coordinates Geographic coordinate conversion is of interest to WikiProject Geographical coordinates, which encourages the use of geographical coordinates in Wikipedia. If you would like to participate, visit the project page.


[edit] Check last coordinate

The last coordinate listed in "Ways to write coordinates" specifies a location significatly different than the others in that list.


[edit] Conversion code

The PHP code is too simple. For instance, given the input value "12.999992", the output is "12° 60.000". This is a classical rounding problem. Here are two Perl subroutines which get it right. The second uses Perl's automatic conversion between numbers and strings, a feature not available in all languages. I don't know PHP well enough to write it, but the Perl code below should be understandable by most programmers. –Peter J. Acklam 10:24, 22 Apr 2005 (UTC)

sub pretty_coord {
    my $coord = abs shift;            # decimal degrees
    my $deg = int $coord;             # compute degrees
    my $min = 60 * ($coord - $deg);   # compute minutes
    if ($min >= 59.9995) {            # if value will be rounded to 60.000
        $min = 0;                     #   set it to zero
        $deg ++;                      #   and increment degrees
    }
    return sprintf("%0.0f° %06.3f", $deg, $min);
};
sub pretty_coord {
    my $coord = abs shift;            # decimal degrees
    my $deg = int $coord;             # compute degrees
    my $min = 60 * ($coord - $deg);   # compute minutes
    $min = sprintf '%.3f', $min;      # round to three digits
    if ($min == 60) {                 # if result is 60
        $min = 0;                     #   set it to zero
        $deg ++;                      #   and increment degrees
    }
    return sprintf("%0.0f° %06.3f", $deg, $min);
};
I've added a new PHP function which I use as part of my website engine. Works well and is more robust & easier to follow than the original "pretty_coord()" example –Andrew N. Thu, 10 Aug 2006 05:04:42 +1000 (UTC)
Is it more robust? It converts "12.5832" into "12:34:60", which should have been "12:35:00". It seems that your code is suffering from the exact same problem as the other code examples. –Peter J. Acklam 11:25, 8 May 2007 (UTC)

[edit] Legal ranges

What are the ranges of degrees, minutes and seconds that are allowed to use? And also what are the allowed number systems for the numbers? What do you think about these values:
23° -45' 22.1"
-23° -45' -22.1"
-(23° 45' 22.1")
23° 61' 61"
23° 45.7' 22.1"

--80.122.38.210 05:28, 20 July 2006 (UTC)