mcoblentz has asked for the wisdom of the Perl Monks concerning the following question:
I could not find any preexisting modules that work with deg:mm:ss the way I needed so building this wasn't too bad - the extra characters in the dataset threw me off a few times though. Is there a better way to put this regex together? Is there a lat/log module I could use? And the input is one field from an array; I separate position into latitude and longitude - how would I return those to an array field? That sounded like a bad idea, so I did not try it.
Anyway, the routine:
sub latlong_cleanup # position comes in; has two parts, lat & long, separated by a comma w +ith trailing stuff, degree symbols, etc. { chomp ($_[0]); #position has trailing cr/lf $_[0] =~ s/\ W\ /\-/; # West longitudes are negative $_[0] =~ s/\ E\ /\ /; # East longitudes are positive $_[0] =~ s/N//; # North latitudes are positive $_[0] =~ s/S\ /\-/; # South latitudes are negative $_[0] =~ s/\'//g; # both lat and longs have trailing strophe. + remove it. $_[0] =~ s/\xB0/\./g; # replace degree character with decimal poin +t (ASCII °) # converts lat/long from deg:mm:ss to decimals my $minutes; my ($lat, $long) = split(/,/, $_[0]); # split the position into la +t/long $minutes = ($lat - int($lat)) /0.60; # "decimal-ize" the minutes $minutes = sprintf("%.2f", $minutes); # only need 2 digits; displa +ys are not that good $lat = int($lat) + $minutes; # put it back together $minutes = ($long - int($long) /0.60); # "decimal-ize" the minutes $minutes = sprintf("%.2f", $minutes); # only need 2 digits; displa +ys are not that good $long = int($long) + $minutes; # put it back together #join the lat/long back to $position; it came in as one field in an +array, it probably # should go back that way $_[0] = join(',', $lat,$long); };
Update: I'd like to thank everyone for their suggestions. The first draft of the Cruise Ship project is off and running. You can see a screen shot of the ships in the Caribbean (at night) at my blog.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Regex strings, deg:mm:ss, and all that
by hipowls (Curate) on Mar 23, 2008 at 01:30 UTC | |
|
Re: Regex strings, deg:mm:ss, and all that
by pc88mxer (Vicar) on Mar 23, 2008 at 02:12 UTC | |
|
Re: Regex strings, deg:mm:ss, and all that
by bobf (Monsignor) on Mar 23, 2008 at 03:22 UTC | |
by BrowserUk (Patriarch) on Mar 23, 2008 at 04:10 UTC |