blib/lib/Geo/Coordinates/Parser.pm

filter: Statementstmt 100% 34 / 34 Branchbran 100% 8 / 8 Conditioncond 87.5% 7 / 8 MC/DCmcdc 80.0% 4 / 5 Subroutinesub 100% 6 / 6 Podpod 100% 3 / 3 totaltotal 96.8% 62 / 64 CC 8 SCAR 12.9
CC
8
Cov
98%
parse
6.0
new
2.0
decimal_delimiter
2.0
CRAP 3.6
↑ Summary
1 package Geo::Coordinates::Parser;
2  
3 1 our $VERSION = '0.01';
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 Geo::Coordinates::DecimalDegrees qw(dms2decimal dm2decimal);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 =pod
10  
11 =head1 NAME
12  
13 Geo::Coordinates::Parser - A coordinate parser class.
14  
15 =head1 SYNOPSIS
16  
17 use Geo::Coordinates::Parser;
18 my $coordinateparser = Geo::Coordinates::Parser->new('.');
19 my $decimaldegree = $coordinateparser->parse(q{E25°42'60"});
20 $decimaldegree = $coordinateparser->parse(q{E25.12346"});
21  
22  
23 =head1 DESCRIPTION
24  
25 This module provides a method for parsing a coordinate string.
26  
27  
28 =head1 METHODS
29  
30 This module provides the following methods:
31  
32 =over 4
33  
34 =item new($decimal_delimiter)
35  
36 Returns a new Geo::Coordinates::Parser object. The decimal
37 delimiter can be given as an argument. If no argument is
38 given then period "." character is used as decimal delimiter.
39  
40 Usage:
41  
42 my $coordinateparser = Geo::Coordinates::Parser->new(); # or
43 my $coordinateparser = Geo::Coordinates::Parser->new('.'); # same as above, or
44 my $coordinateparser = Geo::Coordinates::Parser->new(','); # , is the decimal delimiter
45  
46 =cut
47 2 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
48 2 my $class = shift;
49 2 ▶ my $decimal_delimiter = shift || '.';
Condition100%2/2
shift() || '.'
l!l
11
MC/DC100%1/1
shift() || '.'
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
50 2 my $self = {'decimal_delimiter' => $decimal_delimiter};
51 2 bless($self, $class);
52  
53 2 return $self;
54 }
55  
56 =pod
57  
58 =item parse($coordinatestring)
59  
60 Parses the coordinate string and returns it's decimal value.
61 It uses L<Geo::Coordinates::DecimalDegrees> to turn degrees,
62 minutes and seconds into the equivalent decimal degree. The
63 argument can be either a longitude or a latitude. It doesn't
64 test the sanity of the data. The method simply removes all
65 unnecessary characters and then converts the degrees,
66 minutes and seconds to a decimal degree.
67  
68 Usage:
69  
70 my $decimal = $coordinateparser->parse('E25'42'60');
71  
72 =cut
73 10 ▶sub parse {
Subroutine parse: called CC=6 SCAR=17.9
Pod documented
74 10 my $self = shift;
75 10 my $coordinatestring = shift;
76 10 ▶ unless ($coordinatestring) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($coordinatestring) 2 8 10
77 2 return undef;
78 }
79
80 # Remove all extra
81 8 my $decimal_delimiter = $self->decimal_delimiter;
82 8 $coordinatestring =~ s/[^0-9$decimal_delimiter]/ /g;
83 8 $coordinatestring =~ s/^\s+//;
84 8 $coordinatestring =~ s/\s+$//;
85
86 # Split into degrees, minutes and seconds
87 8 my ($degrees, $minutes, $seconds) = split(/\s+/, $coordinatestring);
88  
89 # Return in decimal format with the right delimiter
90 8 ▶ if ($minutes and $seconds) {
Branch: 2 branches
BranchTrueFalseTotal
if ($minutes and $seconds) { } 3 5 8
elsif ($minutes and not $seconds) { } 3 2 5
Condition100%3/3
$minutes and $seconds
!ll&&!rl&&r
233
MC/DC100%2/2
$minutes and $seconds
$minutes $seconds
Truth table100%3/3
A and B
A= $minutesB= $seconds
ABresult
0X0
100
111
91 3 return dms2decimal($degrees, $minutes, $seconds);
92 ▶ } elsif ($minutes and not $seconds) {
Condition66%2/3
$minutes and not $seconds
!ll&&!rl&&r
203
MC/DC50%1/2
$minutes and not $seconds
$minutes not $seconds
Truth table66%2/3
A and not(B)
A= $minutesB= not $seconds
ABresult
0X0
100
111
93 3 return dm2decimal($degrees, $minutes);
94 } else {
95 2 return $degrees;
96 }
97 }
98  
99 =pod
100  
101 =item decimal_delimiter($decimal_delimiter)
102  
103 Returns the decimal delimiter. If an argument is given then
104 it's sets the delimiter to the given value.
105  
106 Usage:
107  
108 $coordinateparser->decimal_delimiter; # Returns the delimiter
109 $coordinateparser->decimal_delimiter(','); # Sets and returns , as the delimiter
110 $coordinateparser->decimal_delimiter; # Returns now , as the delimiter
111  
112 =cut
113 12 ▶sub decimal_delimiter {
Subroutine decimal_delimiter: called CC=2 SCAR=6.9
Pod documented
114 12 my $self = shift;
115 12 my $decimal_delimiter = shift;
116 12 ▶ if ($decimal_delimiter) {
Branch: 1 branch
BranchTrueFalseTotal
if ($decimal_delimiter) 2 10 12
117 2 $self->{'decimal_delimiter'} = $decimal_delimiter;
118 }
119 12 return $self->{'decimal_delimiter'};
120 }
121  
122 1 1;
123  
124 =pod
125
126 =back
127  
128  
129 =head1 REQUIRES
130  
131 L<Geo::Coordinates::DecimalDegrees>
132  
133  
134 =head1 SEE ALSO
135  
136 L<Geo::Coordinates::DecimalDegrees>
137  
138  
139 =head1 AUTHOR
140  
141 Carl Räihä, <carl.raiha at gmail.com>
142  
143  
144 =head1 COPYRIGHT AND LICENSE
145  
146 Copyright 2005 by Carl Räihä / Frantic Media
147  
148 This library is free software; you can redistribute it and/or modify
149 it under the same terms as Perl itself.
150  
151 =cut
↑ Summary