blib/lib/Geo/Converter/dms2dd.pm

filter: Statementstmt 95.5% 106 / 111 Branchbran 89.6% 52 / 58 Conditioncond 58.3% 14 / 24 MC/DCmcdc 20.0% 3 / 15 Subroutinesub 100% 12 / 12 Podpod 0.0% 0 / 1 totaltotal 84.6% 187 / 221 CC 39 SCAR 23.4
CC
39
Cov
89%
_dms2dd_extract_nums
15.2
_dms2dd_validate_dd_value
13.0
dms2dd
11.0
CRAP 10.4
↑ Summary
1 # package to convert degrees minutes seconds values to decimal degrees
2 # also does some simple validation of decimal degree values as a side effect
3 package Geo::Converter::dms2dd;
4
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
8
9 1 our $VERSION = '0.06';
10
11 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12
13 1 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Regexp::Common qw/number/;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use English qw { -no_match_vars };
Subroutine BEGIN: called CC=1 SCAR=0.0
16
17 1 require Exporter;
18 1 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 our @EXPORT_OK = qw( dms2dd );
20
21 #############################################################
22 ## some stuff to handle values in degrees
23
24 # some regexes
25 1 Readonly my $RE_REAL => qr /$RE{num}{real}/xms;
26 1 Readonly my $RE_INT => qr /$RE{num}{int} /xms;
27 1 Readonly my $RE_HEMI => qr {
28 # the hemisphere if given as text
29 # handle full words, ignoring numbers and punctuation
30 # needs utf solution
31 \s*
32 [NESWnesw]
33 [a-zA-Z]*
34 \s*
35 }xms;
36
37 # a few constants
38 1 Readonly my $MAX_VALID_DD => 360;
39 1 Readonly my $MIN_VALID_DD => -180;
40 1 Readonly my $MAX_VALID_LAT => 90;
41 1 Readonly my $MAX_VALID_LON => 180;
42
43 1 Readonly my $INVALID_CHAR_CONTEXT => 3;
44
45 # how many distinct numbers we can have in a DMS string?
46 1 Readonly my $MAX_DMS_NUM_COUNT => 3;
47
48 1 my $err_msg_pfx = 'DMS2DD Value error: ';
49
50 # convert degrees minutes seconds values into decimal degrees
51 # e.g.;
52 # S23°32'09.567" = -23.5359908333333
53 # 149°23'18.009"E = 149.388335833333
54 30 ▶sub dms2dd {
Subroutine dms2dd: called CC=11 SCAR=24.0
Pod undocumented
55 30 my $args = shift;
56
57 30 my $value = $args->{value};
58 30 ▶ croak "Argument 'value' not supplied\n"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 1 29 30
59 if !defined $value;
60
61 29 my $first_char_invalid;
62 29 ▶ if (not $value =~ m/ \A [\s0-9NEWSnews+-] /xms) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($value =~ / \A [\s0-9NEWSnews+-] /msx) 2 27 29
63 2 $first_char_invalid = substr $value, 0, $INVALID_CHAR_CONTEXT;
64 }
65
66 29 ▶ croak $err_msg_pfx . "Invalid string at start of value: $value\n"
Branch: 1 branch
BranchTrueFalseTotal
if defined $first_char_invalid 2 27 29
67 if defined $first_char_invalid;
68
69 27 my @nums = eval {
70 27 _dms2dd_extract_nums ( { value => $value } );
71 };
72 27 ▶ croak $EVAL_ERROR if ($EVAL_ERROR);
Branch: 1 branch
BranchTrueFalseTotal
if $Geo::Converter::dms2dd::EVAL_ERROR 3 24 27
73
74 24 my $hemi = eval {
75 24 _dms2dd_extract_hemisphere (
76 { value => $value },
77 );
78 };
79 24 ▶ croak $EVAL_ERROR if $EVAL_ERROR;
Branch: 1 branch
BranchTrueFalseTotal
if $Geo::Converter::dms2dd::EVAL_ERROR 2 22 24
80
81 22 my $multiplier = 1;
82 22 ▶ if ($hemi =~ / ^\s* [SsWw-] /xms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hemi =~ / ^\s* [SsWw-] /msx) 11 11 22
83 11 $multiplier = -1;
84 }
85
86 # now apply the defaults
87 # $deg is +ve, as hemispheres are handled separately
88 22 ▶ my $deg = abs ($nums[0] || 0);
Condition50%1/2
$nums[0] || 0
l!l
220
MC/DC0%0/1
$nums[0] || 0
$nums[0]
Truth table50%1/2
A
A= $nums[0]
Aresult
11
00
89 22 ▶ my $min = $nums[1] || 0;
Condition100%2/2
$nums[1] || 0
l!l
193
MC/DC100%1/1
$nums[1] || 0
$nums[1]
Truth table100%2/2
A
A= $nums[1]
Aresult
11
00
90 22 ▶ my $sec = $nums[2] || 0;
Condition100%2/2
$nums[2] || 0
l!l
175
MC/DC100%1/1
$nums[2] || 0
$nums[2]
Truth table100%2/2
A
A= $nums[2]
Aresult
11
00
91
92 22 my $dd = $multiplier
93 * ( $deg
94 + $min / 60
95 + $sec / 3600
96 );
97
98 22 my $valid = eval {
99 22 _dms2dd_validate_dd_value ( {
100 %$args,
101 value => $dd,
102 hemisphere => $hemi,
103 } );
104 };
105 22 ▶ croak $EVAL_ERROR if $EVAL_ERROR;
Branch: 1 branch
BranchTrueFalseTotal
if $Geo::Converter::dms2dd::EVAL_ERROR 3 19 22
106
107 #my $res = join (q{ }, $value, $dd, $multiplier, $hemi, @nums) . "\n";
108
109 19 return $dd;
110 }
111
112 # are the numbers we extracted OK?
113 # must find three or fewer of which only the last can be decimal
114 27 ▶sub _dms2dd_extract_nums {
Subroutine _dms2dd_extract_nums: called CC=13 SCAR=27.2
115 27 my $args = shift;
116
117 27 my $value = $args->{value};
118
119 27 my @nums = $value =~ m/$RE_REAL/gxms;
120 27 my $deg = $nums[0];
121 27 my $min = $nums[1];
122 27 my $sec = $nums[2];
123
124 # some verification
125 27 my $msg;
126
127 27 ▶ if (! defined $deg) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $deg) { } 0 27 27
elsif (scalar @nums > $MAX_DMS_NUM_COUNT) { } 1 26 27
128 0 $msg = 'No numeric values in string';
129 }
130 elsif (scalar @nums > $MAX_DMS_NUM_COUNT) {
131 1 $msg = 'Too many numbers in string';
132 }
133
134 27 ▶ if (defined $sec) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sec) 22 5 27
135 22 ▶ if ($min !~ / \A $RE_INT \z/xms) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $min =~ / \A $RE_INT \z/msx) { } 1 21 22
elsif ($sec < 0 or $sec > 60) { } 0 21 21
136 1 $msg = 'Seconds value given, but minutes value is floating point';
137 }
138 ▶ elsif ($sec < 0 || $sec > 60) {
Condition33%1/3
$sec < 0 or $sec > 60
l!l&&r!l&&!r
0021
MC/DC0%0/2
$sec < 0 or $sec > 60
$sec < 0 $sec > 60
Truth table33%1/3
A or B
A= $sec < 0B= $sec > 60
ABresult
1X1
011
000
139 0 $msg = 'Seconds value is out of range';
140 }
141 }
142
143 27 ▶ if (defined $min) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $min) 25 2 27
144 25 ▶ if ($deg !~ / \A $RE_INT \z/xms) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $deg =~ / \A $RE_INT \z/msx) { } 1 24 25
elsif ($min < 0 or $min > 60) { } 0 24 24
145 1 $msg = 'Minutes value given, but degrees value is floating point';
146 }
147 ▶ elsif ($min < 0 || $min > 60) {
Condition33%1/3
$min < 0 or $min > 60
l!l&&r!l&&!r
0024
MC/DC0%0/2
$min < 0 or $min > 60
$min < 0 $min > 60
Truth table33%1/3
A or B
A= $min < 0B= $min > 60
ABresult
1X1
011
000
148 0 $msg = 'Minutes value is out of range';
149 }
150 }
151
152 # the valid degrees values depend on the hemisphere,
153 # so are trapped elsewhere
154
155 #my $msg_pfx = 'DMS value error: ';
156 27 my $msg_suffix = qq{: '$value'\n};
157
158 27 ▶ croak $err_msg_pfx . $msg . $msg_suffix
Branch: 1 branch
BranchTrueFalseTotal
if $msg 3 24 27
159 if $msg;
160
161 24 ▶ return wantarray ? @nums : \@nums;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 24 0 24
162 }
163
164 22 ▶sub _dms2dd_validate_dd_value {
Subroutine _dms2dd_validate_dd_value: called CC=12 SCAR=25.6
165 22 my $args = shift;
166
167 22 my $is_lat = $args->{is_lat};
168 22 my $is_lon = $args->{is_lon};
169
170 22 my $dd = $args->{value};
171 22 my $hemi = $args->{hemisphere};
172
173 22 my $msg_pfx = 'DMS2DD Coord error: ';
174 22 my $msg;
175
176 # if we know the hemisphere then check it is in bounds,
177 # otherwise it must be in the interval [-180,360]
178 22 ▶ if ($is_lat // ($hemi =~ / ^[SsNn] /xms)) {
Branch: 3 branches
BranchTrueFalseTotal
if ($is_lat // $hemi =~ / ^[SsNn] /msx) { } 10 12 22
elsif ($is_lon // $hemi =~ / [EeWw] /msx) { } 8 4 12
elsif ($dd < $MIN_VALID_DD or $dd > $MAX_VALID_DD) { } 0 4 4
Condition66%2/3
$is_lat // $hemi =~ / ^[SsNn] /msx
l!l&&r!l&&!r
3190
MC/DC0%0/2
$is_lat // $hemi =~ / ^[SsNn] /msx
$is_lat $hemi =~ / ^[SsNn] /msx
Truth table66%2/3
A // B
A= $is_latB= $hemi =~ / ^[SsNn] /msx
ABresult
1X1
011
000
179 10 ▶ if ($is_lon) {
Branch: 2 branches
BranchTrueFalseTotal
if ($is_lon) { } 1 9 10
elsif (abs $dd > $MAX_VALID_LAT) { } 1 8 9
180 1 $msg = "Longitude specified, but latitude found: $dd\n"
181 }
182 elsif (abs ($dd) > $MAX_VALID_LAT) {
183 1 $msg = "Latitude out of bounds: $dd\n"
184 }
185 }
186 ▶ elsif ($is_lon // ($hemi =~ / [EeWw] /xms)) {
Condition66%2/3
$is_lon // $hemi =~ / [EeWw] /msx
l!l&&r!l&&!r
480
MC/DC0%0/2
$is_lon // $hemi =~ / [EeWw] /msx
$is_lon $hemi =~ / [EeWw] /msx
Truth table66%2/3
A // B
A= $is_lonB= $hemi =~ / [EeWw] /msx
ABresult
1X1
011
000
187 8 ▶ if ($is_lat) {
Branch: 2 branches
BranchTrueFalseTotal
if ($is_lat) { } 0 8 8
elsif (abs $dd > $MAX_VALID_LON) { } 1 7 8
188 0 $msg = "Latitude specified, but longitude found\n"
189 }
190 elsif (abs ($dd) > $MAX_VALID_LON) {
191 1 $msg = "Longitude out of bounds: $dd\n"
192 }
193 }
194 ▶ elsif ($dd < $MIN_VALID_DD || $dd > $MAX_VALID_DD) {
Condition33%1/3
$dd < $MIN_VALID_DD or $dd > $MAX_VALID_DD
l!l&&r!l&&!r
004
MC/DC0%0/2
$dd < $MIN_VALID_DD or $dd > $MAX_VALID_DD
$dd < $MIN_VALID_DD $dd > $MAX_VALID_DD
Truth table33%1/3
A or B
A= $dd < $MIN_VALID_DDB= $dd > $MAX_VALID_DD
ABresult
1X1
011
000
195 0 $msg = "Coord out of bounds: $dd\n";
196 }
197 22 ▶ croak "$msg_pfx $msg" if $msg;
Branch: 1 branch
BranchTrueFalseTotal
if $msg 3 19 22
198
199 19 return 1;
200 }
201
202 24 ▶sub _dms2dd_extract_hemisphere {
Subroutine _dms2dd_extract_hemisphere: called CC=6 SCAR=17.9
203 24 my $args = shift;
204
205 24 my $value = $args->{value};
206
207 24 my $hemi;
208 # can start with [NESWnesw-]
209 24 ▶ if ($value =~ m/ \A ( $RE_HEMI | [-] )/xms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ / \A ( $RE_HEMI | [-] )/msx) 14 10 24
210 14 $hemi = $1;
211 }
212 # cannot end with [-]
213 24 ▶ if ($value =~ m/ ( $RE_HEMI ) \z /xms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ / ( $RE_HEMI ) \z /msx) 11 13 24
214 11 my $hemi_end = $1;
215
216 11 ▶ croak "Cannot define hemisphere twice: $value\n"
Branch: 1 branch
BranchTrueFalseTotal
if defined $hemi and defined $hemi_end 2 9 11
Condition66%2/3
defined $hemi and defined $hemi_end
!ll&&!rl&&r
902
MC/DC50%1/2
defined $hemi and defined $hemi_end
defined $hemi defined $hemi_end
Truth table66%2/3
A and B
A= defined $hemiB= defined $hemi_end
ABresult
0X0
100
111
217 if (defined $hemi && defined $hemi_end);
218
219 9 $hemi = $hemi_end;
220 }
221 22 ▶ if (! defined $hemi) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $hemi) 1 21 22
222 1 $hemi = q{};
223 }
224
225 22 return $hemi;
226 }
227
228
229 1 1;
230
231
232 =pod
233
234 =encoding ISO8859-1
235
236 =head1 NAME
237
238 Geo::Converter::dms2dd
239
240 =head1 VERSION
241
242 0.02
243
244 =head1 SYNOPSIS
245
246 use Geo::Converter::dms2dd qw { dms2dd };
247
248 my $dms_value;
249 my $dd_value;
250
251 $dms_value = q{S23°32'09.567"};
252 $dd_value = dms2dd ({value => $dms_value});
253 print $dms_value
254 # -23.5359908333333
255
256 $dms_value = q{149°23'18.009"E};
257 $dd_value = dms2dd ({value => $dms_value});
258 print $dd_value
259 # 149.388335833333
260
261 $dms_value = q{east 149°23'18.009};
262 $dd_value = dms2dd ({value => $dms_value});
263 print $dd_value
264 # 149.388335833333
265
266
267 # The following all croak with warnings:
268
269 $dms_value = q{S23°32'09.567"};
270 $dd_value = dms2dd ({value => $dms_value, is_lon => 1});
271 # Coord error: Longitude specified, but latitude found
272
273 $dms_value = q{149°23'18.009"E};
274 $dd_value = dms2dd ({value => $dms_value, is_lat => 1});
275 # Coord error: Latitude out of bounds: 149.388335833333
276
277 $dms_value = q{149°23'18.009"25}; # extra number
278 $dd_value = dms2dd ({value => $dms_value});
279 # DMS value error: Too many numbers in string: '149°23'18.009"25'
280
281
282 =head1 DESCRIPTION
283
284 Use this module to convert a coordinate value in degrees minutes seconds
285 to decimal degrees. It exports a single sub C<dms2dd> which will
286 parse and convert a single value.
287
288 A reasonable amount of location information is provided in
289 degrees/minutes/seconds (DMS) format, for example from Google Earth, GIS packages or
290 similar. For example, one might be given a location coordinate for just north east
291 of Dingo in Queensland, Australia. Four possible formats are:
292
293 S23°32'09.567", E149°23'18.009"
294 23°32'09.567"S, 149°23'18.009"E
295 -23 32 9.567, +149 23 18.009
296 -23.535991, 149.388336
297
298 The first three coordinates are in degrees/minutes/seconds while the fourth
299 is in decimal degrees. The fourth coordinate can be used in numeric
300 calculations, but the first three must first be converted to decimal degrees.
301
302 The conversion process used in dms2dd is pretty generous in what it treats as DMS,
303 as there is a multitude of variations in punctuation and the like.
304 Up to three numeric values are extracted and any additional text is largely
305 ignored unless it could be interpreted as the hemisphere (see below).
306 It croaks if there are four or more numeric values.
307 If the hemisphere is known or the C<is_lat> or C<is_lon> arguments are specified then
308 values are validated (e.g. latitudes must be in the interval [-90, 90],
309 and longitudes with a hemisphere specified must be within [-180, 180]).
310 Otherwise values between [-180, 360] are accepted. If seconds are specified
311 and minutes have values after the radix (decimal point) then it croaks
312 (e.g. 35 26.5' 22"). Likewise, it croaks for cases like (35.2d 26').
313 It will also croak if you specify the hemisphere at the start and end of the
314 value, even if it is the same hemisphere.
315
316 Note that this module only works on a single value.
317 Call it once each for latitude and longitude values to convert a full coordinate.
318
319 =head1 AUTHOR
320
321 Shawn Laffan S<(I<shawnlaffan@gmail.com>)>.
322
323 =head1 BUGS AND IRRITATIONS
324
325 Hemispheres are very liberally interpreted. So long as the text component
326 starts with a valid character then it is used. This means that
327 (E 35 26') is treated the same as (Egregious 35 26').
328
329 It also does not deal with non-English spellings of north, south, east or west.
330 Hemispheres need to satisfy qr/[NESWnesw+-]/. A solution could be to drop
331 in an appropriate regexp as an argument, or maybe there is an i18n
332 solution. Patches welcome.
333
334 It could probably also give the parsed degrees, minutes and seconds rather
335 than convert them. They are pretty easy to calculate, though.
336
337
338 =head1 LICENSE
339
340 This library is free software; you can redistribute it and/or modify
341 it under the same terms as Perl itself, either Perl version 5.8.9 or,
342 at your option, any later version of Perl 5 you may have available.
343
344 =head1 See also
345
346 L<Geo::Coordinates::DecimalDegrees>, although it requires the
347 degrees, minutes and seconds values to already be parsed from the string.
348
349 =cut
350
351
↑ Summary