OSGB/Grid.pm

filter: Statementstmt 93.5% 188 / 201 Branchbran 79.0% 79 / 100 Conditioncond 58.9% 23 / 39 MC/DCmcdc 44.4% 8 / 18 Subroutinesub 100% 26 / 26 Podpod 100% 7 / 7 totaltotal 84.6% 331 / 391 SCAR 43.8
CC
66
Cov
85%
format_grid
20.2
parse_grid
17.6
random_grid
12.8
CRAP 79.9
↑ Summary OSGB.pm »
1 package Geo::Coordinates::OSGB::Grid;
2  
3 9 ▶use Geo::Coordinates::OSGB::Maps qw{%maps %name_for_map_series};
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use Geo::Coordinates::OSGB qw{get_ostn02_shift_pair};
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 9 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 9 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 9 ▶use 5.008; # At least Perl 5.08 please, be sure to change POD below if you update
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 9 our $VERSION = '2.19';
13  
14 9 our %EXPORT_TAGS = (all => [qw(
15 parse_grid
16 format_grid
17  
18 parse_trad_grid
19 parse_GPS_grid
20 parse_landranger_grid
21 parse_map_grid
22  
23 format_grid_trad
24 format_grid_GPS
25 format_grid_landranger
26 format_grid_map
27  
28 random_grid
29 )]);
30  
31 9 our @EXPORT_OK = ( @{ $EXPORT_TAGS{all} } );
32  
33 9 ▶use constant GRID_SQ_LETTERS => 'VWXYZQRSTULMNOPFGHJKABCDE';
Subroutine BEGIN: called CC=1 SCAR=0.0
34 9 ▶use constant GRID_SIZE => sqrt length GRID_SQ_LETTERS;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 9 ▶use constant MINOR_GRID_SQ_SIZE => 100_000;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 9 ▶use constant MAJOR_GRID_SQ_SIZE => GRID_SIZE * MINOR_GRID_SQ_SIZE;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 9 ▶use constant MAJOR_GRID_SQ_EASTING_OFFSET => 2 * MAJOR_GRID_SQ_SIZE;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 9 ▶use constant MAJOR_GRID_SQ_NORTHING_OFFSET => 1 * MAJOR_GRID_SQ_SIZE;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 9 ▶use constant MAX_GRID_SIZE => MINOR_GRID_SQ_SIZE * length GRID_SQ_LETTERS;
Subroutine BEGIN: called CC=1 SCAR=0.0
40  
41 # Produce a random GR
42 # A simple approach would pick 0 < E < 700000 and 0 < N < 1250000 but that way
43 # many GRs produced would be in the sea, so pick a random map, and then find a
44 # random GR within its bbox and finally check that the resulting pair is
45 # inside the OSTN02 boundary and actually on some map.
46  
47 sub random_grid {
48 2 ▶ my @preferred_sheets = @_;
Subroutine random_grid: called CC=10 SCAR=25.5
Pod documented
49 # assume plain sheet numbers are Landranger
50 2 for my $s (@preferred_sheets) {
51 8 ▶ if ($s =~ m{\A\d+\Z}xsmio) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s =~ /\A\d+\Z/imosx) 7 1 8
52 7 $s =~ s/\A/A:/xsmio
53 }
54 }
55 2 my @sheets;
56 2 ▶ if (@preferred_sheets) {
Branch: 1 branch
BranchTrueFalseTotal
if (@preferred_sheets) { } 2 0 2
57 2 @sheets = grep { exists $maps{$_} } @preferred_sheets;
58 }
59 else {
60 0 @sheets = keys %maps;
61 }
62 2 my $margin = 5000;
63 2 my ($map, $lle, $lln, $ure, $urn);
64 2 my ($easting, $northing);
65 2 while (1) {
66 2 $map = $maps{$sheets[int rand @sheets]};
67 2 ($lle, $lln) = @{$map->{bbox}->[0]};
68 2 ($ure, $urn) = @{$map->{bbox}->[1]};
69 2 $easting = sprintf '%.3f', $lle + rand ($ure-$lle);
70 2 $northing = sprintf '%.3f', $lln + rand ($urn-$lln);
71 # check we are well inside OSTN02 and actually on the chosen map
72 last if get_ostn02_shift_pair($easting, $northing)
73 && get_ostn02_shift_pair($easting, $northing+$margin)
74 && get_ostn02_shift_pair($easting, $northing-$margin)
75 && get_ostn02_shift_pair($easting+$margin, $northing)
76 && get_ostn02_shift_pair($easting-$margin, $northing)
77 2 ▶ && 0 != _winding_number($easting, $northing, $map->{polygon});
Branch: 1 branch
BranchTrueFalseTotal
if get_ostn02_shift_pair($easting, $northing) and get_ostn02_shift_pair($easting, $northing + $margin) and get_ostn02_shift_pair($easting, $northing - $margin) and get_ostn02_shift_pair($easting + $margin, $northing) and get_ostn02_shift_pair($easting - $margin, $northing) and 0 != _winding_number($easting, $northing, $map->{"polygon"}) 2 0 2
Condition33%5/15
get_ostn02_shift_pair($easting, $northing) and get_ostn02_shift_pair($easting, $northing + $margin) and get_ostn02_shift_pair($easting, $northing - $margin) and get_ostn02_shift_pair($easting + $margin, $northing) and get_ostn02_shift_pair($easting - $margin, $northing) and 0 != _winding_number($easting, $northing, $map->{"polygon"})
!ll&&!rl&&r
002
get_ostn02_shift_pair($easting, $northing) and get_ostn02_shift_pair($easting, $northing + $margin) and get_ostn02_shift_pair($easting, $northing - $margin) and get_ostn02_shift_pair($easting + $margin, $northing) and get_ostn02_shift_pair($easting - $margin, $northing)
!ll&&!rl&&r
002
get_ostn02_shift_pair($easting, $northing) and get_ostn02_shift_pair($easting, $northing + $margin) and get_ostn02_shift_pair($easting, $northing - $margin) and get_ostn02_shift_pair($easting + $margin, $northing)
!ll&&!rl&&r
002
get_ostn02_shift_pair($easting, $northing) and get_ostn02_shift_pair($easting, $northing + $margin) and get_ostn02_shift_pair($easting, $northing - $margin)
!ll&&!rl&&r
002
get_ostn02_shift_pair($easting, $northing) and get_ostn02_shift_pair($easting, $northing + $margin)
!ll&&!rl&&r
002
MC/DC0%0/6
get_ostn02_shift_pair($easting, $northing) and get_ostn02_shift_pair($easting, $northing + $margin) and get_ostn02_shift_pair($easting, $northing - $margin) and get_ostn02_shift_pair($easting + $margin, $northing) and get_ostn02_shift_pair($easting - $margin, $northing) and 0 != _winding_number($easting, $northing, $map->{"polygon"})
get_ostn02_shift_pair($easting, $northing) get_ostn02_shift_pair($easting, $northing + $margin) get_ostn02_shift_pair($easting, $northing - $margin) get_ostn02_shift_pair($easting + $margin, $northing) get_ostn02_shift_pair($easting - $margin, $northing) 0 != _winding_number($easting, $northing, $map->{"polygon"})
Truth table14%1/7
A and B and C and D and E and F
A= get_ostn02_shift_pair($easting, $northing)B= get_ostn02_shift_pair($easting, $northing + $margin)C= get_ostn02_shift_pair($easting, $northing - $margin)D= get_ostn02_shift_pair($easting + $margin, $northing)E= get_ostn02_shift_pair($easting - $margin, $northing)F= 0 != _winding_number($easting, $northing, $map->{"polygon"})
ABCDEFresult
0XXXXX0
10XXXX0
110XXX0
1110XX0
11110X0
1111100
1111111
78 }
79 2 return ($easting, $northing);
80 }
81  
82 sub format_grid {
83 226 ▶ my ($easting, $northing, $options) = @_;
Subroutine format_grid: called CC=20 SCAR=30.0
Pod documented
84  
85 226 ▶ my $form = exists $options->{form} ? uc $options->{form} : 'SS EEE NNN';
Branch: 1 branch
BranchTrueFalseTotal
exists $options->{'form'} ? : 89 137 226
86 226 ▶ my $with_maps = exists $options->{maps} ? $options->{maps} : 0;
Branch: 1 branch
BranchTrueFalseTotal
exists $options->{'maps'} ? : 60 166 226
87 226 ▶ my $map_keys = exists $options->{series} ? uc $options->{series} : join '', sort keys %name_for_map_series;
Branch: 1 branch
BranchTrueFalseTotal
exists $options->{'series'} ? : 8 218 226
88  
89 226 my $sq = _grid_to_sq($easting,$northing);
90 226 ▶ if ( !$sq ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sq) 0 226 226
91 0 croak "Too far off the grid: $easting $northing\n";
92 }
93  
94 226 my ($e,$n) = map { int } map { $_ % MINOR_GRID_SQ_SIZE } ($easting, $northing);
95  
96 226 my @sheets = ();
97 226 ▶ if ( $with_maps ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($with_maps) 59 167 226
98 59 ▶ while (my ($k,$m) = each %maps) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %Geo::Coordinates::OSGB::Grid::maps) 66847 59 66906
99 66847 ▶ next if index($map_keys, substr($k,0,1)) == -1;
Branch: 1 branch
BranchTrueFalseTotal
if index($map_keys, substr($k, 0, 1)) == -1 5705 61142 66847
100 61142 ▶ if ($m->{bbox}->[0][0] <= $easting && $easting < $m->{bbox}->[1][0]
Branch: 1 branch
BranchTrueFalseTotal
if ($m->{"bbox"}[0][0] <= $easting and $easting < $m->{"bbox"}[1][0] and $m->{"bbox"}[0][1] <= $northing and $northing < $m->{"bbox"}[1][1]) 209 60933 61142
Condition100%9/9
$m->{"bbox"}[0][0] <= $easting and $easting < $m->{"bbox"}[1][0] and $m->{"bbox"}[0][1] <= $northing and $northing < $m->{"bbox"}[1][1]
!ll&&!rl&&r
591161817209
$m->{"bbox"}[0][0] <= $easting and $easting < $m->{"bbox"}[1][0] and $m->{"bbox"}[0][1] <= $northing
!ll&&!rl&&r
5716419522026
$m->{"bbox"}[0][0] <= $easting and $easting < $m->{"bbox"}[1][0]
!ll&&!rl&&r
30434267303978
MC/DC100%4/4
$m->{"bbox"}[0][0] <= $easting and $easting < $m->{"bbox"}[1][0] and $m->{"bbox"}[0][1] <= $northing and $northing < $m->{"bbox"}[1][1]
$m->{"bbox"}[0][0] <= $easting $easting < $m->{"bbox"}[1][0] $m->{"bbox"}[0][1] <= $northing $northing < $m->{"bbox"}[1][1]
Truth table100%5/5
A and B and C and D
A= $m->{"bbox"}[0][0] <= $eastingB= $easting < $m->{"bbox"}[1][0]C= $m->{"bbox"}[0][1] <= $northingD= $northing < $m->{"bbox"}[1][1]
ABCDresult
0XXX0
10XX0
110X0
11100
11111
101 && $m->{bbox}->[0][1] <= $northing && $northing < $m->{bbox}->[1][1]) {
102 209 my $w = _winding_number($easting, $northing, $m->{polygon});
103 209 ▶ if ($w != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($w != 0) 200 9 209
104 200 push @sheets, $k;
105 }
106 }
107 }
108 59 @sheets = sort @sheets;
109 }
110  
111 # special cases
112 226 ▶ if ( $form eq 'TRAD' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($form eq 'TRAD') { } 2 224 226
elsif ($form eq 'GPS') { } 48 176 224
elsif ($form eq 'SS') { } 1 175 176
113 2 $form = 'SS EEE NNN';
114 }
115 elsif ( $form eq 'GPS' ) {
116 48 $form = 'SS EEEEE NNNNN';
117 }
118 elsif ( $form eq 'SS' ) {
119 1 return $sq;
120 }
121  
122 225 ▶ if ( my ($space_a, $e_spec, $space_b, $n_spec) = $form =~ m{ \A S{1,2}(\s*)(E{1,5})(\s*)(N{1,5}) \Z }iosxm ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = $form =~ / \A S{1,2}(\s*)(E{1,5})(\s*)(N{1,5}) \Z /imosx) 225 0 225
123 225 my $e_len = length $e_spec;
124 225 my $n_len = length $n_spec;
125 225 $e = int($e / 10**(5 - $e_len));
126 225 $n = int($n / 10**(5 - $n_len));
127  
128 225 ▶ if ( wantarray ) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) 49 176 225
129 49 return ($sq, $e, $n, @sheets)
130 }
131  
132 176 my $gr = sprintf '%s%s%0.*d%s%0.*d', $sq, $space_a, $e_len, $e, $space_b, $n_len, $n;
133 176 $gr =~ s/\s+/ /g;
134  
135 176 ▶ if ( $with_maps ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($with_maps) { } 12 164 176
136 12 ▶ if ( @sheets ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@sheets) { } 12 0 12
137 12 return sprintf '%s on %s', $gr, join ', ', @sheets;
138 }
139 else {
140 0 return sprintf '%s is not on any maps in series %s', $gr, $map_keys;
141 }
142 }
143 else {
144 164 return $gr;
145 }
146 }
147  
148 0 croak "Format $form was not recognized";
149 }
150  
151 sub format_grid_trad {
152 7 ▶ my ($easting, $northing) = @_;
Subroutine format_grid_trad: called CC=1 SCAR=0.0
Pod documented
153 7 return format_grid($easting, $northing, { form => 'SS EEE NNN' })
154 }
155  
156 sub format_grid_GPS {
157 10 ▶ my ($easting, $northing) = @_;
Subroutine format_grid_GPS: called CC=1 SCAR=0.0
Pod documented
158 10 return format_grid($easting, $northing, { form => 'SS EEEEE NNNNN' })
159 }
160  
161 sub format_grid_map {
162 12 ▶ my ($easting, $northing, $options) = @_;
Subroutine format_grid_map: called CC=2 SCAR=6.9
Pod documented
163 12 ▶ if ( defined $options ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $options) { } 2 10 12
164 2 $options->{maps} = 1
165 }
166 else {
167 10 $options = { maps => 1 }
168 }
169 12 return format_grid($easting, $northing, $options)
170 }
171  
172 sub format_grid_landranger {
173 4 ▶ my ($easting,$northing) = @_;
Subroutine format_grid_landranger: called CC=6 SCAR=21.8
Pod documented
174  
175 4 my ($sq, $e, $n, @sheets) = format_grid($easting, $northing, { form => 'SS EEE NNN', maps => 1, series => 'A' });
176  
177 4 for my $s (@sheets) {
178 6 $s =~ s/\AA://xsm;
179 }
180  
181 4 ▶ return ($sq, $e, $n, @sheets) if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 3 1 4
182  
183 1 ▶ if (!@sheets ) { return sprintf '%s %03d %03d is not on any Landranger sheet', $sq, $e, $n }
Branch: 1 branch
BranchTrueFalseTotal
unless (@sheets) 0 1 1
184 1 ▶ if ( @sheets==1 ) { return sprintf '%s %03d %03d on Landranger sheet %s' , $sq, $e, $n, $sheets[0] }
Branch: 1 branch
BranchTrueFalseTotal
if (@sheets == 1) 1 0 1
185 0 ▶ if ( @sheets==2 ) { return sprintf '%s %03d %03d on Landranger sheets %s and %s', $sq, $e, $n, @sheets }
Branch: 1 branch
BranchTrueFalseTotal
if (@sheets == 2) 0 0 0
186  
187 0 my $phrase = join ', ', @sheets[0..($#sheets-1)], "and $sheets[-1]";
188 0 return sprintf '%s %03d %03d on Landranger sheets %s', $sq, $e, $n, $phrase;
189  
190 }
191  
192 sub _grid_to_sq {
193 226 ▶ my ($e, $n) = @_;
Subroutine _grid_to_sq: called CC=5 SCAR=18.0
194  
195 226 $e += MAJOR_GRID_SQ_EASTING_OFFSET;
196 226 $n += MAJOR_GRID_SQ_NORTHING_OFFSET;
197 226 ▶ return if !(0 <= $e && $e < MAX_GRID_SIZE && 0 <= $n && $n < MAX_GRID_SIZE);
Branch: 1 branch
BranchTrueFalseTotal
unless 0 <= $e and $e < 2500000 and 0 <= $n and $n < 2500000 0 226 226
Condition33%3/9
0 <= $e and $e < 2500000 and 0 <= $n and $n < 2500000
!ll&&!rl&&r
00226
0 <= $e and $e < 2500000 and 0 <= $n
!ll&&!rl&&r
00226
0 <= $e and $e < 2500000
!ll&&!rl&&r
00226
MC/DC0%0/4
0 <= $e and $e < 2500000 and 0 <= $n and $n < 2500000
0 <= $e $e < 2500000 0 <= $n $n < 2500000
Truth table20%1/5
A and B and C and D
A= 0 <= $eB= $e < 2500000C= 0 <= $nD= $n < 2500000
ABCDresult
0XXX0
10XX0
110X0
11100
11111
198  
199 226 my $major_index = int $e / MAJOR_GRID_SQ_SIZE + GRID_SIZE * int $n / MAJOR_GRID_SQ_SIZE;
200 226 $e = $e % MAJOR_GRID_SQ_SIZE;
201 226 $n = $n % MAJOR_GRID_SQ_SIZE;
202 226 my $minor_index = int $e / MINOR_GRID_SQ_SIZE + GRID_SIZE * int $n / MINOR_GRID_SQ_SIZE;
203 return
204 226 substr(GRID_SQ_LETTERS, $major_index, 1) .
205 substr(GRID_SQ_LETTERS, $minor_index, 1);
206 }
207  
208 sub _get_grid_square_offsets {
209 117 ▶ my $s = shift;
Subroutine _get_grid_square_offsets: called CC=4 SCAR=13.9
210 117 ▶ return if length $s < 2;
Branch: 1 branch
BranchTrueFalseTotal
if length $s < 2 0 117 117
211  
212 117 my $a = index GRID_SQ_LETTERS, uc substr $s, 0, 1;
213 117 ▶ return if 0 > $a;
Branch: 1 branch
BranchTrueFalseTotal
if 0 > $a 8 109 117
214  
215 109 my $b = index GRID_SQ_LETTERS, uc substr $s, 1, 1;
216 109 ▶ return if 0 > $b;
Branch: 1 branch
BranchTrueFalseTotal
if 0 > $b 54 55 109
217  
218 55 my ($X, $Y) = ($a % GRID_SIZE, int $a / GRID_SIZE);
219 55 my ($x, $y) = ($b % GRID_SIZE, int $b / GRID_SIZE);
220  
221 return (
222 55 MAJOR_GRID_SQ_SIZE * $X - MAJOR_GRID_SQ_EASTING_OFFSET + MINOR_GRID_SQ_SIZE * $x,
223 MAJOR_GRID_SQ_SIZE * $Y - MAJOR_GRID_SQ_NORTHING_OFFSET + MINOR_GRID_SQ_SIZE * $y
224 );
225 }
226  
227 sub _get_eastnorthings {
228 112 ▶ my $s = shift;
Subroutine _get_eastnorthings: called CC=4 SCAR=14.1
229 112 my $numbers = $s;
230 112 $numbers =~ tr/0-9//cd; # avoid using "r" here as it requires perl >= 5.14
231 112 my $len = length $numbers;
232 112 ▶ croak 'No easting or northing found' if $len == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $len == 0 0 112 112
233 112 ▶ croak "Easting and northing have different lengths in $s" if $len % 2;
Branch: 1 branch
BranchTrueFalseTotal
if $len % 2 0 112 112
234 112 ▶ croak "Too many digits in $s" if $len > 10;
Branch: 1 branch
BranchTrueFalseTotal
if $len > 10 0 112 112
235  
236 # this trick lets us pad with zeros on the right
237 112 my $e = reverse sprintf '%05d', scalar reverse substr $numbers, 0, $len/2;
238 112 my $n = reverse sprintf '%05d', scalar reverse substr $numbers, $len/2;
239 224 return ($e, $n)
240 }
241  
242 sub parse_grid {
243  
244 117 ▶ my $options = 'HASH' eq ref $_[-1] ? pop @_ : { };
Subroutine parse_grid: called CC=16 SCAR=28.7
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
'HASH' eq ref $_[-1] ? : 42 75 117
245  
246 117 ▶ my $figs = exists $options->{figs} ? $options->{figs} : 3;
Branch: 1 branch
BranchTrueFalseTotal
exists $options->{'figs'} ? : 42 75 117
247  
248 117 my @out;
249  
250 117 ▶ my $s = @_ < 3 ? "@_" : sprintf '%s %0.*d %0.*d', $_[0], $figs, $_[1], $figs, $_[2];
Branch: 1 branch
BranchTrueFalseTotal
@_ < 3 ? : 62 55 117
251  
252 # normal case : TQ 123 456 etc
253 117 ▶ if ( my ($E, $N) = _get_grid_square_offsets($s) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = _get_grid_square_offsets($s)) 55 62 117
254 55 ▶ my ($e, $n) = length $s > 2 ? _get_eastnorthings(substr $s, 2) : (0,0);
Branch: 1 branch
BranchTrueFalseTotal
length $s > 2 ? : 53 2 55
255 55 @out = ($E+$e, $N+$n);
256 55 ▶ return wantarray ? @out : "@out";
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 37 18 55
257 }
258  
259 # sheet id instead of grid sq
260 62 my $sheet_ref_pattern = qr{\A([A-Z]:)?([0-9NEWSOL/]+?)(\.[a-z]+)?(?:[ -/.]([ 0-9]+))?\Z};
261 62 my ($prefix, $sheet, $suffix, $numbers) = $s =~ m/$sheet_ref_pattern/sioxm;
262  
263 62 ▶ if (defined $sheet) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sheet) 62 0 62
264  
265 62 ▶ if ( ! defined $prefix ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $prefix) 8 54 62
266 8 $prefix = 'A:';
267 }
268 62 ▶ if ( ! defined $suffix ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $suffix) 61 1 62
269 61 $suffix = q{};
270 }
271 62 $sheet = $prefix . $sheet . $suffix;
272  
273 62 ▶ if (exists $maps{$sheet}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $Geo::Coordinates::OSGB::Grid::maps{$sheet}) 62 0 62
274 62 my ($E, $N) = @{$maps{$sheet}->{bbox}[0]}; # NB we need the bbox corner so that it is left and below all points on the map
275  
276 62 ▶ if (defined $numbers) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $numbers) 59 3 62
277 59 my ($e, $n) = _get_eastnorthings($numbers);
278 59 $E = $E + ($e-$E) % MINOR_GRID_SQ_SIZE;
279 59 $N = $N + ($n-$N) % MINOR_GRID_SQ_SIZE;
280  
281 59 my $w = _winding_number($E, $N, $maps{$sheet}->{polygon});
282 59 ▶ if ($w == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($w == 0) 0 59 59
283 0 croak sprintf 'Grid reference %s = (%d, %d) is not on sheet %s', scalar format_grid($E,$N), $e, $n, $sheet;
284 }
285 }
286 62 ▶ return wantarray ? ($E, $N) : "$E $N";
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 49 13 62
287 }
288 }
289  
290 # just a pair of numbers
291 0 ▶ if ( @out = $s =~ m{\A (\d+(?:\.\d+)?) \s+ (\d+(?:\.\d+)?) \Z}xsmio ) { # eee nnn
Branch: 1 branch
BranchTrueFalseTotal
if (@out = $s =~ /\A (\d+(?:\.\d+)?) \s+ (\d+(?:\.\d+)?) \Z/imosx) 0 0 0
292 0 ▶ return wantarray ? @out : "@out";
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
293 }
294  
295 0 croak "Failed to parse a grid reference from $s";
296 }
297  
298 9 *parse_trad_grid = \&parse_grid;
299 9 *parse_GPS_grid = \&parse_grid;
300 9 *parse_landranger_grid = \&parse_grid;
301 9 *parse_map_grid = \&parse_grid;
302  
303 # is $pt left of $a--$b?
304 sub _is_left {
305 540 ▶ my ($x, $y, $a, $b) = @_;
Subroutine _is_left: called CC=1 SCAR=0.0
306 540 return ( ($b->[0] - $a->[0]) * ($y - $a->[1]) - ($x - $a->[0]) * ($b->[1] - $a->[1]) );
307 }
308  
309 # adapted from http://geomalgorithms.com/a03-_inclusion.html
310 sub _winding_number {
311 270 ▶ my ($x, $y, $poly) = @_;
Subroutine _winding_number: called CC=7 SCAR=19.5
312 270 my $w = 0;
313 270 ▶ for (my $i=0; $i < $#$poly; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $poly->$#*) 4182 270 4452
314 4182 ▶ if ( $poly->[$i][1] <= $y ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($poly->[$i][1] <= $y) { } 1819 2363 4182
315 1819 ▶ if ($poly->[$i+1][1] > $y && _is_left($x, $y, $poly->[$i], $poly->[$i+1]) > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$poly[$i + 1][1] > $y and _is_left($x, $y, $poly->[$i], $$poly[$i + 1]) > 0) 267 1552 1819
Condition100%3/3
$$poly[$i + 1][1] > $y and _is_left($x, $y, $poly->[$i], $$poly[$i + 1]) > 0
!ll&&!rl&&r
15493267
MC/DC100%2/2
$$poly[$i + 1][1] > $y and _is_left($x, $y, $poly->[$i], $$poly[$i + 1]) > 0
$$poly[$i + 1][1] > $y _is_left($x, $y, $poly->[$i], $$poly[$i + 1]) > 0
Truth table100%3/3
A and B
A= $$poly[$i + 1][1] > $yB= _is_left($x, $y, $poly->[$i], $$poly[$i + 1]) > 0
ABresult
0X0
100
111
316 267 $w++;
317 }
318 }
319 else {
320 2363 ▶ if ($poly->[$i+1][1] <= $y && _is_left($x, $y, $poly->[$i], $poly->[$i+1]) < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$poly[$i + 1][1] <= $y and _is_left($x, $y, $poly->[$i], $$poly[$i + 1]) < 0) 6 2357 2363
Condition100%3/3
$$poly[$i + 1][1] <= $y and _is_left($x, $y, $poly->[$i], $$poly[$i + 1]) < 0
!ll&&!rl&&r
20932646
MC/DC100%2/2
$$poly[$i + 1][1] <= $y and _is_left($x, $y, $poly->[$i], $$poly[$i + 1]) < 0
$$poly[$i + 1][1] <= $y _is_left($x, $y, $poly->[$i], $$poly[$i + 1]) < 0
Truth table100%3/3
A and B
A= $$poly[$i + 1][1] <= $yB= _is_left($x, $y, $poly->[$i], $$poly[$i + 1]) < 0
ABresult
0X0
100
111
321 6 $w--;
322 }
323 }
324 }
325 270 return $w;
326 }
327  
328 9 1;
329  
330 __END__
↑ Summary OSGB.pm »