Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Regex strings, deg:mm:ss, and all that

by bobf (Monsignor)
on Mar 23, 2008 at 03:22 UTC ( [id://675721]=note: print w/replies, xml ) Need Help??


in reply to Regex strings, deg:mm:ss, and all that

You've already got some ideas for parsing the input. For the conversion step, you could reach for CPAN and Geo::Coordinates::DecimalDegrees.

use strict; use warnings; use Geo::Coordinates::DecimalDegrees; my ( $degrees, $minutes, $seconds ) = ( 23, 34, 6 ); my $decimal_degrees = dms2decimal( $degrees, $minutes, $seconds ); printf "%d:%d:%d => %.5f\n", $degrees, $minutes, $seconds, $decimal_de +grees; ( $degrees, $minutes, $seconds ) = decimal2dms( $decimal_degrees ); printf "%.5f => %d:%d:%d\n", $decimal_degrees, $degrees, $minutes, $se +conds;
Output:
23:34:6 => 23.56833 23.56833 => 23:34:5

Note that if precision is important you might want to peek at the code and run some tests. For the example above, a round trip conversion from DDMMSS => decimal degrees => DDMMSS results in a one second difference.

Update: Whups. The printf pattern isn't quite right. Many thanks to BrowserUk++ for catching the error and for providing a much better pattern: %d:%02d:%02.f

Replies are listed 'Best First'.
Re^2: Regex strings, deg:mm:ss, and all that
by BrowserUk (Patriarch) on Mar 23, 2008 at 04:10 UTC
    Note that if precision is important you might want to peek at the code and run some tests. For the example above, a round trip conversion from DDMMSS => decimal degrees => DDMMSS results in a one second difference.

    The problem is with your use of sprintf, or rather %d.

    use strict; use warnings; use Geo::Coordinates::DecimalDegrees; my ( $degrees, $minutes, $seconds ) = ( 23, 34, 6 ); my $decimal_degrees = dms2decimal( $degrees, $minutes, $seconds ); printf "%d:%02d:%02.f => %.5f\n", $degrees, $minutes, $seconds, $decimal_degrees; ( $degrees, $minutes, $seconds ) = decimal2dms( $decimal_degrees ); printf "%.5f => %d:%02d:%02.f\n", $decimal_degrees, $degrees, $minutes, $seconds; __END__ c:\test>junk5 23:34:06 => 23.56833 23.56833 => 23:34:06

    The module doesn't int the seconds, allowing for input and output of fractional seconds. If you don't want the fractions, you should use %.f rather than %d. And while your at it, it's conventional to include leading zeros on coordinates, hence %d:%02d:%02.f.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://675721]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (1)
As of 2024-04-25 03:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found