
Work with Different Coordinate Systems #26
Chapter 3, Mapping Your World
|
113
HACK
Converting Lat/Long to Lat/Long in Perl
If you need more fine-grained control over the conversion, you’ll probably
want to use a program, rather than calculating by hand or using PROJ.4.
Walt Mankowski has written the CPAN module
Geo::Coordinates::
DecimalDegrees
, which takes care of the details. Assuming you have a stan-
dard Perl installation, you should be able to install it with this command:
perl -MCPAN -e 'install Geo::Coordinates::DecimalDegrees'
Here is a bit of Perl to illustrate the use of this module on the same parking
lot latitude:
#!/usr/bin/perl
use Geo::Coordinates::DecimalDegrees;
my ($deg, $min, $sec) = @ARGV;
print "$deg $min' $sec\"\tDMS\n";
my $decimal_degrees = dms2decimal($deg, $min, $sec);
print "$decimal_degrees\tDecimal degrees\n";
($deg, $min, $sec) = decimal2dms($decimal_degrees);
print "$deg $min' $sec\"\tBack to DMS\n";
($deg, $min) = decimal2dm($decimal_degrees);
print "$deg $min'\tDegrees + decimal minutes\n";
Put this sample code in a file called decimal.pl and execute it to yield the fol-
lowing output:
$ perl decimal.pl 38 24 57
38 24' 57" DMS
38.4158333333333 Decimal degrees
38 24' 56.9999999999941" Back to DMS
38 24.9499999999999' Degrees-decimal minutes
Note that rounding causes some loss of accuracy.
HACK
#26
Work with Different Coordinate Systems Hack #26
Your GPS gives you latitude and longitude, ...