blib/lib/Geo/Coordinates/UTM.pm

filter: Statementstmt 98.9% 186 / 188 Branchbran 80.9% 102 / 126 Conditioncond 67.6% 69 / 102 MC/DCmcdc 49.2% 32 / 65 Subroutinesub 94.7% 18 / 19 Podpod 100% 9 / 9 totaltotal 81.7% 416 / 509 CC 100 SCAR 33.0
CC
100
Cov
86%
_latlon_to_utm
55.1
mgrs_to_utm
17.8
_latlon_zone_number
15.0
CRAP 27.1
↑ Summary
1 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package Geo::Coordinates::UTM;
5  
6 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 3 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 3 our @EXPORT = qw( latlon_to_utm latlon_to_utm_force_zone
11 utm_to_latlon utm_to_mgrs
12 latlon_to_mgrs mgrs_to_utm mgrs_to_latlon
13 ellipsoid_info ellipsoid_names );
14  
15 3 our $VERSION = '0.11';
16  
17 3 ▶use Math::Trig;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 3 my $deg2rad = pi / 180;
19 3 my $rad2deg = 180 / pi;
20  
21 # remove all markup from an ellipsoid name, to increase the chance
22 # that a match is found.
23 sub _cleanup_name($)
24 355 ▶{ my $copy = lc(shift);
Subroutine _cleanup_name: called CC=2 SCAR=6.9
25 355 for($copy)
26 355 { s/\([^)]+\)//g; # remove text between parantheses
27 355 s/[\s-]//g; # no blanks or dashes
28 }
29 355 $copy;
30 }
31  
32 # Ellipsoid array (name,equatorial radius,square of eccentricity)
33 # Same data also as hash with key eq name (in variations)
34  
35 3 my (@Ellipsoid, %Ellipsoid);
36  
37  
38 3 ▶BEGIN { # Initialize this before other modules get a chance
Subroutine BEGIN: called CC=2 SCAR=6.9
39 3 @Ellipsoid =
40 ( [ "Airy", 6377563, 0.00667054]
41 , [ "Australian National", 6378160, 0.006694542]
42 , [ "Bessel 1841", 6377397, 0.006674372]
43 , [ "Bessel 1841 Nambia", 6377484, 0.006674372]
44 , [ "Clarke 1866", 6378206, 0.006768658]
45 , [ "Clarke 1880", 6378249, 0.006803511]
46 , [ "Everest 1830 India", 6377276, 0.006637847]
47 , [ "Fischer 1960 Mercury", 6378166, 0.006693422]
48 , [ "Fischer 1968", 6378150, 0.006693422]
49 , [ "GRS 1967", 6378160, 0.006694605]
50 , [ "GRS 1980", 6378137, 0.00669438]
51 , [ "Helmert 1906", 6378200, 0.006693422]
52 , [ "Hough", 6378270, 0.00672267]
53 , [ "International", 6378388, 0.00672267]
54 , [ "Krassovsky", 6378245, 0.006693422]
55 , [ "Modified Airy", 6377340, 0.00667054]
56 , [ "Modified Everest", 6377304, 0.006637847]
57 , [ "Modified Fischer 1960", 6378155, 0.006693422]
58 , [ "South American 1969", 6378160, 0.006694542]
59 , [ "WGS 60", 6378165, 0.006693422]
60 , [ "WGS 66", 6378145, 0.006694542]
61 , [ "WGS-72", 6378135, 0.006694318]
62 , [ "WGS-84", 6378137, 0.00669438 ]
63 , [ "Everest 1830 Malaysia", 6377299, 0.006637847]
64 , [ "Everest 1956 India", 6377301, 0.006637847]
65 , [ "Everest 1964 Malaysia and Singapore", 6377304, 0.006637847]
66 , [ "Everest 1969 Malaysia", 6377296, 0.006637847]
67 , [ "Everest Pakistan", 6377296, 0.006637534]
68 , [ "Indonesian 1974", 6378160, 0.006694609]
69 , [ "Arc 1950", 6378249.145,0.006803481]
70 , [ "NAD 27",6378206.4,0.006768658]
71 , [ "NAD 83",6378137,0.006694384]
72 );
73  
74 # calc ecc as
75 # a = semi major axis
76 # b = semi minor axis
77 # e^2 = (a^2-b^2)/a^2
78 # For clarke 1880 (Arc1950) a=6378249.145 b=6356514.966398753
79 # e^2 (40682062155693.23 - 40405282518051.34) / 40682062155693.23
80 # e^2 = 0.0068034810178165
81
82  
83 3 foreach my $el (@Ellipsoid)
84 96 { my ($name, $eqrad, $eccsq) = @$el;
85 96 $Ellipsoid{$name} = $el;
86 96 $Ellipsoid{_cleanup_name $name} = $el;
87 }
88 }
89  
90 sub _valid_utm_zone($)
91 1117 ▶{ my $char = shift;
Subroutine _valid_utm_zone: called CC=1 SCAR=0.0
92 1117 index("CDEFGHJKLMNPQRSTUVWX", $char) >= 0;
93 }
94  
95 # Returns all pre-defined ellipsoid names, sorted alphabetically
96 sub ellipsoid_names()
97 0 ▶{ map { $_->[0] } @Ellipsoid;
Subroutine ellipsoid_names: not called CC=1 SCAR=6.9
Pod documented
98 }
99  
100 # Returns "official" name, equator radius and square eccentricity
101 # The specified name can be numeric (for compatibility reasons) or
102 # a more-or-less exact name
103 # Examples: my($name, $r, $sqecc) = ellipsoid_info 'wgs84';
104 # my($name, $r, $sqecc) = ellipsoid_info 'WGS 84';
105 # my($name, $r, $sqecc) = ellipsoid_info 'WGS-84';
106 # my($name, $r, $sqecc) = ellipsoid_info 'WGS-84 (new specs)';
107 # my($name, $r, $sqecc) = ellipsoid_info 22;
108  
109 sub ellipsoid_info($)
110 3077 ▶{ my $id = shift;
Subroutine ellipsoid_info: called CC=4 SCAR=14.2
Pod documented
111  
112 my $el = $id !~ m/\D/
113 ? $Ellipsoid[$id-1] # old system counted from 1
114 3077 ▶ : $Ellipsoid{$id} || $Ellipsoid{_cleanup_name $id};
Branch: 1 branch
BranchTrueFalseTotal
!($id =~ /\D/) ? : 2 3075 3077
Condition66%2/3
$Ellipsoid{$id} or $Ellipsoid{&_cleanup_name($id)}
l!l&&r!l&&!r
28162590
MC/DC0%0/2
$Ellipsoid{$id} or $Ellipsoid{&_cleanup_name($id)}
$Ellipsoid{$id} $Ellipsoid{&_cleanup_name($id)}
Truth table66%2/3
A or B
A= $Ellipsoid{$id}B= $Ellipsoid{&_cleanup_name($id)}
ABresult
1X1
011
000
115  
116 3077 ▶ defined $el ? @$el : ();
Branch: 1 branch
BranchTrueFalseTotal
defined $el ? : 3077 0 3077
117 }
118  
119 # Expects Ellipsoid Number or name, Latitude, Longitude
120 # (Latitude and Longitude in decimal degrees)
121 # Returns UTM Zone, UTM Easting, UTM Northing
122  
123 sub latlon_to_utm($$$)
124 1039 ▶{ my ($ellips, $latitude, $longitude) = @_;
Subroutine latlon_to_utm: called CC=3 SCAR=11.8
Pod documented
125  
126 1039 ▶ croak "Longitude value ($longitude) invalid."
Branch: 1 branch
BranchTrueFalseTotal
if $longitude < -180 or $longitude > 180 0 1039 1039
Condition33%1/3
$longitude < -180 or $longitude > 180
l!l&&r!l&&!r
001039
MC/DC0%0/2
$longitude < -180 or $longitude > 180
$longitude < -180 $longitude > 180
Truth table33%1/3
A or B
A= $longitude < -180B= $longitude > 180
ABresult
1X1
011
000
127 if $longitude < -180 || $longitude > 180;
128  
129 1039 my $long2 = $longitude - int(($longitude + 180)/360) * 360;
130 1039 my $zone = _latlon_zone_number($latitude, $long2);
131  
132 1039 _latlon_to_utm($ellips, $zone, $latitude, $long2);
133 }
134  
135 sub latlon_to_utm_force_zone($$$$)
136 999 ▶{ my ($ellips, $zone, $latitude, $longitude) = @_;
Subroutine latlon_to_utm_force_zone: called CC=5 SCAR=18.4
Pod documented
137  
138 999 ▶ croak "Longitude value ($longitude) invalid."
Branch: 1 branch
BranchTrueFalseTotal
if $longitude < -180 or $longitude > 180 0 999 999
Condition33%1/3
$longitude < -180 or $longitude > 180
l!l&&r!l&&!r
00999
MC/DC0%0/2
$longitude < -180 or $longitude > 180
$longitude < -180 $longitude > 180
Truth table33%1/3
A or B
A= $longitude < -180B= $longitude > 180
ABresult
1X1
011
000
139 if $longitude < -180 || $longitude > 180;
140  
141 999 my $long2 = $longitude - int(($longitude + 180)/360) * 360;
142  
143 999 my ($zone_number) = $zone =~ /^(\d+)[CDEFGHJKLMNPQRSTUVWX]?$/i;
144  
145 999 ▶ croak "Zone value ($zone) invalid."
Branch: 1 branch
BranchTrueFalseTotal
unless defined $zone_number and $zone_number <= 60 0 999 999
Condition33%1/3
defined $zone_number and $zone_number <= 60
!ll&&!rl&&r
00999
MC/DC0%0/2
defined $zone_number and $zone_number <= 60
defined $zone_number $zone_number <= 60
Truth table33%1/3
A and B
A= defined $zone_numberB= $zone_number <= 60
ABresult
0X0
100
111
146 unless defined($zone_number) && $zone_number <= 60;
147  
148 999 _latlon_to_utm($ellips, $zone_number, $latitude, $long2);
149 }
150  
151 sub _latlon_zone_number
152 1039 ▶{ my ($latitude, $long2) = @_;
Subroutine _latlon_zone_number: called CC=15 SCAR=27.1
153  
154 1039 my $zone = int( ($long2 + 180)/6) + 1;
155 1039 ▶ if($latitude >= 56.0 && $latitude < 64.0 && $long2 >= 3.0 && $long2 < 12.0)
Branch: 1 branch
BranchTrueFalseTotal
if ($latitude >= 56 and $latitude < 64 and $long2 >= 3 and $long2 < 12) 1 1038 1039
Condition100%9/9
$latitude >= 56 and $latitude < 64 and $long2 >= 3 and $long2 < 12
!ll&&!rl&&r
1012261
$latitude >= 56 and $latitude < 64 and $long2 >= 3
!ll&&!rl&&r
9773527
$latitude >= 56 and $latitude < 64
!ll&&!rl&&r
86111662
MC/DC100%4/4
$latitude >= 56 and $latitude < 64 and $long2 >= 3 and $long2 < 12
$latitude >= 56 $latitude < 64 $long2 >= 3 $long2 < 12
Truth table100%5/5
A and B and C and D
A= $latitude >= 56B= $latitude < 64C= $long2 >= 3D= $long2 < 12
ABCDresult
0XXX0
10XX0
110X0
11100
11111
156 1 { $zone = 32;
157 }
158 1039 ▶ if($latitude >= 72.0 && $latitude < 84.0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($latitude >= 72 and $latitude < 84) 82 957 1039
Condition66%2/3
$latitude >= 72 and $latitude < 84
!ll&&!rl&&r
957082
MC/DC50%1/2
$latitude >= 72 and $latitude < 84
$latitude >= 72 $latitude < 84
Truth table66%2/3
A and B
A= $latitude >= 72B= $latitude < 84
ABresult
0X0
100
111
159 82 ▶ $zone = ($long2 >= 0.0 && $long2 < 9.0) ? 31
Branch: 4 branches
BranchTrueFalseTotal
$long2 >= 0.0 && $long2 < 9.0 ? : 3 79 82
$long2 >= 9.0 && $long2 < 21.0 ? : 2 77 79
$long2 >= 21.0 && $long2 < 33.0 ? : 4 73 77
$long2 >= 33.0 && $long2 < 42.0 ? : 2 71 73
Condition100%12/12
$long2 >= 0 and $long2 < 9
!ll&&!rl&&r
40393
$long2 >= 9 and $long2 < 21
!ll&&!rl&&r
40372
$long2 >= 21 and $long2 < 33
!ll&&!rl&&r
40334
$long2 >= 33 and $long2 < 42
!ll&&!rl&&r
40312
MC/DC100%2/2
$long2 >= 0 and $long2 < 9
$long2 >= 0 $long2 < 9
MC/DC100%2/2
$long2 >= 9 and $long2 < 21
$long2 >= 9 $long2 < 21
MC/DC100%2/2
$long2 >= 21 and $long2 < 33
$long2 >= 21 $long2 < 33
MC/DC100%2/2
$long2 >= 33 and $long2 < 42
$long2 >= 33 $long2 < 42
Truth table100%3/3
A and B
A= $long2 >= 0B= $long2 < 9
ABresult
0X0
100
111
Truth table100%3/3
A and B
A= $long2 >= 9B= $long2 < 21
ABresult
0X0
100
111
Truth table100%3/3
A and B
A= $long2 >= 21B= $long2 < 33
ABresult
0X0
100
111
Truth table100%3/3
A and B
A= $long2 >= 33B= $long2 < 42
ABresult
0X0
100
111
160 : ($long2 >= 9.0 && $long2 < 21.0) ? 33
161 : ($long2 >= 21.0 && $long2 < 33.0) ? 35
162 : ($long2 >= 33.0 && $long2 < 42.0) ? 37
163 : $zone;
164 }
165 1039 return $zone;
166 }
167  
168 sub _latlon_to_utm
169 2038 ▶{ my ($ellips, $zone, $latitude, $long2) = @_;
Subroutine _latlon_to_utm: called CC=43 SCAR=40.1
170  
171 2038 ▶ my ($name, $radius, $eccentricity) = ellipsoid_info $ellips
Branch: 1 branch
BranchTrueFalseTotal
() = &ellipsoid_info($ellips) or croak("Ellipsoid value ($ellips) invalid.") 2038 0 2038
172 or croak "Ellipsoid value ($ellips) invalid.";
173  
174 2038 my $lat_radian = $deg2rad * $latitude;
175 2038 my $long_radian = $deg2rad * $long2;
176  
177 2038 my $k0 = 0.9996; # scale
178  
179 2038 my $longorigin = ($zone - 1)*6 - 180 + 3;
180 2038 my $longoriginradian = $deg2rad * $longorigin;
181 2038 my $eccentprime = $eccentricity/(1-$eccentricity);
182
183 2038 my $N = $radius / sqrt(1-$eccentricity * sin($lat_radian)*sin($lat_radian));
184 2038 my $T = tan($lat_radian) * tan($lat_radian);
185 2038 my $C = $eccentprime * cos($lat_radian)*cos($lat_radian);
186 2038 my $A = cos($lat_radian) * ($long_radian - $longoriginradian);
187 2038 my $M = $radius
188 * ( ( 1 - $eccentricity/4 - 3 * $eccentricity * $eccentricity/64
189 - 5 * $eccentricity * $eccentricity * $eccentricity/256
190 ) * $lat_radian
191 - ( 3 * $eccentricity/8 + 3 * $eccentricity * $eccentricity/32
192 + 45 * $eccentricity * $eccentricity * $eccentricity/1024
193 ) * sin(2 * $lat_radian)
194 + ( 15 * $eccentricity * $eccentricity/256 +
195 45 * $eccentricity * $eccentricity * $eccentricity/1024
196 ) * sin(4 * $lat_radian)
197 - ( 35 * $eccentricity * $eccentricity * $eccentricity/3072
198 ) * sin(6 * $lat_radian)
199 );
200  
201 2038 my $utm_easting = $k0*$N*($A+(1-$T+$C)*$A*$A*$A/6
202 + (5-18*$T+$T*$T+72*$C-58*$eccentprime)*$A*$A*$A*$A*$A/120)
203 + 500000.0;
204  
205 2038 my $utm_northing= $k0 * ( $M + $N*tan($lat_radian) * ( $A*$A/2+(5-$T+9*$C+4*$C*$C)*$A*$A*$A*$A/24 + (61-58*$T+$T*$T+600*$C-330*$eccentprime) * $A*$A*$A*$A*$A*$A/720));
206  
207 2038 ▶ $utm_northing += 10000000.0 if $latitude < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $latitude < 0 992 1046 2038
208  
209 2038 ▶ my $utm_letter
Branch: 20 branches
BranchTrueFalseTotal
84 >= $latitude && $latitude >= 72 ? : 162 1876 2038
72 > $latitude && $latitude >= 64 ? : 68 1808 1876
64 > $latitude && $latitude >= 56 ? : 118 1690 1808
56 > $latitude && $latitude >= 48 ? : 93 1597 1690
48 > $latitude && $latitude >= 40 ? : 103 1494 1597
40 > $latitude && $latitude >= 32 ? : 108 1386 1494
32 > $latitude && $latitude >= 24 ? : 74 1312 1386
24 > $latitude && $latitude >= 16 ? : 91 1221 1312
16 > $latitude && $latitude >= 8 ? : 113 1108 1221
8 > $latitude && $latitude >= 0 ? : 116 992 1108
0 > $latitude && $latitude >= -8 ? : 87 905 992
-8 > $latitude && $latitude >= -16 ? : 86 819 905
-16 > $latitude && $latitude >= -24 ? : 87 732 819
-24 > $latitude && $latitude >= -32 ? : 92 640 732
-32 > $latitude && $latitude >= -40 ? : 119 521 640
-40 > $latitude && $latitude >= -48 ? : 81 440 521
-48 > $latitude && $latitude >= -56 ? : 87 353 440
-56 > $latitude && $latitude >= -64 ? : 107 246 353
-64 > $latitude && $latitude >= -72 ? : 102 144 246
-72 > $latitude && $latitude >= -80 ? : 144 0 144
Condition65%39/60
84 >= $latitude and $latitude >= 72
!ll&&!rl&&r
01876162
72 > $latitude and $latitude >= 64
!ll&&!rl&&r
0180868
64 > $latitude and $latitude >= 56
!ll&&!rl&&r
01690118
56 > $latitude and $latitude >= 48
!ll&&!rl&&r
0159793
48 > $latitude and $latitude >= 40
!ll&&!rl&&r
01494103
40 > $latitude and $latitude >= 32
!ll&&!rl&&r
01386108
32 > $latitude and $latitude >= 24
!ll&&!rl&&r
0131274
24 > $latitude and $latitude >= 16
!ll&&!rl&&r
0122191
16 > $latitude and $latitude >= 8
!ll&&!rl&&r
01108113
8 > $latitude and $latitude >= 0
!ll&&!rl&&r
0992116
0 > $latitude and $latitude >= -8
!ll&&!rl&&r
090587
-8 > $latitude and $latitude >= -16
!ll&&!rl&&r
081986
-16 > $latitude and $latitude >= -24
!ll&&!rl&&r
073287
-24 > $latitude and $latitude >= -32
!ll&&!rl&&r
064092
-32 > $latitude and $latitude >= -40
!ll&&!rl&&r
0521119
-40 > $latitude and $latitude >= -48
!ll&&!rl&&r
044081
-48 > $latitude and $latitude >= -56
!ll&&!rl&&r
035387
-56 > $latitude and $latitude >= -64
!ll&&!rl&&r
0246107
-64 > $latitude and $latitude >= -72
!ll&&!rl&&r
0144102
-72 > $latitude and $latitude >= -80
!ll&&!rl&&r
00144
MC/DC50%1/2
84 >= $latitude and $latitude >= 72
84 >= $latitude $latitude >= 72
MC/DC50%1/2
72 > $latitude and $latitude >= 64
72 > $latitude $latitude >= 64
MC/DC50%1/2
64 > $latitude and $latitude >= 56
64 > $latitude $latitude >= 56
MC/DC50%1/2
56 > $latitude and $latitude >= 48
56 > $latitude $latitude >= 48
MC/DC50%1/2
48 > $latitude and $latitude >= 40
48 > $latitude $latitude >= 40
MC/DC50%1/2
40 > $latitude and $latitude >= 32
40 > $latitude $latitude >= 32
MC/DC50%1/2
32 > $latitude and $latitude >= 24
32 > $latitude $latitude >= 24
MC/DC50%1/2
24 > $latitude and $latitude >= 16
24 > $latitude $latitude >= 16
MC/DC50%1/2
16 > $latitude and $latitude >= 8
16 > $latitude $latitude >= 8
MC/DC50%1/2
8 > $latitude and $latitude >= 0
8 > $latitude $latitude >= 0
MC/DC50%1/2
0 > $latitude and $latitude >= -8
0 > $latitude $latitude >= -8
MC/DC50%1/2
-8 > $latitude and $latitude >= -16
-8 > $latitude $latitude >= -16
MC/DC50%1/2
-16 > $latitude and $latitude >= -24
-16 > $latitude $latitude >= -24
MC/DC50%1/2
-24 > $latitude and $latitude >= -32
-24 > $latitude $latitude >= -32
MC/DC50%1/2
-32 > $latitude and $latitude >= -40
-32 > $latitude $latitude >= -40
MC/DC50%1/2
-40 > $latitude and $latitude >= -48
-40 > $latitude $latitude >= -48
MC/DC50%1/2
-48 > $latitude and $latitude >= -56
-48 > $latitude $latitude >= -56
MC/DC50%1/2
-56 > $latitude and $latitude >= -64
-56 > $latitude $latitude >= -64
MC/DC50%1/2
-64 > $latitude and $latitude >= -72
-64 > $latitude $latitude >= -72
MC/DC0%0/2
-72 > $latitude and $latitude >= -80
-72 > $latitude $latitude >= -80
Truth table66%2/3
A and B
A= 84 >= $latitudeB= $latitude >= 72
ABresult
0X0
100
111
Truth table66%2/3
A and B
A= 72 > $latitudeB= $latitude >= 64
ABresult
0X0
100
111
Truth table66%2/3
A and B
A= 64 > $latitudeB= $latitude >= 56
ABresult
0X0
100
111
Truth table66%2/3
A and B
A= 56 > $latitudeB= $latitude >= 48
ABresult
0X0
100
111
Truth table66%2/3
A and B
A= 48 > $latitudeB= $latitude >= 40
ABresult
0X0
100
111
Truth table66%2/3
A and B
A= 40 > $latitudeB= $latitude >= 32
ABresult
0X0
100
111
Truth table66%2/3
A and B
A= 32 > $latitudeB= $latitude >= 24
ABresult
0X0
100
111
Truth table66%2/3
A and B
A= 24 > $latitudeB= $latitude >= 16
ABresult
0X0
100
111
Truth table66%2/3
A and B
A= 16 > $latitudeB= $latitude >= 8
ABresult
0X0
100
111
Truth table66%2/3
A and B
A= 8 > $latitudeB= $latitude >= 0
ABresult
0X0
100
111
Truth table66%2/3
A and B
A= 0 > $latitudeB= $latitude >= -8
ABresult
0X0
100
111
Truth table66%2/3
A and B
A= -8 > $latitudeB= $latitude >= -16
ABresult
0X0
100
111
Truth table66%2/3
A and B
A= -16 > $latitudeB= $latitude >= -24
ABresult
0X0
100
111
Truth table66%2/3
A and B
A= -24 > $latitudeB= $latitude >= -32
ABresult
0X0
100
111
Truth table66%2/3
A and B
A= -32 > $latitudeB= $latitude >= -40
ABresult
0X0
100
111
Truth table66%2/3
A and B
A= -40 > $latitudeB= $latitude >= -48
ABresult
0X0
100
111
Truth table66%2/3
A and B
A= -48 > $latitudeB= $latitude >= -56
ABresult
0X0
100
111
Truth table66%2/3
A and B
A= -56 > $latitudeB= $latitude >= -64
ABresult
0X0
100
111
Truth table66%2/3
A and B
A= -64 > $latitudeB= $latitude >= -72
ABresult
0X0
100
111
Truth table33%1/3
A and B
A= -72 > $latitudeB= $latitude >= -80
ABresult
0X0
100
111
210 = ( 84 >= $latitude && $latitude >= 72) ? 'X'
211 : ( 72 > $latitude && $latitude >= 64) ? 'W'
212 : ( 64 > $latitude && $latitude >= 56) ? 'V'
213 : ( 56 > $latitude && $latitude >= 48) ? 'U'
214 : ( 48 > $latitude && $latitude >= 40) ? 'T'
215 : ( 40 > $latitude && $latitude >= 32) ? 'S'
216 : ( 32 > $latitude && $latitude >= 24) ? 'R'
217 : ( 24 > $latitude && $latitude >= 16) ? 'Q'
218 : ( 16 > $latitude && $latitude >= 8) ? 'P'
219 : ( 8 > $latitude && $latitude >= 0) ? 'N'
220 : ( 0 > $latitude && $latitude >= -8) ? 'M'
221 : ( -8 > $latitude && $latitude >= -16) ? 'L'
222 : (-16 > $latitude && $latitude >= -24) ? 'K'
223 : (-24 > $latitude && $latitude >= -32) ? 'J'
224 : (-32 > $latitude && $latitude >= -40) ? 'H'
225 : (-40 > $latitude && $latitude >= -48) ? 'G'
226 : (-48 > $latitude && $latitude >= -56) ? 'F'
227 : (-56 > $latitude && $latitude >= -64) ? 'E'
228 : (-64 > $latitude && $latitude >= -72) ? 'D'
229 : (-72 > $latitude && $latitude >= -80) ? 'C'
230 : croak "Latitude ($latitude) out of UTM range.";
231  
232 2038 $zone .= $utm_letter;
233  
234 2038 ($zone, $utm_easting, $utm_northing);
235 }
236  
237 # Expects Ellipsoid Number or name, UTM zone, UTM Easting, UTM Northing
238 # Returns Latitude, Longitude
239 # (Latitude and Longitude in decimal degrees, UTM Zone e.g. 23S)
240  
241 sub utm_to_latlon($$$$)
242 1039 ▶{ my ($ellips, $zone, $easting, $northing) = @_;
Subroutine utm_to_latlon: called CC=4 SCAR=13.9
Pod documented
243  
244 1039 ▶ my ($name, $radius, $eccentricity) = ellipsoid_info $ellips
Branch: 1 branch
BranchTrueFalseTotal
() = &ellipsoid_info($ellips) or croak("Ellipsoid value ($ellips) invalid.") 1039 0 1039
245 or croak "Ellipsoid value ($ellips) invalid.";
246
247 1039 my $zone_number = $zone;
248 1039 my $zone_letter = chop $zone_number;
249  
250 1039 ▶ croak "UTM zone ($zone_letter) invalid."
Branch: 1 branch
BranchTrueFalseTotal
unless &_valid_utm_zone($zone_letter) 0 1039 1039
251 unless _valid_utm_zone $zone_letter;
252  
253 1039 my $k0 = 0.9996;
254 1039 my $x = $easting - 500000; # Remove Longitude offset
255 1039 my $y = $northing;
256  
257 # Set hemisphere (1=Northern, 0=Southern)
258 1039 my $hemisphere = $zone_letter ge 'N';
259 1039 ▶ $y -= 10000000.0 unless $hemisphere; # Remove Southern Offset
Branch: 1 branch
BranchTrueFalseTotal
unless $hemisphere 505 534 1039
260  
261 1039 my $longorigin = ($zone_number - 1)*6 - 180 + 3;
262 1039 my $eccPrimeSquared = ($eccentricity)/(1-$eccentricity);
263 1039 my $M = $y/$k0;
264 1039 my $mu = $M/($radius*(1-$eccentricity/4-3*$eccentricity*$eccentricity/64-5*$eccentricity*$eccentricity*$eccentricity/256));
265  
266 1039 my $e1 = (1-sqrt(1-$eccentricity))/(1+sqrt(1-$eccentricity));
267 1039 my $phi1rad = $mu+(3*$e1/2-27*$e1*$e1*$e1/32)*sin(2*$mu)+(21*$e1*$e1/16-55*$e1*$e1*$e1*$e1/32)*sin(4*$mu)+(151*$e1*$e1*$e1/96)*sin(6*$mu);
268 1039 my $phi1 = $phi1rad*$rad2deg;
269 1039 my $N1 = $radius/sqrt(1-$eccentricity*sin($phi1rad)*sin($phi1rad));
270 1039 my $T1 = tan($phi1rad)*tan($phi1rad);
271 1039 my $C1 = $eccentricity*cos($phi1rad)*cos($phi1rad);
272 1039 my $R1 = $radius * (1-$eccentricity)
273 / ((1-$eccentricity*sin($phi1rad)*sin($phi1rad))**1.5);
274 1039 my $D = $x/($N1*$k0);
275  
276 1039 my $Latitude = $phi1rad-($N1*tan($phi1rad)/$R1)*($D*$D/2-(5+3*$T1+10*$C1-4*$C1*$C1-9*$eccPrimeSquared)*$D*$D*$D*$D/24+(61+90*$T1+298*$C1+45*$T1*$T1-252*$eccPrimeSquared-3*$C1*$C1)*$D*$D*$D*$D*$D*$D/720);
277 1039 $Latitude = $Latitude * $rad2deg;
278  
279 1039 my $Longitude = ($D-(1+2*$T1+$C1)*$D*$D*$D/6+(5-2*$C1+28*$T1-3*$C1*$C1+8*$eccPrimeSquared+24*$T1*$T1)*$D*$D*$D*$D*$D/120)/cos($phi1rad);
280 1039 $Longitude = $longorigin + $Longitude * $rad2deg;
281  
282 1039 ($Latitude, $Longitude);
283 }
284  
285 sub utm_to_mgrs($$$)
286 39 ▶{ my ($zone,$easting,$northing) = @_;
Subroutine utm_to_mgrs: called CC=14 SCAR=26.8
Pod documented
287 39 my $zone_number = $zone;
288 39 my $zone_letter = chop $zone_number;
289  
290 39 ▶ croak "UTM zone ($zone_letter) invalid."
Branch: 1 branch
BranchTrueFalseTotal
unless &_valid_utm_zone($zone_letter) 0 39 39
291 unless _valid_utm_zone $zone_letter;
292  
293 39 my $northing_zones="ABCDEFGHJKLMNPQRSTUV";
294 39 my $rnd_north=sprintf("%d",$northing);
295 39 my $north_split=length($rnd_north)-5;
296 39 ▶ $north_split=0 if $north_split < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $north_split < 0 0 39 39
297 39 my $mgrs_north=substr($rnd_north,(length($rnd_north)-5));
298 39 ▶ $rnd_north -=2000000 while ($rnd_north >= 2000000);
Branch: 1 branch
BranchTrueFalseTotal
$rnd_north >= 2000000 and do { $rnd_north -= 2000000 } 83 39 122
299 39 ▶ $rnd_north+=2000000 if $rnd_north < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $rnd_north < 0 0 39 39
300 39 my $num_north=int($rnd_north/100000);
301 39 ▶ $num_north+=5 if not ($zone_number % 2);
Branch: 1 branch
BranchTrueFalseTotal
unless $zone_number % 2 14 25 39
302 39 ▶ $num_north-=20 until $num_north <20;
Branch: 1 branch
BranchTrueFalseTotal
$num_north < 20 or do { $num_north -= 20 } 39 0 39
303 39 my $lett_north=substr($northing_zones,$num_north,1);
304  
305 39 my $rnd_east=sprintf("%d",$easting);
306 39 my $east_split=length($rnd_east)-5;
307 39 ▶ $east_split=0 if $east_split < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $east_split < 0 0 39 39
308 39 my $mgrs_east=substr($rnd_east,(length($rnd_east)-5));
309 39 my $num_east=substr($rnd_east,0,(length($rnd_east)-5));
310 39 ▶ $num_east=0 if not $num_east;
Branch: 1 branch
BranchTrueFalseTotal
unless $num_east 0 39 39
311 39 my $mgrs_zone=$zone_number;
312 39 ▶ $mgrs_zone-=3 until $mgrs_zone < 4;
Branch: 1 branch
BranchTrueFalseTotal
$mgrs_zone < 4 or do { $mgrs_zone -= 3 } 39 371 410
313 # zones are 6deg wide, mgrs letters are 18deg = 8 per zone
314 # calculate which zone required
315 39 ▶ my $easting_zones
Branch: 3 branches
BranchTrueFalseTotal
$mgrs_zone == 1 ? : 11 28 39
$mgrs_zone == 2 ? : 13 15 28
$mgrs_zone == 3 ? : 15 0 15
316 = ( $mgrs_zone == 1) ? 'ABCDEFGH'
317 : ( $mgrs_zone == 2) ? 'JKLMNPQR'
318 : ( $mgrs_zone == 3) ? 'STUVWXYZ'
319 : croak "Could not calculate MGRS zone.";
320 39 $num_east--;
321 39 ▶ my $lett_east=substr($easting_zones,$num_east,1) or croak "Could not detect Easting Zone for MGRS coordinate";
Branch: 1 branch
BranchTrueFalseTotal
my $lett_east = substr($easting_zones, $num_east, 1) or croak("Could not detect Easting Zone for MGRS coordinate") 39 0 39
322  
323 39 my $MGRS="$zone$lett_east$lett_north$mgrs_east$mgrs_north";
324 39 ($MGRS);
325 }
326  
327 sub latlon_to_mgrs($$$)
328 39 ▶{ my ($ellips, $latitude, $longitude) = @_;
Subroutine latlon_to_mgrs: called CC=1 SCAR=0.0
Pod documented
329 39 my ($zone,$x_coord,$y_coord)=latlon_to_utm($ellips, $latitude, $longitude);
330 39 my $mgrs_string=utm_to_mgrs($zone,$x_coord,$y_coord);
331 39 ($mgrs_string);
332 }
333  
334  
335 sub mgrs_to_utm($)
336 39 ▶{ my ($mgrs_string) = @_;
Subroutine mgrs_to_utm: called CC=17 SCAR=28.8
Pod documented
337 39 my $zone = substr($mgrs_string,0,2);
338 39 ▶ $mgrs_string = "0$mgrs_string" if !($zone =~ /^\d+$/);
Branch: 1 branch
BranchTrueFalseTotal
unless $zone =~ /^\d+$/ 7 32 39
339 39 $zone = substr($mgrs_string,0,3);
340 39 my $zone_number = $zone;
341 39 my $zone_letter = chop $zone_number;
342  
343 39 ▶ croak "UTM zone ($zone_letter) invalid."
Branch: 1 branch
BranchTrueFalseTotal
unless &_valid_utm_zone($zone_letter) 0 39 39
344 unless _valid_utm_zone $zone_letter;
345  
346 39 my $first_letter = substr($mgrs_string,3,1);
347 39 ▶ croak "MGRS zone ($first_letter) invalid."
Branch: 1 branch
BranchTrueFalseTotal
unless $first_letter =~ /[ABCDEFGHJKLMNPQRSTUVWXYZ]/ 0 39 39
348 unless $first_letter =~ /[ABCDEFGHJKLMNPQRSTUVWXYZ]/;
349  
350 39 my $second_letter = substr($mgrs_string,4,1);
351 39 ▶ croak "MGRS zone ($second_letter) invalid."
Branch: 1 branch
BranchTrueFalseTotal
unless $second_letter =~ /[ABCDEFGHJKLMNPQRSTUV]/ 0 39 39
352 unless $second_letter =~ /[ABCDEFGHJKLMNPQRSTUV]/;
353  
354 39 my $coords=substr($mgrs_string,5);
355 39 my $coord_len=length($coords);
356 39 ▶ croak "MGRS coords ($coords) invalid."
Branch: 1 branch
BranchTrueFalseTotal
unless $coord_len > 0 and $coord_len <= 10 and not $coord_len % 2 0 39 39
Condition33%2/6
$coord_len > 0 and $coord_len <= 10 and not $coord_len % 2
!ll&&!rl&&r
0039
$coord_len > 0 and $coord_len <= 10
!ll&&!rl&&r
0039
MC/DC0%0/3
$coord_len > 0 and $coord_len <= 10 and not $coord_len % 2
$coord_len > 0 $coord_len <= 10 not $coord_len % 2
Truth table25%1/4
A and B and not(C)
A= $coord_len > 0B= $coord_len <= 10C= not $coord_len % 2
ABCresult
0XX0
10X0
1100
1111
357 unless ((($coord_len > 0) and ($coord_len <= 10)) and !($coord_len % 2));
358
359 39 $coord_len=int($coord_len/2);
360 39 my $x_coord=substr($coords,0,$coord_len);
361 39 my $y_coord=substr($coords,$coord_len);
362 39 $x_coord*=10 ** (5 - $coord_len);
363 39 $y_coord*=10 ** (5 - $coord_len);
364  
365 39 ▶ my $east_pos
Branch: 3 branches
BranchTrueFalseTotal
$first_letter =~ /[ABCDEFGH]/ ? : 11 28 39
$first_letter =~ /[JKLMNPQR]/ ? : 13 15 28
$first_letter =~ /[STUVWXYZ]/ ? : 15 0 15
366 = ( $first_letter =~ /[ABCDEFGH]/) ? index('ABCDEFGH',$first_letter)
367 : ( $first_letter =~ /[JKLMNPQR]/) ? index('JKLMNPQR',$first_letter)
368 : ( $first_letter =~ /[STUVWXYZ]/) ? index('STUVWXYZ',$first_letter)
369 : croak "Could not calculate MGRS Easting zone.";
370 39 ▶ croak "MGRS Letter $first_letter invalid." if $east_pos < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $east_pos < 0 0 39 39
371 39 $east_pos++;
372 39 $east_pos*=100000;
373 39 $x_coord+=$east_pos;
374  
375 39 my $northing_zones="ABCDEFGHJKLMNPQRSTUV";
376 39 my $north_pos=index($northing_zones,$second_letter);
377 39 ▶ croak "MGRS Letter $second_letter invalid." if $north_pos < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $north_pos < 0 0 39 39
378 39 $north_pos++;
379 39 ▶ $north_pos-=5 if not ($zone_number % 2);
Branch: 1 branch
BranchTrueFalseTotal
unless $zone_number % 2 14 25 39
380 39 ▶ $north_pos+=20 until $north_pos > 0;
Branch: 1 branch
BranchTrueFalseTotal
$north_pos > 0 or do { $north_pos += 20 } 39 0 39
381 39 ▶ if ($zone_letter =~ /[NPQRSTUVWX]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($zone_letter =~ /[NPQRSTUVWX]/) { } 21 18 39
382 # Northern hemisphere
383 21 my $tmpNorth=index('NPQRSTUVWX',$zone_letter);
384 21 $tmpNorth++;
385 21 $tmpNorth*=8;
386 21 $tmpNorth*=10/9;
387 21 $tmpNorth=int((($tmpNorth-$north_pos)/20)+0.5)*20;
388 21 $north_pos+=$tmpNorth;
389 21 $north_pos*=100000;
390 21 $north_pos-=100000;
391 21 $y_coord+=$north_pos;
392 }
393 else {
394 #Southern Hemisphere
395 18 my $tmpNorth=index('CDEFGHJKLM',$zone_letter);
396 18 $tmpNorth*=8;
397 18 $tmpNorth*=10/9;
398 18 $tmpNorth=int((($tmpNorth-$north_pos)/20)+0.5)*20;
399 18 $north_pos+=$tmpNorth;
400 18 $north_pos*=100000;
401 18 $north_pos-=100000;
402 18 ▶ $north_pos+=2000000 if $zone_letter ne "C";
Branch: 1 branch
BranchTrueFalseTotal
if $zone_letter ne "C" 16 2 18
403 18 $y_coord+=$north_pos;
404 }
405  
406 39 ($zone,$x_coord,$y_coord);
407 }
408  
409 sub mgrs_to_latlon($$)
410 39 ▶{ my ($ellips, $mgrs_string) = @_;
Subroutine mgrs_to_latlon: called CC=1 SCAR=0.0
Pod documented
411 39 my ($zone,$x_coord,$y_coord)=mgrs_to_utm($mgrs_string);
412 39 my ($latitude,$longitude)=utm_to_latlon($ellips,$zone,$x_coord,$y_coord);
413 39 ($latitude,$longitude);
414 }
415  
416 3 1;
417  
418 __END__
↑ Summary